Integrated resource model related changes with eclipse plug-ins.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / manager / ResourceManager.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package oic.simulator.serviceprovider.manager;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.Vector;
29
30 import oic.simulator.serviceprovider.Activator;
31 import oic.simulator.serviceprovider.model.AttributeElement;
32 import oic.simulator.serviceprovider.model.MetaProperty;
33 import oic.simulator.serviceprovider.model.Resource;
34 import oic.simulator.serviceprovider.model.ResourceType;
35 import oic.simulator.serviceprovider.model.SingleResource;
36 import oic.simulator.serviceprovider.utils.Constants;
37 import oic.simulator.serviceprovider.utils.Utility;
38
39 import org.eclipse.core.runtime.IProgressMonitor;
40 import org.eclipse.swt.widgets.Display;
41 import org.oic.simulator.ArrayProperty;
42 import org.oic.simulator.AttributeProperty;
43 import org.oic.simulator.AttributeProperty.Type;
44 import org.oic.simulator.AttributeValue;
45 import org.oic.simulator.AttributeValue.TypeInfo;
46 import org.oic.simulator.AttributeValue.ValueType;
47 import org.oic.simulator.BooleanProperty;
48 import org.oic.simulator.DeviceInfo;
49 import org.oic.simulator.DeviceListener;
50 import org.oic.simulator.DoubleProperty;
51 import org.oic.simulator.ILogger.Level;
52 import org.oic.simulator.IntegerProperty;
53 import org.oic.simulator.PlatformInfo;
54 import org.oic.simulator.SimulatorException;
55 import org.oic.simulator.SimulatorManager;
56 import org.oic.simulator.SimulatorResourceAttribute;
57 import org.oic.simulator.SimulatorResourceModel;
58 import org.oic.simulator.StringProperty;
59 import org.oic.simulator.server.Observer;
60 import org.oic.simulator.server.SimulatorResource;
61 import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
62 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
63 import org.oic.simulator.server.SimulatorResource.ObserverListener;
64 import org.oic.simulator.server.SimulatorResource.ResourceModelChangeListener;
65 import org.oic.simulator.server.SimulatorSingleResource;
66
67 /**
68  * This class acts as an interface between the simulator java SDK and the
69  * various UI modules. It maintains all the details of resources and provides
70  * other UI modules with the information required. It also handles model change,
71  * automation, and observer related events from native layer and propagates
72  * those events to the registered UI listeners.
73  */
74 public class ResourceManager {
75
76     private Data                           data;
77
78     private Resource                       currentResourceInSelection;
79
80     private ResourceModelChangeListener    resourceModelChangeListener;
81
82     private AutoUpdateListener             automationListener;
83
84     private ObserverListener               observer;
85
86     private DeviceListener                 deviceListener;
87
88     private NotificationSynchronizerThread synchronizerThread;
89
90     private Thread                         threadHandle;
91
92     private DeviceInfo                     deviceInfo;
93     private PlatformInfo                   platformInfo;
94
95     private String                         deviceName;
96
97     public ResourceManager() {
98         data = new Data();
99
100         deviceListener = new DeviceListener() {
101
102             @Override
103             public void onDeviceFound(final String host,
104                     final DeviceInfo deviceInfo) {
105                 if (null != ResourceManager.this.deviceInfo
106                         || null == deviceInfo || null == host) {
107                     return;
108                 }
109                 synchronizerThread.addToQueue(new Runnable() {
110                     @Override
111                     public void run() {
112                         String rcvdDeviceName = deviceInfo.getName();
113                         if (null == rcvdDeviceName) {
114                             return;
115                         }
116                         if (deviceName.equalsIgnoreCase(rcvdDeviceName)) {
117                             ResourceManager.this.deviceInfo = deviceInfo;
118
119                             // Notify the UI Listeners
120                             UiListenerHandler.getInstance()
121                                     .deviceInfoReceivedNotification();
122                         }
123                     }
124                 });
125             }
126         };
127
128         resourceModelChangeListener = new ResourceModelChangeListener() {
129
130             @Override
131             public void onResourceModelChanged(final String resourceURI,
132                     final SimulatorResourceModel resourceModelN) {
133                 synchronizerThread.addToQueue(new Runnable() {
134
135                     @Override
136                     public void run() {
137                         if (null == resourceURI || null == resourceModelN) {
138                             return;
139                         }
140
141                         Display.getDefault().asyncExec(new Runnable() {
142                             @Override
143                             public void run() {
144                                 Resource resource = data
145                                         .getResourceByURI(resourceURI);
146                                 if (null != resource) {
147                                     try {
148                                         resource.updateResourceRepresentation(resourceModelN);
149                                     } catch (NumberFormatException e) {
150                                         Activator
151                                                 .getDefault()
152                                                 .getLogManager()
153                                                 .log(Level.ERROR.ordinal(),
154                                                         new Date(),
155                                                         "Error while trying to update the attributes.\n"
156                                                                 + Utility
157                                                                         .getSimulatorErrorString(
158                                                                                 e,
159                                                                                 null));
160                                     }
161                                 }
162                             }
163                         });
164                     }
165                 });
166             }
167         };
168
169         automationListener = new AutoUpdateListener() {
170
171             @Override
172             public void onUpdateComplete(final String resourceURI,
173                     final int automationId) {
174                 synchronizerThread.addToQueue(new Runnable() {
175
176                     @Override
177                     public void run() {
178                         SingleResource resource = data
179                                 .getSingleResourceByURI(resourceURI);
180                         if (null == resource) {
181                             return;
182                         }
183                         // Checking whether this notification is for an
184                         // attribute or a resource
185                         if (resource.isResourceAutomationInProgress()) {
186                             changeResourceLevelAutomationStatus(resource, false);
187                             // Notify the UI listeners
188                             UiListenerHandler.getInstance()
189                                     .automationCompleteUINotification(resource,
190                                             null);
191                         } else if (resource.isAttributeAutomationInProgress()) {
192                             // Find the attribute with the given automation id
193                             final AttributeElement attribute = getAttributeWithGivenAutomationId(
194                                     resource, automationId);
195                             if (null != attribute) {
196                                 attribute.setAutoUpdateState(false);
197                                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
198                             } else {
199                                 // Setting the attribute automation status to
200                                 // false.
201                                 resource.setAttributeAutomationInProgress(false);
202                             }
203                         }
204                     }
205                 });
206             }
207         };
208
209         observer = new ObserverListener() {
210
211             public void onObserverChanged(final String resourceURI,
212                     final int status, final Observer observer) {
213                 new Thread() {
214                     @Override
215                     public void run() {
216                         if (null == resourceURI || null == observer) {
217                             return;
218                         }
219                         Resource resource = data.getResourceByURI(resourceURI);
220                         if (null == resource) {
221                             return;
222                         }
223                         // Update the observers information
224                         if (status == 0) {
225                             resource.addObserverInfo(observer);
226                         } else {
227                             resource.removeObserverInfo(observer);
228                         }
229                         // Notify the UI listeners
230                         UiListenerHandler.getInstance()
231                                 .observerListChangedUINotification(resource);
232                     }
233                 }.start();
234             }
235
236             @Override
237             public void onObserverAdded(String resourceURI, Observer observer) {
238                 onObserverChanged(resourceURI, 0, observer);
239             }
240
241             @Override
242             public void onObserverRemoved(String resourceURI, Observer observer) {
243                 onObserverChanged(resourceURI, 1, observer);
244             }
245         };
246
247         synchronizerThread = new NotificationSynchronizerThread();
248         threadHandle = new Thread(synchronizerThread);
249         threadHandle.setName("Simulator service provider event queue");
250         threadHandle.start();
251
252         // Set the default device name.
253         deviceName = "IoTivity Simulator";
254         try {
255             SimulatorManager.setDeviceInfo(deviceName);
256         } catch (SimulatorException e) {
257             Activator
258                     .getDefault()
259                     .getLogManager()
260                     .log(Level.ERROR.ordinal(),
261                             new Date(),
262                             "Error while registering the device info.\n"
263                                     + Utility.getSimulatorErrorString(e, null));
264         }
265
266         // Set the default platform information.
267         platformInfo = new PlatformInfo();
268         platformInfo.setPlatformID("Samsung Platform Identifier");
269         platformInfo.setManufacturerName("Samsung");
270         platformInfo.setManufacturerUrl("www.samsung.com");
271         platformInfo.setModelNumber("Samsung Model Num01");
272         platformInfo.setDateOfManufacture("2015-09-10T11:10:30Z");
273         platformInfo.setPlatformVersion("PlatformVersion01");
274         platformInfo.setOperationSystemVersion("OSVersion01");
275         platformInfo.setHardwareVersion("HardwareVersion01");
276         platformInfo.setFirmwareVersion("FirwareVersion01");
277         platformInfo.setSupportUrl("http://www.samsung.com/support");
278         platformInfo.setSystemTime("2015-09-10T11:10:30Z");
279         try {
280             SimulatorManager.setPlatformInfo(platformInfo);
281         } catch (SimulatorException e) {
282             Activator
283                     .getDefault()
284                     .getLogManager()
285                     .log(Level.ERROR.ordinal(),
286                             new Date(),
287                             "Error while registering the platform info.\n"
288                                     + Utility.getSimulatorErrorString(e, null));
289         }
290
291         // Get the device information to show other details of the device in UI.
292         try {
293             SimulatorManager.findDevices("", deviceListener);
294         } catch (SimulatorException e) {
295             Activator
296                     .getDefault()
297                     .getLogManager()
298                     .log(Level.ERROR.ordinal(),
299                             new Date(),
300                             "Failed to get the local device information.\n"
301                                     + Utility.getSimulatorErrorString(e, null));
302         }
303     }
304
305     private static class NotificationSynchronizerThread implements Runnable {
306
307         LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
308
309         @Override
310         public void run() {
311             while (!Thread.interrupted()) {
312                 synchronized (this) {
313                     try {
314                         while (notificationQueue.isEmpty()) {
315                             this.wait();
316                             break;
317                         }
318                     } catch (InterruptedException e) {
319                         return;
320                     }
321                 }
322
323                 Runnable thread;
324                 synchronized (this) {
325                     thread = notificationQueue.pop();
326                 }
327                 try {
328                     thread.run();
329                 } catch (Exception e) {
330                     if (e instanceof InterruptedException) {
331                         return;
332                     }
333                     e.printStackTrace();
334                 }
335             }
336         }
337
338         public void addToQueue(Runnable event) {
339             synchronized (this) {
340                 notificationQueue.add(event);
341                 this.notify();
342             }
343         }
344     }
345
346     public void setDeviceInfo(List<MetaProperty> metaProperties) {
347         if (null == metaProperties || metaProperties.size() < 1) {
348             return;
349         }
350         Iterator<MetaProperty> itr = metaProperties.iterator();
351         MetaProperty prop;
352         String propName;
353         String propValue;
354         boolean found = false;
355         while (itr.hasNext()) {
356             prop = itr.next();
357             propName = prop.getPropName();
358             propValue = prop.getPropValue();
359             if (propName.equals(Constants.DEVICE_NAME)) {
360                 this.deviceName = propValue;
361                 found = true;
362                 break;
363             }
364         }
365
366         if (!found) {
367             return;
368         }
369
370         try {
371             SimulatorManager.setDeviceInfo(deviceName);
372         } catch (SimulatorException e) {
373             Activator
374                     .getDefault()
375                     .getLogManager()
376                     .log(Level.ERROR.ordinal(),
377                             new Date(),
378                             "Error while registering the device info.\n"
379                                     + Utility.getSimulatorErrorString(e, null));
380         }
381     }
382
383     public boolean isDeviceInfoValid(List<MetaProperty> metaProperties) {
384         if (null == metaProperties || metaProperties.size() < 1) {
385             return false;
386         }
387
388         Iterator<MetaProperty> itr = metaProperties.iterator();
389         MetaProperty prop;
390         String propName;
391         String propValue;
392         while (itr.hasNext()) {
393             prop = itr.next();
394             propName = prop.getPropName();
395             propValue = prop.getPropValue();
396             if (propName.equals(Constants.DEVICE_NAME)) {
397                 if (null == propValue || propValue.length() < 1) {
398                     return false;
399                 }
400                 break;
401             }
402         }
403         return true;
404     }
405
406     public List<MetaProperty> getDeviceInfo() {
407         List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
408         metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, deviceName));
409         if (null != deviceInfo) {
410             metaProperties.add(new MetaProperty(Constants.DEVICE_ID, deviceInfo
411                     .getID()));
412             metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
413                     deviceInfo.getSpecVersion()));
414             metaProperties.add(new MetaProperty(Constants.DEVICE_DMV,
415                     deviceInfo.getDataModelVersion()));
416         }
417         return metaProperties;
418     }
419
420     public List<MetaProperty> getPlatformInfo() {
421         List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
422         metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
423                 .getPlatformID()));
424         metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
425                 platformInfo.getManufacturerName()));
426         metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
427                 platformInfo.getManufacturerUrl()));
428         metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
429                 platformInfo.getModelNumber()));
430         metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
431                 platformInfo.getDateOfManufacture()));
432         metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
433                 platformInfo.getPlatformVersion()));
434         metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
435                 platformInfo.getOperationSystemVersion()));
436         metaProperties.add(new MetaProperty(
437                 Constants.PLATFORM_HARDWARE_VERSION, platformInfo
438                         .getHardwareVersion()));
439         metaProperties.add(new MetaProperty(
440                 Constants.PLATFORM_FIRMWARE_VERSION, platformInfo
441                         .getFirmwareVersion()));
442         metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
443                 platformInfo.getSupportUrl()));
444         metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
445                 platformInfo.getSystemTime()));
446         return metaProperties;
447     }
448
449     public void setPlatformInfo(List<MetaProperty> metaProperties) {
450         if (null == metaProperties || metaProperties.size() < 1) {
451             return;
452         }
453         Iterator<MetaProperty> itr = metaProperties.iterator();
454         MetaProperty prop;
455         String propName;
456         String propValue;
457         while (itr.hasNext()) {
458             prop = itr.next();
459             propName = prop.getPropName();
460             propValue = prop.getPropValue();
461             if (propName.equals(Constants.PLATFORM_ID)) {
462                 platformInfo.setPlatformID(propValue);
463             } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) {
464                 platformInfo.setManufacturerName(propValue);
465             } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) {
466                 platformInfo.setManufacturerUrl(propValue);
467             } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) {
468                 platformInfo.setModelNumber(propValue);
469             } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) {
470                 platformInfo.setDateOfManufacture(propValue);
471             } else if (propName.equals(Constants.PLATFORM_VERSION)) {
472                 platformInfo.setPlatformVersion(propValue);
473             } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) {
474                 platformInfo.setOperationSystemVersion(propValue);
475             } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) {
476                 platformInfo.setHardwareVersion(propValue);
477             } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) {
478                 platformInfo.setFirmwareVersion(propValue);
479             } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) {
480                 platformInfo.setSupportUrl(propValue);
481             } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) {
482                 platformInfo.setSystemTime(propValue);
483             }
484         }
485         try {
486             SimulatorManager.setPlatformInfo(platformInfo);
487         } catch (SimulatorException e) {
488             Activator
489                     .getDefault()
490                     .getLogManager()
491                     .log(Level.ERROR.ordinal(),
492                             new Date(),
493                             "Error while registering the platform info.\n"
494                                     + Utility.getSimulatorErrorString(e, null));
495         }
496     }
497
498     public boolean isPlatformInfoValid(List<MetaProperty> metaProperties) {
499         if (null == metaProperties || metaProperties.size() < 1) {
500             return false;
501         }
502         Iterator<MetaProperty> itr = metaProperties.iterator();
503         MetaProperty prop;
504         String propValue;
505         while (itr.hasNext()) {
506             prop = itr.next();
507             propValue = prop.getPropValue();
508             if (null == propValue || propValue.length() < 1) {
509                 return false;
510             }
511         }
512         return true;
513     }
514
515     public synchronized Resource getCurrentResourceInSelection() {
516         return currentResourceInSelection;
517     }
518
519     public synchronized void setCurrentResourceInSelection(Resource resource) {
520         this.currentResourceInSelection = resource;
521     }
522
523     public boolean isResourceExist(String resourceURI) {
524         return data.isResourceExist(resourceURI);
525     }
526
527     public boolean isAnyResourceExist() {
528         return data.isAnyResourceExist();
529     }
530
531     public boolean createSingleResource(SingleResource resource,
532             Map<String, SimulatorResourceAttribute> attributes)
533             throws SimulatorException {
534         if (null == resource) {
535             return false;
536         }
537
538         try {
539             // Create the resource.
540             SimulatorResource jSimulatorResource = SimulatorManager
541                     .createResource(SimulatorResource.Type.SINGLE,
542                             resource.getResourceName(),
543                             resource.getResourceURI(),
544                             resource.getResourceType());
545             if (null == jSimulatorResource
546                     || !(jSimulatorResource instanceof SimulatorSingleResource)) {
547                 return false;
548             }
549             SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
550             resource.setSimulatorResource(jSimulatorSingleResource);
551
552             // Cancel discoverable property if requested by user.
553             if (!resource.isDiscoverable()) {
554                 jSimulatorSingleResource.setDiscoverable(false);
555             }
556
557             // Cancel observable property if requested by user.
558             if (!resource.isObservable()) {
559                 jSimulatorSingleResource.setObservable(false);
560             }
561
562             // Set the model change listener.
563             jSimulatorSingleResource
564                     .setResourceModelChangeListener(resourceModelChangeListener);
565
566             // Set the observer listener if the resource is observable.
567             if (resource.isObservable()) {
568                 jSimulatorSingleResource.setObserverListener(observer);
569             }
570
571             // Add attributes.
572             if (null != attributes && !attributes.isEmpty()) {
573                 SimulatorResourceAttribute value;
574                 for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
575                         .entrySet()) {
576                     value = entry.getValue();
577                     if (null != value)
578                         jSimulatorSingleResource.addAttribute(value);
579                 }
580
581                 // Get the resource model java object reference.
582                 resource.setResourceModel(jSimulatorSingleResource
583                         .getResourceModel());
584
585                 resource.createResourceRepresentation(jSimulatorSingleResource
586                         .getAttributes());
587             }
588
589             // Set the resource interfaces.
590             jSimulatorSingleResource
591                     .setInterface(Utility.convertSetToVectorString(resource
592                             .getResourceInterfaces()));
593
594             // Register the resource with the platform.
595             jSimulatorSingleResource.start();
596             resource.setStarted(true);
597         } catch (SimulatorException e) {
598             Activator
599                     .getDefault()
600                     .getLogManager()
601                     .log(Level.ERROR.ordinal(), new Date(),
602                             Utility.getSimulatorErrorString(e, null));
603             throw e;
604         }
605
606         // Add to local cache.
607         data.addResource(resource);
608
609         // Update UI listeners
610         UiListenerHandler.getInstance().resourceCreatedUINotification(
611                 ResourceType.SINGLE);
612
613         return true;
614     }
615
616     public Resource createResourceByRAML(String configFilePath)
617             throws SimulatorException {
618         Resource resource = null;
619         try {
620             // Create the resource
621             SimulatorResource jSimulatorResource = SimulatorManager
622                     .createResource(configFilePath);
623             if (null == jSimulatorResource) {
624                 return null;
625             }
626             if (jSimulatorResource instanceof SimulatorSingleResource) {
627                 resource = new SingleResource();
628             } else {
629                 return null;
630             }
631             resource.setSimulatorResource(jSimulatorResource);
632
633             // Fetch and locally store the resource name and uri.
634             String uri = jSimulatorResource.getURI();
635             if (null == uri || uri.trim().isEmpty()) {
636                 return null;
637             }
638             resource.setResourceURI(uri.trim());
639
640             String name = jSimulatorResource.getName();
641             if (null == name || name.trim().isEmpty()) {
642                 return null;
643             }
644             resource.setResourceName(name.trim());
645         } catch (SimulatorException e) {
646             Activator
647                     .getDefault()
648                     .getLogManager()
649                     .log(Level.ERROR.ordinal(), new Date(),
650                             Utility.getSimulatorErrorString(e, null));
651             throw e;
652         }
653         return resource;
654     }
655
656     /**
657      * This method can set/change the resource uri and name of an already
658      * created resource which is not yet registered with the platform. This
659      * method registers the model change and observer listeners, registers the
660      * resource, fetches the resource attributes, updates the local cache and
661      * notifies the UI listeners.
662      */
663     public boolean completeSingleResourceCreationByRAML(Resource resource,
664             String uri, String name, boolean multiInstance)
665             throws SimulatorException {
666         if (null == resource || !(resource instanceof SingleResource)) {
667             return false;
668         }
669         try {
670             SingleResource singleRes = (SingleResource) resource;
671
672             SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
673                     .getSimulatorResource();
674             if (null == jSimulatorSingleResource) {
675                 return false;
676             }
677
678             // Update resource URI and Name if they are changed.
679             String newUri = uri.trim();
680             String newName = name.trim();
681
682             if (multiInstance) {
683                 singleRes.setResourceURI(newUri);
684                 singleRes.setResourceName(newName);
685             } else {
686                 if (!singleRes.getResourceURI().equals(newUri)) {
687                     jSimulatorSingleResource.setURI(newUri);
688                     singleRes.setResourceURI(newUri);
689                 }
690                 if (!singleRes.getResourceName().equals(newName)) {
691                     jSimulatorSingleResource.setName(newName);
692                     singleRes.setResourceName(newName);
693                 }
694             }
695
696             // Set the model change listener.
697             jSimulatorSingleResource
698                     .setResourceModelChangeListener(resourceModelChangeListener);
699
700             // Set the observer listener if the resource is observable.
701             if (jSimulatorSingleResource.isObservable()) {
702                 jSimulatorSingleResource.setObserverListener(observer);
703                 singleRes.setObservable(true);
704             }
705
706             // Fetch the resource model.
707             SimulatorResourceModel jResModel = jSimulatorSingleResource
708                     .getResourceModel();
709             if (null == jResModel) {
710                 return false;
711             }
712             singleRes.setResourceModel(jResModel);
713
714             // Fetch the basic details of the resource.
715             singleRes.setResourceType(jSimulatorSingleResource
716                     .getResourceType());
717             singleRes
718                     .setResourceInterfaces(Utility
719                             .convertVectorToSet(jSimulatorSingleResource
720                                     .getInterface()));
721
722             // Fetch the resource attributes.
723             singleRes.createResourceRepresentation(jSimulatorSingleResource
724                     .getAttributes());
725
726             // Register the resource with the platform.
727             jSimulatorSingleResource.start();
728             singleRes.setStarted(true);
729
730             // Add to local cache.
731             data.addResource(singleRes);
732
733             // Update UI listeners for single instance creation
734             if (!multiInstance)
735                 UiListenerHandler.getInstance().resourceCreatedUINotification(
736                         ResourceType.SINGLE);
737         } catch (Exception e) {
738             Activator
739                     .getDefault()
740                     .getLogManager()
741                     .log(Level.ERROR.ordinal(), new Date(),
742                             Utility.getSimulatorErrorString(e, null));
743             throw e;
744         }
745         return true;
746     }
747
748     public Set<SingleResource> createSingleResourceMultiInstances(
749             String configFile, int count, IProgressMonitor progressMonitor)
750             throws SimulatorException {
751         Set<SingleResource> resultSet;
752         try {
753             resultSet = new HashSet<SingleResource>();
754             Vector<SimulatorResource> jSimulatorResources = SimulatorManager
755                     .createResource(configFile, count);
756             if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
757                 return null;
758             }
759             SimulatorSingleResource jResource;
760             SingleResource resource;
761             boolean result;
762             for (SimulatorResource jSimulatorResource : jSimulatorResources) {
763                 // If the resource creation progress is canceled, then stop the
764                 // creation and stop/delete
765                 // the resources created already.
766                 if (progressMonitor.isCanceled()) {
767                     removeSingleResources(resultSet);
768                     return null;
769                 }
770                 jResource = (SimulatorSingleResource) jSimulatorResource;
771                 resource = new SingleResource();
772                 resource.setSimulatorResource(jResource);
773                 try {
774                     result = completeSingleResourceCreationByRAML(resource,
775                             jResource.getURI(), jResource.getName(), true);
776                     if (result) {
777                         resultSet.add(resource);
778                     }
779                 } catch (SimulatorException eInner) {
780                     Activator
781                             .getDefault()
782                             .getLogManager()
783                             .log(Level.ERROR.ordinal(),
784                                     new Date(),
785                                     Utility.getSimulatorErrorString(eInner,
786                                             null));
787                 }
788                 progressMonitor.worked(1);
789             }
790         } catch (SimulatorException eOuter) {
791             Activator
792                     .getDefault()
793                     .getLogManager()
794                     .log(Level.ERROR.ordinal(), new Date(),
795                             Utility.getSimulatorErrorString(eOuter, null));
796             throw eOuter;
797         }
798         return resultSet;
799     }
800
801     public List<Resource> getResourceList() {
802         List<Resource> resourceList = data.getResources();
803         if (null == resourceList) {
804             return null;
805         }
806         // Sort the list
807         Collections.sort(resourceList, Utility.resourceComparator);
808
809         return resourceList;
810     }
811
812     public List<SingleResource> getSingleResourceList() {
813         List<SingleResource> resourceList = data.getSingleResources();
814         if (null == resourceList) {
815             return null;
816         }
817         // Sort the list
818         Collections.sort(resourceList, Utility.singleResourceComparator);
819
820         return resourceList;
821     }
822
823     public void removeSingleResources(Set<SingleResource> resources)
824             throws SimulatorException {
825         if (null == resources) {
826             return;
827         }
828         Iterator<SingleResource> itr = resources.iterator();
829         while (itr.hasNext()) {
830             removeResource(itr.next());
831         }
832     }
833
834     public void removeResource(Resource res) throws SimulatorException {
835         // Unregister the resource from the platform.
836         SimulatorResource simRes = res.getSimulatorResource();
837         try {
838             simRes.stop();
839         } catch (SimulatorException e) {
840             Activator
841                     .getDefault()
842                     .getLogManager()
843                     .log(Level.ERROR.ordinal(), new Date(),
844                             Utility.getSimulatorErrorString(e, null));
845             throw e;
846         }
847
848         // Delete this resource
849         data.deleteResource(res);
850     }
851
852     public boolean isUriUnique(List<MetaProperty> properties) {
853         if (null == properties) {
854             return false;
855         }
856         MetaProperty prop;
857         Iterator<MetaProperty> itr = properties.iterator();
858         while (itr.hasNext()) {
859             prop = itr.next();
860             if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
861                 String uri = prop.getPropValue();
862                 return !data.isResourceExist(uri);
863             }
864         }
865         return false;
866     }
867
868     public void resourceSelectionChanged(final Resource selectedResource) {
869         new Thread() {
870             @Override
871             public void run() {
872                 if (null != selectedResource) {
873                     setCurrentResourceInSelection(selectedResource);
874                 } else {
875                     setCurrentResourceInSelection(null);
876                 }
877                 // Notify all observers for resource selection change event
878                 UiListenerHandler.getInstance()
879                         .resourceSelectionChangedUINotification(
880                                 selectedResource);
881             }
882         }.start();
883     }
884
885     public List<MetaProperty> getMetaProperties(Resource resource) {
886         if (null != resource) {
887             String propName;
888             String propValue;
889
890             List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
891
892             for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
893                 propName = Constants.META_PROPERTIES[index];
894                 if (propName.equals(Constants.RESOURCE_NAME)) {
895                     propValue = resource.getResourceName();
896                 } else if (propName.equals(Constants.RESOURCE_URI)) {
897                     propValue = resource.getResourceURI();
898                 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
899                     propValue = resource.getResourceType();
900                 } else if (propName.equals(Constants.INTERFACE_TYPES)) {
901                     Set<String> ifTypes = resource.getResourceInterfaces();
902                     if (null != ifTypes && !ifTypes.isEmpty()) {
903                         propValue = "";
904                         Iterator<String> itr = ifTypes.iterator();
905                         while (itr.hasNext()) {
906                             propValue += itr.next();
907                             if (itr.hasNext()) {
908                                 propValue += ", ";
909                             }
910                         }
911                     } else {
912                         propValue = null;
913                     }
914                 } else {
915                     propValue = null;
916                 }
917                 if (null != propValue) {
918                     metaPropertyList.add(new MetaProperty(propName, propValue));
919                 }
920             }
921             return metaPropertyList;
922         }
923         return null;
924     }
925
926     public boolean startResource(Resource resource) throws SimulatorException {
927         if (null == resource) {
928             return false;
929         }
930         SimulatorResource server = resource.getSimulatorResource();
931         if (null == server) {
932             return false;
933         }
934         try {
935             server.start();
936             resource.setStarted(true);
937         } catch (SimulatorException e) {
938             Activator
939                     .getDefault()
940                     .getLogManager()
941                     .log(Level.ERROR.ordinal(),
942                             new Date(),
943                             "There is an error while starting the resource.\n"
944                                     + Utility.getSimulatorErrorString(e, null));
945             throw e;
946         }
947         return true;
948     }
949
950     public boolean stopResource(Resource resource) throws SimulatorException {
951         if (null == resource) {
952             return false;
953         }
954         SimulatorResource server = resource.getSimulatorResource();
955         if (null == server) {
956             return false;
957         }
958         try {
959             server.stop();
960             resource.setStarted(false);
961         } catch (SimulatorException e) {
962             Activator
963                     .getDefault()
964                     .getLogManager()
965                     .log(Level.ERROR.ordinal(),
966                             new Date(),
967                             "There is an error while stopping the resource.\n"
968                                     + Utility.getSimulatorErrorString(e, null));
969             throw e;
970         }
971         return true;
972     }
973
974     public boolean changeResourceName(Resource resource, String newName)
975             throws SimulatorException {
976         if (null == resource || null == newName) {
977             return false;
978         }
979
980         if (!stopResource(resource)) {
981             return false;
982         }
983
984         SimulatorResource server = resource.getSimulatorResource();
985         try {
986             server.setName(newName);
987             resource.setResourceName(newName);
988         } catch (SimulatorException e) {
989             Activator
990                     .getDefault()
991                     .getLogManager()
992                     .log(Level.ERROR.ordinal(),
993                             new Date(),
994                             "There is an error while changing the resource name.\n"
995                                     + Utility.getSimulatorErrorString(e, null));
996             throw e;
997         }
998
999         if (!startResource(resource)) {
1000             return false;
1001         }
1002
1003         return true;
1004     }
1005
1006     public boolean changeResourceURI(Resource resource, String newURI)
1007             throws SimulatorException {
1008         if (null == resource || null == newURI) {
1009             return false;
1010         }
1011
1012         if (!stopResource(resource)) {
1013             return false;
1014         }
1015
1016         String curURI = resource.getResourceURI();
1017         setResourceURI(resource, newURI);
1018
1019         try {
1020             if (!startResource(resource)) {
1021                 return false;
1022             }
1023         } catch (SimulatorException e) {
1024             setResourceURI(resource, curURI);
1025         }
1026
1027         return true;
1028     }
1029
1030     public void setResourceURI(Resource resource, String newURI)
1031             throws SimulatorException {
1032         String curURI = resource.getResourceURI();
1033         SimulatorResource server = resource.getSimulatorResource();
1034         try {
1035             server.setURI(newURI);
1036             data.changeResourceURI(resource, curURI, newURI);
1037         } catch (SimulatorException e) {
1038             Activator
1039                     .getDefault()
1040                     .getLogManager()
1041                     .log(Level.ERROR.ordinal(),
1042                             new Date(),
1043                             "There is an error while changing the resource URI.\n"
1044                                     + Utility.getSimulatorErrorString(e, null));
1045             throw e;
1046         }
1047     }
1048
1049     public boolean updateResourceProperties(Resource resource,
1050             List<MetaProperty> properties, boolean uriChanged,
1051             boolean nameChanged) throws SimulatorException {
1052         if (null == resource || null == properties) {
1053             return false;
1054         }
1055
1056         // Updating the properties
1057         Iterator<MetaProperty> itr = properties.iterator();
1058         MetaProperty property;
1059         String propName;
1060         String propValue;
1061         String resName = null;
1062         String resURI = null;
1063         while (itr.hasNext()) {
1064             property = itr.next();
1065             if (null == property) {
1066                 continue;
1067             }
1068             propName = property.getPropName();
1069             propValue = property.getPropValue();
1070             if (propName.equals(Constants.RESOURCE_NAME)) {
1071                 resName = propValue;
1072             } else if (propName.equals(Constants.RESOURCE_URI)) {
1073                 resURI = propValue;
1074             }
1075         }
1076
1077         if (nameChanged) {
1078             if (!changeResourceName(resource, resName)) {
1079                 return false;
1080             }
1081
1082             // Notify UI Listeners
1083             UiListenerHandler.getInstance().propertiesChangedUINotification(
1084                     Resource.class);
1085         }
1086
1087         if (uriChanged) {
1088             if (!changeResourceURI(resource, resURI)) {
1089                 return false;
1090             }
1091         }
1092
1093         return true;
1094     }
1095
1096     public boolean updateResourceInterfaces(Resource resource,
1097             Set<String> newIfSet) throws SimulatorException {
1098         if (null == resource || null == newIfSet || newIfSet.isEmpty()) {
1099             return false;
1100         }
1101         SimulatorResource jResource = resource.getSimulatorResource();
1102         if (null == jResource) {
1103             return false;
1104         }
1105         Set<String> curIfSet = resource.getResourceInterfaces();
1106         Iterator<String> itr = curIfSet.iterator();
1107         String interfaceType;
1108         boolean resourceRestartRequired = false;
1109         while (itr.hasNext()) {
1110             interfaceType = itr.next();
1111             if (!newIfSet.contains(interfaceType)) {
1112                 resourceRestartRequired = true;
1113                 break;
1114             }
1115         }
1116
1117         try {
1118             // As there is no support from native layer for interface removal,
1119             // supporting it from the simulator requires restarting the resource
1120             // if any existing interfaces are removed.
1121
1122             if (resourceRestartRequired) {
1123                 stopResource(resource);
1124                 jResource.setInterface(Utility
1125                         .convertSetToVectorString(newIfSet));
1126                 startResource(resource);
1127             } else {
1128                 // Existing interfaces are not removed.
1129                 itr = newIfSet.iterator();
1130                 while (itr.hasNext()) {
1131                     interfaceType = itr.next();
1132                     if (!curIfSet.contains(interfaceType)) {
1133                         jResource.addInterface(interfaceType);
1134                     }
1135                 }
1136             }
1137         } catch (SimulatorException e) {
1138             Activator
1139                     .getDefault()
1140                     .getLogManager()
1141                     .log(Level.ERROR.ordinal(),
1142                             new Date(),
1143                             "There is an error while changing the interface types."
1144                                     + Utility.getSimulatorErrorString(e, null));
1145             throw e;
1146         }
1147
1148         // Set the resource interfaces.
1149         resource.setResourceInterfaces(newIfSet);
1150
1151         return true;
1152     }
1153
1154     public boolean attributeValueUpdated(SingleResource resource,
1155             String attributeName, AttributeValue value) {
1156         if (null != resource && null != attributeName && null != value) {
1157             SimulatorSingleResource simRes = (SimulatorSingleResource) resource
1158                     .getSimulatorResource();
1159             if (null != simRes) {
1160                 try {
1161                     simRes.updateAttribute(attributeName, value);
1162                     return true;
1163                 } catch (SimulatorException e) {
1164                     Activator
1165                             .getDefault()
1166                             .getLogManager()
1167                             .log(Level.ERROR.ordinal(), new Date(),
1168                                     Utility.getSimulatorErrorString(e, null));
1169                 }
1170             }
1171         }
1172         return false;
1173     }
1174
1175     public boolean isResourceStarted(Resource resource) {
1176         if (null == resource) {
1177             return false;
1178         }
1179         return resource.isStarted();
1180     }
1181
1182     public boolean isPropertyValueInvalid(Resource resource,
1183             List<MetaProperty> properties, String propName) {
1184         if (null == resource || null == properties || null == propName) {
1185             return false;
1186         }
1187         boolean invalid = false;
1188         MetaProperty prop;
1189         Iterator<MetaProperty> itr = properties.iterator();
1190         while (itr.hasNext()) {
1191             prop = itr.next();
1192             if (prop.getPropName().equals(propName)) {
1193                 String value = prop.getPropValue();
1194                 if (propName.equals(Constants.RESOURCE_URI)) {
1195                     if (!Utility.isUriValid(value)) {
1196                         invalid = true;
1197                     }
1198                 } else {
1199                     if (null == value || value.trim().isEmpty()) {
1200                         invalid = true;
1201                     }
1202                 }
1203             }
1204         }
1205         return invalid;
1206     }
1207
1208     public boolean isPropValueChanged(Resource resource,
1209             List<MetaProperty> properties, String propName) {
1210         if (null == resource || null == properties || null == propName) {
1211             return false;
1212         }
1213         boolean changed = false;
1214         MetaProperty prop;
1215         String oldValue;
1216         Iterator<MetaProperty> itr = properties.iterator();
1217         while (itr.hasNext()) {
1218             prop = itr.next();
1219             if (prop.getPropName().equals(propName)) {
1220                 oldValue = getPropertyValueFromResource(resource, propName);
1221                 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1222                     changed = true;
1223                 }
1224                 break;
1225             }
1226         }
1227         return changed;
1228     }
1229
1230     private String getPropertyValueFromResource(Resource resource,
1231             String propName) {
1232         if (null == resource || null == propName) {
1233             return null;
1234         }
1235         if (propName.equals(Constants.RESOURCE_URI)) {
1236             return resource.getResourceURI();
1237         } else if (propName.equals(Constants.RESOURCE_NAME)) {
1238             return resource.getResourceName();
1239         } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1240             return resource.getResourceType();
1241         } else {
1242             return null;
1243         }
1244     }
1245
1246     public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
1247         if (null == att) {
1248             return false;
1249         }
1250         AttributeProperty prop = att.property();
1251         if (null == prop) {
1252             return false;
1253         }
1254
1255         if (prop.getType() == Type.INTEGER) {
1256             IntegerProperty intProperty = prop.asInteger();
1257             if (null != intProperty) {
1258                 return (intProperty.hasRange() || intProperty.hasValues());
1259             } else {
1260                 return false;
1261             }
1262         } else if (prop.getType() == Type.DOUBLE) {
1263             DoubleProperty dblProperty = prop.asDouble();
1264             if (null != dblProperty) {
1265                 return (dblProperty.hasRange() || dblProperty.hasValues());
1266             } else {
1267                 return false;
1268             }
1269         } else if (prop.getType() == Type.STRING) {
1270             StringProperty stringProperty = prop.asString();
1271             if (null != stringProperty) {
1272                 return stringProperty.hasValues();
1273             } else {
1274                 return false;
1275             }
1276         }
1277
1278         return true;
1279     }
1280
1281     public int startAutomation(SingleResource resource,
1282             AttributeElement attribute, AutoUpdateType autoType,
1283             int autoUpdateInterval) {
1284         int autoId = -1;
1285         if (null != resource && null != attribute) {
1286             SimulatorSingleResource server = (SimulatorSingleResource) resource
1287                     .getSimulatorResource();
1288             if (null != server) {
1289                 String attrName = attribute.getSimulatorResourceAttribute()
1290                         .name();
1291                 try {
1292                     autoId = server.startAttributeUpdation(attrName, autoType,
1293                             autoUpdateInterval, automationListener);
1294                 } catch (SimulatorException e) {
1295                     Activator
1296                             .getDefault()
1297                             .getLogManager()
1298                             .log(Level.ERROR.ordinal(),
1299                                     new Date(),
1300                                     "[" + e.getClass().getSimpleName() + "]"
1301                                             + e.code().toString() + "-"
1302                                             + e.message());
1303                     return -1;
1304                 }
1305                 if (-1 != autoId) {
1306                     attribute.setAutoUpdateId(autoId);
1307                     attribute.setAutoUpdateType(autoType);
1308                     attribute.setAutoUpdateInterval(autoUpdateInterval);
1309                     attribute.setAutoUpdateState(true);
1310                     resource.setAttributeAutomationInProgress(true);
1311                 }
1312             }
1313         }
1314         return autoId;
1315     }
1316
1317     public void stopAutomation(SingleResource resource, AttributeElement att,
1318             int autoId) {
1319         if (null != resource) {
1320             SimulatorSingleResource server = (SimulatorSingleResource) resource
1321                     .getSimulatorResource();
1322             if (null != server) {
1323                 try {
1324                     server.stopUpdation(autoId);
1325                 } catch (SimulatorException e) {
1326                     Activator
1327                             .getDefault()
1328                             .getLogManager()
1329                             .log(Level.ERROR.ordinal(),
1330                                     new Date(),
1331                                     "[" + e.getClass().getSimpleName() + "]"
1332                                             + e.code().toString() + "-"
1333                                             + e.message());
1334                     return;
1335                 }
1336                 // Change the automation status
1337                 att.setAutoUpdateState(false);
1338                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1339             }
1340         }
1341     }
1342
1343     public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
1344             int autoUpdateInterval, final SingleResource resource) {
1345         if (null == resource) {
1346             return false;
1347         }
1348         boolean status = false;
1349         // Invoke the native automation method
1350         SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1351                 .getSimulatorResource();
1352         if (null != resourceServer) {
1353             int autoId = -1;
1354             try {
1355                 autoId = resourceServer.startResourceUpdation(autoType,
1356                         autoUpdateInterval, automationListener);
1357             } catch (SimulatorException e) {
1358                 Activator
1359                         .getDefault()
1360                         .getLogManager()
1361                         .log(Level.ERROR.ordinal(), new Date(),
1362                                 Utility.getSimulatorErrorString(e, null));
1363                 autoId = -1;
1364             }
1365             if (-1 != autoId) {
1366                 // Automation request accepted.
1367                 changeResourceLevelAutomationStatus(resource, true);
1368
1369                 resource.setAutomationId(autoId);
1370
1371                 // Notify the UI listeners in a different thread.
1372                 Thread notifyThread = new Thread() {
1373                     public void run() {
1374                         UiListenerHandler.getInstance()
1375                                 .resourceAutomationStartedUINotification(
1376                                         resource);
1377                     };
1378                 };
1379                 notifyThread.setPriority(Thread.MAX_PRIORITY);
1380                 notifyThread.start();
1381
1382                 status = true;
1383             }
1384         }
1385         return status;
1386     }
1387
1388     public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
1389         if (null == resource) {
1390             return false;
1391         }
1392         final int autoId = resource.getAutomationId();
1393         if (-1 == autoId) {
1394             return false;
1395         }
1396         SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1397                 .getSimulatorResource();
1398         if (null == resourceServer) {
1399             return false;
1400         }
1401         // Call native method
1402         try {
1403             resourceServer.stopUpdation(autoId);
1404         } catch (SimulatorException e) {
1405             Activator
1406                     .getDefault()
1407                     .getLogManager()
1408                     .log(Level.ERROR.ordinal(), new Date(),
1409                             Utility.getSimulatorErrorString(e, null));
1410             return false;
1411         }
1412
1413         // Notify the UI Listeners. Invoke the automation complete callback.
1414         Thread stopThread = new Thread() {
1415             public void run() {
1416                 automationListener.onUpdateComplete(resource.getResourceURI(),
1417                         autoId);
1418             }
1419         };
1420         stopThread.start();
1421         return true;
1422     }
1423
1424     private boolean isAnyAttributeInAutomation(SingleResource resource) {
1425         if (null == resource || null == resource.getResourceRepresentation()) {
1426             return false;
1427         }
1428
1429         Map<String, AttributeElement> attributes = resource
1430                 .getResourceRepresentation().getAttributes();
1431         if (null == attributes || 0 == attributes.size())
1432             return false;
1433
1434         for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1435             if (entry.getValue().isAutoUpdateInProgress())
1436                 return true;
1437         }
1438
1439         return false;
1440     }
1441
1442     // Changes the automation state of the resource and its attributes
1443     private void changeResourceLevelAutomationStatus(SingleResource resource,
1444             boolean status) {
1445
1446         if (null == resource || null == resource.getResourceRepresentation()) {
1447             return;
1448         }
1449
1450         Map<String, AttributeElement> attributes = resource
1451                 .getResourceRepresentation().getAttributes();
1452         if (null == attributes || 0 == attributes.size())
1453             return;
1454
1455         for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1456             entry.getValue().setAutoUpdateState(status);
1457         }
1458
1459         resource.setResourceAutomationInProgress(status);
1460     }
1461
1462     private AttributeElement getAttributeWithGivenAutomationId(
1463             SingleResource resource, int automationId) {
1464         if (null == resource || null == resource.getResourceRepresentation()) {
1465             return null;
1466         }
1467
1468         Map<String, AttributeElement> attributes = resource
1469                 .getResourceRepresentation().getAttributes();
1470         if (null == attributes || 0 == attributes.size())
1471             return null;
1472
1473         for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1474             if (automationId == entry.getValue().getAutoUpdateId())
1475                 return entry.getValue();
1476         }
1477
1478         return null;
1479     }
1480
1481     public boolean isResourceAutomationStarted(SingleResource resource) {
1482         boolean status = false;
1483         if (null != resource) {
1484             status = resource.isResourceAutomationInProgress();
1485         }
1486         return status;
1487     }
1488
1489     public boolean isAttributeAutomationStarted(SingleResource resource) {
1490         if (null == resource) {
1491             return false;
1492         }
1493         return resource.isAttributeAutomationInProgress();
1494     }
1495
1496     public void notifyObserverRequest(Resource resource, int observerId) {
1497         if (null == resource) {
1498             return;
1499         }
1500         SimulatorResource simulatorResource = resource.getSimulatorResource();
1501         if (null == simulatorResource) {
1502             return;
1503         }
1504         try {
1505             simulatorResource.notifyObserver(observerId);
1506         } catch (SimulatorException e) {
1507             Activator
1508                     .getDefault()
1509                     .getLogManager()
1510                     .log(Level.ERROR.ordinal(), new Date(),
1511                             Utility.getSimulatorErrorString(e, null));
1512         }
1513     }
1514
1515     public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1516         if (null == att) {
1517             return null;
1518         }
1519
1520         AttributeValue val = att.value();
1521         if (null == val) {
1522             return null;
1523         }
1524
1525         TypeInfo type = val.typeInfo();
1526
1527         AttributeProperty prop = att.property();
1528         if (null == prop) {
1529             return null;
1530         }
1531
1532         List<String> values = new ArrayList<String>();
1533
1534         if (type.mType != ValueType.RESOURCEMODEL) {
1535             if (type.mType == ValueType.ARRAY) {
1536                 if (type.mDepth == 1) {
1537                     ArrayProperty arrayProperty = prop.asArray();
1538                     if (null != arrayProperty) {
1539                         AttributeProperty childProp = arrayProperty
1540                                 .getElementProperty();
1541                         switch (childProp.getType()) {
1542                             case INTEGER:
1543                                 IntegerProperty intProperty = childProp
1544                                         .asInteger();
1545                                 if (null != intProperty) {
1546                                     values.addAll(getAllValues(intProperty,
1547                                             Type.INTEGER));
1548                                 }
1549                                 break;
1550                             case DOUBLE:
1551                                 DoubleProperty dblProperty = childProp
1552                                         .asDouble();
1553                                 if (null != dblProperty) {
1554                                     values.addAll(getAllValues(dblProperty,
1555                                             Type.DOUBLE));
1556                                 }
1557                                 break;
1558                             case BOOLEAN:
1559                                 BooleanProperty boolProperty = childProp
1560                                         .asBoolean();
1561                                 if (null != boolProperty) {
1562                                     values.addAll(getAllValues(boolProperty,
1563                                             Type.BOOLEAN));
1564                                 }
1565                                 break;
1566                             case STRING:
1567                                 StringProperty stringProperty = childProp
1568                                         .asString();
1569                                 if (null != stringProperty) {
1570                                     values.addAll(getAllValues(stringProperty,
1571                                             Type.STRING));
1572                                 }
1573                                 break;
1574                             default:
1575                                 break;
1576                         }
1577                     }
1578                 }
1579             } else {
1580                 switch (prop.getType()) {
1581                     case INTEGER:
1582                         IntegerProperty intProperty = prop.asInteger();
1583                         if (null != intProperty) {
1584                             values.addAll(getAllValues(intProperty,
1585                                     Type.INTEGER));
1586                         }
1587                         break;
1588                     case DOUBLE:
1589                         DoubleProperty dblProperty = prop.asDouble();
1590                         if (null != dblProperty) {
1591                             values.addAll(getAllValues(dblProperty, Type.DOUBLE));
1592                         }
1593                         break;
1594                     case BOOLEAN:
1595                         BooleanProperty boolProperty = prop.asBoolean();
1596                         if (null != boolProperty) {
1597                             values.addAll(getAllValues(boolProperty,
1598                                     Type.BOOLEAN));
1599                         }
1600                         break;
1601                     case STRING:
1602                         StringProperty stringProperty = prop.asString();
1603                         if (null != stringProperty) {
1604                             values.addAll(getAllValues(stringProperty,
1605                                     Type.STRING));
1606                         }
1607                         break;
1608                     default:
1609                         break;
1610                 }
1611             }
1612         }
1613
1614         return values;
1615     }
1616
1617     public List<String> getAllValues(IntegerProperty intProperty,
1618             AttributeProperty.Type type) {
1619         List<String> values = new ArrayList<String>();
1620
1621         if (intProperty.hasRange()) {
1622             int min = (int) intProperty.min();
1623             int max = (int) intProperty.max();
1624             for (int iVal = min; iVal <= max; iVal++) {
1625                 values.add(String.valueOf(iVal));
1626             }
1627         } else if (intProperty.hasValues()) {
1628             for (Integer val : intProperty.getValues()) {
1629                 values.add(String.valueOf(val));
1630             }
1631         } else {
1632             // Adding the default value.
1633             values.add(String.valueOf(intProperty.getDefaultValue()));
1634         }
1635         return values;
1636     }
1637
1638     public List<String> getAllValues(DoubleProperty dblProperty,
1639             AttributeProperty.Type type) {
1640         List<String> values = new ArrayList<String>();
1641
1642         if (dblProperty.hasRange()) {
1643             double min = (double) dblProperty.min();
1644             double max = (double) dblProperty.max();
1645             for (double iVal = min; iVal <= max; iVal = iVal + 1) {
1646                 values.add(String.valueOf(iVal));
1647             }
1648         } else if (dblProperty.hasValues()) {
1649             for (Double val : dblProperty.getValues()) {
1650                 values.add(String.valueOf(val));
1651             }
1652         } else {
1653             // Adding the default value.
1654             values.add(String.valueOf(dblProperty.getDefaultValue()));
1655         }
1656         return values;
1657     }
1658
1659     public List<String> getAllValues(BooleanProperty boolProperty,
1660             AttributeProperty.Type type) {
1661         List<String> values = new ArrayList<String>();
1662         values.add("true");
1663         values.add("false");
1664         return values;
1665     }
1666
1667     public List<String> getAllValues(StringProperty stringProperty,
1668             AttributeProperty.Type type) {
1669         List<String> values = new ArrayList<String>();
1670
1671         if (stringProperty.hasValues()) {
1672             for (String val : stringProperty.getValues()) {
1673                 values.add(String.valueOf(val));
1674             }
1675         } else {
1676             // Adding the default value.
1677             values.add(String.valueOf(stringProperty.getDefaultValue()));
1678         }
1679         return values;
1680     }
1681
1682     public int getResourceCount() {
1683         return data.getResourceCount();
1684     }
1685
1686     public void shutdown() {
1687         threadHandle.interrupt();
1688     }
1689 }