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