2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package oic.simulator.serviceprovider.manager;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
27 import java.util.Vector;
29 import oic.simulator.serviceprovider.Activator;
30 import oic.simulator.serviceprovider.model.AttributeElement;
31 import oic.simulator.serviceprovider.model.MetaProperty;
32 import oic.simulator.serviceprovider.model.Resource;
33 import oic.simulator.serviceprovider.model.ResourceType;
34 import oic.simulator.serviceprovider.model.SingleResource;
35 import oic.simulator.serviceprovider.utils.Constants;
36 import oic.simulator.serviceprovider.utils.Utility;
38 import org.eclipse.swt.widgets.Display;
39 import org.oic.simulator.AttributeProperty;
40 import org.oic.simulator.AttributeProperty.Type;
41 import org.oic.simulator.AttributeValue;
42 import org.oic.simulator.AttributeValue.TypeInfo;
43 import org.oic.simulator.AttributeValue.ValueType;
44 import org.oic.simulator.DeviceInfo;
45 import org.oic.simulator.DeviceListener;
46 import org.oic.simulator.ILogger.Level;
47 import org.oic.simulator.PlatformInfo;
48 import org.oic.simulator.SimulatorException;
49 import org.oic.simulator.SimulatorManager;
50 import org.oic.simulator.SimulatorResourceAttribute;
51 import org.oic.simulator.SimulatorResourceModel;
52 import org.oic.simulator.server.Observer;
53 import org.oic.simulator.server.SimulatorResource;
54 import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
55 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
56 import org.oic.simulator.server.SimulatorResource.ObserverListener;
57 import org.oic.simulator.server.SimulatorResource.ResourceModelChangeListener;
58 import org.oic.simulator.server.SimulatorSingleResource;
61 * This class acts as an interface between the simulator java SDK and the
62 * various UI modules. It maintains all the details of resources and provides
63 * other UI modules with the information required. It also handles model change,
64 * automation, and observer related events from native layer and propagates
65 * those events to the registered UI listeners.
67 public class ResourceManager {
71 private Resource currentResourceInSelection;
73 private ResourceModelChangeListener resourceModelChangeListener;
75 private AutoUpdateListener automationListener;
77 private ObserverListener observer;
79 private DeviceListener deviceListener;
81 private NotificationSynchronizerThread synchronizerThread;
83 private Thread threadHandle;
85 private DeviceInfo deviceInfo;
86 private PlatformInfo platformInfo;
88 private String deviceName;
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 deviceListener = new DeviceListener() {
134 public void onDeviceFound(final String host,
135 final DeviceInfo deviceInfo) {
136 if (null != ResourceManager.this.deviceInfo
137 || null == deviceInfo || null == host) {
140 synchronizerThread.addToQueue(new Runnable() {
143 String rcvdDeviceName = deviceInfo.getName();
144 if (null == rcvdDeviceName) {
147 if (deviceName.equalsIgnoreCase(rcvdDeviceName)) {
148 ResourceManager.this.deviceInfo = deviceInfo;
150 // Notify the UI Listeners
151 UiListenerHandler.getInstance()
152 .deviceInfoReceivedNotification();
159 // Get the device information to show other details of the device in UI.
161 SimulatorManager.findDevices("", deviceListener);
162 } catch (SimulatorException e) {
166 .log(Level.ERROR.ordinal(),
168 "Failed to get the local device information.\n"
169 + Utility.getSimulatorErrorString(e, null));
172 resourceModelChangeListener = new ResourceModelChangeListener() {
175 public void onResourceModelChanged(final String resourceURI,
176 final SimulatorResourceModel resourceModelN) {
177 synchronizerThread.addToQueue(new Runnable() {
181 if (null == resourceURI || null == resourceModelN) {
185 Display.getDefault().asyncExec(new Runnable() {
188 Resource resource = data
189 .getResourceByURI(resourceURI);
190 if (null != resource) {
192 resource.setResourceRepresentation(resourceModelN);
193 } catch (NumberFormatException e) {
197 .log(Level.ERROR.ordinal(),
199 "Error while trying to update the attributes.\n"
201 .getSimulatorErrorString(
213 automationListener = new AutoUpdateListener() {
216 public void onUpdateComplete(final String resourceURI,
217 final int automationId) {
218 synchronizerThread.addToQueue(new Runnable() {
222 SingleResource resource = data
223 .getSingleResourceByURI(resourceURI);
224 if (null == resource) {
227 // Checking whether this notification is for an
228 // attribute or a resource
229 if (resource.isResourceAutomationInProgress()) {
230 changeResourceLevelAutomationStatus(resource, false);
231 // Notify the UI listeners
232 UiListenerHandler.getInstance()
233 .automationCompleteUINotification(resource,
235 } else if (resource.isAttributeAutomationInProgress()) {
236 // Find the attribute with the given automation id
237 final AttributeElement attribute = getAttributeWithGivenAutomationId(
238 resource, automationId);
239 if (null != attribute) {
240 attribute.setAutoUpdateState(false);
241 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
243 // Setting the attribute automation status to
245 resource.setAttributeAutomationInProgress(false);
253 observer = new ObserverListener() {
255 public void onObserverChanged(final String resourceURI,
256 final int status, final Observer observer) {
260 if (null == resourceURI || null == observer) {
263 Resource resource = data.getResourceByURI(resourceURI);
264 if (null == resource) {
267 // Update the observers information
269 resource.addObserverInfo(observer);
271 resource.removeObserverInfo(observer);
273 // Notify the UI listeners
274 UiListenerHandler.getInstance()
275 .observerListChangedUINotification(resource);
281 public void onObserverAdded(String resourceURI, Observer observer) {
282 onObserverChanged(resourceURI, 0, observer);
286 public void onObserverRemoved(String resourceURI, Observer observer) {
287 onObserverChanged(resourceURI, 1, observer);
291 synchronizerThread = new NotificationSynchronizerThread();
292 threadHandle = new Thread(synchronizerThread);
293 threadHandle.setName("Simulator service provider event queue");
294 threadHandle.start();
297 private static class NotificationSynchronizerThread implements Runnable {
299 LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
303 while (!Thread.interrupted()) {
304 synchronized (this) {
306 while (notificationQueue.isEmpty()) {
310 } catch (InterruptedException e) {
316 synchronized (this) {
317 thread = notificationQueue.pop();
321 } catch (Exception e) {
322 if (e instanceof InterruptedException) {
330 public void addToQueue(Runnable event) {
331 synchronized (this) {
332 notificationQueue.add(event);
338 public void setDeviceInfo(List<MetaProperty> metaProperties) {
339 if (null == metaProperties || metaProperties.size() < 1) {
342 Iterator<MetaProperty> itr = metaProperties.iterator();
346 boolean found = false;
347 while (itr.hasNext()) {
349 propName = prop.getPropName();
350 propValue = prop.getPropValue();
351 if (propName.equals(Constants.DEVICE_NAME)) {
352 this.deviceName = propValue;
363 SimulatorManager.setDeviceInfo(deviceName);
364 } catch (SimulatorException e) {
368 .log(Level.ERROR.ordinal(),
370 "Error while registering the device info.\n"
371 + Utility.getSimulatorErrorString(e, null));
375 public boolean isDeviceInfoValid(List<MetaProperty> metaProperties) {
376 if (null == metaProperties || metaProperties.size() < 1) {
380 Iterator<MetaProperty> itr = metaProperties.iterator();
384 while (itr.hasNext()) {
386 propName = prop.getPropName();
387 propValue = prop.getPropValue();
388 if (propName.equals(Constants.DEVICE_NAME)) {
389 if (null == propValue || propValue.length() < 1) {
398 public List<MetaProperty> getDeviceInfo() {
399 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
400 metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, deviceName));
401 if (null != deviceInfo) {
402 metaProperties.add(new MetaProperty(Constants.DEVICE_ID, deviceInfo
404 metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
405 deviceInfo.getSpecVersion()));
406 metaProperties.add(new MetaProperty(Constants.DEVICE_DMV,
407 deviceInfo.getDataModelVersion()));
409 return metaProperties;
412 public List<MetaProperty> getPlatformInfo() {
413 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
414 metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
416 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
417 platformInfo.getManufacturerName()));
418 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
419 platformInfo.getManufacturerUrl()));
420 metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
421 platformInfo.getModelNumber()));
422 metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
423 platformInfo.getDateOfManufacture()));
424 metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
425 platformInfo.getPlatformVersion()));
426 metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
427 platformInfo.getOperationSystemVersion()));
428 metaProperties.add(new MetaProperty(
429 Constants.PLATFORM_HARDWARE_VERSION, platformInfo
430 .getHardwareVersion()));
431 metaProperties.add(new MetaProperty(
432 Constants.PLATFORM_FIRMWARE_VERSION, platformInfo
433 .getFirmwareVersion()));
434 metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
435 platformInfo.getSupportUrl()));
436 metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
437 platformInfo.getSystemTime()));
438 return metaProperties;
441 public void setPlatformInfo(List<MetaProperty> metaProperties) {
442 if (null == metaProperties || metaProperties.size() < 1) {
445 Iterator<MetaProperty> itr = metaProperties.iterator();
449 while (itr.hasNext()) {
451 propName = prop.getPropName();
452 propValue = prop.getPropValue();
453 if (propName.equals(Constants.PLATFORM_ID)) {
454 platformInfo.setPlatformID(propValue);
455 } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) {
456 platformInfo.setManufacturerName(propValue);
457 } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) {
458 platformInfo.setManufacturerUrl(propValue);
459 } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) {
460 platformInfo.setModelNumber(propValue);
461 } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) {
462 platformInfo.setDateOfManufacture(propValue);
463 } else if (propName.equals(Constants.PLATFORM_VERSION)) {
464 platformInfo.setPlatformVersion(propValue);
465 } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) {
466 platformInfo.setOperationSystemVersion(propValue);
467 } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) {
468 platformInfo.setHardwareVersion(propValue);
469 } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) {
470 platformInfo.setFirmwareVersion(propValue);
471 } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) {
472 platformInfo.setSupportUrl(propValue);
473 } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) {
474 platformInfo.setSystemTime(propValue);
478 SimulatorManager.setPlatformInfo(platformInfo);
479 } catch (SimulatorException e) {
483 .log(Level.ERROR.ordinal(),
485 "Error while registering the platform info.\n"
486 + Utility.getSimulatorErrorString(e, null));
490 public boolean isPlatformInfoValid(List<MetaProperty> metaProperties) {
491 if (null == metaProperties || metaProperties.size() < 1) {
494 Iterator<MetaProperty> itr = metaProperties.iterator();
497 while (itr.hasNext()) {
499 propValue = prop.getPropValue();
500 if (null == propValue || propValue.length() < 1) {
507 public synchronized Resource getCurrentResourceInSelection() {
508 return currentResourceInSelection;
511 public synchronized void setCurrentResourceInSelection(Resource resource) {
512 this.currentResourceInSelection = resource;
515 public boolean isResourceExist(String resourceURI) {
516 return data.isResourceExist(resourceURI);
519 public boolean isAnyResourceExist() {
520 return data.isAnyResourceExist();
523 public boolean createSingleResource(SingleResource resource,
524 Map<String, SimulatorResourceAttribute> attributes)
525 throws SimulatorException {
526 if (null == resource) {
531 // Create the resource.
532 SimulatorResource jSimulatorResource = SimulatorManager
533 .createResource(SimulatorResource.Type.SINGLE,
534 resource.getResourceName(),
535 resource.getResourceURI(),
536 resource.getResourceType());
537 if (null == jSimulatorResource
538 || !(jSimulatorResource instanceof SimulatorSingleResource)) {
541 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
542 resource.setSimulatorResource(jSimulatorSingleResource);
544 // Cancel observable property if requested by user.
545 if (!resource.isObservable()) {
546 jSimulatorSingleResource.setObservable(false);
549 // Set the model change listener.
550 jSimulatorSingleResource
551 .setResourceModelChangeListener(resourceModelChangeListener);
553 // Set the observer listener if the resource is observable.
554 if (resource.isObservable()) {
555 jSimulatorSingleResource.setObserverListener(observer);
559 if (null != attributes && !attributes.isEmpty()) {
560 SimulatorResourceAttribute value;
561 for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
563 value = entry.getValue();
565 jSimulatorSingleResource.addAttribute(value);
568 // Get the resource model java object reference.
569 resource.setResourceModel(jSimulatorSingleResource
570 .getResourceModel());
572 resource.setResourceRepresentation(resource.getResourceModel());
575 // Register the resource with the platform.
576 jSimulatorSingleResource.start();
577 resource.setStarted(true);
579 // Get the resource interfaces
580 resource.setResourceInterfaces(Utility
581 .convertVectorToSet(jSimulatorSingleResource.getInterface()));
582 } catch (SimulatorException e) {
586 .log(Level.ERROR.ordinal(), new Date(),
587 Utility.getSimulatorErrorString(e, null));
591 // Add to local cache.
592 data.addResource(resource);
594 // Update UI listeners
595 UiListenerHandler.getInstance().resourceCreatedUINotification(
596 ResourceType.SINGLE);
601 public Resource createResourceByRAML(String configFilePath)
602 throws SimulatorException {
603 Resource resource = null;
605 // Create the resource
606 SimulatorResource jSimulatorResource = SimulatorManager
607 .createResource(configFilePath);
608 if (null == jSimulatorResource) {
611 if (jSimulatorResource instanceof SimulatorSingleResource) {
612 resource = new SingleResource();
616 resource.setSimulatorResource(jSimulatorResource);
618 // Fetch and locally store the resource name and uri.
619 String uri = jSimulatorResource.getURI();
620 if (null == uri || uri.trim().isEmpty()) {
623 resource.setResourceURI(uri.trim());
625 String name = jSimulatorResource.getName();
626 if (null == name || name.trim().isEmpty()) {
629 resource.setResourceName(name.trim());
630 } catch (SimulatorException e) {
634 .log(Level.ERROR.ordinal(), new Date(),
635 Utility.getSimulatorErrorString(e, null));
642 * This method can set/change the resource uri and name of an already
643 * created resource which is not yet registered with the platform. This
644 * method registers the model change and observer listeners, registers the
645 * resource, fetches the resource attributes, updates the local cache and
646 * notifies the UI listeners.
648 public boolean completeSingleResourceCreationByRAML(Resource resource,
649 String uri, String name, boolean multiInstance)
650 throws SimulatorException {
651 if (null == resource || !(resource instanceof SingleResource)) {
655 SingleResource singleRes = (SingleResource) resource;
657 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
658 .getSimulatorResource();
659 if (null == jSimulatorSingleResource) {
663 // Update resource URI and Name if they are changed.
664 String newUri = uri.trim();
665 String newName = name.trim();
668 singleRes.setResourceURI(newUri);
669 singleRes.setResourceName(newName);
671 if (!singleRes.getResourceURI().equals(newUri)) {
672 jSimulatorSingleResource.setURI(newUri);
673 singleRes.setResourceURI(newUri);
675 if (!singleRes.getResourceName().equals(newName)) {
676 jSimulatorSingleResource.setName(newName);
677 singleRes.setResourceName(newName);
681 // Set the model change listener.
682 jSimulatorSingleResource
683 .setResourceModelChangeListener(resourceModelChangeListener);
685 // Set the observer listener if the resource is observable.
686 if (jSimulatorSingleResource.isObservable()) {
687 jSimulatorSingleResource.setObserverListener(observer);
688 singleRes.setObservable(true);
691 // Fetch the resource model.
692 SimulatorResourceModel jResModel = jSimulatorSingleResource
694 if (null == jResModel) {
697 singleRes.setResourceModel(jResModel);
699 // Fetch the basic details of the resource.
700 singleRes.setResourceType(jSimulatorSingleResource
703 .setResourceInterfaces(Utility
704 .convertVectorToSet(jSimulatorSingleResource
707 // Fetch the resource attributes.
708 singleRes.setResourceRepresentation(jResModel);
710 // Register the resource with the platform.
711 jSimulatorSingleResource.start();
712 singleRes.setStarted(true);
714 // Add to local cache.
715 data.addResource(singleRes);
717 // Update UI listeners for single instance creation
719 UiListenerHandler.getInstance().resourceCreatedUINotification(
720 ResourceType.SINGLE);
721 } catch (Exception e) {
725 .log(Level.ERROR.ordinal(), new Date(),
726 Utility.getSimulatorErrorString(e, null));
732 public int createSingleResourceMultiInstances(String configFile, int count)
733 throws SimulatorException {
736 Vector<SimulatorResource> jSimulatorResources = SimulatorManager
737 .createResource(configFile, count);
738 if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
741 SimulatorSingleResource jResource;
742 SingleResource resource;
744 for (SimulatorResource jSimulatorResource : jSimulatorResources) {
745 jResource = (SimulatorSingleResource) jSimulatorResource;
746 resource = new SingleResource();
747 resource.setSimulatorResource(jResource);
749 result = completeSingleResourceCreationByRAML(resource,
750 jResource.getURI(), jResource.getName(), true);
754 } catch (SimulatorException eInner) {
758 .log(Level.ERROR.ordinal(),
760 Utility.getSimulatorErrorString(eInner,
764 if (createCount > 0) {
765 UiListenerHandler.getInstance().resourceCreatedUINotification(
766 ResourceType.SINGLE);
768 } catch (SimulatorException eOuter) {
772 .log(Level.ERROR.ordinal(), new Date(),
773 Utility.getSimulatorErrorString(eOuter, null));
779 public List<Resource> getResourceList() {
780 List<Resource> resourceList = data.getResources();
781 if (null == resourceList) {
785 Collections.sort(resourceList, Utility.resourceComparator);
790 public List<SingleResource> getSingleResourceList() {
791 List<SingleResource> resourceList = data.getSingleResources();
792 if (null == resourceList) {
796 Collections.sort(resourceList, Utility.singleResourceComparator);
801 public void removeSingleResources(Set<SingleResource> resources)
802 throws SimulatorException {
803 if (null == resources) {
806 Iterator<SingleResource> itr = resources.iterator();
807 while (itr.hasNext()) {
808 removeResource(itr.next());
812 public void removeResource(Resource res) throws SimulatorException {
813 // Unregister the resource from the platform.
814 SimulatorResource simRes = res.getSimulatorResource();
817 } catch (SimulatorException e) {
821 .log(Level.ERROR.ordinal(), new Date(),
822 Utility.getSimulatorErrorString(e, null));
826 // Delete this resource
827 data.deleteResource(res);
830 public boolean isUriUnique(List<MetaProperty> properties) {
831 if (null == properties) {
835 Iterator<MetaProperty> itr = properties.iterator();
836 while (itr.hasNext()) {
838 if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
839 String uri = prop.getPropValue();
840 return !data.isResourceExist(uri);
846 public void resourceSelectionChanged(final Resource selectedResource) {
850 if (null != selectedResource) {
851 setCurrentResourceInSelection(selectedResource);
853 setCurrentResourceInSelection(null);
855 // Notify all observers for resource selection change event
856 UiListenerHandler.getInstance()
857 .resourceSelectionChangedUINotification(
863 public List<MetaProperty> getMetaProperties(Resource resource) {
864 if (null != resource) {
868 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
870 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
871 propName = Constants.META_PROPERTIES[index];
872 if (propName.equals(Constants.RESOURCE_NAME)) {
873 propValue = resource.getResourceName();
874 } else if (propName.equals(Constants.RESOURCE_URI)) {
875 propValue = resource.getResourceURI();
876 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
877 propValue = resource.getResourceType();
881 if (null != propValue) {
882 metaPropertyList.add(new MetaProperty(propName, propValue));
885 return metaPropertyList;
890 public boolean startResource(Resource resource) throws SimulatorException {
891 if (null == resource) {
894 SimulatorResource server = resource.getSimulatorResource();
895 if (null == server) {
900 resource.setStarted(true);
901 } catch (SimulatorException e) {
905 .log(Level.ERROR.ordinal(),
907 "There is an error while starting the resource.\n"
908 + Utility.getSimulatorErrorString(e, null));
914 public boolean stopResource(Resource resource) throws SimulatorException {
915 if (null == resource) {
918 SimulatorResource server = resource.getSimulatorResource();
919 if (null == server) {
924 resource.setStarted(false);
925 } catch (SimulatorException e) {
929 .log(Level.ERROR.ordinal(),
931 "There is an error while stopping the resource.\n"
932 + Utility.getSimulatorErrorString(e, null));
938 public boolean changeResourceName(Resource resource, String newName)
939 throws SimulatorException {
940 if (null == resource || null == newName) {
944 if (!stopResource(resource)) {
948 SimulatorResource server = resource.getSimulatorResource();
950 server.setName(newName);
951 resource.setResourceName(newName);
952 } catch (SimulatorException e) {
956 .log(Level.ERROR.ordinal(),
958 "There is an error while changing the resource name.\n"
959 + Utility.getSimulatorErrorString(e, null));
963 if (!startResource(resource)) {
970 public boolean changeResourceURI(Resource resource, String newURI)
971 throws SimulatorException {
972 if (null == resource || null == newURI) {
976 if (!stopResource(resource)) {
980 String curURI = resource.getResourceURI();
981 setResourceURI(resource, newURI);
984 if (!startResource(resource)) {
987 } catch (SimulatorException e) {
988 setResourceURI(resource, curURI);
994 public void setResourceURI(Resource resource, String newURI)
995 throws SimulatorException {
996 String curURI = resource.getResourceURI();
997 SimulatorResource server = resource.getSimulatorResource();
999 server.setURI(newURI);
1000 data.changeResourceURI(resource, curURI, newURI);
1001 } catch (SimulatorException e) {
1005 .log(Level.ERROR.ordinal(),
1007 "There is an error while changing the resource URI.\n"
1008 + Utility.getSimulatorErrorString(e, null));
1013 public boolean updateResourceProperties(Resource resource,
1014 List<MetaProperty> properties, boolean uriChanged,
1015 boolean nameChanged) throws SimulatorException {
1016 if (null == resource || null == properties) {
1020 // Updating the properties
1021 Iterator<MetaProperty> itr = properties.iterator();
1022 MetaProperty property;
1025 String resName = null;
1026 String resURI = null;
1027 while (itr.hasNext()) {
1028 property = itr.next();
1029 if (null == property) {
1032 propName = property.getPropName();
1033 propValue = property.getPropValue();
1034 if (propName.equals(Constants.RESOURCE_NAME)) {
1035 resName = propValue;
1036 } else if (propName.equals(Constants.RESOURCE_URI)) {
1042 if (!changeResourceName(resource, resName)) {
1046 // Notify UI Listeners
1047 UiListenerHandler.getInstance().propertiesChangedUINotification(
1052 if (!changeResourceURI(resource, resURI)) {
1060 public boolean attributeValueUpdated(SingleResource resource,
1061 String attributeName, AttributeValue value) {
1062 if (null != resource && null != attributeName && null != value) {
1063 SimulatorSingleResource simRes = (SimulatorSingleResource) resource
1064 .getSimulatorResource();
1065 if (null != simRes) {
1067 simRes.updateAttribute(attributeName, value);
1069 } catch (SimulatorException e) {
1073 .log(Level.ERROR.ordinal(), new Date(),
1074 Utility.getSimulatorErrorString(e, null));
1081 public boolean isResourceStarted(Resource resource) {
1082 if (null == resource) {
1085 return resource.isStarted();
1088 public boolean isPropertyValueInvalid(Resource resource,
1089 List<MetaProperty> properties, String propName) {
1090 if (null == resource || null == properties || null == propName) {
1093 boolean invalid = false;
1095 Iterator<MetaProperty> itr = properties.iterator();
1096 while (itr.hasNext()) {
1098 if (prop.getPropName().equals(propName)) {
1099 String value = prop.getPropValue();
1100 if (propName.equals(Constants.RESOURCE_URI)) {
1101 if (!Utility.isUriValid(value)) {
1105 if (null == value || value.trim().isEmpty()) {
1114 public boolean isPropValueChanged(Resource resource,
1115 List<MetaProperty> properties, String propName) {
1116 if (null == resource || null == properties || null == propName) {
1119 boolean changed = false;
1122 Iterator<MetaProperty> itr = properties.iterator();
1123 while (itr.hasNext()) {
1125 if (prop.getPropName().equals(propName)) {
1126 oldValue = getPropertyValueFromResource(resource, propName);
1127 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1136 private String getPropertyValueFromResource(Resource resource,
1138 if (null == resource || null == propName) {
1141 if (propName.equals(Constants.RESOURCE_URI)) {
1142 return resource.getResourceURI();
1143 } else if (propName.equals(Constants.RESOURCE_NAME)) {
1144 return resource.getResourceName();
1145 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1146 return resource.getResourceType();
1152 public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
1156 AttributeProperty prop = att.property();
1160 Type attProp = prop.type();
1161 if (attProp == Type.UNKNOWN) {
1167 public int startAutomation(SingleResource resource,
1168 AttributeElement attribute, AutoUpdateType autoType,
1169 int autoUpdateInterval) {
1171 if (null != resource && null != attribute) {
1172 SimulatorSingleResource server = (SimulatorSingleResource) resource
1173 .getSimulatorResource();
1174 if (null != server) {
1175 String attrName = attribute.getSimulatorResourceAttribute()
1178 autoId = server.startAttributeUpdation(attrName, autoType,
1179 autoUpdateInterval, automationListener);
1180 } catch (SimulatorException e) {
1184 .log(Level.ERROR.ordinal(),
1186 "[" + e.getClass().getSimpleName() + "]"
1187 + e.code().toString() + "-"
1192 attribute.setAutoUpdateId(autoId);
1193 attribute.setAutoUpdateType(autoType);
1194 attribute.setAutoUpdateInterval(autoUpdateInterval);
1195 attribute.setAutoUpdateState(true);
1196 resource.setAttributeAutomationInProgress(true);
1203 public void stopAutomation(SingleResource resource, AttributeElement att,
1205 if (null != resource) {
1206 SimulatorSingleResource server = (SimulatorSingleResource) resource
1207 .getSimulatorResource();
1208 if (null != server) {
1210 server.stopUpdation(autoId);
1211 } catch (SimulatorException e) {
1215 .log(Level.ERROR.ordinal(),
1217 "[" + e.getClass().getSimpleName() + "]"
1218 + e.code().toString() + "-"
1222 // Change the automation status
1223 att.setAutoUpdateState(false);
1224 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1229 public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
1230 int autoUpdateInterval, final SingleResource resource) {
1231 if (null == resource) {
1234 boolean status = false;
1235 changeResourceLevelAutomationStatus(resource, true);
1236 // Invoke the native automation method
1237 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1238 .getSimulatorResource();
1239 if (null != resourceServer) {
1242 autoId = resourceServer.startResourceUpdation(autoType,
1243 autoUpdateInterval, automationListener);
1244 } catch (SimulatorException e) {
1248 .log(Level.ERROR.ordinal(), new Date(),
1249 Utility.getSimulatorErrorString(e, null));
1253 // Automation request failed and hence status is being
1255 changeResourceLevelAutomationStatus(resource, false);
1257 // Automation request accepted.
1258 resource.setAutomationId(autoId);
1260 // Notify the UI listeners in a different thread.
1261 Thread notifyThread = new Thread() {
1263 UiListenerHandler.getInstance()
1264 .resourceAutomationStartedUINotification(
1268 notifyThread.setPriority(Thread.MAX_PRIORITY);
1269 notifyThread.start();
1277 public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
1278 if (null == resource) {
1281 final int autoId = resource.getAutomationId();
1285 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1286 .getSimulatorResource();
1287 if (null == resourceServer) {
1290 // Call native method
1292 resourceServer.stopUpdation(autoId);
1293 } catch (SimulatorException e) {
1297 .log(Level.ERROR.ordinal(), new Date(),
1298 Utility.getSimulatorErrorString(e, null));
1302 // Notify the UI Listeners. Invoke the automation complete callback.
1303 Thread stopThread = new Thread() {
1305 automationListener.onUpdateComplete(resource.getResourceURI(),
1313 private boolean isAnyAttributeInAutomation(SingleResource resource) {
1314 if (null == resource || null == resource.getResourceRepresentation()) {
1318 Map<String, AttributeElement> attributes = resource
1319 .getResourceRepresentation().getAttributes();
1320 if (null == attributes || 0 == attributes.size())
1323 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1324 if (entry.getValue().isAutoUpdateInProgress())
1331 // Changes the automation state of the resource and its attributes
1332 private void changeResourceLevelAutomationStatus(SingleResource resource,
1335 if (null == resource || null == resource.getResourceRepresentation()) {
1339 Map<String, AttributeElement> attributes = resource
1340 .getResourceRepresentation().getAttributes();
1341 if (null == attributes || 0 == attributes.size())
1344 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1345 entry.getValue().setAutoUpdateState(status);
1348 resource.setResourceAutomationInProgress(status);
1351 private AttributeElement getAttributeWithGivenAutomationId(
1352 SingleResource resource, int automationId) {
1353 if (null == resource || null == resource.getResourceRepresentation()) {
1357 Map<String, AttributeElement> attributes = resource
1358 .getResourceRepresentation().getAttributes();
1359 if (null == attributes || 0 == attributes.size())
1362 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1363 if (automationId == entry.getValue().getAutoUpdateId())
1364 return entry.getValue();
1370 public boolean isResourceAutomationStarted(SingleResource resource) {
1371 boolean status = false;
1372 if (null != resource) {
1373 status = resource.isResourceAutomationInProgress();
1378 public boolean isAttributeAutomationStarted(SingleResource resource) {
1379 if (null == resource) {
1382 return resource.isAttributeAutomationInProgress();
1385 public void notifyObserverRequest(Resource resource, int observerId) {
1386 if (null == resource) {
1389 SimulatorResource simulatorResource = resource.getSimulatorResource();
1390 if (null == simulatorResource) {
1394 simulatorResource.notifyObserver(observerId);
1395 } catch (SimulatorException e) {
1399 .log(Level.ERROR.ordinal(), new Date(),
1400 Utility.getSimulatorErrorString(e, null));
1404 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1409 AttributeValue val = att.value();
1414 TypeInfo type = val.typeInfo();
1416 AttributeProperty prop = att.property();
1421 List<String> values = new ArrayList<String>();
1423 Type valuesType = prop.type();
1425 if (valuesType == Type.UNKNOWN) {
1426 // Adding the default value
1427 values.add(Utility.getAttributeValueAsString(val));
1431 if (type.mType != ValueType.RESOURCEMODEL) {
1432 if (type.mType == ValueType.ARRAY) {
1433 if (type.mDepth == 1) {
1434 AttributeProperty childProp = prop.getChildProperty();
1435 if (null != childProp) {
1436 valuesType = childProp.type();
1437 if (valuesType == Type.RANGE) {
1438 List<String> list = getRangeForPrimitiveNonArrayAttributes(
1439 childProp, type.mBaseType);
1441 values.addAll(list);
1443 } else if (valuesType == Type.VALUESET) {
1444 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
1445 childProp.valueSet(), type.mBaseType);
1447 values.addAll(list);
1453 if (valuesType == Type.RANGE) {
1454 List<String> list = getRangeForPrimitiveNonArrayAttributes(
1457 values.addAll(list);
1459 } else if (valuesType == Type.VALUESET) {
1460 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
1461 prop.valueSet(), type.mType);
1463 values.addAll(list);
1472 public List<String> getRangeForPrimitiveNonArrayAttributes(
1473 AttributeProperty prop, ValueType type) {
1478 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
1482 List<String> values = new ArrayList<String>();
1485 int min = (int) prop.min();
1486 int max = (int) prop.max();
1487 for (int iVal = min; iVal <= max; iVal++) {
1488 values.add(String.valueOf(iVal));
1492 double minD = (double) prop.min();
1493 double maxD = (double) prop.max();
1494 for (double iVal = minD; iVal <= maxD; iVal = iVal + 1.0) {
1495 values.add(String.valueOf(iVal));
1503 public List<String> getAllowedValuesForPrimitiveNonArrayAttributes(
1504 AttributeValue[] attValues, ValueType type) {
1505 if (null == attValues || attValues.length < 1) {
1509 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
1514 List<String> values = new ArrayList<String>();
1515 for (AttributeValue val : attValues) {
1523 values.add(String.valueOf(obj));
1528 public int getResourceCount() {
1529 return data.getResourceCount();
1532 public void shutdown() {
1533 threadHandle.interrupt();