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