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.HashSet;
24 import java.util.Iterator;
25 import java.util.LinkedList;
26 import java.util.List;
30 import oic.simulator.serviceprovider.Activator;
31 import oic.simulator.serviceprovider.listener.IAutomationUIListener;
32 import oic.simulator.serviceprovider.listener.IObserverListChangedUIListener;
33 import oic.simulator.serviceprovider.listener.IResourceListChangedUIListener;
34 import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
35 import oic.simulator.serviceprovider.listener.IResourceSelectionChangedUIListener;
36 import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
37 import oic.simulator.serviceprovider.resource.MetaProperty;
38 import oic.simulator.serviceprovider.resource.ModelChangeNotificationType;
39 import oic.simulator.serviceprovider.resource.SimulatorResource;
40 import oic.simulator.serviceprovider.resource.StandardConfiguration;
41 import oic.simulator.serviceprovider.utils.Constants;
42 import oic.simulator.serviceprovider.utils.Utility;
44 import org.eclipse.swt.graphics.Image;
45 import org.oic.simulator.IAutomation;
46 import org.oic.simulator.ILogger.Level;
47 import org.oic.simulator.ResourceAttribute;
48 import org.oic.simulator.ResourceAttribute.Range;
49 import org.oic.simulator.ResourceAttribute.Type;
50 import org.oic.simulator.SimulatorException;
51 import org.oic.simulator.SimulatorManager;
52 import org.oic.simulator.SimulatorResourceModel;
53 import org.oic.simulator.serviceprovider.AutomationType;
54 import org.oic.simulator.serviceprovider.IObserver;
55 import org.oic.simulator.serviceprovider.IResourceModelChangedListener;
56 import org.oic.simulator.serviceprovider.ObserverInfo;
57 import org.oic.simulator.serviceprovider.SimulatorResourceServer;
60 * This class acts as an interface between the simulator java SDK and the
61 * various UI modules. It maintains all the details of resources and provides
62 * other UI modules with the information required. It also handles model change,
63 * automation, and observer related events from native layer and propagates
64 * those events to the registered UI listeners.
66 public class ResourceManager {
68 private Map<String, Map<String, SimulatorResource>> resourceMap;
70 private Map<String, ArrayList<String>> orderedResourceUriMap;
72 private StandardConfiguration stdConfig;
74 private SimulatorResource currentResourceInSelection;
76 private List<IResourceListChangedUIListener> resourceListChangedUIListeners;
78 private List<IResourceSelectionChangedUIListener> resourceSelectionChangedUIListeners;
80 private List<IResourceModelChangedUIListener> resourceModelChangedUIListeners;
82 private List<IAutomationUIListener> automationUIListeners;
84 private List<IObserverListChangedUIListener> observerUIListeners;
86 private IResourceModelChangedListener resourceModelChangeListener;
88 private IAutomation automationListener;
90 private IObserver observer;
92 private NotificationSynchronizerThread synchronizerThread;
94 private Thread threadHandle;
96 public ResourceManager() {
97 resourceMap = new HashMap<String, Map<String, SimulatorResource>>();
98 orderedResourceUriMap = new HashMap<String, ArrayList<String>>();
99 stdConfig = new StandardConfiguration();
101 resourceListChangedUIListeners = new ArrayList<IResourceListChangedUIListener>();
102 resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
103 resourceModelChangedUIListeners = new ArrayList<IResourceModelChangedUIListener>();
104 automationUIListeners = new ArrayList<IAutomationUIListener>();
105 observerUIListeners = new ArrayList<IObserverListChangedUIListener>();
107 resourceModelChangeListener = new IResourceModelChangedListener() {
110 public void onResourceModelChanged(final String resourceURI,
111 final SimulatorResourceModel resourceModelN) {
112 synchronizerThread.addToQueue(new Runnable() {
116 if (null == resourceURI || null == resourceModelN) {
119 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
120 if (null == resource) {
124 ModelChangeNotificationType notificationType;
125 // Fetch the resource attributes
126 Map<String, LocalResourceAttribute> resourceAttributeMapNew;
127 resourceAttributeMapNew = fetchResourceAttributesFromModel(resourceModelN);
128 if (null == resourceAttributeMapNew) {
129 resource.setResourceAttributesMap(null);
130 resourceModelChangedUINotification(
131 ModelChangeNotificationType.NO_ATTRIBUTES_IN_MODEL,
136 // Update the resource with new model data
137 Map<String, LocalResourceAttribute> resourceAttributeMapOld;
138 resourceAttributeMapOld = resource
139 .getResourceAttributesMap();
140 if (null == resourceAttributeMapOld) {
141 resource.setResourceAttributesMap(resourceAttributeMapNew);
142 resourceModelChangedUINotification(
143 ModelChangeNotificationType.ATTRIBUTE_ADDED,
147 Set<LocalResourceAttribute> valueChangeSet = new HashSet<LocalResourceAttribute>();
148 notificationType = compareAndUpdateLocalAttributes(
149 resourceAttributeMapOld,
150 resourceAttributeMapNew, valueChangeSet);
151 if (notificationType != ModelChangeNotificationType.NONE) {
152 // Update the UI listeners
153 resourceModelChangedUINotification(
154 notificationType, resourceURI,
162 automationListener = new IAutomation() {
165 public void onAutomationComplete(final String resourceURI,
166 final int automationId) {
167 synchronizerThread.addToQueue(new Runnable() {
171 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
172 if (null == resource) {
175 // Checking whether this notification is for an
176 // attribute or a resource
177 if (resource.isResourceAutomationInProgress()) {
178 changeResourceLevelAutomationStatus(resource, false);
179 // Notify the UI listeners
180 automationCompleteUINotification(resourceURI, null);
181 } else if (resource.isAttributeAutomationInProgress()) {
182 // Find the attribute with the given automation id
183 LocalResourceAttribute attribute;
184 attribute = getAttributeWithGivenAutomationId(
185 resource, automationId);
186 if (null != attribute) {
187 attribute.setAutomationInProgress(false);
188 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
189 // Notify the UI listeners
190 automationCompleteUINotification(resourceURI,
191 attribute.getAttributeName());
194 // Ignoring the notification as there are no
195 // known automation for the current resource.
202 observer = new IObserver() {
205 public void onObserverChanged(final String resourceURI,
206 final int status, final ObserverInfo observer) {
210 if (null == resourceURI || null == observer) {
213 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
214 if (null == resource) {
217 // Update the observers information
219 resource.addObserverInfo(observer);
221 resource.removeObserverInfo(observer);
223 // Notify the UI listeners
224 observerListChangedUINotification(resourceURI);
230 synchronizerThread = new NotificationSynchronizerThread();
231 threadHandle = new Thread(synchronizerThread);
232 threadHandle.setName("Simulator service provider event queue");
233 threadHandle.start();
236 private static class NotificationSynchronizerThread implements Runnable {
238 LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
242 while (!Thread.interrupted()) {
243 synchronized (this) {
245 while (notificationQueue.isEmpty()) {
249 } catch (InterruptedException e) {
255 synchronized (this) {
256 thread = notificationQueue.pop();
260 } catch (Exception e) {
261 if (e instanceof InterruptedException) {
269 public void addToQueue(Runnable event) {
270 synchronized (this) {
271 notificationQueue.add(event);
277 // This method gives a list of available RAML resource configurations.
278 public Map<String, String> getResourceConfigurationList() {
279 return stdConfig.getStandardResourceConfigurationList();
282 public String getConfigFilePath(String fileName) {
283 return stdConfig.getFilePath(fileName);
286 public void addResourceListChangedUIListener(
287 IResourceListChangedUIListener resourceListChangedUIListener) {
288 synchronized (resourceListChangedUIListeners) {
289 resourceListChangedUIListeners.add(resourceListChangedUIListener);
293 public void addResourceSelectionChangedUIListener(
294 IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
295 synchronized (resourceSelectionChangedUIListeners) {
296 resourceSelectionChangedUIListeners
297 .add(resourceSelectionChangedUIListener);
301 public void addResourceModelChangedUIListener(
302 IResourceModelChangedUIListener resourceModelChangedUIListener) {
303 synchronized (resourceModelChangedUIListeners) {
304 resourceModelChangedUIListeners.add(resourceModelChangedUIListener);
308 public void addAutomationUIListener(
309 IAutomationUIListener automationUIListener) {
310 synchronized (automationUIListeners) {
311 automationUIListeners.add(automationUIListener);
315 public void addObserverListChangedUIListener(
316 IObserverListChangedUIListener observerListChangedUIListener) {
317 synchronized (observerUIListeners) {
318 observerUIListeners.add(observerListChangedUIListener);
322 public void removeResourceListChangedUIListener(
323 IResourceListChangedUIListener listener) {
324 synchronized (resourceListChangedUIListeners) {
325 if (null != listener && resourceListChangedUIListeners.size() > 0) {
326 resourceListChangedUIListeners.remove(listener);
331 public void removeResourceSelectionChangedUIListener(
332 IResourceSelectionChangedUIListener listener) {
333 synchronized (resourceSelectionChangedUIListeners) {
335 && resourceSelectionChangedUIListeners.size() > 0) {
336 resourceSelectionChangedUIListeners.remove(listener);
341 public void removeResourceModelChangedUIListener(
342 IResourceModelChangedUIListener listener) {
343 synchronized (resourceModelChangedUIListeners) {
344 if (null != listener && resourceModelChangedUIListeners.size() > 0) {
345 resourceModelChangedUIListeners.remove(listener);
350 public void removeAutomationUIListener(IAutomationUIListener listener) {
351 synchronized (automationUIListeners) {
352 if (null != listener && automationUIListeners.size() > 0) {
353 automationUIListeners.remove(listener);
358 public void removeObserverListChangedUIListener(
359 IObserverListChangedUIListener listener) {
360 synchronized (observerUIListeners) {
361 if (null != listener && observerUIListeners.size() > 0) {
362 observerUIListeners.remove(listener);
367 public synchronized SimulatorResource getCurrentResourceInSelection() {
368 return currentResourceInSelection;
371 public synchronized void setCurrentResourceInSelection(
372 SimulatorResource resource) {
373 this.currentResourceInSelection = resource;
376 private void addResourceUriToOrderedMap(String resourceType,
377 String resourceURI) {
378 if (null != resourceURI && null != resourceType) {
379 synchronized (orderedResourceUriMap) {
380 ArrayList<String> uriListForType = orderedResourceUriMap
382 if (null == uriListForType) {
383 uriListForType = new ArrayList<String>();
384 orderedResourceUriMap.put(resourceType, uriListForType);
386 uriListForType.add(resourceURI);
391 private void removeResourceUriFromOrderedMap(String resourceType,
392 String resourceURI) {
393 synchronized (orderedResourceUriMap) {
394 if (null != resourceURI && null != resourceType) {
395 ArrayList<String> uriListForType = orderedResourceUriMap
397 if (null != uriListForType) {
398 uriListForType.remove(resourceURI);
399 if (uriListForType.size() < 1) {
400 orderedResourceUriMap.remove(resourceType);
403 } else if (null != resourceURI) {
404 orderedResourceUriMap.remove(resourceType);
406 orderedResourceUriMap.clear();
411 private void addResourceToMap(SimulatorResource simulatorResource) {
412 if (null != simulatorResource) {
413 synchronized (resourceMap) {
414 Map<String, SimulatorResource> resourceTypeMap;
415 resourceTypeMap = resourceMap.get(simulatorResource
417 if (null == resourceTypeMap) {
418 resourceTypeMap = new HashMap<String, SimulatorResource>();
419 resourceMap.put(simulatorResource.getResourceType(),
422 resourceTypeMap.put(simulatorResource.getResourceURI(),
428 private void addResourceToMap(String resourceType,
429 Map<String, SimulatorResource> newResourceTypeMap) {
430 if (null != resourceType && null != newResourceTypeMap) {
431 synchronized (resourceMap) {
432 Map<String, SimulatorResource> resourceTypeMap = resourceMap
434 if (null != resourceTypeMap) {
435 resourceTypeMap.putAll(newResourceTypeMap);
437 resourceMap.put(resourceType, newResourceTypeMap);
443 private void removeResourceFromMap(String resourceType, String resourceURI) {
444 if (null != resourceURI && null != resourceType) {
445 synchronized (resourceMap) {
446 Map<String, SimulatorResource> resourceTypeMap = resourceMap
448 if (null != resourceTypeMap) {
449 resourceTypeMap.remove(resourceURI);
450 if (resourceTypeMap.size() < 1) {
451 resourceMap.remove(resourceType);
458 public boolean isResourceExist(String resourceURI) {
459 boolean result = false;
460 if (null != resourceURI) {
461 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
462 if (null != resource) {
469 public void createResource(final String configFilePath) {
473 SimulatorResourceServer resourceServerN;
475 resourceServerN = SimulatorManager.createResource(
476 configFilePath, resourceModelChangeListener);
477 } catch (SimulatorException e) {
481 .log(Level.ERROR.ordinal(),
483 "[" + e.getClass().getSimpleName() + "]"
484 + e.code().toString() + "-"
488 SimulatorResource simulatorResource;
489 simulatorResource = fetchResourceData(resourceServerN);
490 if (null != simulatorResource) {
491 addResourceToMap(simulatorResource);
492 addResourceUriToOrderedMap(
493 simulatorResource.getResourceType(),
494 simulatorResource.getResourceURI());
495 resourceCreatedUINotification();
497 // Set the observer for the created resource
499 resourceServerN.setObserverCallback(observer);
500 } catch (SimulatorException e) {
504 .log(Level.ERROR.ordinal(),
506 "[" + e.getClass().getSimpleName()
507 + "]" + e.code().toString()
508 + "-" + e.message());
511 // Print the resource data
512 simulatorResource.printResourceInfo();
518 public void createResource(final String configFilePath,
519 final int noOfInstances) {
523 Map<String, SimulatorResource> resourceTypeMap;
524 SimulatorResourceServer[] simulatorResourceServers = null;
526 simulatorResourceServers = SimulatorManager.createResource(
527 configFilePath, noOfInstances,
528 resourceModelChangeListener);
529 } catch (SimulatorException e) {
533 .log(Level.ERROR.ordinal(),
535 "[" + e.getClass().getSimpleName() + "]"
536 + e.code().toString() + "-"
540 if (null == simulatorResourceServers) {
543 resourceTypeMap = new HashMap<String, SimulatorResource>();
544 SimulatorResource resource;
546 for (SimulatorResourceServer resourceServerN : simulatorResourceServers) {
547 resource = fetchResourceData(resourceServerN);
548 if (null != resource) {
549 uri = resource.getResourceURI();
550 resourceTypeMap.put(uri, resource);
551 addResourceUriToOrderedMap(resource.getResourceType(),
554 // Set the observer for the created resource
556 resourceServerN.setObserverCallback(observer);
557 } catch (SimulatorException e) {
561 .log(Level.ERROR.ordinal(),
563 "[" + e.getClass().getSimpleName()
564 + "]" + e.code().toString()
565 + "-" + e.message());
569 // Find the resourceType and add it to the local data
570 // structure and notify UI Listeners
571 if (resourceTypeMap.size() > 0) {
573 Set<String> uriSet = resourceTypeMap.keySet();
574 Iterator<String> itr = uriSet.iterator();
576 SimulatorResource simResource = resourceTypeMap.get(itr
578 if (null != simResource) {
579 resourceType = simResource.getResourceType();
581 addResourceToMap(resourceType, resourceTypeMap);
582 resourceCreatedUINotification();
590 private SimulatorResource fetchResourceData(
591 SimulatorResourceServer resourceServerN) {
592 SimulatorResource simulatorResource = null;
593 if (null != resourceServerN) {
594 simulatorResource = new SimulatorResource();
595 simulatorResource.setResourceServer(resourceServerN);
596 simulatorResource.setResourceURI(resourceServerN.getURI());
598 .setResourceType(resourceServerN.getResourceType());
599 simulatorResource.setResourceName(resourceServerN.getName());
600 simulatorResource.setResourceInterface(resourceServerN
601 .getInterfaceType());
603 SimulatorResourceModel resourceModelN;
605 resourceModelN = resourceServerN.getModel();
606 } catch (SimulatorException e) {
610 .log(Level.ERROR.ordinal(),
612 "[" + e.getClass().getSimpleName() + "]"
613 + e.code().toString() + "-"
617 if (null != resourceModelN) {
618 simulatorResource.setResourceModel(resourceModelN);
620 // Fetch the resource attributes
621 Map<String, LocalResourceAttribute> resourceAttributeMap;
622 resourceAttributeMap = fetchResourceAttributesFromModel(resourceModelN);
623 if (null != resourceAttributeMap) {
625 .setResourceAttributesMap(resourceAttributeMap);
629 return simulatorResource;
632 private Map<String, LocalResourceAttribute> fetchResourceAttributesFromModel(
633 SimulatorResourceModel resourceModelN) {
634 Map<String, LocalResourceAttribute> resourceAttributeMap = null;
635 if (null != resourceModelN) {
636 Map<String, ResourceAttribute> attributeMapN;
638 attributeMapN = resourceModelN.getAttributes();
639 } catch (SimulatorException e) {
643 .log(Level.ERROR.ordinal(),
645 "[" + e.getClass().getSimpleName() + "]"
646 + e.code().toString() + "-"
650 if (null != attributeMapN) {
651 resourceAttributeMap = new HashMap<String, LocalResourceAttribute>();
653 Set<String> attNameSet = attributeMapN.keySet();
655 ResourceAttribute attributeN;
656 LocalResourceAttribute attribute;
657 Iterator<String> attNameItr = attNameSet.iterator();
658 while (attNameItr.hasNext()) {
659 attName = attNameItr.next();
660 attributeN = attributeMapN.get(attName);
661 if (null != attributeN) {
662 attribute = new LocalResourceAttribute();
663 attribute.setResourceAttribute(attributeN);
665 // Set the attribute value
666 Object valueObj = attributeN.getValue();
667 if (null != valueObj) {
668 attribute.setAttributeValue(valueObj);
671 // Set the attribute value list.
672 attribute.setAttValues(getValueList(attributeN));
674 // Initially disabling the automation
675 attribute.setAutomationInProgress(false);
677 // TODO: Temporarily setting the interval to 500.
678 // This value should come from the native layer.
679 // Native implementation is in progress.
681 .setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
683 // Setting the default automation type
685 .setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);
687 resourceAttributeMap.put(attName, attribute);
692 return resourceAttributeMap;
695 // This method gives all known possible values of the attribute in string
696 // format. It takes allowed values or range of values whichever is available
697 private List<String> getValueList(ResourceAttribute attributeN) {
698 Object[] allowedValues = attributeN.getAllowedValues();
699 List<String> valueList = new ArrayList<String>();
700 if (null != allowedValues && allowedValues.length > 0) {
701 for (Object value : allowedValues) {
703 valueList.add(String.valueOf(value));
707 Type valueType = attributeN.getBaseType();
708 Range range = attributeN.getRange();
710 Object min = range.getMin();
711 Object max = range.getMax();
712 if (valueType == Type.INT) {
713 int minI = (Integer) min;
714 int maxI = (Integer) max;
715 for (int value = minI; value <= maxI; value++) {
716 valueList.add(String.valueOf(value));
718 } else if (valueType == Type.DOUBLE) {
719 double minD = (Double) min;
720 double maxD = (Double) max;
721 for (double value = minD; value <= maxD; value++) {
722 valueList.add(String.valueOf(value));
727 Object attValue = attributeN.getValue();
728 if (valueList.size() < 1 && null != attValue) {
729 valueList.add(String.valueOf(attValue));
734 public void deleteResourceByURI(final String resourceURI) {
735 if (null != resourceURI) {
739 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
740 if (null != resource) {
741 String resourceType = resource.getResourceType();
743 // Unregister the resource from the platform
744 deleteResource(resource);
746 // Delete from the local data structure
747 deleteLocalResourceDetails(resourceType, resourceURI);
749 // Notify the UI listener for removing this resource
751 resourceDeletedUINotification();
753 if (null != currentResourceInSelection
754 && resource == currentResourceInSelection) {
755 // Listeners might query the resource being deleted
756 // if exists. So set the currently selection to
758 setCurrentResourceInSelection(null);
760 // Notify all observers for resource selection
762 resourceSelectionChangedUINotification();
770 private SimulatorResource getSimulatorResourceByURI(String resourceURI) {
771 SimulatorResource resource = null;
772 if (null != resourceURI) {
773 synchronized (resourceMap) {
774 Set<String> typeSet = resourceMap.keySet();
775 Iterator<String> typeItr = typeSet.iterator();
777 Map<String, SimulatorResource> resourceTypeMap;
778 while (typeItr.hasNext()) {
779 resourceType = typeItr.next();
780 resourceTypeMap = resourceMap.get(resourceType);
781 if (null != resourceTypeMap) {
782 resource = resourceTypeMap.get(resourceURI);
783 if (null != resource) {
793 private void deleteResource(SimulatorResource resource) {
794 if (null != resource) {
795 SimulatorResourceServer resourceServerN = resource
796 .getResourceServer();
797 if (null != resourceServerN) {
799 SimulatorManager.deleteResource(resourceServerN);
800 } catch (SimulatorException e) {
804 .log(Level.ERROR.ordinal(),
806 "[" + e.getClass().getSimpleName() + "]"
807 + e.code().toString() + "-"
814 public void deleteResourceByType(final String resourceType) {
815 if (null != resourceType) {
819 // Unregister the resources from the platform
820 deleteResource(resourceType);
822 // Delete from the local data structure
823 deleteLocalResourceDetails(resourceType, null);
825 // Notify the UI listener for removing this resource from UI
826 resourceDeletedUINotification();
828 if (null != currentResourceInSelection
829 && resourceType.equals(currentResourceInSelection
830 .getResourceType())) {
831 // Listeners might query the resource being deleted if
832 // exists. So set the currently selection to null.
833 setCurrentResourceInSelection(null);
835 // Notify all observers for resource selection change
837 resourceSelectionChangedUINotification();
844 private void deleteResource(String resourceType) {
845 if (null != resourceType) {
847 SimulatorManager.deleteResources(resourceType);
848 } catch (SimulatorException e) {
852 .log(Level.ERROR.ordinal(),
854 "[" + e.getClass().getSimpleName() + "]"
855 + e.code().toString() + "-"
861 public void deleteAllResources() {
865 // Unregister the resources from the platform
868 // Delete from the local data structure
869 deleteLocalResourceDetails(null, null);
871 // Notify the UI listener for removing this resource from UI
872 resourceDeletedUINotification();
874 // Listeners might query the resource being deleted if exists.
875 // So set the currently selection to null.
876 setCurrentResourceInSelection(null);
878 // Notify all observers for resource selection change event
879 resourceSelectionChangedUINotification();
884 private void deleteResource() {
886 SimulatorManager.deleteResources(null);
887 } catch (SimulatorException e) {
891 .log(Level.ERROR.ordinal(),
893 "[" + e.getClass().getSimpleName() + "]"
894 + e.code().toString() + "-" + e.message());
898 private void deleteLocalResourceDetails(String resourceType,
899 String resourceURI) {
900 if (null != resourceType && null != resourceURI) {
901 removeResourceFromMap(resourceType, resourceURI);
902 removeResourceUriFromOrderedMap(resourceType, resourceURI);
904 synchronized (resourceMap) {
905 if (null != resourceType) {
906 removeResourceUriFromOrderedMap(resourceType, null);
907 resourceMap.remove(resourceType);
910 removeResourceUriFromOrderedMap(null, null);
916 private void resourceCreatedUINotification() {
917 synchronized (resourceListChangedUIListeners) {
918 if (resourceListChangedUIListeners.size() > 0) {
919 IResourceListChangedUIListener listener;
920 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
922 while (listenerItr.hasNext()) {
923 listener = listenerItr.next();
924 if (null != listener) {
925 listener.onResourceCreation();
932 private void resourceDeletedUINotification() {
933 synchronized (resourceListChangedUIListeners) {
934 if (resourceListChangedUIListeners.size() > 0) {
935 IResourceListChangedUIListener listener;
936 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
938 while (listenerItr.hasNext()) {
939 listener = listenerItr.next();
940 if (null != listener) {
941 listener.onResourceDeletion();
948 private void resourceSelectionChangedUINotification() {
949 synchronized (resourceSelectionChangedUIListeners) {
950 if (resourceSelectionChangedUIListeners.size() > 0) {
951 IResourceSelectionChangedUIListener listener;
952 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
954 while (listenerItr.hasNext()) {
955 listener = listenerItr.next();
956 if (null != listener) {
957 listener.onResourceSelectionChange();
964 private void resourceModelChangedUINotification(
965 ModelChangeNotificationType notificationType, String resourceURI,
966 Set<LocalResourceAttribute> valueChangeSet) {
967 synchronized (resourceModelChangedUIListeners) {
968 if (resourceModelChangedUIListeners.size() > 0
969 && notificationType != ModelChangeNotificationType.NONE
970 && null != resourceURI) {
971 IResourceModelChangedUIListener listener;
972 Iterator<IResourceModelChangedUIListener> listenerItr = resourceModelChangedUIListeners
974 while (listenerItr.hasNext()) {
975 listener = listenerItr.next();
976 if (null != listener) {
977 listener.onResourceModelChange(notificationType,
978 resourceURI, valueChangeSet);
985 private void resourceAutomationStartedUINotification(String resourceURI) {
986 synchronized (automationUIListeners) {
987 if (automationUIListeners.size() > 0 && null != resourceURI) {
988 IAutomationUIListener listener;
989 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
991 while (listenerItr.hasNext()) {
992 listener = listenerItr.next();
993 if (null != listener) {
994 listener.onResourceAutomationStart(resourceURI);
1001 private void automationCompleteUINotification(String resourceURI,
1003 synchronized (automationUIListeners) {
1004 if (automationUIListeners.size() > 0 && null != resourceURI) {
1005 IAutomationUIListener listener;
1006 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
1008 while (listenerItr.hasNext()) {
1009 listener = listenerItr.next();
1010 if (null != listener) {
1011 listener.onAutomationComplete(resourceURI, attName);
1018 private void observerListChangedUINotification(String resourceURI) {
1019 synchronized (observerUIListeners) {
1020 if (observerUIListeners.size() > 0 && null != resourceURI) {
1021 IObserverListChangedUIListener listener;
1022 Iterator<IObserverListChangedUIListener> listenerItr = observerUIListeners
1024 while (listenerItr.hasNext()) {
1025 listener = listenerItr.next();
1026 if (null != listener) {
1027 listener.onObserverListChanged(resourceURI);
1034 public List<String> getResourceTypeList() {
1035 List<String> typeList = null;
1036 synchronized (resourceMap) {
1037 if (resourceMap.size() > 0) {
1038 typeList = new ArrayList<String>();
1039 Set<String> typeSet = resourceMap.keySet();
1040 Iterator<String> typeItr = typeSet.iterator();
1041 while (typeItr.hasNext()) {
1042 typeList.add(typeItr.next());
1049 public boolean isTypeExist(String resType) {
1050 synchronized (resourceMap) {
1051 if (resourceMap.containsKey(resType)) {
1058 public List<String> getURIList() {
1059 List<String> list = null;
1060 synchronized (orderedResourceUriMap) {
1061 Set<String> typeSet = orderedResourceUriMap.keySet();
1062 List<String> typeList = Utility.convertSetToList(typeSet);
1063 if (null == typeList || typeList.size() < 1) {
1066 list = new ArrayList<String>();
1069 Collections.sort(typeList);
1071 // Add all URIs to the output list
1072 Iterator<String> typeItr = typeList.iterator();
1073 while (typeItr.hasNext()) {
1074 List<String> l = orderedResourceUriMap.get(typeItr.next());
1083 public void resourceSelectionChanged(final String selectedItem) {
1087 // Check whether the item selected is a resource or resource
1089 if (isTypeExist(selectedItem)) {
1090 // Given item is a resource Type
1091 setCurrentResourceInSelection(null);
1093 // Given item is a resource URI
1094 SimulatorResource resource = getSimulatorResourceByURI(selectedItem);
1095 if (null != resource) {
1096 setCurrentResourceInSelection(resource);
1098 setCurrentResourceInSelection(null);
1101 // Notify all observers for resource selection change event
1102 resourceSelectionChangedUINotification();
1107 public List<MetaProperty> getMetaProperties(SimulatorResource resource) {
1108 if (null != resource) {
1112 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1114 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1115 propName = Constants.META_PROPERTIES[index];
1116 if (propName.equals(Constants.RESOURCE_URI)) {
1117 propValue = resource.getResourceURI();
1118 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1119 propValue = resource.getResourceType();
1120 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
1121 // TODO: Temporarily ignoring till the implementation.
1126 if (null != propValue) {
1127 metaPropertyList.add(new MetaProperty(propName, propValue));
1131 return metaPropertyList;
1136 public List<LocalResourceAttribute> getAttributes(SimulatorResource resource) {
1137 List<LocalResourceAttribute> attList = null;
1138 if (null != resource) {
1139 Map<String, LocalResourceAttribute> attMap = resource
1140 .getResourceAttributesMap();
1141 if (null != attMap && attMap.size() > 0) {
1142 attList = new ArrayList<LocalResourceAttribute>();
1143 Set<String> attNameSet = attMap.keySet();
1145 LocalResourceAttribute attribute;
1146 Iterator<String> attNameItr = attNameSet.iterator();
1147 while (attNameItr.hasNext()) {
1148 attName = attNameItr.next();
1149 attribute = attMap.get(attName);
1150 if (null != attribute) {
1151 attList.add(attribute);
1159 public void attributeValueUpdated(SimulatorResource resource,
1160 String attributeName, String value) {
1161 if (null != resource && null != attributeName && null != value) {
1162 SimulatorResourceServer server = resource.getResourceServer();
1163 if (null != server) {
1164 LocalResourceAttribute att = resource
1165 .getAttribute(attributeName);
1169 Type baseType = att.getAttValBaseType();
1171 if (baseType == Type.STRING) {
1172 server.updateAttributeString(attributeName, value);
1173 } else if (baseType == Type.INT) {
1176 val = Integer.parseInt(value);
1177 } catch (NumberFormatException nfe) {
1180 server.updateAttributeInteger(attributeName, val);
1181 } else if (baseType == Type.DOUBLE) {
1184 val = Double.parseDouble(value);
1185 } catch (NumberFormatException nfe) {
1188 server.updateAttributeDouble(attributeName, val);
1189 } else if (baseType == Type.BOOL) {
1191 val = Boolean.parseBoolean(value);
1192 server.updateAttributeBoolean(attributeName, val);
1194 } catch (SimulatorException e) {
1198 .log(Level.ERROR.ordinal(),
1200 "[" + e.getClass().getSimpleName() + "]"
1201 + e.code().toString() + "-"
1208 private ModelChangeNotificationType compareAndUpdateLocalAttributes(
1209 Map<String, LocalResourceAttribute> resourceAttributeMapOld,
1210 Map<String, LocalResourceAttribute> resourceAttributeMapNew,
1211 Set<LocalResourceAttribute> valueChangeSet) {
1212 ModelChangeNotificationType notificationType = ModelChangeNotificationType.NONE;
1213 if (null != resourceAttributeMapOld && null != resourceAttributeMapNew) {
1214 Set<String> oldMapKeySet = resourceAttributeMapOld.keySet();
1215 Iterator<String> attributeMapOldItr = oldMapKeySet.iterator();
1217 LocalResourceAttribute attributeOld;
1218 LocalResourceAttribute attributeNew;
1223 while (attributeMapOldItr.hasNext()) {
1224 attName = attributeMapOldItr.next();
1225 if (resourceAttributeMapNew.containsKey(attName)) {
1226 attributeOld = resourceAttributeMapOld.get(attName);
1227 attributeNew = resourceAttributeMapNew.get(attName);
1228 // Copy the attribute value from new to old if the value
1230 // Comparing only the attribute's value considering the
1231 // fact that only the value can be changed
1232 if (null != attributeOld && null != attributeNew) {
1233 attValueOld = attributeOld.getAttributeValue();
1234 attValueNew = attributeNew.getAttributeValue();
1236 oldValueStr = String.valueOf(attValueOld);
1237 newValueStr = String.valueOf(attValueNew);
1239 if (null != oldValueStr && null != newValueStr) {
1240 if (!oldValueStr.equals(newValueStr)) {
1241 attributeOld.setAttributeValue(attValueNew);
1242 notificationType = ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED;
1243 valueChangeSet.add(attributeOld);
1247 resourceAttributeMapNew.remove(attName);
1249 // Attribute doesn't exist in the new model. Hence
1250 // removing it from the model.
1251 resourceAttributeMapOld.remove(attName);
1252 notificationType = ModelChangeNotificationType.ATTRIBUTE_REMOVED;
1255 // Check for new attributes in the new model
1256 if (resourceAttributeMapNew.size() > 0) {
1257 Set<String> remainingAttSet = resourceAttributeMapNew.keySet();
1258 Iterator<String> remainingAttItr = remainingAttSet.iterator();
1259 LocalResourceAttribute attribute;
1260 while (remainingAttItr.hasNext()) {
1261 attName = remainingAttItr.next();
1262 if (null != attName) {
1263 attribute = resourceAttributeMapNew.get(attName);
1264 if (null != attribute) {
1265 resourceAttributeMapOld.put(attName, attribute);
1269 notificationType = ModelChangeNotificationType.ATTRIBUTE_ADDED;
1272 return notificationType;
1275 public int startAutomation(SimulatorResource resource,
1276 LocalResourceAttribute attribute, AutomationType autoType,
1277 int autoUpdateInterval) {
1279 if (null != resource && null != attribute) {
1280 SimulatorResourceServer resourceServerN = resource
1281 .getResourceServer();
1282 if (null != resourceServerN) {
1283 String attrName = attribute.getAttributeName();
1285 autoId = resourceServerN.startAttributeAutomation(attrName,
1286 autoType, automationListener);
1287 } catch (SimulatorException e) {
1291 .log(Level.ERROR.ordinal(),
1293 "[" + e.getClass().getSimpleName() + "]"
1294 + e.code().toString() + "-"
1299 attribute.setAutomationId(autoId);
1300 attribute.setAutomationType(autoType);
1301 attribute.setAutomationUpdateInterval(autoUpdateInterval);
1302 attribute.setAutomationInProgress(true);
1303 resource.setAttributeAutomationInProgress(true);
1305 attribute.setAutomationInProgress(false);
1312 public void stopAutomation(SimulatorResource resource,
1313 LocalResourceAttribute att, int autoId) {
1314 if (null != resource) {
1315 SimulatorResourceServer resourceServerN = resource
1316 .getResourceServer();
1317 if (null != resourceServerN) {
1319 resourceServerN.stopAutomation(autoId);
1320 } catch (SimulatorException e) {
1324 .log(Level.ERROR.ordinal(),
1326 "[" + e.getClass().getSimpleName() + "]"
1327 + e.code().toString() + "-"
1331 // Change the automation status
1332 att.setAutomationInProgress(false);
1333 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1338 private boolean isAnyAttributeInAutomation(SimulatorResource resource) {
1339 if (null == resource) {
1342 Map<String, LocalResourceAttribute> attMap = resource
1343 .getResourceAttributesMap();
1344 if (null == attMap) {
1347 boolean status = false;
1348 Set<String> keySet = attMap.keySet();
1349 Iterator<String> attItr = keySet.iterator();
1350 while (attItr.hasNext()) {
1351 LocalResourceAttribute attribute = attMap.get(attItr.next());
1352 if (attribute.isAutomationInProgress()) {
1360 private LocalResourceAttribute getAttributeWithGivenAutomationId(
1361 SimulatorResource resource, int automationId) {
1362 LocalResourceAttribute targetAttribute = null;
1363 if (null != resource) {
1364 Map<String, LocalResourceAttribute> attributeMap = resource
1365 .getResourceAttributesMap();
1366 if (null != attributeMap) {
1367 Set<String> attNameSet = attributeMap.keySet();
1368 Iterator<String> attNameItr = attNameSet.iterator();
1370 LocalResourceAttribute attribute;
1371 while (attNameItr.hasNext()) {
1372 attName = attNameItr.next();
1373 if (null != attName) {
1374 attribute = attributeMap.get(attName);
1375 if (null != attribute) {
1376 if (attribute.isAutomationInProgress()
1377 && (attribute.getAutomationId() == automationId)) {
1378 targetAttribute = attribute;
1386 return targetAttribute;
1389 public boolean startResourceAutomationUIRequest(final String resourceURI) {
1390 if (null == resourceURI) {
1393 boolean status = false;
1394 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1395 if (null != resource) {
1396 changeResourceLevelAutomationStatus(resource, true);
1398 // Invoke the native automation method
1399 SimulatorResourceServer resourceServer = resource
1400 .getResourceServer();
1401 if (null != resourceServer) {
1402 // TODO: Temporarily handling the normal one-time automation for
1406 autoId = resourceServer.startResourceAutomation(
1407 AutomationType.NORMAL, automationListener);
1408 } catch (SimulatorException e) {
1412 .log(Level.ERROR.ordinal(),
1414 "[" + e.getClass().getSimpleName() + "]"
1415 + e.code().toString() + "-"
1420 // Automation request failed and hence status is being
1422 changeResourceLevelAutomationStatus(resource, false);
1424 // Automation request accepted.
1425 resource.setAutomationId(autoId);
1427 // Notify the UI listeners in a different thread.
1428 Thread notifyThread = new Thread() {
1430 resourceAutomationStartedUINotification(resourceURI);
1433 notifyThread.setPriority(Thread.MAX_PRIORITY);
1434 notifyThread.start();
1443 public boolean stopResourceAutomationUIRequest(final String resourceURI) {
1444 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1445 if (null == resource) {
1448 final int autoId = resource.getAutomationId();
1452 SimulatorResourceServer resourceServer = resource.getResourceServer();
1453 if (null == resourceServer) {
1456 // Call native method
1458 resourceServer.stopAutomation(autoId);
1459 } catch (SimulatorException e) {
1463 .log(Level.ERROR.ordinal(),
1465 "[" + e.getClass().getSimpleName() + "]"
1466 + e.code().toString() + "-" + e.message());
1470 // Notify the UI Listeners. Invoke the automation complete callback.
1471 Thread stopThread = new Thread() {
1473 automationListener.onAutomationComplete(resourceURI, autoId);
1480 // Changes the automation state of the resource and its attributes
1481 private void changeResourceLevelAutomationStatus(
1482 SimulatorResource resource, boolean status) {
1484 Map<String, LocalResourceAttribute> attributeMap = resource
1485 .getResourceAttributesMap();
1486 if (null != attributeMap) {
1487 Set<String> attrNameSet = attributeMap.keySet();
1488 Iterator<String> attrNameItr = attrNameSet.iterator();
1490 LocalResourceAttribute attribute;
1491 while (attrNameItr.hasNext()) {
1492 attrName = attrNameItr.next();
1493 attribute = attributeMap.get(attrName);
1494 if (null != attribute) {
1495 attribute.setAutomationInProgress(status);
1499 resource.setResourceAutomationInProgress(status);
1502 public boolean isResourceAutomationStarted(String resourceURI) {
1503 boolean status = false;
1504 if (null == resourceURI) {
1508 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1509 if (null != resource) {
1510 status = resource.isResourceAutomationInProgress();
1515 public boolean isAttributeAutomationStarted(String resourceURI) {
1516 boolean status = false;
1517 if (null == resourceURI) {
1520 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1521 if (null != resource) {
1522 status = resource.isAttributeAutomationInProgress();
1527 public LocalResourceAttribute getAttributeByResourceURI(String uri,
1529 if (null == uri || null == attName) {
1532 SimulatorResource resource = getSimulatorResourceByURI(uri);
1533 if (null == resource) {
1536 Map<String, LocalResourceAttribute> attMap = resource
1537 .getResourceAttributesMap();
1538 if (null == attMap) {
1541 return attMap.get(attName);
1544 public void notifyObserverRequest(SimulatorResource res, int observerId) {
1548 SimulatorResourceServer server = res.getResourceServer();
1549 if (null == server) {
1553 server.notifyObserver(observerId);
1554 } catch (SimulatorException e) {
1558 .log(Level.ERROR.ordinal(),
1560 "[" + e.getClass().getSimpleName() + "]"
1561 + e.code().toString() + "-" + e.message());
1565 public Image getImage(String resourceURI) {
1566 if (null == resourceURI) {
1569 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1570 if (null == resource) {
1573 return Activator.getDefault().getImageRegistry()
1574 .get(resource.getResourceType());
1577 public void shutdown() {
1578 threadHandle.interrupt();