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.HashMap;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
28 import java.util.Vector;
30 import oic.simulator.serviceprovider.Activator;
31 import oic.simulator.serviceprovider.model.CollectionResource;
32 import oic.simulator.serviceprovider.model.Device;
33 import oic.simulator.serviceprovider.model.LocalResourceAttribute;
34 import oic.simulator.serviceprovider.model.MetaProperty;
35 import oic.simulator.serviceprovider.model.Resource;
36 import oic.simulator.serviceprovider.model.ResourceType;
37 import oic.simulator.serviceprovider.model.SRMItem;
38 import oic.simulator.serviceprovider.model.SingleResource;
39 import oic.simulator.serviceprovider.utils.Constants;
40 import oic.simulator.serviceprovider.utils.Utility;
42 import org.oic.simulator.AttributeProperty;
43 import org.oic.simulator.AttributeProperty.Type;
44 import org.oic.simulator.AttributeValue;
45 import org.oic.simulator.AttributeValue.TypeInfo;
46 import org.oic.simulator.AttributeValue.ValueType;
47 import org.oic.simulator.ILogger.Level;
48 import org.oic.simulator.PlatformInfo;
49 import org.oic.simulator.SimulatorException;
50 import org.oic.simulator.SimulatorManager;
51 import org.oic.simulator.SimulatorResourceAttribute;
52 import org.oic.simulator.SimulatorResourceModel;
53 import org.oic.simulator.server.Observer;
54 import org.oic.simulator.server.SimulatorCollectionResource;
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;
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.
69 public class ResourceManager {
73 private Resource currentResourceInSelection;
75 private Device currentDeviceInSelection;
77 private ResourceModelChangeListener resourceModelChangeListener;
79 private AutoUpdateListener automationListener;
81 private ObserverListener observer;
83 private NotificationSynchronizerThread synchronizerThread;
85 private Thread threadHandle;
87 private String deviceName;
88 private PlatformInfo platformInfo;
90 public ResourceManager() {
93 // Set the default device and platform information
94 deviceName = "IoTivity Simulator";
96 SimulatorManager.setDeviceInfo(deviceName);
97 } catch (SimulatorException e) {
101 .log(Level.ERROR.ordinal(),
103 "Error while registering the device info.\n"
104 + Utility.getSimulatorErrorString(e, null));
107 platformInfo = new PlatformInfo();
108 platformInfo.setPlatformID("Samsung Platform Identifier");
109 platformInfo.setManufacturerName("Samsung");
110 platformInfo.setManufacturerUrl("www.samsung.com");
111 platformInfo.setModelNumber("Samsung Model Num01");
112 platformInfo.setDateOfManufacture("2015-09-10T11:10:30Z");
113 platformInfo.setPlatformVersion("PlatformVersion01");
114 platformInfo.setOperationSystemVersion("OSVersion01");
115 platformInfo.setHardwareVersion("HardwareVersion01");
116 platformInfo.setFirmwareVersion("FirwareVersion01");
117 platformInfo.setSupportUrl("http://www.samsung.com/support");
118 platformInfo.setSystemTime("2015-09-10T11:10:30Z");
120 SimulatorManager.setPlatformInfo(platformInfo);
121 } catch (SimulatorException e) {
125 .log(Level.ERROR.ordinal(),
127 "Error while registering the platform info.\n"
128 + Utility.getSimulatorErrorString(e, null));
131 resourceModelChangeListener = new ResourceModelChangeListener() {
134 public void onResourceModelChanged(final String resourceURI,
135 final SimulatorResourceModel resourceModelN) {
136 synchronizerThread.addToQueue(new Runnable() {
140 if (null == resourceURI || null == resourceModelN) {
144 Resource resource = data.getResourceByURI(resourceURI);
145 if (null == resource) {
149 resource.setResourceModel(resourceModelN);
151 // 7. Fetch the resource attributes.
152 Map<String, LocalResourceAttribute> resourceAttributeMap;
154 resourceAttributeMap = fetchResourceAttributesFromModel(resourceModelN);
155 if (null != resourceAttributeMap) {
156 resource.setResourceAttributes(resourceAttributeMap);
158 } catch (SimulatorException e) {
159 // TODO Auto-generated catch block
163 UiListenerHandler.getInstance()
164 .resourceModelChangedUINotification(resource);
170 automationListener = new AutoUpdateListener() {
173 public void onUpdateComplete(final String resourceURI,
174 final int automationId) {
175 synchronizerThread.addToQueue(new Runnable() {
179 SingleResource resource = data
180 .getSingleResourceByURI(resourceURI);
181 if (null == resource) {
184 // Checking whether this notification is for an
185 // attribute or a resource
186 if (resource.isResourceAutomationInProgress()) {
187 changeResourceLevelAutomationStatus(resource, false);
188 // Notify the UI listeners
189 UiListenerHandler.getInstance()
190 .automationCompleteUINotification(resource,
192 } else if (resource.isAttributeAutomationInProgress()) {
193 // Find the attribute with the given automation id
194 LocalResourceAttribute attribute;
195 attribute = getAttributeWithGivenAutomationId(
196 resource, automationId);
197 if (null != attribute) {
198 attribute.setAutomationInProgress(false);
199 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
200 // Notify the UI listeners
203 .automationCompleteUINotification(
206 .getResourceAttributeRef()
209 // TODO: Temporarily reset the attribute
210 // automation status to false for making
211 // resource-level automation work after
212 // attribute-level automations.
213 resource.setAttributeAutomationInProgress(false);
221 observer = new ObserverListener() {
223 public void onObserverChanged(final String resourceURI,
224 final int status, final Observer observer) {
228 if (null == resourceURI || null == observer) {
231 Resource resource = data.getResourceByURI(resourceURI);
232 if (null == resource) {
235 // Update the observers information
237 resource.addObserverInfo(observer);
239 resource.removeObserverInfo(observer);
241 // Notify the UI listeners
242 UiListenerHandler.getInstance()
243 .observerListChangedUINotification(resource);
249 public void onObserverAdded(String resourceURI, Observer observer) {
250 onObserverChanged(resourceURI, 0, observer);
254 public void onObserverRemoved(String resourceURI, Observer observer) {
255 onObserverChanged(resourceURI, 1, observer);
259 synchronizerThread = new NotificationSynchronizerThread();
260 threadHandle = new Thread(synchronizerThread);
261 threadHandle.setName("Simulator service provider event queue");
262 threadHandle.start();
265 private static class NotificationSynchronizerThread implements Runnable {
267 LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
271 while (!Thread.interrupted()) {
272 synchronized (this) {
274 while (notificationQueue.isEmpty()) {
278 } catch (InterruptedException e) {
284 synchronized (this) {
285 thread = notificationQueue.pop();
289 } catch (Exception e) {
290 if (e instanceof InterruptedException) {
298 public void addToQueue(Runnable event) {
299 synchronized (this) {
300 notificationQueue.add(event);
306 public String getDeviceName() {
310 public void setDeviceName(String deviceName) {
311 this.deviceName = deviceName;
313 SimulatorManager.setDeviceInfo(deviceName);
314 } catch (SimulatorException e) {
318 .log(Level.ERROR.ordinal(),
320 "Error while registering the device info.\n"
321 + Utility.getSimulatorErrorString(e, null));
325 public List<MetaProperty> getPlatformInfo() {
326 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
327 metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
329 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
330 platformInfo.getManufacturerName()));
331 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
332 platformInfo.getManufacturerUrl()));
333 metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
334 platformInfo.getModelNumber()));
335 metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
336 platformInfo.getDateOfManufacture()));
337 metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
338 platformInfo.getPlatformVersion()));
339 metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
340 platformInfo.getOperationSystemVersion()));
341 metaProperties.add(new MetaProperty(
342 Constants.PLATFORM_HARDWARE_VERSION, platformInfo
343 .getHardwareVersion()));
344 metaProperties.add(new MetaProperty(
345 Constants.PLATFORM_FIRMWARE_VERSION, platformInfo
346 .getFirmwareVersion()));
347 metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
348 platformInfo.getSupportUrl()));
349 metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
350 platformInfo.getSystemTime()));
351 return metaProperties;
354 public void setPlatformInfo(List<MetaProperty> metaProperties) {
355 if (null == metaProperties || metaProperties.size() < 1) {
358 Iterator<MetaProperty> itr = metaProperties.iterator();
362 while (itr.hasNext()) {
364 propName = prop.getPropName();
365 propValue = prop.getPropValue();
366 if (propName.equals(Constants.PLATFORM_ID)) {
367 platformInfo.setPlatformID(propValue);
368 } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) {
369 platformInfo.setManufacturerName(propValue);
370 } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) {
371 platformInfo.setManufacturerUrl(propValue);
372 } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) {
373 platformInfo.setModelNumber(propValue);
374 } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) {
375 platformInfo.setDateOfManufacture(propValue);
376 } else if (propName.equals(Constants.PLATFORM_VERSION)) {
377 platformInfo.setPlatformVersion(propValue);
378 } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) {
379 platformInfo.setOperationSystemVersion(propValue);
380 } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) {
381 platformInfo.setHardwareVersion(propValue);
382 } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) {
383 platformInfo.setFirmwareVersion(propValue);
384 } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) {
385 platformInfo.setSupportUrl(propValue);
386 } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) {
387 platformInfo.setSystemTime(propValue);
391 SimulatorManager.setPlatformInfo(platformInfo);
392 } catch (SimulatorException e) {
396 .log(Level.ERROR.ordinal(),
398 "Error while registering the platform info.\n"
399 + Utility.getSimulatorErrorString(e, null));
403 public synchronized Resource getCurrentResourceInSelection() {
404 return currentResourceInSelection;
407 public synchronized void setCurrentResourceInSelection(Resource resource) {
408 this.currentResourceInSelection = resource;
411 public synchronized Device getCurrentDeviceInSelection() {
412 return currentDeviceInSelection;
415 public synchronized void setCurrentDeviceInSelection(Device dev) {
416 this.currentDeviceInSelection = dev;
419 public boolean isResourceExist(String resourceURI) {
420 return data.isResourceExist(resourceURI);
423 public boolean isAnyResourceExist() {
424 return data.isAnyResourceExist();
427 public boolean createSingleResource(SingleResource resource)
428 throws SimulatorException {
429 if (null == resource) {
432 String resType = (String) resource.getResourceTypes().toArray()[0];
434 // 1. Create the resource.
435 SimulatorResource jSimulatorResource = SimulatorManager
436 .createResource(SimulatorResource.Type.SINGLE,
437 resource.getResourceName(),
438 resource.getResourceURI(), resType);
439 if (null == jSimulatorResource
440 || !(jSimulatorResource instanceof SimulatorSingleResource)) {
443 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
444 resource.setSimulatorResource(jSimulatorSingleResource);
446 // 2. Cancel observable property if requested by user.
447 if (!resource.isObservable()) {
448 jSimulatorSingleResource.setObservable(false);
451 // 3. Set the model change listener.
452 jSimulatorSingleResource
453 .setResourceModelChangeListener(resourceModelChangeListener);
455 // 4. Set the observer listener if the resource is observable.
456 if (resource.isObservable()) {
457 jSimulatorSingleResource.setObserverListener(observer);
460 // 5. Add attributes.
461 Map<String, LocalResourceAttribute> attributes = resource
462 .getResourceAttributes();
463 if (null != attributes && !attributes.isEmpty()) {
464 Set<String> keySet = attributes.keySet();
465 Iterator<String> itr = keySet.iterator();
468 LocalResourceAttribute localAtt;
469 SimulatorResourceAttribute simResAtt;
471 while (itr.hasNext()) {
472 attName = itr.next();
473 localAtt = attributes.get(attName);
474 if (null == localAtt) {
477 simResAtt = localAtt.getResourceAttributeRef();
478 if (null == simResAtt) {
481 jSimulatorSingleResource.addAttribute(simResAtt);
484 // 6. Get the resource model java object reference.
485 resource.setResourceModel(jSimulatorSingleResource
486 .getResourceModel());
489 // 7. Register the resource with the platform.
490 jSimulatorSingleResource.start();
491 resource.setStarted(true);
492 } catch (SimulatorException e) {
496 .log(Level.ERROR.ordinal(), new Date(),
497 Utility.getSimulatorErrorString(e, null));
501 // 8. Add to local cache.
502 data.addResource(resource);
504 // 9. Update UI listeners
505 UiListenerHandler.getInstance().resourceCreatedUINotification(
506 ResourceType.SINGLE);
511 public boolean createCollectionResource(CollectionResource resource)
512 throws SimulatorException {
513 if (null == resource) {
516 String resType = (String) resource.getResourceTypes().toArray()[0];
518 // 1. Create the resource.
519 SimulatorResource jSimulatorResource = SimulatorManager
520 .createResource(SimulatorResource.Type.COLLECTION,
521 resource.getResourceName(),
522 resource.getResourceURI(), resType);
523 if (null == jSimulatorResource
524 || !(jSimulatorResource instanceof SimulatorCollectionResource)) {
527 SimulatorCollectionResource jSimulatorCollectionResource = (SimulatorCollectionResource) jSimulatorResource;
528 resource.setSimulatorResource(jSimulatorCollectionResource);
530 // 2. Cancel observable property if requested by user.
531 if (!resource.isObservable()) {
532 jSimulatorCollectionResource.setObservable(false);
535 // 3. Set the observer listener if the resource is observable.
536 if (resource.isObservable()) {
537 jSimulatorCollectionResource.setObserverListener(observer);
540 // 4. Set the model change listener.
541 jSimulatorCollectionResource
542 .setResourceModelChangeListener(resourceModelChangeListener);
544 // 5. Register the resource with the platform.
545 jSimulatorCollectionResource.start();
546 resource.setStarted(true);
547 } catch (SimulatorException e) {
551 .log(Level.ERROR.ordinal(), new Date(),
552 Utility.getSimulatorErrorString(e, null));
556 // 6. Add to local cache.
557 data.addResource(resource);
559 // 7. Update UI listeners
560 UiListenerHandler.getInstance().resourceCreatedUINotification(
561 ResourceType.COLLECTION);
566 public Resource createResourceByRAML(String configFilePath)
567 throws SimulatorException {
568 Resource resource = null;
570 // 1. Create the resource
571 SimulatorResource jSimulatorResource = SimulatorManager
572 .createResource(configFilePath);
573 if (null == jSimulatorResource) {
576 if (jSimulatorResource instanceof SimulatorSingleResource) {
577 resource = new SingleResource();
579 resource = new CollectionResource();
581 resource.setSimulatorResource(jSimulatorResource);
583 // 2. Fetch and locally store the resource name and uri.
584 String uri = jSimulatorResource.getURI();
585 if (null == uri || uri.trim().isEmpty()) {
588 resource.setResourceURI(uri.trim());
590 String name = jSimulatorResource.getName();
591 if (null == name || name.trim().isEmpty()) {
594 resource.setResourceName(name.trim());
595 } catch (SimulatorException e) {
599 .log(Level.ERROR.ordinal(), new Date(),
600 Utility.getSimulatorErrorString(e, null));
607 * This method can set/change the resource uri and name of an already
608 * created resource which is not yet registered with the platform. This
609 * method registers the model change and observer listeners, registers the
610 * resource, fetches the resource attributes, updates the local cache and
611 * notifies the UI listeners.
613 public boolean completeSingleResourceCreationByRAML(Resource resource,
614 String uri, String name, boolean multiInstance)
615 throws SimulatorException {
616 if (null == resource || !(resource instanceof SingleResource)) {
620 SingleResource singleRes = (SingleResource) resource;
622 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
623 .getSimulatorResource();
624 if (null == jSimulatorSingleResource) {
628 // 1. Update resource URI and Name if they are changed.
629 String newUri = uri.trim();
630 String newName = name.trim();
633 singleRes.setResourceURI(newUri);
634 singleRes.setResourceName(newName);
636 if (!singleRes.getResourceURI().equals(newUri)) {
637 jSimulatorSingleResource.setURI(newUri);
638 singleRes.setResourceURI(newUri);
640 if (!singleRes.getResourceName().equals(newName)) {
641 jSimulatorSingleResource.setName(newName);
642 singleRes.setResourceName(newName);
646 // 2. Set the model change listener.
647 jSimulatorSingleResource
648 .setResourceModelChangeListener(resourceModelChangeListener);
650 // 3. Set the observer listener if the resource is observable.
651 if (jSimulatorSingleResource.isObservable()) {
652 jSimulatorSingleResource.setObserverListener(observer);
653 singleRes.setObservable(true);
656 // 4. Fetch the resource model.
657 SimulatorResourceModel jResModel = jSimulatorSingleResource
659 if (null == jResModel) {
662 singleRes.setResourceModel(jResModel);
664 // 5. Fetch the basic details of the resource.
665 singleRes.addResourceType(jSimulatorSingleResource
668 .setResourceInterfaces(Utility
669 .convertVectorToSet(jSimulatorSingleResource
672 // 6. Register the resource with the platform.
673 jSimulatorSingleResource.start();
674 singleRes.setStarted(true);
676 // 7. Fetch the resource attributes.
677 Map<String, LocalResourceAttribute> resourceAttributeMap;
678 resourceAttributeMap = fetchResourceAttributesFromModel(jResModel);
679 if (null != resourceAttributeMap) {
680 singleRes.setResourceAttributes(resourceAttributeMap);
683 // 8. Add to local cache.
684 data.addResource(singleRes);
686 // 9. Update UI listeners for single instance creation
688 UiListenerHandler.getInstance().resourceCreatedUINotification(
689 ResourceType.SINGLE);
690 } catch (SimulatorException e) {
694 .log(Level.ERROR.ordinal(), new Date(),
695 Utility.getSimulatorErrorString(e, null));
702 * This method can set/change the resource uri and name of an already
703 * created resource which is not yet registered with the platform. This
704 * method registers the model change and observer listeners, registers the
705 * resource, fetches the resource attributes, updates the local cache and
706 * notifies the UI listeners.
708 public boolean completeCollectionResourceCreationByRAML(Resource resource,
709 String uri, String name) throws SimulatorException {
710 if (null == resource || !(resource instanceof CollectionResource)) {
714 CollectionResource collectionRes = (CollectionResource) resource;
716 SimulatorCollectionResource jSimulatorCollectionResource = (SimulatorCollectionResource) resource
717 .getSimulatorResource();
718 if (null == jSimulatorCollectionResource) {
722 // 1. Update resource URI and Name if they are changed.
723 String newUri = uri.trim();
724 String newName = name.trim();
726 if (!collectionRes.getResourceURI().equals(newUri)) {
727 jSimulatorCollectionResource.setURI(newUri);
728 collectionRes.setResourceURI(newUri);
730 if (!collectionRes.getResourceName().equals(newName)) {
731 jSimulatorCollectionResource.setName(newName);
732 collectionRes.setResourceName(newName);
735 // 2. Set the model change listener.
736 jSimulatorCollectionResource
737 .setResourceModelChangeListener(resourceModelChangeListener);
739 // 3. Fetch the resource model.
740 SimulatorResourceModel jResModel = jSimulatorCollectionResource
742 if (null == jResModel) {
745 collectionRes.setResourceModel(jResModel);
747 // 4. Fetch the basic details of the resource.
748 collectionRes.addResourceType(jSimulatorCollectionResource
750 collectionRes.setResourceInterfaces(Utility
751 .convertVectorToSet(jSimulatorCollectionResource
754 // 5. Set the observer listener if the resource is observable.
755 if (jSimulatorCollectionResource.isObservable()) {
756 jSimulatorCollectionResource.setObserverListener(observer);
757 collectionRes.setObservable(true);
760 // 6. Register the resource with the platform.
761 jSimulatorCollectionResource.start();
762 collectionRes.setStarted(true);
764 // 7. Fetch the resource attributes.
765 Map<String, LocalResourceAttribute> resourceAttributeMap;
766 resourceAttributeMap = fetchResourceAttributesFromModel(jResModel);
767 if (null != resourceAttributeMap) {
768 collectionRes.setResourceAttributes(resourceAttributeMap);
771 // 6. Add to local cache.
772 data.addResource(collectionRes);
774 // 7. Update UI listeners for single instance creation
775 UiListenerHandler.getInstance().resourceCreatedUINotification(
776 ResourceType.COLLECTION);
777 } catch (SimulatorException e) {
781 .log(Level.ERROR.ordinal(), new Date(),
782 Utility.getSimulatorErrorString(e, null));
788 public int createSingleResourceMultiInstances(String configFile, int count)
789 throws SimulatorException {
792 Vector<SimulatorResource> jSimulatorResources = SimulatorManager
793 .createResource(configFile, count);
794 if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
797 SimulatorSingleResource jResource;
798 SingleResource resource;
800 for (SimulatorResource jSimulatorResource : jSimulatorResources) {
801 jResource = (SimulatorSingleResource) jSimulatorResource;
802 resource = new SingleResource();
803 resource.setSimulatorResource(jResource);
805 result = completeSingleResourceCreationByRAML(resource,
806 jResource.getURI(), jResource.getName(), true);
810 } catch (SimulatorException eInner) {
814 .log(Level.ERROR.ordinal(),
816 Utility.getSimulatorErrorString(eInner,
820 if (createCount > 0) {
821 UiListenerHandler.getInstance().resourceCreatedUINotification(
822 ResourceType.SINGLE);
824 } catch (SimulatorException eOuter) {
828 .log(Level.ERROR.ordinal(), new Date(),
829 Utility.getSimulatorErrorString(eOuter, null));
835 public void createDevice(String deviceName, Set<Resource> childs) {
837 Device dev = new Device();
838 dev.setDeviceName(deviceName);
841 // 2. Add children to device
842 if (null != childs && !childs.isEmpty())
843 addResourceToDevice(dev, childs);
845 // 3. Update ui listeners
846 UiListenerHandler.getInstance().resourceListUpdateUINotification(
847 ResourceType.DEVICE);
850 private Map<String, LocalResourceAttribute> fetchResourceAttributesFromModel(
851 SimulatorResourceModel jResModel) throws SimulatorException {
852 Map<String, LocalResourceAttribute> resourceAttributeMap = null;
853 if (null != jResModel) {
854 Map<String, SimulatorResourceAttribute> jAttributeMap;
855 jAttributeMap = jResModel.getAttributes();
856 if (null != jAttributeMap) {
857 resourceAttributeMap = new HashMap<String, LocalResourceAttribute>();
858 Iterator<String> itr = jAttributeMap.keySet().iterator();
860 SimulatorResourceAttribute jResAtt;
861 LocalResourceAttribute localAtt;
862 while (itr.hasNext()) {
863 attName = itr.next();
864 if (null != attName) {
865 jResAtt = jAttributeMap.get(attName);
866 if (null != jResAtt) {
867 localAtt = new LocalResourceAttribute();
869 localAtt.setResourceAttributeRef(jResAtt);
871 // Initially disabling the automation
872 localAtt.setAutomationInProgress(false);
874 // Assigning the default automation interval
875 localAtt.setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
877 // Setting the default automation type
878 localAtt.setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);
880 resourceAttributeMap.put(attName, localAtt);
886 return resourceAttributeMap;
890 // This method gives all known possible values of the attribute in string
891 // format. It takes allowed values or range of values whichever is available
892 private List<String> getValueList(SimulatorResourceAttribute attributeN) {
893 AttributeProperty attProp = attributeN.property();
894 if (null == attProp) {
897 List<String> valueList = new ArrayList<String>();
898 Type valuesType = attProp.type();
899 if (valuesType == Type.VALUESET) {
900 Object[] allowedValues = attProp.valueSet();
901 if (null != allowedValues && allowedValues.length > 0) {
902 for (Object value : allowedValues) {
904 valueList.add(String.valueOf(((AttributeValue) value)
909 } else if (valuesType == Type.RANGE) {
910 double minD = attProp.min();
911 double maxD = attProp.max();
912 for (double value = minD; value <= maxD; value++) {
913 valueList.add(String.valueOf(value));
916 Object attValue = attributeN.value().get();
917 if (valueList.size() < 1 && null != attValue) {
918 valueList.add(String.valueOf(attValue));
923 public List<Resource> getResourceList() {
924 List<Resource> resourceList = data.getResources();
925 if (null == resourceList) {
929 Collections.sort(resourceList, Utility.resourceComparator);
934 public List<SingleResource> getSingleResourceList() {
935 List<SingleResource> resourceList = data.getSingleResources();
936 if (null == resourceList) {
940 Collections.sort(resourceList, Utility.singleResourceComparator);
945 public List<CollectionResource> getCollectionResourceList() {
946 List<CollectionResource> resourceList = data.getCollectionResources();
947 if (null == resourceList) {
951 Collections.sort(resourceList, Utility.collectionResourceComparator);
956 public List<Device> getDeviceList() {
957 List<Device> deviceList = data.getDevices();
958 if (null == deviceList) {
962 Collections.sort(deviceList, Utility.deviceComparator);
966 // Returns the number of resources which are added properly to the
968 public int addResourceToCollection(CollectionResource collectionParent,
969 Set<Resource> childs) {
970 if (null == collectionParent || null == childs || childs.isEmpty()) {
973 Iterator<Resource> itr = childs.iterator();
975 int count = childs.size();
976 while (itr.hasNext()) {
979 addResourceToCollection(collectionParent, res);
980 } catch (SimulatorException e) {
987 public void addResourceToCollection(CollectionResource collectionParent,
988 Resource child) throws SimulatorException {
989 if (null == collectionParent || null == child) {
993 // 1. Add child to collection
994 collectionParent.addChildResource(child);
996 // 2. Add a reference to the collection in the child
997 if (child instanceof SingleResource) {
998 ((SingleResource) child)
999 .addCollectionMembership(collectionParent);
1001 ((CollectionResource) child).addMembership(collectionParent);
1003 } catch (SimulatorException e) {
1007 .log(Level.ERROR.ordinal(), new Date(),
1008 Utility.getSimulatorErrorString(e, null));
1013 public int addResourceToCollection(Set<CollectionResource> collections,
1015 if (null == collections || collections.isEmpty() || null == child) {
1018 Iterator<CollectionResource> itr = collections.iterator();
1019 CollectionResource res;
1020 int count = collections.size();
1021 while (itr.hasNext()) {
1024 addResourceToCollection(res, child);
1025 } catch (SimulatorException e) {
1032 public void addResourceToDevice(Device dev, Set<Resource> childs) {
1033 // 1. Add children to the device.
1034 dev.addChildResource(childs);
1036 // 2. Add a reference to the device in all children.
1037 Iterator<Resource> itr = childs.iterator();
1039 while (itr.hasNext()) {
1041 if (res instanceof SingleResource) {
1042 ((SingleResource) res).addDeviceMembership(dev);
1044 ((CollectionResource) res).addDeviceMembership(dev);
1049 public void addResourceToDevice(Device dev, Resource child) {
1050 // 1. Add child to the device.
1051 dev.addChildResource(child);
1053 // 2. Add a reference to the device in the child.
1054 if (child instanceof SingleResource) {
1055 ((SingleResource) child).addDeviceMembership(dev);
1057 ((CollectionResource) child).addDeviceMembership(dev);
1061 public void addResourceToDevice(Set<Device> devices, Resource child) {
1062 // 1. Add device reference in child.
1063 if (child instanceof SingleResource)
1064 ((SingleResource) child).addDeviceMembership(devices);
1066 ((CollectionResource) child).addDeviceMembership(devices);
1068 // 2. Add a reference to the child in all devices.
1069 Iterator<Device> itr = devices.iterator();
1071 while (itr.hasNext()) {
1073 dev.addChildResource(child);
1077 public int removeResourceFromCollection(
1078 Set<CollectionResource> collections, Resource resource) {
1079 // 1. Remove the reference of resource from all the collections.
1080 Iterator<CollectionResource> itr = collections.iterator();
1081 CollectionResource colRes;
1082 int count = collections.size();
1083 while (itr.hasNext()) {
1084 colRes = itr.next();
1086 removeResourceFromCollection(colRes, resource);
1087 } catch (SimulatorException e) {
1095 public void removeResourceFromDevice(Set<Device> devices, Resource resource) {
1096 // 1. Remove the reference of resource from all the devices.
1097 Iterator<Device> itr = devices.iterator();
1099 while (itr.hasNext()) {
1101 dev.removeChildResource(resource);
1104 // 2. Remove the reference of devices from the resource.
1105 resource.removeDeviceMembership(devices);
1108 // Returns the count of resources removed from the collection
1109 public int removeResourcesFromCollection(CollectionResource colRes,
1110 Set<Resource> resources) {
1111 Iterator<Resource> itr = resources.iterator();
1113 int count = resources.size();
1114 while (itr.hasNext()) {
1117 removeResourceFromCollection(colRes, res);
1118 } catch (SimulatorException e) {
1125 public void removeResourcesFromDevice(Device dev, Set<Resource> resources) {
1126 Iterator<Resource> itr = resources.iterator();
1128 while (itr.hasNext()) {
1130 res.removeDeviceMembership(dev);
1132 dev.removeChildResource(resources);
1135 public void removeResourceFromCollection(CollectionResource parent,
1136 Resource child) throws SimulatorException {
1138 // 1. Remove the child from the parent
1139 parent.removeChildResource(child);
1141 // 2. Remove the reference to parent from child
1142 if (child instanceof SingleResource) {
1143 ((SingleResource) child).removeCollectionMembership(parent);
1145 ((CollectionResource) child).removeMembership(parent);
1147 } catch (SimulatorException e) {
1151 .log(Level.ERROR.ordinal(), new Date(),
1152 Utility.getSimulatorErrorString(e, null));
1157 public void removeResourceFromDevice(Device parent, Resource child) {
1158 // 1. Remove the reference to parent from child
1159 child.removeDeviceMembership(parent);
1161 // 2. Remove the child from the parent
1162 parent.removeChildResource(child);
1165 public void removeSingleResources(Set<SingleResource> resources)
1166 throws SimulatorException {
1167 if (null == resources) {
1170 Iterator<SingleResource> itr = resources.iterator();
1171 while (itr.hasNext()) {
1172 removeResource(itr.next());
1176 public void removeCollectionResources(Set<CollectionResource> resources)
1177 throws SimulatorException {
1178 if (null == resources) {
1181 Iterator<CollectionResource> itr = resources.iterator();
1182 while (itr.hasNext()) {
1183 removeResource(itr.next());
1187 public void removeResource(Resource res) throws SimulatorException {
1188 // 1. Unregister the resource from the platform.
1189 SimulatorResource simRes = res.getSimulatorResource();
1192 } catch (SimulatorException e) {
1196 .log(Level.ERROR.ordinal(), new Date(),
1197 Utility.getSimulatorErrorString(e, null));
1201 Set<CollectionResource> collectionMembership;
1202 Set<Device> deviceMembership;
1204 if (res instanceof SingleResource) {
1205 collectionMembership = ((SingleResource) res)
1206 .getCollectionMembership();
1207 deviceMembership = ((SingleResource) res).getDeviceMembership();
1209 collectionMembership = ((CollectionResource) res).getMembership();
1210 deviceMembership = ((CollectionResource) res).getDeviceMembership();
1213 // 2. Delete from the collections to which this resource is a member.
1214 if (null != collectionMembership && !collectionMembership.isEmpty()) {
1215 removeResourceFromCollection(collectionMembership, res);
1218 // 3. Delete from the devices to which this resource is a member.
1219 if (null != deviceMembership && !deviceMembership.isEmpty()) {
1220 removeResourceFromDevice(deviceMembership, res);
1223 // 4. Delete this resource
1224 data.deleteResource(res);
1227 public void removeDevice(Device dev) {
1228 Set<Resource> childs = dev.getChildResources();
1229 if (null != childs && !childs.isEmpty()) {
1230 // 1. Remove the reference from all the children.
1231 Iterator<Resource> itr = childs.iterator();
1233 while (itr.hasNext()) {
1235 res.removeDeviceMembership(dev);
1238 // 2. Delete the device.
1239 data.deleteDevice(dev);
1242 public boolean isUriUnique(List<MetaProperty> properties) {
1243 if (null == properties) {
1247 Iterator<MetaProperty> itr = properties.iterator();
1248 while (itr.hasNext()) {
1250 if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
1251 String uri = prop.getPropValue();
1252 return !data.isResourceExist(uri);
1258 public List<CollectionResource> getCollectionsForAddingToSingleResource(
1259 SingleResource resource) {
1260 List<CollectionResource> collectionResources = data
1261 .getCollectionResources();
1262 if (null == collectionResources || collectionResources.isEmpty()) {
1266 Set<CollectionResource> collectionMembership;
1267 collectionMembership = resource.getCollectionMembership();
1268 if (null == collectionMembership || collectionMembership.isEmpty()) {
1269 return collectionResources;
1272 if (collectionMembership.size() == collectionResources.size()) {
1276 collectionResources.removeAll(collectionMembership);
1279 Collections.sort(collectionResources,
1280 Utility.collectionResourceComparator);
1282 return collectionResources;
1285 public List<SingleResource> getSingleTypeResourcesForAddingToCollectionResource(
1286 CollectionResource colRes) {
1287 List<SingleResource> singleResources = data.getSingleResources();
1288 if (null == singleResources || singleResources.isEmpty()) {
1292 Set<SingleResource> childs;
1293 childs = colRes.getSingleTypeChildResources();
1294 if (null == childs || childs.isEmpty()) {
1295 return singleResources;
1298 if (childs.size() == singleResources.size()) {
1302 singleResources.removeAll(childs);
1305 Collections.sort(singleResources, Utility.singleResourceComparator);
1307 return singleResources;
1310 public List<SingleResource> getSingleTypeResourcesForAddingToDevice(
1312 List<SingleResource> singleResources = data.getSingleResources();
1313 if (null == singleResources || singleResources.isEmpty()) {
1317 Set<SingleResource> childs;
1318 childs = dev.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<CollectionResource> getCollectionTypeResourcesForAddingToCollectionResource(
1336 CollectionResource colRes) {
1337 List<CollectionResource> collectionResources = data
1338 .getCollectionResources();
1339 if (null == collectionResources || collectionResources.isEmpty()) {
1343 // Remove the colRes from the list
1344 collectionResources.remove(colRes);
1346 Set<CollectionResource> childs;
1347 childs = colRes.getCollectionTypeChildResources();
1348 if (null == childs || childs.isEmpty()) {
1349 return collectionResources;
1352 if (childs.size() == collectionResources.size()) {
1356 collectionResources.removeAll(childs);
1359 Collections.sort(collectionResources,
1360 Utility.collectionResourceComparator);
1362 return collectionResources;
1365 public List<CollectionResource> getCollectionTypeResourcesForAddingToDevice(
1367 List<CollectionResource> collectionResources = data
1368 .getCollectionResources();
1369 if (null == collectionResources || collectionResources.isEmpty()) {
1373 Set<CollectionResource> childs;
1374 childs = dev.getCollectionTypeChildResources();
1375 if (null == childs || childs.isEmpty()) {
1376 return collectionResources;
1379 if (childs.size() == collectionResources.size()) {
1383 collectionResources.removeAll(childs);
1386 Collections.sort(collectionResources,
1387 Utility.collectionResourceComparator);
1389 return collectionResources;
1392 public List<Device> getDevicesForAddingToResource(Resource resource) {
1393 List<Device> devices = data.getDevices();
1394 if (null == devices || devices.isEmpty()) {
1398 Set<Device> deviceMembership;
1399 if (resource instanceof SingleResource) {
1400 deviceMembership = ((SingleResource) resource)
1401 .getDeviceMembership();
1403 deviceMembership = ((CollectionResource) resource)
1404 .getDeviceMembership();
1406 if (null == deviceMembership || deviceMembership.isEmpty()) {
1410 if (devices.size() == deviceMembership.size()) {
1414 devices.removeAll(deviceMembership);
1417 Collections.sort(devices, Utility.deviceComparator);
1422 public List<CollectionResource> getResourceReferences(
1423 SingleResource resource) {
1424 List<CollectionResource> resources = Utility
1425 .getCollectionResourceListFromSet(resource
1426 .getCollectionMembership());
1427 if (null == resources || resources.isEmpty()) {
1431 Collections.sort(resources, Utility.collectionResourceComparator);
1436 public List<Device> getDeviceReferences(Resource resource) {
1437 Set<Device> deviceMembership;
1438 if (resource instanceof SingleResource) {
1439 deviceMembership = ((SingleResource) resource)
1440 .getDeviceMembership();
1442 deviceMembership = ((CollectionResource) resource)
1443 .getDeviceMembership();
1446 List<Device> devices = Utility.getDeviceListFromSet(deviceMembership);
1447 if (null == devices || devices.isEmpty()) {
1451 Collections.sort(devices, Utility.deviceComparator);
1456 public List<SingleResource> getSingleTypeChilds(CollectionResource colRes) {
1457 Set<SingleResource> childs = colRes.getSingleTypeChildResources();
1458 return Utility.getSingleResourceListFromSet(childs);
1461 public List<SingleResource> getSingleTypeChilds(Device dev) {
1462 Set<SingleResource> childs = dev.getSingleTypeChildResources();
1463 return Utility.getSingleResourceListFromSet(childs);
1466 public List<CollectionResource> getCollectionTypeChilds(
1467 CollectionResource colRes) {
1468 Set<CollectionResource> childs = colRes
1469 .getCollectionTypeChildResources();
1470 return Utility.getCollectionResourceListFromSet(childs);
1473 public List<CollectionResource> getCollectionTypeChilds(Device dev) {
1474 Set<CollectionResource> childs = dev.getCollectionTypeChildResources();
1475 return Utility.getCollectionResourceListFromSet(childs);
1479 * public void deleteResourceByURI(String resourceURI) { if (null !=
1480 * resourceURI) { Resource resource =
1481 * getSimulatorResourceByURI(resourceURI); if (null != resource) { //
1482 * Unregister the resource from the platform deleteResource(resource);
1484 * // Delete from the local data structure deleteLocalResourceDetails(null,
1487 * // Notify the UI listener for removing this resource // from UI //
1488 * resourceDeletedUINotification(); if (resource ==
1489 * getCurrentResourceInSelection()) { // Listeners might query the resource
1490 * being deleted // if exists. So set the currently selection to // null.
1491 * setCurrentResourceInSelection(null);
1493 * // Notify all observers for resource selection // change event //
1494 * resourceSelectionChangedUINotification(); } } } }
1496 * private SingleResource getSimulatorResourceByURI(String resourceURI) {
1497 * SingleResource resource = null; if (null != resourceURI) { synchronized
1498 * (resourceMap) { resource = resourceMap.get(resourceURI); } } return
1501 * private void deleteResource(SingleResource resource) { if (null !=
1502 * resource) { SimulatorResourceServer resourceServerN = resource
1503 * .getResourceServer(); if (null != resourceServerN) { try {
1504 * SimulatorManager.deleteResource(resourceServerN); } catch
1505 * (SimulatorException e) { Activator .getDefault() .getLogManager()
1506 * .log(Level.ERROR.ordinal(), new Date(),
1507 * Utility.getSimulatorErrorString(e, null)); } } } }
1509 * public void deleteResourceByType(final String resourceType) {
1510 * System.out.println(resourceType + "to be deleted."); if (null !=
1511 * resourceType) { new Thread() {
1513 * @Override public void run() { // Unregister the resources from the
1514 * platform deleteResource(resourceType);
1516 * // Delete from the local data structure
1517 * deleteLocalResourceDetails(resourceType, null);
1519 * // Notify the UI listener for removing this resource from UI
1520 * resourceDeletedUINotification();
1522 * if (null != currentResourceInSelection &&
1523 * currentResourceInSelection.getResourceTypes() .contains(resourceType)) {
1524 * // Listeners might query the resource being deleted if // exists. So set
1525 * the currently selection to null. setCurrentResourceInSelection(null);
1527 * // Notify all observers for resource selection change // event
1528 * resourceSelectionChangedUINotification(null); } } }.start(); } }
1530 * private void deleteResource(String resourceType) { if (null !=
1531 * resourceType) { try { SimulatorManager.deleteResources(resourceType); }
1532 * catch (SimulatorException e) { Activator .getDefault() .getLogManager()
1533 * .log(Level.ERROR.ordinal(), new Date(),
1534 * Utility.getSimulatorErrorString(e, null)); } } }
1536 * public void deleteAllResources() { new Thread() {
1538 * @Override public void run() { // Unregister the resources from the
1539 * platform deleteResource();
1541 * // Delete from the local data structure deleteLocalResourceDetails(null,
1544 * // Notify the UI listener for removing this resource from UI
1545 * resourceDeletedUINotification();
1547 * // Listeners might query the resource being deleted if exists. // So set
1548 * the currently selection to null. setCurrentResourceInSelection(null);
1550 * // Notify all observers for resource selection change event
1551 * resourceSelectionChangedUINotification(null); } }.start(); }
1553 * private void deleteResource() { try {
1554 * SimulatorManager.deleteResources(null); } catch (SimulatorException e) {
1555 * Activator .getDefault() .getLogManager() .log(Level.ERROR.ordinal(), new
1556 * Date(), Utility.getSimulatorErrorString(e, null)); } }
1558 * private void deleteLocalResourceDetails(String resourceType, String
1559 * resourceURI) { synchronized (resourceMap) { if (null == resourceType &&
1560 * null == resourceURI) { resourceMap.clear(); } else if (null !=
1561 * resourceType) { Set<String> uriSet = resourceMap.keySet(); if (null ==
1562 * uriSet) { return; } String uri; SingleResource simpleRes;
1563 * Iterator<String> uriItr = uriSet.iterator(); while (uriItr.hasNext()) {
1564 * uri = uriItr.next(); simpleRes = resourceMap.get(uri); if
1565 * (simpleRes.getResourceTypes().contains(resourceType)) { uriItr.remove();
1566 * } } } else { removeResourceFromMap(resourceURI); } } }
1570 * public Set<String> getResourceTypeList() { Set<String> types = null;
1571 * synchronized (resourceMap) { if (resourceMap.size() > 0) { types = new
1572 * TreeSet<String>(); Set<String> typeSet = resourceMap.keySet();
1573 * Iterator<String> typeItr = typeSet.iterator(); SingleResource resource;
1574 * while (typeItr.hasNext()) { resource = resourceMap.get(typeItr.next());
1575 * Set<String> subTypes = resource.getResourceTypes();
1576 * types.addAll(subTypes); } } } return types; }
1579 public void resourceSelectionChanged(final Resource selectedResource) {
1584 setCurrentDeviceInSelection(null);
1586 if (null != selectedResource) {
1587 setCurrentResourceInSelection(selectedResource);
1589 setCurrentResourceInSelection(null);
1591 // Notify all observers for resource selection change event
1592 UiListenerHandler.getInstance()
1593 .resourceSelectionChangedUINotification(
1599 public void deviceSelectionChanged(final Device selectedDevice) {
1604 setCurrentResourceInSelection(null);
1606 if (null != selectedDevice) {
1607 setCurrentDeviceInSelection(selectedDevice);
1609 setCurrentDeviceInSelection(null);
1611 // Notify all observers for resource selection change event
1612 UiListenerHandler.getInstance()
1613 .deviceSelectionChangedUINotification(selectedDevice);
1618 public List<MetaProperty> getMetaProperties(Resource resource) {
1619 if (null != resource) {
1623 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1625 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1626 propName = Constants.META_PROPERTIES[index];
1627 if (propName.equals(Constants.RESOURCE_NAME)) {
1628 propValue = resource.getResourceName();
1629 } else if (propName.equals(Constants.RESOURCE_URI)) {
1630 propValue = resource.getResourceURI();
1631 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1632 propValue = resource.getResourceTypes().toString();
1636 if (null != propValue) {
1637 metaPropertyList.add(new MetaProperty(propName, propValue));
1640 return metaPropertyList;
1645 public List<MetaProperty> getMetaProperties(Device dev) {
1647 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1648 metaPropertyList.add(new MetaProperty(Constants.DEVICE_NAME, dev
1650 return metaPropertyList;
1655 public boolean startResource(Resource resource) throws SimulatorException {
1656 if (null == resource) {
1659 SimulatorResource server = resource.getSimulatorResource();
1660 if (null == server) {
1665 resource.setStarted(true);
1666 } catch (SimulatorException e) {
1670 .log(Level.ERROR.ordinal(),
1672 "There is an error while starting the resource.\n"
1673 + Utility.getSimulatorErrorString(e, null));
1679 public boolean stopResource(Resource resource) throws SimulatorException {
1680 if (null == resource) {
1683 SimulatorResource server = resource.getSimulatorResource();
1684 if (null == server) {
1689 resource.setStarted(false);
1690 } catch (SimulatorException e) {
1694 .log(Level.ERROR.ordinal(),
1696 "There is an error while stopping the resource.\n"
1697 + Utility.getSimulatorErrorString(e, null));
1703 public boolean changeResourceName(Resource resource, String newName)
1704 throws SimulatorException {
1705 if (null == resource || null == newName) {
1709 if (!stopResource(resource)) {
1713 SimulatorResource server = resource.getSimulatorResource();
1715 server.setName(newName);
1716 resource.setResourceName(newName);
1717 } catch (SimulatorException e) {
1721 .log(Level.ERROR.ordinal(),
1723 "There is an error while changing the resource name.\n"
1724 + Utility.getSimulatorErrorString(e, null));
1728 if (!startResource(resource)) {
1735 public boolean changeDeviceName(Device dev, String newName) {
1736 if (null == dev || null == newName) {
1739 data.changeDeviceName(dev, dev.getDeviceName(), newName);
1743 public boolean changeResourceURI(Resource resource, String newURI)
1744 throws SimulatorException {
1745 if (null == resource || null == newURI) {
1749 if (!stopResource(resource)) {
1753 String curURI = resource.getResourceURI();
1754 SimulatorResource server = resource.getSimulatorResource();
1756 server.setURI(newURI);
1757 data.changeResourceURI(resource, curURI, newURI);
1758 } catch (SimulatorException e) {
1762 .log(Level.ERROR.ordinal(),
1764 "There is an error while changing the resource URI.\n"
1765 + Utility.getSimulatorErrorString(e, null));
1769 if (!startResource(resource)) {
1775 public boolean updateResourceProperties(Resource resource,
1776 List<MetaProperty> properties, boolean uriChanged,
1777 boolean nameChanged) throws SimulatorException {
1778 if (null == resource || null == properties) {
1782 // Updating the properties
1783 Iterator<MetaProperty> itr = properties.iterator();
1784 MetaProperty property;
1787 String resName = null;
1788 String resURI = null;
1789 while (itr.hasNext()) {
1790 property = itr.next();
1791 if (null == property) {
1794 propName = property.getPropName();
1795 propValue = property.getPropValue();
1796 if (propName.equals(Constants.RESOURCE_NAME)) {
1797 resName = propValue;
1798 } else if (propName.equals(Constants.RESOURCE_URI)) {
1804 if (!changeResourceName(resource, resName)) {
1808 // Notify UI Listeners
1809 UiListenerHandler.getInstance().propertiesChangedUINotification(
1814 if (!changeResourceURI(resource, resURI)) {
1822 public boolean updateDeviceProperties(Device dev,
1823 List<MetaProperty> properties) {
1824 if (null == dev || null == properties) {
1828 // Updating the properties
1829 Iterator<MetaProperty> itr = properties.iterator();
1830 MetaProperty property;
1833 String devName = null;
1834 while (itr.hasNext()) {
1835 property = itr.next();
1836 if (null == property) {
1839 propName = property.getPropName();
1840 propValue = property.getPropValue();
1841 if (propName.equals(Constants.DEVICE_NAME)) {
1842 devName = propValue;
1846 if (!changeDeviceName(dev, devName)) {
1850 // Notify UI Listeners
1851 UiListenerHandler.getInstance().propertiesChangedUINotification(
1857 public List<LocalResourceAttribute> getAttributes(Resource resource) {
1858 List<LocalResourceAttribute> attList = null;
1859 if (null != resource) {
1860 Map<String, LocalResourceAttribute> attMap = resource
1861 .getResourceAttributes();
1862 if (null != attMap && attMap.size() > 0) {
1863 attList = new ArrayList<LocalResourceAttribute>();
1864 Set<String> attNameSet = attMap.keySet();
1866 LocalResourceAttribute attribute;
1867 Iterator<String> attNameItr = attNameSet.iterator();
1868 while (attNameItr.hasNext()) {
1869 attName = attNameItr.next();
1870 attribute = attMap.get(attName);
1871 if (null != attribute) {
1872 attList.add(attribute);
1880 public List<SimulatorResourceAttribute> getAttributes(
1881 SimulatorResourceModel model) {
1882 List<SimulatorResourceAttribute> attList = null;
1883 if (null != model) {
1884 Map<String, SimulatorResourceAttribute> attMap = model
1886 if (null != attMap && attMap.size() > 0) {
1887 attList = new ArrayList<SimulatorResourceAttribute>();
1888 Set<String> attNameSet = attMap.keySet();
1890 SimulatorResourceAttribute attribute;
1891 Iterator<String> attNameItr = attNameSet.iterator();
1892 while (attNameItr.hasNext()) {
1893 attName = attNameItr.next();
1894 attribute = attMap.get(attName);
1895 if (null != attribute) {
1896 attList.add(attribute);
1904 public List<SRMItem> getIndexedAttributes(SimulatorResourceModel[] model) {
1905 List<SRMItem> indexedAttList = null;
1906 if (null != model && model.length > 0) {
1907 indexedAttList = new ArrayList<SRMItem>();
1909 for (SimulatorResourceModel m : model) {
1910 indexedAttList.add(new SRMItem(i++, m));
1913 return indexedAttList;
1916 public void attributeValueUpdated(SingleResource resource,
1917 String attributeName, AttributeValue value) {
1918 if (null != resource && null != attributeName && null != value) {
1919 SimulatorSingleResource simRes = (SimulatorSingleResource) resource
1920 .getSimulatorResource();
1921 if (null != simRes) {
1923 simRes.updateAttribute(attributeName, value);
1924 } catch (SimulatorException e) {
1928 .log(Level.ERROR.ordinal(), new Date(),
1929 Utility.getSimulatorErrorString(e, null));
1936 * private ModelChangeNotificationType compareAndUpdateLocalAttributes(
1937 * Map<String, LocalResourceAttribute> resourceAttributeMapOld, Map<String,
1938 * LocalResourceAttribute> resourceAttributeMapNew,
1939 * Set<LocalResourceAttribute> valueChangeSet) { ModelChangeNotificationType
1940 * notificationType = ModelChangeNotificationType.NONE; if (null !=
1941 * resourceAttributeMapOld && null != resourceAttributeMapNew) { Set<String>
1942 * oldMapKeySet = resourceAttributeMapOld.keySet(); Iterator<String>
1943 * attributeMapOldItr = oldMapKeySet.iterator(); String attName;
1944 * LocalResourceAttribute attributeOld; LocalResourceAttribute attributeNew;
1945 * Object attValueOld; Object attValueNew; String oldValueStr; String
1946 * newValueStr; while (attributeMapOldItr.hasNext()) { attName =
1947 * attributeMapOldItr.next(); if
1948 * (resourceAttributeMapNew.containsKey(attName)) { attributeOld =
1949 * resourceAttributeMapOld.get(attName); attributeNew =
1950 * resourceAttributeMapNew.get(attName); // Copy the attribute value from
1951 * new to old if the value // has been changed // Comparing only the
1952 * attribute's value considering the // fact that only the value can be
1953 * changed if (null != attributeOld && null != attributeNew) { attValueOld =
1954 * attributeOld.getAttributeValue(); attValueNew =
1955 * attributeNew.getAttributeValue();
1957 * oldValueStr = String.valueOf(attValueOld); newValueStr =
1958 * String.valueOf(attValueNew);
1960 * if (null != oldValueStr && null != newValueStr) { if
1961 * (!oldValueStr.equals(newValueStr)) {
1962 * attributeOld.setAttributeValue(attValueNew); notificationType =
1963 * ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED;
1964 * valueChangeSet.add(attributeOld); } } }
1965 * resourceAttributeMapNew.remove(attName); } else { // Attribute doesn't
1966 * exist in the new model. Hence // removing it from the model.
1967 * resourceAttributeMapOld.remove(attName); notificationType =
1968 * ModelChangeNotificationType.ATTRIBUTE_REMOVED; } } // Check for new
1969 * attributes in the new model if (resourceAttributeMapNew.size() > 0) {
1970 * Set<String> remainingAttSet = resourceAttributeMapNew.keySet();
1971 * Iterator<String> remainingAttItr = remainingAttSet.iterator();
1972 * LocalResourceAttribute attribute; while (remainingAttItr.hasNext()) {
1973 * attName = remainingAttItr.next(); if (null != attName) { attribute =
1974 * resourceAttributeMapNew.get(attName); if (null != attribute) {
1975 * resourceAttributeMapOld.put(attName, attribute); } } } notificationType =
1976 * ModelChangeNotificationType.ATTRIBUTE_ADDED; } } return notificationType;
1980 // TODO: This method should get the status from the native layer.
1981 public boolean isResourceStarted(Resource resource) {
1982 if (null == resource) {
1985 return resource.isStarted();
1988 public boolean isPropertyValueInvalid(Resource resource,
1989 List<MetaProperty> properties, String propName) {
1990 if (null == resource || null == properties || null == propName) {
1993 boolean invalid = false;
1995 Iterator<MetaProperty> itr = properties.iterator();
1996 while (itr.hasNext()) {
1998 if (prop.getPropName().equals(propName)) {
1999 String value = prop.getPropValue();
2000 if (null == value || value.trim().isEmpty()) {
2008 public boolean isPropertyValueInvalid(Device dev,
2009 List<MetaProperty> properties, String propName) {
2010 if (null == dev || null == properties || null == propName) {
2013 boolean invalid = false;
2015 Iterator<MetaProperty> itr = properties.iterator();
2016 while (itr.hasNext()) {
2018 if (prop.getPropName().equals(propName)) {
2019 String value = prop.getPropValue();
2020 if (null == value || value.trim().isEmpty()) {
2028 public boolean isPropValueChanged(Resource resource,
2029 List<MetaProperty> properties, String propName) {
2030 if (null == resource || null == properties || null == propName) {
2033 boolean changed = false;
2036 Iterator<MetaProperty> itr = properties.iterator();
2037 while (itr.hasNext()) {
2039 if (prop.getPropName().equals(propName)) {
2040 oldValue = getPropertyValueFromResource(resource, propName);
2041 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
2050 public boolean isPropValueChanged(Device dev,
2051 List<MetaProperty> properties, String propName) {
2052 if (null == dev || null == properties || null == propName) {
2055 boolean changed = false;
2058 Iterator<MetaProperty> itr = properties.iterator();
2059 while (itr.hasNext()) {
2061 if (prop.getPropName().equals(propName)) {
2062 oldValue = dev.getDeviceName();
2063 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
2072 private String getPropertyValueFromResource(Resource resource,
2074 if (null == resource || null == propName) {
2077 if (propName.equals(Constants.RESOURCE_URI)) {
2078 return resource.getResourceURI();
2079 } else if (propName.equals(Constants.RESOURCE_NAME)) {
2080 return resource.getResourceName();
2081 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
2082 return resource.getResourceTypes().toString();
2088 public boolean isURIChanged(Resource resource, List<MetaProperty> properties) {
2089 if (null == resource || null == properties) {
2092 boolean changed = false;
2094 Iterator<MetaProperty> itr = properties.iterator();
2095 while (itr.hasNext()) {
2097 if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
2098 if (!prop.getPropValue().equals(resource.getResourceURI())) {
2107 public boolean startResource(SingleResource resource) {
2108 if (null == resource || resource.isStarted()) {
2112 SimulatorResource server = resource.getSimulatorResource();
2113 if (null == server) {
2118 resource.setStarted(true);
2120 } catch (SimulatorException e) {
2124 .log(Level.ERROR.ordinal(), new Date(),
2125 Utility.getSimulatorErrorString(e, null));
2132 public boolean stopResource(SingleResource resource) {
2133 if (null == resource || !resource.isStarted()) {
2137 SimulatorResource server = resource.getSimulatorResource();
2138 if (null == server) {
2143 resource.setStarted(false);
2145 } catch (SimulatorException e) {
2149 .log(Level.ERROR.ordinal(), new Date(),
2150 Utility.getSimulatorErrorString(e, null));
2157 public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
2161 AttributeProperty prop = att.property();
2165 Type attProp = prop.type();
2166 if (attProp == Type.UNKNOWN) {
2172 public int startAutomation(SingleResource resource,
2173 LocalResourceAttribute attribute, AutoUpdateType autoType,
2174 int autoUpdateInterval) {
2176 if (null != resource && null != attribute) {
2177 SimulatorSingleResource server = (SimulatorSingleResource) resource
2178 .getSimulatorResource();
2179 if (null != server) {
2180 String attrName = attribute.getResourceAttributeRef().name();
2182 autoId = server.startAttributeUpdation(attrName, autoType,
2183 autoUpdateInterval, automationListener);
2184 } catch (SimulatorException e) {
2188 .log(Level.ERROR.ordinal(),
2190 "[" + e.getClass().getSimpleName() + "]"
2191 + e.code().toString() + "-"
2196 attribute.setAutomationId(autoId);
2197 attribute.setAutomationType(autoType);
2198 attribute.setAutomationUpdateInterval(autoUpdateInterval);
2199 attribute.setAutomationInProgress(true);
2200 resource.setAttributeAutomationInProgress(true);
2202 attribute.setAutomationInProgress(false);
2209 public void stopAutomation(SingleResource resource,
2210 LocalResourceAttribute att, int autoId) {
2211 if (null != resource) {
2212 SimulatorSingleResource server = (SimulatorSingleResource) resource
2213 .getSimulatorResource();
2214 if (null != server) {
2216 server.stopUpdation(autoId);
2217 } catch (SimulatorException e) {
2221 .log(Level.ERROR.ordinal(),
2223 "[" + e.getClass().getSimpleName() + "]"
2224 + e.code().toString() + "-"
2228 // Change the automation status
2229 att.setAutomationInProgress(false);
2230 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
2235 public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
2236 int autoUpdateInterval, final SingleResource resource) {
2237 if (null == resource) {
2240 boolean status = false;
2241 changeResourceLevelAutomationStatus(resource, true);
2242 // Invoke the native automation method
2243 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
2244 .getSimulatorResource();
2245 if (null != resourceServer) {
2248 autoId = resourceServer.startResourceUpdation(autoType,
2249 autoUpdateInterval, automationListener);
2250 } catch (SimulatorException e) {
2254 .log(Level.ERROR.ordinal(), new Date(),
2255 Utility.getSimulatorErrorString(e, null));
2259 // Automation request failed and hence status is being
2261 changeResourceLevelAutomationStatus(resource, false);
2263 // Automation request accepted.
2264 resource.setAutomationId(autoId);
2266 // Notify the UI listeners in a different thread.
2267 Thread notifyThread = new Thread() {
2269 UiListenerHandler.getInstance()
2270 .resourceAutomationStartedUINotification(
2274 notifyThread.setPriority(Thread.MAX_PRIORITY);
2275 notifyThread.start();
2283 public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
2284 if (null == resource) {
2287 final int autoId = resource.getAutomationId();
2291 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
2292 .getSimulatorResource();
2293 if (null == resourceServer) {
2296 // Call native method
2298 resourceServer.stopUpdation(autoId);
2299 } catch (SimulatorException e) {
2303 .log(Level.ERROR.ordinal(), new Date(),
2304 Utility.getSimulatorErrorString(e, null));
2308 // Notify the UI Listeners. Invoke the automation complete callback.
2309 Thread stopThread = new Thread() {
2311 automationListener.onUpdateComplete(resource.getResourceURI(),
2319 private boolean isAnyAttributeInAutomation(SingleResource resource) {
2320 if (null == resource) {
2323 Map<String, LocalResourceAttribute> attMap = resource
2324 .getResourceAttributes();
2325 if (null == attMap) {
2328 boolean status = false;
2329 Set<String> keySet = attMap.keySet();
2330 Iterator<String> attItr = keySet.iterator();
2331 while (attItr.hasNext()) {
2332 LocalResourceAttribute attribute = attMap.get(attItr.next());
2333 if (attribute.isAutomationInProgress()) {
2341 // Changes the automation state of the resource and its attributes
2342 private void changeResourceLevelAutomationStatus(SingleResource resource,
2345 Map<String, LocalResourceAttribute> attributeMap = resource
2346 .getResourceAttributes();
2347 if (null != attributeMap) {
2348 Set<String> attrNameSet = attributeMap.keySet();
2349 Iterator<String> attrNameItr = attrNameSet.iterator();
2351 LocalResourceAttribute attribute;
2352 while (attrNameItr.hasNext()) {
2353 attrName = attrNameItr.next();
2354 attribute = attributeMap.get(attrName);
2355 if (null != attribute) {
2356 attribute.setAutomationInProgress(status);
2360 resource.setResourceAutomationInProgress(status);
2363 private LocalResourceAttribute getAttributeWithGivenAutomationId(
2364 SingleResource resource, int automationId) {
2365 LocalResourceAttribute targetAttribute = null;
2366 if (null != resource) {
2367 Map<String, LocalResourceAttribute> attributeMap = resource
2368 .getResourceAttributes();
2369 if (null != attributeMap) {
2370 Set<String> attNameSet = attributeMap.keySet();
2371 Iterator<String> attNameItr = attNameSet.iterator();
2373 LocalResourceAttribute attribute;
2374 while (attNameItr.hasNext()) {
2375 attName = attNameItr.next();
2376 if (null != attName) {
2377 attribute = attributeMap.get(attName);
2378 if (null != attribute) {
2379 if (attribute.isAutomationInProgress()
2380 && (attribute.getAutomationId() == automationId)) {
2381 targetAttribute = attribute;
2389 return targetAttribute;
2392 public boolean isResourceAutomationStarted(SingleResource resource) {
2393 boolean status = false;
2394 if (null != resource) {
2395 status = resource.isResourceAutomationInProgress();
2400 public boolean isAttributeAutomationStarted(SingleResource resource) {
2401 if (null == resource) {
2404 return resource.isAttributeAutomationInProgress();
2407 public LocalResourceAttribute getAttributeByResourceURI(
2408 SingleResource resource, String attName) {
2409 if (null == resource || null == attName) {
2412 Map<String, LocalResourceAttribute> attMap = resource
2413 .getResourceAttributes();
2414 if (null == attMap) {
2417 return attMap.get(attName);
2420 public void notifyObserverRequest(Resource resource, int observerId) {
2421 if (null == resource) {
2424 SimulatorResource simulatorResource = resource.getSimulatorResource();
2425 if (null == simulatorResource) {
2429 simulatorResource.notifyObserver(observerId);
2430 } catch (SimulatorException e) {
2434 .log(Level.ERROR.ordinal(), new Date(),
2435 Utility.getSimulatorErrorString(e, null));
2439 public void shutdown() {
2440 threadHandle.interrupt();
2443 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
2448 AttributeValue val = att.value();
2453 TypeInfo type = val.typeInfo();
2455 AttributeProperty prop = att.property();
2460 List<String> values = new ArrayList<String>();
2462 Type valuesType = prop.type();
2464 if (valuesType == Type.UNKNOWN) {
2465 // Adding the default value
2466 values.add(Utility.getAttributeValueAsString(val));
2470 if (type.mType != ValueType.RESOURCEMODEL) {
2471 if (type.mType == ValueType.ARRAY) {
2472 if (type.mDepth == 1) {
2473 AttributeProperty childProp = prop.getChildProperty();
2474 if (null != childProp) {
2475 valuesType = childProp.type();
2476 if (valuesType == Type.RANGE) {
2477 List<String> list = getRangeForPrimitiveNonArrayAttributes(
2478 childProp, type.mBaseType);
2480 values.addAll(list);
2482 } else if (valuesType == Type.VALUESET) {
2483 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
2484 childProp.valueSet(), type.mBaseType);
2486 values.addAll(list);
2492 if (valuesType == Type.RANGE) {
2493 List<String> list = getRangeForPrimitiveNonArrayAttributes(
2496 values.addAll(list);
2498 } else if (valuesType == Type.VALUESET) {
2499 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
2500 prop.valueSet(), type.mType);
2502 values.addAll(list);
2511 public List<String> getRangeForPrimitiveNonArrayAttributes(
2512 AttributeProperty prop, ValueType type) {
2517 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
2521 List<String> values = new ArrayList<String>();
2524 int min = (int) prop.min();
2525 int max = (int) prop.max();
2526 for (int iVal = min; iVal <= max; iVal++) {
2527 values.add(String.valueOf(iVal));
2531 double minD = (double) prop.min();
2532 double maxD = (double) prop.max();
2533 for (double iVal = minD; iVal <= maxD; iVal = iVal + 1.0) {
2534 values.add(String.valueOf(iVal));
2542 public List<String> getAllowedValuesForPrimitiveNonArrayAttributes(
2543 AttributeValue[] attValues, ValueType type) {
2544 if (null == attValues || attValues.length < 1) {
2548 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
2553 List<String> values = new ArrayList<String>();
2554 for (AttributeValue val : attValues) {
2562 values.add(String.valueOf(obj));