2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.serviceprovider.manager;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
27 import java.util.Vector;
29 import oic.simulator.serviceprovider.Activator;
30 import oic.simulator.serviceprovider.model.AttributeElement;
31 import oic.simulator.serviceprovider.model.CollectionResource;
32 import oic.simulator.serviceprovider.model.Device;
33 import oic.simulator.serviceprovider.model.MetaProperty;
34 import oic.simulator.serviceprovider.model.Resource;
35 import oic.simulator.serviceprovider.model.ResourceType;
36 import oic.simulator.serviceprovider.model.SingleResource;
37 import oic.simulator.serviceprovider.utils.Constants;
38 import oic.simulator.serviceprovider.utils.Utility;
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.SimulatorCollectionResource;
56 import org.oic.simulator.server.SimulatorResource;
57 import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
58 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
59 import org.oic.simulator.server.SimulatorResource.ObserverListener;
60 import org.oic.simulator.server.SimulatorResource.ResourceModelChangeListener;
61 import org.oic.simulator.server.SimulatorSingleResource;
64 * This class acts as an interface between the simulator java SDK and the
65 * various UI modules. It maintains all the details of resources and provides
66 * other UI modules with the information required. It also handles model change,
67 * automation, and observer related events from native layer and propagates
68 * those events to the registered UI listeners.
70 public class ResourceManager {
74 private Resource currentResourceInSelection;
76 private Device currentDeviceInSelection;
78 private ResourceModelChangeListener resourceModelChangeListener;
80 private AutoUpdateListener automationListener;
82 private ObserverListener observer;
84 private DeviceListener deviceListener;
86 private NotificationSynchronizerThread synchronizerThread;
88 private Thread threadHandle;
90 private DeviceInfo deviceInfo;
91 private PlatformInfo platformInfo;
93 private String deviceName;
95 public ResourceManager() {
98 // Set the default device and platform information
99 deviceName = "IoTivity Simulator";
101 SimulatorManager.setDeviceInfo(deviceName);
102 } catch (SimulatorException e) {
106 .log(Level.ERROR.ordinal(),
108 "Error while registering the device info.\n"
109 + Utility.getSimulatorErrorString(e, null));
112 platformInfo = new PlatformInfo();
113 platformInfo.setPlatformID("Samsung Platform Identifier");
114 platformInfo.setManufacturerName("Samsung");
115 platformInfo.setManufacturerUrl("www.samsung.com");
116 platformInfo.setModelNumber("Samsung Model Num01");
117 platformInfo.setDateOfManufacture("2015-09-10T11:10:30Z");
118 platformInfo.setPlatformVersion("PlatformVersion01");
119 platformInfo.setOperationSystemVersion("OSVersion01");
120 platformInfo.setHardwareVersion("HardwareVersion01");
121 platformInfo.setFirmwareVersion("FirwareVersion01");
122 platformInfo.setSupportUrl("http://www.samsung.com/support");
123 platformInfo.setSystemTime("2015-09-10T11:10:30Z");
125 SimulatorManager.setPlatformInfo(platformInfo);
126 } catch (SimulatorException e) {
130 .log(Level.ERROR.ordinal(),
132 "Error while registering the platform info.\n"
133 + Utility.getSimulatorErrorString(e, null));
136 deviceListener = new DeviceListener() {
139 public void onDeviceFound(final String host,
140 final DeviceInfo deviceInfo) {
141 if (null != ResourceManager.this.deviceInfo
142 || null == deviceInfo || null == host) {
145 synchronizerThread.addToQueue(new Runnable() {
148 String rcvdDeviceName = deviceInfo.getName();
149 if (null == rcvdDeviceName) {
152 if (deviceName.equalsIgnoreCase(rcvdDeviceName)) {
153 ResourceManager.this.deviceInfo = deviceInfo;
155 // Notify the UI Listeners
156 UiListenerHandler.getInstance()
157 .deviceInfoReceivedNotification();
164 // Get the device information to show other details of the device in UI.
166 SimulatorManager.findDevices("", deviceListener);
167 } catch (SimulatorException e) {
171 .log(Level.ERROR.ordinal(),
173 "Failed to get the local device information.\n"
174 + Utility.getSimulatorErrorString(e, null));
177 resourceModelChangeListener = new ResourceModelChangeListener() {
180 public void onResourceModelChanged(final String resourceURI,
181 final SimulatorResourceModel resourceModelN) {
182 synchronizerThread.addToQueue(new Runnable() {
186 if (null == resourceURI || null == resourceModelN) {
190 Display.getDefault().asyncExec(new Runnable() {
193 Resource resource = data
194 .getResourceByURI(resourceURI);
195 if (null != resource) {
197 resource.setResourceRepresentation(resourceModelN);
198 } catch (NumberFormatException e) {
202 .log(Level.ERROR.ordinal(),
204 "Error while trying to update the attributes.\n"
206 .getSimulatorErrorString(
218 automationListener = new AutoUpdateListener() {
221 public void onUpdateComplete(final String resourceURI,
222 final int automationId) {
223 synchronizerThread.addToQueue(new Runnable() {
227 SingleResource resource = data
228 .getSingleResourceByURI(resourceURI);
229 if (null == resource) {
232 // Checking whether this notification is for an
233 // attribute or a resource
234 if (resource.isResourceAutomationInProgress()) {
235 changeResourceLevelAutomationStatus(resource, false);
236 // Notify the UI listeners
237 UiListenerHandler.getInstance()
238 .automationCompleteUINotification(resource,
240 } else if (resource.isAttributeAutomationInProgress()) {
241 // Find the attribute with the given automation id
242 final AttributeElement attribute = getAttributeWithGivenAutomationId(
243 resource, automationId);
244 if (null != attribute) {
245 attribute.setAutoUpdateState(false);
246 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
248 // Setting the attribute automation status to
250 resource.setAttributeAutomationInProgress(false);
258 observer = new ObserverListener() {
260 public void onObserverChanged(final String resourceURI,
261 final int status, final Observer observer) {
265 if (null == resourceURI || null == observer) {
268 Resource resource = data.getResourceByURI(resourceURI);
269 if (null == resource) {
272 // Update the observers information
274 resource.addObserverInfo(observer);
276 resource.removeObserverInfo(observer);
278 // Notify the UI listeners
279 UiListenerHandler.getInstance()
280 .observerListChangedUINotification(resource);
286 public void onObserverAdded(String resourceURI, Observer observer) {
287 onObserverChanged(resourceURI, 0, observer);
291 public void onObserverRemoved(String resourceURI, Observer observer) {
292 onObserverChanged(resourceURI, 1, observer);
296 synchronizerThread = new NotificationSynchronizerThread();
297 threadHandle = new Thread(synchronizerThread);
298 threadHandle.setName("Simulator service provider event queue");
299 threadHandle.start();
302 private static class NotificationSynchronizerThread implements Runnable {
304 LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
308 while (!Thread.interrupted()) {
309 synchronized (this) {
311 while (notificationQueue.isEmpty()) {
315 } catch (InterruptedException e) {
321 synchronized (this) {
322 thread = notificationQueue.pop();
326 } catch (Exception e) {
327 if (e instanceof InterruptedException) {
335 public void addToQueue(Runnable event) {
336 synchronized (this) {
337 notificationQueue.add(event);
343 public void setDeviceInfo(List<MetaProperty> metaProperties) {
344 if (null == metaProperties || metaProperties.size() < 1) {
347 Iterator<MetaProperty> itr = metaProperties.iterator();
351 boolean found = false;
352 while (itr.hasNext()) {
354 propName = prop.getPropName();
355 propValue = prop.getPropValue();
356 if (propName.equals(Constants.DEVICE_NAME)) {
357 this.deviceName = propValue;
368 SimulatorManager.setDeviceInfo(deviceName);
369 } catch (SimulatorException e) {
373 .log(Level.ERROR.ordinal(),
375 "Error while registering the device info.\n"
376 + Utility.getSimulatorErrorString(e, null));
380 public boolean isDeviceInfoValid(List<MetaProperty> metaProperties) {
381 if (null == metaProperties || metaProperties.size() < 1) {
385 Iterator<MetaProperty> itr = metaProperties.iterator();
389 while (itr.hasNext()) {
391 propName = prop.getPropName();
392 propValue = prop.getPropValue();
393 if (propName.equals(Constants.DEVICE_NAME)) {
394 if (null == propValue || propValue.length() < 1) {
403 public List<MetaProperty> getDeviceInfo() {
404 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
405 metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, deviceName));
406 if (null != deviceInfo) {
407 metaProperties.add(new MetaProperty(Constants.DEVICE_ID, deviceInfo
409 metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
410 deviceInfo.getSpecVersion()));
411 metaProperties.add(new MetaProperty(Constants.DEVICE_DMV,
412 deviceInfo.getDataModelVersion()));
414 return metaProperties;
417 public List<MetaProperty> getPlatformInfo() {
418 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
419 metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
421 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
422 platformInfo.getManufacturerName()));
423 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
424 platformInfo.getManufacturerUrl()));
425 metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
426 platformInfo.getModelNumber()));
427 metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
428 platformInfo.getDateOfManufacture()));
429 metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
430 platformInfo.getPlatformVersion()));
431 metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
432 platformInfo.getOperationSystemVersion()));
433 metaProperties.add(new MetaProperty(
434 Constants.PLATFORM_HARDWARE_VERSION, platformInfo
435 .getHardwareVersion()));
436 metaProperties.add(new MetaProperty(
437 Constants.PLATFORM_FIRMWARE_VERSION, platformInfo
438 .getFirmwareVersion()));
439 metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
440 platformInfo.getSupportUrl()));
441 metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
442 platformInfo.getSystemTime()));
443 return metaProperties;
446 public void setPlatformInfo(List<MetaProperty> metaProperties) {
447 if (null == metaProperties || metaProperties.size() < 1) {
450 Iterator<MetaProperty> itr = metaProperties.iterator();
454 while (itr.hasNext()) {
456 propName = prop.getPropName();
457 propValue = prop.getPropValue();
458 if (propName.equals(Constants.PLATFORM_ID)) {
459 platformInfo.setPlatformID(propValue);
460 } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) {
461 platformInfo.setManufacturerName(propValue);
462 } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) {
463 platformInfo.setManufacturerUrl(propValue);
464 } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) {
465 platformInfo.setModelNumber(propValue);
466 } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) {
467 platformInfo.setDateOfManufacture(propValue);
468 } else if (propName.equals(Constants.PLATFORM_VERSION)) {
469 platformInfo.setPlatformVersion(propValue);
470 } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) {
471 platformInfo.setOperationSystemVersion(propValue);
472 } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) {
473 platformInfo.setHardwareVersion(propValue);
474 } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) {
475 platformInfo.setFirmwareVersion(propValue);
476 } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) {
477 platformInfo.setSupportUrl(propValue);
478 } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) {
479 platformInfo.setSystemTime(propValue);
483 SimulatorManager.setPlatformInfo(platformInfo);
484 } catch (SimulatorException e) {
488 .log(Level.ERROR.ordinal(),
490 "Error while registering the platform info.\n"
491 + Utility.getSimulatorErrorString(e, null));
495 public boolean isPlatformInfoValid(List<MetaProperty> metaProperties) {
496 if (null == metaProperties || metaProperties.size() < 1) {
499 Iterator<MetaProperty> itr = metaProperties.iterator();
502 while (itr.hasNext()) {
504 propValue = prop.getPropValue();
505 if (null == propValue || propValue.length() < 1) {
512 public synchronized Resource getCurrentResourceInSelection() {
513 return currentResourceInSelection;
516 public synchronized void setCurrentResourceInSelection(Resource resource) {
517 this.currentResourceInSelection = resource;
520 public synchronized Device getCurrentDeviceInSelection() {
521 return currentDeviceInSelection;
524 public synchronized void setCurrentDeviceInSelection(Device dev) {
525 this.currentDeviceInSelection = dev;
528 public boolean isResourceExist(String resourceURI) {
529 return data.isResourceExist(resourceURI);
532 public boolean isAnyResourceExist() {
533 return data.isAnyResourceExist();
536 public boolean createSingleResource(SingleResource resource,
537 Map<String, SimulatorResourceAttribute> attributes)
538 throws SimulatorException {
539 if (null == resource) {
542 String resType = (String) resource.getResourceTypes().toArray()[0];
544 // 1. Create the resource.
545 SimulatorResource jSimulatorResource = SimulatorManager
546 .createResource(SimulatorResource.Type.SINGLE,
547 resource.getResourceName(),
548 resource.getResourceURI(), resType);
549 if (null == jSimulatorResource
550 || !(jSimulatorResource instanceof SimulatorSingleResource)) {
553 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
554 resource.setSimulatorResource(jSimulatorSingleResource);
556 // 2. Cancel observable property if requested by user.
557 if (!resource.isObservable()) {
558 jSimulatorSingleResource.setObservable(false);
561 // 3. Set the model change listener.
562 jSimulatorSingleResource
563 .setResourceModelChangeListener(resourceModelChangeListener);
565 // 4. Set the observer listener if the resource is observable.
566 if (resource.isObservable()) {
567 jSimulatorSingleResource.setObserverListener(observer);
570 // 5. Add attributes.
571 if (null != attributes && !attributes.isEmpty()) {
572 SimulatorResourceAttribute value;
573 for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
575 value = entry.getValue();
577 jSimulatorSingleResource.addAttribute(value);
580 // 6. Get the resource model java object reference.
581 resource.setResourceModel(jSimulatorSingleResource
582 .getResourceModel());
584 resource.setResourceRepresentation(resource.getResourceModel());
587 // 6. Register the resource with the platform.
588 jSimulatorSingleResource.start();
589 resource.setStarted(true);
590 } catch (SimulatorException e) {
594 .log(Level.ERROR.ordinal(), new Date(),
595 Utility.getSimulatorErrorString(e, null));
599 // 8. Add to local cache.
600 data.addResource(resource);
602 // 9. Update UI listeners
603 UiListenerHandler.getInstance().resourceCreatedUINotification(
604 ResourceType.SINGLE);
609 public boolean createCollectionResource(CollectionResource resource)
610 throws SimulatorException {
611 if (null == resource) {
614 String resType = (String) resource.getResourceTypes().toArray()[0];
616 // 1. Create the resource.
617 SimulatorResource jSimulatorResource = SimulatorManager
618 .createResource(SimulatorResource.Type.COLLECTION,
619 resource.getResourceName(),
620 resource.getResourceURI(), resType);
621 if (null == jSimulatorResource
622 || !(jSimulatorResource instanceof SimulatorCollectionResource)) {
625 SimulatorCollectionResource jSimulatorCollectionResource = (SimulatorCollectionResource) jSimulatorResource;
626 resource.setSimulatorResource(jSimulatorCollectionResource);
628 // 2. Cancel observable property if requested by user.
629 if (!resource.isObservable()) {
630 jSimulatorCollectionResource.setObservable(false);
633 // 3. Set the observer listener if the resource is observable.
634 if (resource.isObservable()) {
635 jSimulatorCollectionResource.setObserverListener(observer);
638 // 4. Set the model change listener.
639 jSimulatorCollectionResource
640 .setResourceModelChangeListener(resourceModelChangeListener);
642 // 5. Fetch the model and attributes.
643 resource.setResourceRepresentation(jSimulatorCollectionResource
644 .getResourceModel());
646 // 6. Register the resource with the platform.
647 jSimulatorCollectionResource.start();
648 resource.setStarted(true);
649 } catch (SimulatorException e) {
653 .log(Level.ERROR.ordinal(), new Date(),
654 Utility.getSimulatorErrorString(e, null));
658 // 6. Add to local cache.
659 data.addResource(resource);
661 // 7. Update UI listeners
662 UiListenerHandler.getInstance().resourceCreatedUINotification(
663 ResourceType.COLLECTION);
668 public Resource createResourceByRAML(String configFilePath)
669 throws SimulatorException {
670 Resource resource = null;
672 // 1. Create the resource
673 SimulatorResource jSimulatorResource = SimulatorManager
674 .createResource(configFilePath);
675 if (null == jSimulatorResource) {
678 if (jSimulatorResource instanceof SimulatorSingleResource) {
679 resource = new SingleResource();
681 resource = new CollectionResource();
683 resource.setSimulatorResource(jSimulatorResource);
685 // 2. Fetch and locally store the resource name and uri.
686 String uri = jSimulatorResource.getURI();
687 if (null == uri || uri.trim().isEmpty()) {
690 resource.setResourceURI(uri.trim());
692 String name = jSimulatorResource.getName();
693 if (null == name || name.trim().isEmpty()) {
696 resource.setResourceName(name.trim());
697 } catch (SimulatorException e) {
701 .log(Level.ERROR.ordinal(), new Date(),
702 Utility.getSimulatorErrorString(e, null));
709 * This method can set/change the resource uri and name of an already
710 * created resource which is not yet registered with the platform. This
711 * method registers the model change and observer listeners, registers the
712 * resource, fetches the resource attributes, updates the local cache and
713 * notifies the UI listeners.
715 public boolean completeSingleResourceCreationByRAML(Resource resource,
716 String uri, String name, boolean multiInstance)
717 throws SimulatorException {
718 if (null == resource || !(resource instanceof SingleResource)) {
722 SingleResource singleRes = (SingleResource) resource;
724 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
725 .getSimulatorResource();
726 if (null == jSimulatorSingleResource) {
730 // 1. Update resource URI and Name if they are changed.
731 String newUri = uri.trim();
732 String newName = name.trim();
735 singleRes.setResourceURI(newUri);
736 singleRes.setResourceName(newName);
738 if (!singleRes.getResourceURI().equals(newUri)) {
739 jSimulatorSingleResource.setURI(newUri);
740 singleRes.setResourceURI(newUri);
742 if (!singleRes.getResourceName().equals(newName)) {
743 jSimulatorSingleResource.setName(newName);
744 singleRes.setResourceName(newName);
748 // 2. Set the model change listener.
749 jSimulatorSingleResource
750 .setResourceModelChangeListener(resourceModelChangeListener);
752 // 3. Set the observer listener if the resource is observable.
753 if (jSimulatorSingleResource.isObservable()) {
754 jSimulatorSingleResource.setObserverListener(observer);
755 singleRes.setObservable(true);
758 // 4. Fetch the resource model.
759 SimulatorResourceModel jResModel = jSimulatorSingleResource
761 if (null == jResModel) {
764 singleRes.setResourceModel(jResModel);
766 // 5. Fetch the basic details of the resource.
767 singleRes.addResourceType(jSimulatorSingleResource
770 .setResourceInterfaces(Utility
771 .convertVectorToSet(jSimulatorSingleResource
774 // 6. Fetch the resource attributes.
775 singleRes.setResourceRepresentation(jResModel);
777 // 7. Register the resource with the platform.
778 jSimulatorSingleResource.start();
779 singleRes.setStarted(true);
781 // 8. Add to local cache.
782 data.addResource(singleRes);
784 // 9. Update UI listeners for single instance creation
786 UiListenerHandler.getInstance().resourceCreatedUINotification(
787 ResourceType.SINGLE);
788 } catch (Exception e) {
792 .log(Level.ERROR.ordinal(), new Date(),
793 Utility.getSimulatorErrorString(e, null));
800 * This method can set/change the resource uri and name of an already
801 * created resource which is not yet registered with the platform. This
802 * method registers the model change and observer listeners, registers the
803 * resource, fetches the resource attributes, updates the local cache and
804 * notifies the UI listeners.
806 public boolean completeCollectionResourceCreationByRAML(Resource resource,
807 String uri, String name) throws SimulatorException {
808 if (null == resource || !(resource instanceof CollectionResource)) {
812 CollectionResource collectionRes = (CollectionResource) resource;
814 SimulatorCollectionResource jSimulatorCollectionResource = null;
817 jSimulatorCollectionResource = (SimulatorCollectionResource) resource
818 .getSimulatorResource();
819 if (null == jSimulatorCollectionResource) {
823 // 1. Update resource URI and Name if they are changed.
824 String newUri = uri.trim();
825 String newName = name.trim();
827 if (!collectionRes.getResourceURI().equals(newUri)) {
828 jSimulatorCollectionResource.setURI(newUri);
829 collectionRes.setResourceURI(newUri);
831 if (!collectionRes.getResourceName().equals(newName)) {
832 jSimulatorCollectionResource.setName(newName);
833 collectionRes.setResourceName(newName);
836 // 2. Set the model change listener.
837 jSimulatorCollectionResource
838 .setResourceModelChangeListener(resourceModelChangeListener);
840 // 3. Fetch the resource model.
841 SimulatorResourceModel jResModel = jSimulatorCollectionResource
843 if (null == jResModel) {
846 collectionRes.setResourceModel(jResModel);
848 // 4. Fetch the basic details of the resource.
849 collectionRes.addResourceType(jSimulatorCollectionResource
851 collectionRes.setResourceInterfaces(Utility
852 .convertVectorToSet(jSimulatorCollectionResource
855 // 5. Set the observer listener if the resource is observable.
856 if (jSimulatorCollectionResource.isObservable()) {
857 jSimulatorCollectionResource.setObserverListener(observer);
858 collectionRes.setObservable(true);
861 // 6. Fetch the resource attributes.
862 collectionRes.setResourceRepresentation(jResModel);
864 // 7. Register the resource with the platform.
865 jSimulatorCollectionResource.start();
866 collectionRes.setStarted(true);
868 // 8. Add to local cache.
869 data.addResource(collectionRes);
871 // 9. Update UI listeners for single instance creation
872 UiListenerHandler.getInstance().resourceCreatedUINotification(
873 ResourceType.COLLECTION);
874 } catch (Exception e) {
878 .log(Level.ERROR.ordinal(), new Date(),
879 Utility.getSimulatorErrorString(e, null));
885 public int createSingleResourceMultiInstances(String configFile, int count)
886 throws SimulatorException {
889 Vector<SimulatorResource> jSimulatorResources = SimulatorManager
890 .createResource(configFile, count);
891 if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
894 SimulatorSingleResource jResource;
895 SingleResource resource;
897 for (SimulatorResource jSimulatorResource : jSimulatorResources) {
898 jResource = (SimulatorSingleResource) jSimulatorResource;
899 resource = new SingleResource();
900 resource.setSimulatorResource(jResource);
902 result = completeSingleResourceCreationByRAML(resource,
903 jResource.getURI(), jResource.getName(), true);
907 } catch (SimulatorException eInner) {
911 .log(Level.ERROR.ordinal(),
913 Utility.getSimulatorErrorString(eInner,
917 if (createCount > 0) {
918 UiListenerHandler.getInstance().resourceCreatedUINotification(
919 ResourceType.SINGLE);
921 } catch (SimulatorException eOuter) {
925 .log(Level.ERROR.ordinal(), new Date(),
926 Utility.getSimulatorErrorString(eOuter, null));
932 public Device createDevice(String deviceName, Set<Resource> childs) {
934 Device dev = new Device();
935 dev.setDeviceName(deviceName);
938 // 2. Add children to device
939 if (null != childs && !childs.isEmpty())
940 addResourceToDevice(dev, childs);
942 // 3. Update ui listeners
943 UiListenerHandler.getInstance().resourceListUpdateUINotification(
944 ResourceType.DEVICE);
948 public List<Resource> getResourceList() {
949 List<Resource> resourceList = data.getResources();
950 if (null == resourceList) {
954 Collections.sort(resourceList, Utility.resourceComparator);
959 public List<SingleResource> getSingleResourceList() {
960 List<SingleResource> resourceList = data.getSingleResources();
961 if (null == resourceList) {
965 Collections.sort(resourceList, Utility.singleResourceComparator);
970 public List<CollectionResource> getCollectionResourceList() {
971 List<CollectionResource> resourceList = data.getCollectionResources();
972 if (null == resourceList) {
976 Collections.sort(resourceList, Utility.collectionResourceComparator);
981 public List<Device> getDeviceList() {
982 List<Device> deviceList = data.getDevices();
983 if (null == deviceList) {
987 Collections.sort(deviceList, Utility.deviceComparator);
991 // Returns the number of resources which are added properly to the
993 public int addResourceToCollection(CollectionResource collectionParent,
994 Set<Resource> childs) {
995 if (null == collectionParent || null == childs || childs.isEmpty()) {
998 Iterator<Resource> itr = childs.iterator();
1000 int count = childs.size();
1001 while (itr.hasNext()) {
1004 addResourceToCollection(collectionParent, res);
1005 } catch (SimulatorException e) {
1012 public void addResourceToCollection(CollectionResource collectionParent,
1013 Resource child) throws SimulatorException {
1014 if (null == collectionParent || null == child) {
1018 // 1. Add child to collection
1019 collectionParent.addChildResource(child);
1021 // 2. Add a reference to the collection in the child
1022 if (child instanceof SingleResource) {
1023 ((SingleResource) child)
1024 .addCollectionMembership(collectionParent);
1026 ((CollectionResource) child).addMembership(collectionParent);
1028 } catch (SimulatorException e) {
1032 .log(Level.ERROR.ordinal(), new Date(),
1033 Utility.getSimulatorErrorString(e, null));
1038 public int addResourceToCollection(Set<CollectionResource> collections,
1040 if (null == collections || collections.isEmpty() || null == child) {
1043 Iterator<CollectionResource> itr = collections.iterator();
1044 CollectionResource res;
1045 int count = collections.size();
1046 while (itr.hasNext()) {
1049 addResourceToCollection(res, child);
1050 } catch (SimulatorException e) {
1057 public void addResourceToDevice(Device dev, Set<Resource> childs) {
1058 // 1. Add children to the device.
1059 dev.addChildResource(childs);
1061 // 2. Add a reference to the device in all children.
1062 Iterator<Resource> itr = childs.iterator();
1064 while (itr.hasNext()) {
1066 if (res instanceof SingleResource) {
1067 ((SingleResource) res).addDeviceMembership(dev);
1069 ((CollectionResource) res).addDeviceMembership(dev);
1074 public void addResourceToDevice(Device dev, Resource child) {
1075 // 1. Add child to the device.
1076 dev.addChildResource(child);
1078 // 2. Add a reference to the device in the child.
1079 if (child instanceof SingleResource) {
1080 ((SingleResource) child).addDeviceMembership(dev);
1082 ((CollectionResource) child).addDeviceMembership(dev);
1086 public void addResourceToDevice(Set<Device> devices, Resource child) {
1087 // 1. Add device reference in child.
1088 if (child instanceof SingleResource)
1089 ((SingleResource) child).addDeviceMembership(devices);
1091 ((CollectionResource) child).addDeviceMembership(devices);
1093 // 2. Add a reference to the child in all devices.
1094 Iterator<Device> itr = devices.iterator();
1096 while (itr.hasNext()) {
1098 dev.addChildResource(child);
1102 public int removeResourceFromCollection(
1103 Set<CollectionResource> collections, Resource resource) {
1104 // 1. Remove the reference of resource from all the collections.
1105 Iterator<CollectionResource> itr = collections.iterator();
1106 CollectionResource colRes;
1107 int count = collections.size();
1108 while (itr.hasNext()) {
1109 colRes = itr.next();
1111 removeResourceFromCollection(colRes, resource);
1112 } catch (SimulatorException e) {
1120 public void removeResourceFromDevice(Set<Device> devices, Resource resource) {
1121 // 1. Remove the reference of resource from all the devices.
1122 Iterator<Device> itr = devices.iterator();
1124 while (itr.hasNext()) {
1126 dev.removeChildResource(resource);
1129 // 2. Remove the reference of devices from the resource.
1130 resource.removeDeviceMembership(devices);
1133 // Returns the count of resources removed from the collection
1134 public int removeResourcesFromCollection(CollectionResource colRes,
1135 Set<Resource> resources) {
1136 Iterator<Resource> itr = resources.iterator();
1138 int count = resources.size();
1139 while (itr.hasNext()) {
1142 removeResourceFromCollection(colRes, res);
1143 } catch (SimulatorException e) {
1150 public void removeResourcesFromDevice(Device dev, Set<Resource> resources) {
1151 Iterator<Resource> itr = resources.iterator();
1153 while (itr.hasNext()) {
1155 res.removeDeviceMembership(dev);
1157 dev.removeChildResource(resources);
1160 public void removeResourceFromCollection(CollectionResource parent,
1161 Resource child) throws SimulatorException {
1163 // 1. Remove the child from the parent
1164 parent.removeChildResource(child);
1166 // 2. Remove the reference to parent from child
1167 if (child instanceof SingleResource) {
1168 ((SingleResource) child).removeCollectionMembership(parent);
1170 ((CollectionResource) child).removeMembership(parent);
1172 } catch (SimulatorException e) {
1176 .log(Level.ERROR.ordinal(), new Date(),
1177 Utility.getSimulatorErrorString(e, null));
1182 public void removeResourceFromDevice(Device parent, Resource child) {
1183 // 1. Remove the reference to parent from child
1184 child.removeDeviceMembership(parent);
1186 // 2. Remove the child from the parent
1187 parent.removeChildResource(child);
1190 public void removeSingleResources(Set<SingleResource> resources)
1191 throws SimulatorException {
1192 if (null == resources) {
1195 Iterator<SingleResource> itr = resources.iterator();
1196 while (itr.hasNext()) {
1197 removeResource(itr.next());
1201 public void removeCollectionResources(Set<CollectionResource> resources)
1202 throws SimulatorException {
1203 if (null == resources) {
1206 Iterator<CollectionResource> itr = resources.iterator();
1207 while (itr.hasNext()) {
1208 removeResource(itr.next());
1212 public void removeResource(Resource res) throws SimulatorException {
1213 // 1. Unregister the resource from the platform.
1214 SimulatorResource simRes = res.getSimulatorResource();
1217 } catch (SimulatorException e) {
1221 .log(Level.ERROR.ordinal(), new Date(),
1222 Utility.getSimulatorErrorString(e, null));
1226 Set<CollectionResource> collectionMembership;
1227 Set<Device> deviceMembership;
1229 if (res instanceof SingleResource) {
1230 collectionMembership = ((SingleResource) res)
1231 .getCollectionMembership();
1232 deviceMembership = ((SingleResource) res).getDeviceMembership();
1234 collectionMembership = ((CollectionResource) res).getMembership();
1235 deviceMembership = ((CollectionResource) res).getDeviceMembership();
1238 // 2. Delete from the collections to which this resource is a member.
1239 if (null != collectionMembership && !collectionMembership.isEmpty()) {
1240 removeResourceFromCollection(collectionMembership, res);
1243 // 3. Delete from the devices to which this resource is a member.
1244 if (null != deviceMembership && !deviceMembership.isEmpty()) {
1245 removeResourceFromDevice(deviceMembership, res);
1248 // 4. Delete this resource
1249 data.deleteResource(res);
1252 public void removeDevice(Device dev) {
1253 Set<Resource> childs = dev.getChildResources();
1254 if (null != childs && !childs.isEmpty()) {
1255 // 1. Remove the reference from all the children.
1256 Iterator<Resource> itr = childs.iterator();
1258 while (itr.hasNext()) {
1260 res.removeDeviceMembership(dev);
1263 // 2. Delete the device.
1264 data.deleteDevice(dev);
1267 public boolean isUriUnique(List<MetaProperty> properties) {
1268 if (null == properties) {
1272 Iterator<MetaProperty> itr = properties.iterator();
1273 while (itr.hasNext()) {
1275 if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
1276 String uri = prop.getPropValue();
1277 return !data.isResourceExist(uri);
1283 public List<CollectionResource> getCollectionsForAddingToSingleResource(
1284 SingleResource resource) {
1285 List<CollectionResource> collectionResources = data
1286 .getCollectionResources();
1287 if (null == collectionResources || collectionResources.isEmpty()) {
1291 Set<CollectionResource> collectionMembership;
1292 collectionMembership = resource.getCollectionMembership();
1293 if (null == collectionMembership || collectionMembership.isEmpty()) {
1294 return collectionResources;
1297 if (collectionMembership.size() == collectionResources.size()) {
1301 collectionResources.removeAll(collectionMembership);
1304 Collections.sort(collectionResources,
1305 Utility.collectionResourceComparator);
1307 return collectionResources;
1310 public List<SingleResource> getSingleTypeResourcesForAddingToCollectionResource(
1311 CollectionResource colRes) {
1312 List<SingleResource> singleResources = data.getSingleResources();
1313 if (null == singleResources || singleResources.isEmpty()) {
1317 Set<SingleResource> childs;
1318 childs = colRes.getSingleTypeChildResources();
1319 if (null == childs || childs.isEmpty()) {
1320 return singleResources;
1323 if (childs.size() == singleResources.size()) {
1327 singleResources.removeAll(childs);
1330 Collections.sort(singleResources, Utility.singleResourceComparator);
1332 return singleResources;
1335 public List<SingleResource> getSingleTypeResourcesForAddingToDevice(
1337 List<SingleResource> singleResources = data.getSingleResources();
1338 if (null == singleResources || singleResources.isEmpty()) {
1342 Set<SingleResource> childs;
1343 childs = dev.getSingleTypeChildResources();
1344 if (null == childs || childs.isEmpty()) {
1345 return singleResources;
1348 if (childs.size() == singleResources.size()) {
1352 singleResources.removeAll(childs);
1355 Collections.sort(singleResources, Utility.singleResourceComparator);
1357 return singleResources;
1360 public List<CollectionResource> getCollectionTypeResourcesForAddingToCollectionResource(
1361 CollectionResource colRes) {
1362 List<CollectionResource> collectionResources = data
1363 .getCollectionResources();
1364 if (null == collectionResources || collectionResources.isEmpty()) {
1368 // Remove the colRes from the list
1369 collectionResources.remove(colRes);
1371 Set<CollectionResource> childs;
1372 childs = colRes.getCollectionTypeChildResources();
1373 if (null == childs || childs.isEmpty()) {
1374 return collectionResources;
1377 if (childs.size() == collectionResources.size()) {
1381 collectionResources.removeAll(childs);
1384 Collections.sort(collectionResources,
1385 Utility.collectionResourceComparator);
1387 return collectionResources;
1390 public List<CollectionResource> getCollectionTypeResourcesForAddingToDevice(
1392 List<CollectionResource> collectionResources = data
1393 .getCollectionResources();
1394 if (null == collectionResources || collectionResources.isEmpty()) {
1398 Set<CollectionResource> childs;
1399 childs = dev.getCollectionTypeChildResources();
1400 if (null == childs || childs.isEmpty()) {
1401 return collectionResources;
1404 if (childs.size() == collectionResources.size()) {
1408 collectionResources.removeAll(childs);
1411 Collections.sort(collectionResources,
1412 Utility.collectionResourceComparator);
1414 return collectionResources;
1417 public List<Device> getDevicesForAddingToResource(Resource resource) {
1418 List<Device> devices = data.getDevices();
1419 if (null == devices || devices.isEmpty()) {
1423 Set<Device> deviceMembership;
1424 if (resource instanceof SingleResource) {
1425 deviceMembership = ((SingleResource) resource)
1426 .getDeviceMembership();
1428 deviceMembership = ((CollectionResource) resource)
1429 .getDeviceMembership();
1431 if (null == deviceMembership || deviceMembership.isEmpty()) {
1435 if (devices.size() == deviceMembership.size()) {
1439 devices.removeAll(deviceMembership);
1442 Collections.sort(devices, Utility.deviceComparator);
1447 public List<CollectionResource> getResourceReferences(
1448 SingleResource resource) {
1449 List<CollectionResource> resources = Utility
1450 .getCollectionResourceListFromSet(resource
1451 .getCollectionMembership());
1452 if (null == resources || resources.isEmpty()) {
1456 Collections.sort(resources, Utility.collectionResourceComparator);
1461 public List<Device> getDeviceReferences(Resource resource) {
1462 Set<Device> deviceMembership;
1463 if (resource instanceof SingleResource) {
1464 deviceMembership = ((SingleResource) resource)
1465 .getDeviceMembership();
1467 deviceMembership = ((CollectionResource) resource)
1468 .getDeviceMembership();
1471 List<Device> devices = Utility.getDeviceListFromSet(deviceMembership);
1472 if (null == devices || devices.isEmpty()) {
1476 Collections.sort(devices, Utility.deviceComparator);
1481 public List<SingleResource> getSingleTypeChilds(CollectionResource colRes) {
1482 Set<SingleResource> childs = colRes.getSingleTypeChildResources();
1483 return Utility.getSingleResourceListFromSet(childs);
1486 public List<SingleResource> getSingleTypeChilds(Device dev) {
1487 Set<SingleResource> childs = dev.getSingleTypeChildResources();
1488 return Utility.getSingleResourceListFromSet(childs);
1491 public List<CollectionResource> getCollectionTypeChilds(
1492 CollectionResource colRes) {
1493 Set<CollectionResource> childs = colRes
1494 .getCollectionTypeChildResources();
1495 return Utility.getCollectionResourceListFromSet(childs);
1498 public List<CollectionResource> getCollectionTypeChilds(Device dev) {
1499 Set<CollectionResource> childs = dev.getCollectionTypeChildResources();
1500 return Utility.getCollectionResourceListFromSet(childs);
1503 public void resourceSelectionChanged(final Resource selectedResource) {
1508 setCurrentDeviceInSelection(null);
1510 if (null != selectedResource) {
1511 setCurrentResourceInSelection(selectedResource);
1513 setCurrentResourceInSelection(null);
1515 // Notify all observers for resource selection change event
1516 UiListenerHandler.getInstance()
1517 .resourceSelectionChangedUINotification(
1523 public void deviceSelectionChanged(final Device selectedDevice) {
1528 setCurrentResourceInSelection(null);
1530 if (null != selectedDevice) {
1531 setCurrentDeviceInSelection(selectedDevice);
1533 setCurrentDeviceInSelection(null);
1535 // Notify all observers for resource selection change event
1536 UiListenerHandler.getInstance()
1537 .deviceSelectionChangedUINotification(selectedDevice);
1542 public List<MetaProperty> getMetaProperties(Resource resource) {
1543 if (null != resource) {
1547 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1549 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1550 propName = Constants.META_PROPERTIES[index];
1551 if (propName.equals(Constants.RESOURCE_NAME)) {
1552 propValue = resource.getResourceName();
1553 } else if (propName.equals(Constants.RESOURCE_URI)) {
1554 propValue = resource.getResourceURI();
1555 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1556 Set<String> resTypes = resource.getResourceTypes();
1557 if (null != resTypes && !resTypes.isEmpty()) {
1559 Iterator<String> itr = resTypes.iterator();
1560 while (itr.hasNext()) {
1561 propValue += itr.next();
1562 if (itr.hasNext()) {
1572 if (null != propValue) {
1573 metaPropertyList.add(new MetaProperty(propName, propValue));
1576 return metaPropertyList;
1581 public List<MetaProperty> getMetaProperties(Device dev) {
1583 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1584 metaPropertyList.add(new MetaProperty(Constants.DEVICE_NAME, dev
1586 return metaPropertyList;
1591 public boolean startResource(Resource resource) throws SimulatorException {
1592 if (null == resource) {
1595 SimulatorResource server = resource.getSimulatorResource();
1596 if (null == server) {
1601 resource.setStarted(true);
1602 } catch (SimulatorException e) {
1606 .log(Level.ERROR.ordinal(),
1608 "There is an error while starting the resource.\n"
1609 + Utility.getSimulatorErrorString(e, null));
1615 public boolean stopResource(Resource resource) throws SimulatorException {
1616 if (null == resource) {
1619 SimulatorResource server = resource.getSimulatorResource();
1620 if (null == server) {
1625 resource.setStarted(false);
1626 } catch (SimulatorException e) {
1630 .log(Level.ERROR.ordinal(),
1632 "There is an error while stopping the resource.\n"
1633 + Utility.getSimulatorErrorString(e, null));
1639 public boolean changeResourceName(Resource resource, String newName)
1640 throws SimulatorException {
1641 if (null == resource || null == newName) {
1645 if (!stopResource(resource)) {
1649 SimulatorResource server = resource.getSimulatorResource();
1651 server.setName(newName);
1652 resource.setResourceName(newName);
1653 } catch (SimulatorException e) {
1657 .log(Level.ERROR.ordinal(),
1659 "There is an error while changing the resource name.\n"
1660 + Utility.getSimulatorErrorString(e, null));
1664 if (!startResource(resource)) {
1671 public boolean changeDeviceName(Device dev, String newName) {
1672 if (null == dev || null == newName) {
1675 data.changeDeviceName(dev, dev.getDeviceName(), newName);
1679 public boolean changeResourceURI(Resource resource, String newURI)
1680 throws SimulatorException {
1681 if (null == resource || null == newURI) {
1685 if (!stopResource(resource)) {
1689 String curURI = resource.getResourceURI();
1690 setResourceURI(resource, newURI);
1693 if (!startResource(resource)) {
1696 } catch (SimulatorException e) {
1697 setResourceURI(resource, curURI);
1703 public void setResourceURI(Resource resource, String newURI)
1704 throws SimulatorException {
1705 String curURI = resource.getResourceURI();
1706 SimulatorResource server = resource.getSimulatorResource();
1708 server.setURI(newURI);
1709 data.changeResourceURI(resource, curURI, newURI);
1710 } catch (SimulatorException e) {
1714 .log(Level.ERROR.ordinal(),
1716 "There is an error while changing the resource URI.\n"
1717 + Utility.getSimulatorErrorString(e, null));
1722 public boolean updateResourceProperties(Resource resource,
1723 List<MetaProperty> properties, boolean uriChanged,
1724 boolean nameChanged) throws SimulatorException {
1725 if (null == resource || null == properties) {
1729 // Updating the properties
1730 Iterator<MetaProperty> itr = properties.iterator();
1731 MetaProperty property;
1734 String resName = null;
1735 String resURI = null;
1736 while (itr.hasNext()) {
1737 property = itr.next();
1738 if (null == property) {
1741 propName = property.getPropName();
1742 propValue = property.getPropValue();
1743 if (propName.equals(Constants.RESOURCE_NAME)) {
1744 resName = propValue;
1745 } else if (propName.equals(Constants.RESOURCE_URI)) {
1751 if (!changeResourceName(resource, resName)) {
1755 // Notify UI Listeners
1756 UiListenerHandler.getInstance().propertiesChangedUINotification(
1761 if (!changeResourceURI(resource, resURI)) {
1769 public boolean updateDeviceProperties(Device dev,
1770 List<MetaProperty> properties) {
1771 if (null == dev || null == properties) {
1775 // Updating the properties
1776 Iterator<MetaProperty> itr = properties.iterator();
1777 MetaProperty property;
1780 String devName = null;
1781 while (itr.hasNext()) {
1782 property = itr.next();
1783 if (null == property) {
1786 propName = property.getPropName();
1787 propValue = property.getPropValue();
1788 if (propName.equals(Constants.DEVICE_NAME)) {
1789 devName = propValue;
1793 if (!changeDeviceName(dev, devName)) {
1797 // Notify UI Listeners
1798 UiListenerHandler.getInstance().propertiesChangedUINotification(
1804 public boolean attributeValueUpdated(SingleResource resource,
1805 String attributeName, AttributeValue value) {
1806 if (null != resource && null != attributeName && null != value) {
1807 SimulatorSingleResource simRes = (SimulatorSingleResource) resource
1808 .getSimulatorResource();
1809 if (null != simRes) {
1811 simRes.updateAttribute(attributeName, value);
1813 } catch (SimulatorException e) {
1817 .log(Level.ERROR.ordinal(), new Date(),
1818 Utility.getSimulatorErrorString(e, null));
1825 // TODO: This method should get the status from the native layer.
1826 public boolean isResourceStarted(Resource resource) {
1827 if (null == resource) {
1830 return resource.isStarted();
1833 public boolean isPropertyValueInvalid(Resource resource,
1834 List<MetaProperty> properties, String propName) {
1835 if (null == resource || null == properties || null == propName) {
1838 boolean invalid = false;
1840 Iterator<MetaProperty> itr = properties.iterator();
1841 while (itr.hasNext()) {
1843 if (prop.getPropName().equals(propName)) {
1844 String value = prop.getPropValue();
1845 if (propName.equals(Constants.RESOURCE_URI)) {
1846 if (!Utility.isUriValid(value)) {
1850 if (null == value || value.trim().isEmpty()) {
1859 public boolean isPropertyValueInvalid(Device dev,
1860 List<MetaProperty> properties, String propName) {
1861 if (null == dev || null == properties || null == propName) {
1864 boolean invalid = false;
1866 Iterator<MetaProperty> itr = properties.iterator();
1867 while (itr.hasNext()) {
1869 if (prop.getPropName().equals(propName)) {
1870 String value = prop.getPropValue();
1871 if (null == value || value.trim().isEmpty()) {
1879 public boolean isPropValueChanged(Resource resource,
1880 List<MetaProperty> properties, String propName) {
1881 if (null == resource || null == properties || null == propName) {
1884 boolean changed = false;
1887 Iterator<MetaProperty> itr = properties.iterator();
1888 while (itr.hasNext()) {
1890 if (prop.getPropName().equals(propName)) {
1891 oldValue = getPropertyValueFromResource(resource, propName);
1892 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1901 public boolean isPropValueChanged(Device dev,
1902 List<MetaProperty> properties, String propName) {
1903 if (null == dev || null == properties || null == propName) {
1906 boolean changed = false;
1909 Iterator<MetaProperty> itr = properties.iterator();
1910 while (itr.hasNext()) {
1912 if (prop.getPropName().equals(propName)) {
1913 oldValue = dev.getDeviceName();
1914 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1923 private String getPropertyValueFromResource(Resource resource,
1925 if (null == resource || null == propName) {
1928 if (propName.equals(Constants.RESOURCE_URI)) {
1929 return resource.getResourceURI();
1930 } else if (propName.equals(Constants.RESOURCE_NAME)) {
1931 return resource.getResourceName();
1932 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1933 return resource.getResourceTypes().toString();
1939 public boolean isURIChanged(Resource resource, List<MetaProperty> properties) {
1940 if (null == resource || null == properties) {
1943 boolean changed = false;
1945 Iterator<MetaProperty> itr = properties.iterator();
1946 while (itr.hasNext()) {
1948 if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
1949 if (!prop.getPropValue().equals(resource.getResourceURI())) {
1958 public boolean startResource(SingleResource resource) {
1959 if (null == resource || resource.isStarted()) {
1963 SimulatorResource server = resource.getSimulatorResource();
1964 if (null == server) {
1969 resource.setStarted(true);
1971 } catch (SimulatorException e) {
1975 .log(Level.ERROR.ordinal(), new Date(),
1976 Utility.getSimulatorErrorString(e, null));
1983 public boolean stopResource(SingleResource resource) {
1984 if (null == resource || !resource.isStarted()) {
1988 SimulatorResource server = resource.getSimulatorResource();
1989 if (null == server) {
1994 resource.setStarted(false);
1996 } catch (SimulatorException e) {
2000 .log(Level.ERROR.ordinal(), new Date(),
2001 Utility.getSimulatorErrorString(e, null));
2008 public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
2012 AttributeProperty prop = att.property();
2016 Type attProp = prop.type();
2017 if (attProp == Type.UNKNOWN) {
2023 public int startAutomation(SingleResource resource,
2024 AttributeElement attribute, AutoUpdateType autoType,
2025 int autoUpdateInterval) {
2027 if (null != resource && null != attribute) {
2028 SimulatorSingleResource server = (SimulatorSingleResource) resource
2029 .getSimulatorResource();
2030 if (null != server) {
2031 String attrName = attribute.getSimulatorResourceAttribute()
2034 autoId = server.startAttributeUpdation(attrName, autoType,
2035 autoUpdateInterval, automationListener);
2036 } catch (SimulatorException e) {
2040 .log(Level.ERROR.ordinal(),
2042 "[" + e.getClass().getSimpleName() + "]"
2043 + e.code().toString() + "-"
2048 attribute.setAutoUpdateId(autoId);
2049 attribute.setAutoUpdateType(autoType);
2050 attribute.setAutoUpdateInterval(autoUpdateInterval);
2051 attribute.setAutoUpdateState(true);
2052 resource.setAttributeAutomationInProgress(true);
2059 public void stopAutomation(SingleResource resource, AttributeElement att,
2061 if (null != resource) {
2062 SimulatorSingleResource server = (SimulatorSingleResource) resource
2063 .getSimulatorResource();
2064 if (null != server) {
2066 server.stopUpdation(autoId);
2067 } catch (SimulatorException e) {
2071 .log(Level.ERROR.ordinal(),
2073 "[" + e.getClass().getSimpleName() + "]"
2074 + e.code().toString() + "-"
2078 // Change the automation status
2079 att.setAutoUpdateState(false);
2080 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
2085 public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
2086 int autoUpdateInterval, final SingleResource resource) {
2087 if (null == resource) {
2090 boolean status = false;
2091 changeResourceLevelAutomationStatus(resource, true);
2092 // Invoke the native automation method
2093 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
2094 .getSimulatorResource();
2095 if (null != resourceServer) {
2098 autoId = resourceServer.startResourceUpdation(autoType,
2099 autoUpdateInterval, automationListener);
2100 } catch (SimulatorException e) {
2104 .log(Level.ERROR.ordinal(), new Date(),
2105 Utility.getSimulatorErrorString(e, null));
2109 // Automation request failed and hence status is being
2111 changeResourceLevelAutomationStatus(resource, false);
2113 // Automation request accepted.
2114 resource.setAutomationId(autoId);
2116 // Notify the UI listeners in a different thread.
2117 Thread notifyThread = new Thread() {
2119 UiListenerHandler.getInstance()
2120 .resourceAutomationStartedUINotification(
2124 notifyThread.setPriority(Thread.MAX_PRIORITY);
2125 notifyThread.start();
2133 public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
2134 if (null == resource) {
2137 final int autoId = resource.getAutomationId();
2141 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
2142 .getSimulatorResource();
2143 if (null == resourceServer) {
2146 // Call native method
2148 resourceServer.stopUpdation(autoId);
2149 } catch (SimulatorException e) {
2153 .log(Level.ERROR.ordinal(), new Date(),
2154 Utility.getSimulatorErrorString(e, null));
2158 // Notify the UI Listeners. Invoke the automation complete callback.
2159 Thread stopThread = new Thread() {
2161 automationListener.onUpdateComplete(resource.getResourceURI(),
2169 private boolean isAnyAttributeInAutomation(SingleResource resource) {
2170 if (null == resource || null == resource.getResourceRepresentation()) {
2174 Map<String, AttributeElement> attributes = resource
2175 .getResourceRepresentation().getAttributes();
2176 if (null == attributes || 0 == attributes.size())
2179 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
2180 if (entry.getValue().isAutoUpdateInProgress())
2187 // Changes the automation state of the resource and its attributes
2188 private void changeResourceLevelAutomationStatus(SingleResource resource,
2191 if (null == resource || null == resource.getResourceRepresentation()) {
2195 Map<String, AttributeElement> attributes = resource
2196 .getResourceRepresentation().getAttributes();
2197 if (null == attributes || 0 == attributes.size())
2200 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
2201 entry.getValue().setAutoUpdateState(status);
2204 resource.setResourceAutomationInProgress(status);
2207 private AttributeElement getAttributeWithGivenAutomationId(
2208 SingleResource resource, int automationId) {
2209 if (null == resource || null == resource.getResourceRepresentation()) {
2213 Map<String, AttributeElement> attributes = resource
2214 .getResourceRepresentation().getAttributes();
2215 if (null == attributes || 0 == attributes.size())
2218 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
2219 if (automationId == entry.getValue().getAutoUpdateId())
2220 return entry.getValue();
2226 public boolean isResourceAutomationStarted(SingleResource resource) {
2227 boolean status = false;
2228 if (null != resource) {
2229 status = resource.isResourceAutomationInProgress();
2234 public boolean isAttributeAutomationStarted(SingleResource resource) {
2235 if (null == resource) {
2238 return resource.isAttributeAutomationInProgress();
2241 public void notifyObserverRequest(Resource resource, int observerId) {
2242 if (null == resource) {
2245 SimulatorResource simulatorResource = resource.getSimulatorResource();
2246 if (null == simulatorResource) {
2250 simulatorResource.notifyObserver(observerId);
2251 } catch (SimulatorException e) {
2255 .log(Level.ERROR.ordinal(), new Date(),
2256 Utility.getSimulatorErrorString(e, null));
2260 public void shutdown() {
2261 threadHandle.interrupt();
2264 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
2269 AttributeValue val = att.value();
2274 TypeInfo type = val.typeInfo();
2276 AttributeProperty prop = att.property();
2281 List<String> values = new ArrayList<String>();
2283 Type valuesType = prop.type();
2285 if (valuesType == Type.UNKNOWN) {
2286 // Adding the default value
2287 values.add(Utility.getAttributeValueAsString(val));
2291 if (type.mType != ValueType.RESOURCEMODEL) {
2292 if (type.mType == ValueType.ARRAY) {
2293 if (type.mDepth == 1) {
2294 AttributeProperty childProp = prop.getChildProperty();
2295 if (null != childProp) {
2296 valuesType = childProp.type();
2297 if (valuesType == Type.RANGE) {
2298 List<String> list = getRangeForPrimitiveNonArrayAttributes(
2299 childProp, type.mBaseType);
2301 values.addAll(list);
2303 } else if (valuesType == Type.VALUESET) {
2304 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
2305 childProp.valueSet(), type.mBaseType);
2307 values.addAll(list);
2313 if (valuesType == Type.RANGE) {
2314 List<String> list = getRangeForPrimitiveNonArrayAttributes(
2317 values.addAll(list);
2319 } else if (valuesType == Type.VALUESET) {
2320 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
2321 prop.valueSet(), type.mType);
2323 values.addAll(list);
2332 public List<String> getRangeForPrimitiveNonArrayAttributes(
2333 AttributeProperty prop, ValueType type) {
2338 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
2342 List<String> values = new ArrayList<String>();
2345 int min = (int) prop.min();
2346 int max = (int) prop.max();
2347 for (int iVal = min; iVal <= max; iVal++) {
2348 values.add(String.valueOf(iVal));
2352 double minD = (double) prop.min();
2353 double maxD = (double) prop.max();
2354 for (double iVal = minD; iVal <= maxD; iVal = iVal + 1.0) {
2355 values.add(String.valueOf(iVal));
2363 public List<String> getAllowedValuesForPrimitiveNonArrayAttributes(
2364 AttributeValue[] attValues, ValueType type) {
2365 if (null == attValues || attValues.length < 1) {
2369 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
2374 List<String> values = new ArrayList<String>();
2375 for (AttributeValue val : attValues) {
2383 values.add(String.valueOf(obj));
2388 public int getResourceCount() {
2389 return data.getResourceCount();