X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fsimulator%2Fjava%2Feclipse-plugin%2FServiceProviderPlugin%2Fsrc%2Foic%2Fsimulator%2Fserviceprovider%2Fmanager%2FResourceManager.java;h=ac01be264cb6584d8b6ea685821d3de31d9d07fe;hb=974d43540192dc00421865d09802ff1c8fcf2644;hp=d52a1e2388696528930187f48aa88b102def2751;hpb=7033427e9b2787649d5b3a7769a32520e19b47d2;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java index d52a1e2..ac01be2 100644 --- a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java +++ b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java @@ -16,45 +16,55 @@ package oic.simulator.serviceprovider.manager; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.swt.widgets.Display; + +import java.text.NumberFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; - -import oic.simulator.serviceprovider.Activator; -import oic.simulator.serviceprovider.listener.IAutomationUIListener; -import oic.simulator.serviceprovider.listener.IObserverListChangedUIListener; -import oic.simulator.serviceprovider.listener.IResourceListChangedUIListener; -import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener; -import oic.simulator.serviceprovider.listener.IResourceSelectionChangedUIListener; -import oic.simulator.serviceprovider.resource.LocalResourceAttribute; -import oic.simulator.serviceprovider.resource.MetaProperty; -import oic.simulator.serviceprovider.resource.ModelChangeNotificationType; -import oic.simulator.serviceprovider.resource.SimulatorResource; -import oic.simulator.serviceprovider.resource.StandardConfiguration; -import oic.simulator.serviceprovider.utils.Constants; -import oic.simulator.serviceprovider.utils.Utility; - -import org.eclipse.swt.graphics.Image; -import org.oic.simulator.IAutomation; +import java.util.Vector; + +import org.oic.simulator.ArrayProperty; +import org.oic.simulator.AttributeProperty; +import org.oic.simulator.AttributeProperty.Type; +import org.oic.simulator.AttributeValue; +import org.oic.simulator.AttributeValue.TypeInfo; +import org.oic.simulator.AttributeValue.ValueType; +import org.oic.simulator.BooleanProperty; +import org.oic.simulator.DeviceInfo; +import org.oic.simulator.DeviceListener; +import org.oic.simulator.DoubleProperty; import org.oic.simulator.ILogger.Level; -import org.oic.simulator.ResourceAttribute; -import org.oic.simulator.ResourceAttribute.Range; -import org.oic.simulator.ResourceAttribute.Type; +import org.oic.simulator.IntegerProperty; +import org.oic.simulator.PlatformInfo; import org.oic.simulator.SimulatorException; import org.oic.simulator.SimulatorManager; +import org.oic.simulator.SimulatorResourceAttribute; import org.oic.simulator.SimulatorResourceModel; -import org.oic.simulator.serviceprovider.AutomationType; -import org.oic.simulator.serviceprovider.IObserver; -import org.oic.simulator.serviceprovider.IResourceModelChangedListener; -import org.oic.simulator.serviceprovider.ObserverInfo; -import org.oic.simulator.serviceprovider.SimulatorResourceServer; +import org.oic.simulator.StringProperty; +import org.oic.simulator.server.Observer; +import org.oic.simulator.server.SimulatorResource; +import org.oic.simulator.server.SimulatorResource.AutoUpdateListener; +import org.oic.simulator.server.SimulatorResource.AutoUpdateType; +import org.oic.simulator.server.SimulatorResource.ObserverListener; +import org.oic.simulator.server.SimulatorResource.ResourceModelChangeListener; +import org.oic.simulator.server.SimulatorSingleResource; + +import oic.simulator.serviceprovider.Activator; +import oic.simulator.serviceprovider.model.AttributeElement; +import oic.simulator.serviceprovider.model.MetaProperty; +import oic.simulator.serviceprovider.model.Resource; +import oic.simulator.serviceprovider.model.ResourceType; +import oic.simulator.serviceprovider.model.SingleResource; +import oic.simulator.serviceprovider.utils.Constants; +import oic.simulator.serviceprovider.utils.Utility; /** * This class acts as an interface between the simulator java SDK and the @@ -65,49 +75,59 @@ import org.oic.simulator.serviceprovider.SimulatorResourceServer; */ public class ResourceManager { - private Map> resourceMap; - - private Map> orderedResourceUriMap; - - private Map displayNameMap; + private Data data; - private StandardConfiguration stdConfig; + private Resource currentResourceInSelection; - private SimulatorResource currentResourceInSelection; + private ResourceModelChangeListener resourceModelChangeListener; - private List resourceListChangedUIListeners; + private AutoUpdateListener automationListener; - private List resourceSelectionChangedUIListeners; + private ObserverListener observer; - private List resourceModelChangedUIListeners; + private DeviceListener deviceListener; - private List automationUIListeners; + private NotificationSynchronizerThread synchronizerThread; - private List observerUIListeners; + private Thread threadHandle; - private IResourceModelChangedListener resourceModelChangeListener; + private DeviceInfo deviceInfo; + private PlatformInfo platformInfo; - private IAutomation automationListener; + private String deviceName; - private IObserver observer; - - private NotificationSynchronizerThread synchronizerThread; + public ResourceManager() { + data = new Data(); - private Thread threadHandle; + deviceListener = new DeviceListener() { - public ResourceManager() { - resourceMap = new HashMap>(); - orderedResourceUriMap = new HashMap>(); - displayNameMap = new HashMap(); - stdConfig = new StandardConfiguration(); + @Override + public void onDeviceFound(final String host, + final DeviceInfo deviceInfo) { + if (null != ResourceManager.this.deviceInfo + || null == deviceInfo || null == host) { + return; + } + synchronizerThread.addToQueue(new Runnable() { + @Override + public void run() { + String rcvdDeviceName = deviceInfo.getName(); + if (null == rcvdDeviceName) { + return; + } + if (deviceName.equalsIgnoreCase(rcvdDeviceName)) { + ResourceManager.this.deviceInfo = deviceInfo; - resourceListChangedUIListeners = new ArrayList(); - resourceSelectionChangedUIListeners = new ArrayList(); - resourceModelChangedUIListeners = new ArrayList(); - automationUIListeners = new ArrayList(); - observerUIListeners = new ArrayList(); + // Notify the UI Listeners + UiListenerHandler.getInstance() + .deviceInfoReceivedNotification(); + } + } + }); + } + }; - resourceModelChangeListener = new IResourceModelChangedListener() { + resourceModelChangeListener = new ResourceModelChangeListener() { @Override public void onResourceModelChanged(final String resourceURI, @@ -119,59 +139,46 @@ public class ResourceManager { if (null == resourceURI || null == resourceModelN) { return; } - SimulatorResource resource = getSimulatorResourceByURI(resourceURI); - if (null == resource) { - return; - } - - ModelChangeNotificationType notificationType; - // Fetch the resource attributes - Map resourceAttributeMapNew; - resourceAttributeMapNew = fetchResourceAttributesFromModel(resourceModelN); - if (null == resourceAttributeMapNew) { - resource.setResourceAttributesMap(null); - resourceModelChangedUINotification( - ModelChangeNotificationType.NO_ATTRIBUTES_IN_MODEL, - resourceURI, null); - return; - } - // Update the resource with new model data - Map resourceAttributeMapOld; - resourceAttributeMapOld = resource - .getResourceAttributesMap(); - if (null == resourceAttributeMapOld) { - resource.setResourceAttributesMap(resourceAttributeMapNew); - resourceModelChangedUINotification( - ModelChangeNotificationType.ATTRIBUTE_ADDED, - resourceURI, null); - return; - } - Set valueChangeSet = new HashSet(); - notificationType = compareAndUpdateLocalAttributes( - resourceAttributeMapOld, - resourceAttributeMapNew, valueChangeSet); - if (notificationType != ModelChangeNotificationType.NONE) { - // Update the UI listeners - resourceModelChangedUINotification( - notificationType, resourceURI, - valueChangeSet); - } + Display.getDefault().asyncExec(new Runnable() { + @Override + public void run() { + Resource resource = data + .getResourceByURI(resourceURI); + if (null != resource) { + try { + resource.updateResourceRepresentation(resourceModelN); + } catch (NumberFormatException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "Error while trying to update the attributes.\n" + + Utility + .getSimulatorErrorString( + e, + null)); + } + } + } + }); } }); } }; - automationListener = new IAutomation() { + automationListener = new AutoUpdateListener() { @Override - public void onAutomationComplete(final String resourceURI, + public void onUpdateComplete(final String resourceURI, final int automationId) { synchronizerThread.addToQueue(new Runnable() { @Override public void run() { - SimulatorResource resource = getSimulatorResourceByURI(resourceURI); + SingleResource resource = data + .getSingleResourceByURI(resourceURI); if (null == resource) { return; } @@ -180,40 +187,38 @@ public class ResourceManager { if (resource.isResourceAutomationInProgress()) { changeResourceLevelAutomationStatus(resource, false); // Notify the UI listeners - automationCompleteUINotification(resourceURI, null); + UiListenerHandler.getInstance() + .automationCompleteUINotification(resource, + null); } else if (resource.isAttributeAutomationInProgress()) { // Find the attribute with the given automation id - LocalResourceAttribute attribute; - attribute = getAttributeWithGivenAutomationId( + final AttributeElement attribute = getAttributeWithGivenAutomationId( resource, automationId); if (null != attribute) { - attribute.setAutomationInProgress(false); + attribute.setAutoUpdateState(false); resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource)); - // Notify the UI listeners - automationCompleteUINotification(resourceURI, - attribute.getAttributeName()); + } else { + // Setting the attribute automation status to + // false. + resource.setAttributeAutomationInProgress(false); } - } else { - // Ignoring the notification as there are no - // known automation for the current resource. } } }); } }; - observer = new IObserver() { + observer = new ObserverListener() { - @Override public void onObserverChanged(final String resourceURI, - final int status, final ObserverInfo observer) { + final int status, final Observer observer) { new Thread() { @Override public void run() { if (null == resourceURI || null == observer) { return; } - SimulatorResource resource = getSimulatorResourceByURI(resourceURI); + Resource resource = data.getResourceByURI(resourceURI); if (null == resource) { return; } @@ -224,16 +229,79 @@ public class ResourceManager { resource.removeObserverInfo(observer); } // Notify the UI listeners - observerListChangedUINotification(resourceURI); + UiListenerHandler.getInstance() + .observerListChangedUINotification(resource); } }.start(); } + + @Override + public void onObserverAdded(String resourceURI, Observer observer) { + onObserverChanged(resourceURI, 0, observer); + } + + @Override + public void onObserverRemoved(String resourceURI, Observer observer) { + onObserverChanged(resourceURI, 1, observer); + } }; synchronizerThread = new NotificationSynchronizerThread(); threadHandle = new Thread(synchronizerThread); threadHandle.setName("Simulator service provider event queue"); threadHandle.start(); + + // Set the default device name. + deviceName = "IoTivity Simulator"; + try { + SimulatorManager.setDeviceInfo(deviceName); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "Error while registering the device info.\n" + + Utility.getSimulatorErrorString(e, null)); + } + + // Set the default platform information. + platformInfo = new PlatformInfo(); + platformInfo.setPlatformID("Samsung Platform Identifier"); + platformInfo.setManufacturerName("Samsung"); + platformInfo.setManufacturerUrl("www.samsung.com"); + platformInfo.setModelNumber("Samsung Model Num01"); + platformInfo.setDateOfManufacture("2015-09-10T11:10:30Z"); + platformInfo.setPlatformVersion("PlatformVersion01"); + platformInfo.setOperationSystemVersion("OSVersion01"); + platformInfo.setHardwareVersion("HardwareVersion01"); + platformInfo.setFirmwareVersion("FirwareVersion01"); + platformInfo.setSupportUrl("http://www.samsung.com/support"); + platformInfo.setSystemTime("2015-09-10T11:10:30Z"); + try { + SimulatorManager.setPlatformInfo(platformInfo); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "Error while registering the platform info.\n" + + Utility.getSimulatorErrorString(e, null)); + } + + // Get the device information to show other details of the device in UI. + try { + SimulatorManager.findDevices("", deviceListener); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "Failed to get the local device information.\n" + + Utility.getSimulatorErrorString(e, null)); + } } private static class NotificationSynchronizerThread implements Runnable { @@ -277,1079 +345,954 @@ public class ResourceManager { } } - // This method gives a list of available RAML resource configurations. - public Map getResourceConfigurationList() { - return stdConfig.getStandardResourceConfigurationList(); - } - - public String getConfigFilePath(String fileName) { - return stdConfig.getFilePath(fileName); - } - - public void addResourceListChangedUIListener( - IResourceListChangedUIListener resourceListChangedUIListener) { - synchronized (resourceListChangedUIListeners) { - resourceListChangedUIListeners.add(resourceListChangedUIListener); + public void setDeviceInfo(List metaProperties) { + if (null == metaProperties || metaProperties.size() < 1) { + return; } - } - - public void addResourceSelectionChangedUIListener( - IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) { - synchronized (resourceSelectionChangedUIListeners) { - resourceSelectionChangedUIListeners - .add(resourceSelectionChangedUIListener); + Iterator itr = metaProperties.iterator(); + MetaProperty prop; + String propName; + String propValue; + boolean found = false; + while (itr.hasNext()) { + prop = itr.next(); + propName = prop.getPropName(); + propValue = prop.getPropValue(); + if (propName.equals(Constants.DEVICE_NAME)) { + this.deviceName = propValue; + found = true; + break; + } } - } - public void addResourceModelChangedUIListener( - IResourceModelChangedUIListener resourceModelChangedUIListener) { - synchronized (resourceModelChangedUIListeners) { - resourceModelChangedUIListeners.add(resourceModelChangedUIListener); + if (!found) { + return; } - } - public void addAutomationUIListener( - IAutomationUIListener automationUIListener) { - synchronized (automationUIListeners) { - automationUIListeners.add(automationUIListener); + try { + SimulatorManager.setDeviceInfo(deviceName); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "Error while registering the device info.\n" + + Utility.getSimulatorErrorString(e, null)); } } - public void addObserverListChangedUIListener( - IObserverListChangedUIListener observerListChangedUIListener) { - synchronized (observerUIListeners) { - observerUIListeners.add(observerListChangedUIListener); + public boolean isDeviceInfoValid(List metaProperties) { + if (null == metaProperties || metaProperties.size() < 1) { + return false; } - } - public void removeResourceListChangedUIListener( - IResourceListChangedUIListener listener) { - synchronized (resourceListChangedUIListeners) { - if (null != listener && resourceListChangedUIListeners.size() > 0) { - resourceListChangedUIListeners.remove(listener); + Iterator itr = metaProperties.iterator(); + MetaProperty prop; + String propName; + String propValue; + while (itr.hasNext()) { + prop = itr.next(); + propName = prop.getPropName(); + propValue = prop.getPropValue(); + if (propName.equals(Constants.DEVICE_NAME)) { + if (null == propValue || propValue.length() < 1) { + return false; + } + break; } } + return true; } - public void removeResourceSelectionChangedUIListener( - IResourceSelectionChangedUIListener listener) { - synchronized (resourceSelectionChangedUIListeners) { - if (null != listener - && resourceSelectionChangedUIListeners.size() > 0) { - resourceSelectionChangedUIListeners.remove(listener); - } + public List getDeviceInfo() { + List metaProperties = new ArrayList(); + metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, deviceName)); + if (null != deviceInfo) { + metaProperties.add(new MetaProperty(Constants.DEVICE_ID, deviceInfo + .getID())); + metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION, + deviceInfo.getSpecVersion())); + metaProperties.add(new MetaProperty(Constants.DEVICE_DMV, + deviceInfo.getDataModelVersion())); + } + return metaProperties; + } + + public List getPlatformInfo() { + List metaProperties = new ArrayList(); + metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo + .getPlatformID())); + metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME, + platformInfo.getManufacturerName())); + metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL, + platformInfo.getManufacturerUrl())); + metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO, + platformInfo.getModelNumber())); + metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC, + platformInfo.getDateOfManufacture())); + metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION, + platformInfo.getPlatformVersion())); + metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION, + platformInfo.getOperationSystemVersion())); + metaProperties.add(new MetaProperty( + Constants.PLATFORM_HARDWARE_VERSION, platformInfo + .getHardwareVersion())); + metaProperties.add(new MetaProperty( + Constants.PLATFORM_FIRMWARE_VERSION, platformInfo + .getFirmwareVersion())); + metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL, + platformInfo.getSupportUrl())); + metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME, + platformInfo.getSystemTime())); + return metaProperties; + } + + public void setPlatformInfo(List metaProperties) { + if (null == metaProperties || metaProperties.size() < 1) { + return; } - } - - public void removeResourceModelChangedUIListener( - IResourceModelChangedUIListener listener) { - synchronized (resourceModelChangedUIListeners) { - if (null != listener && resourceModelChangedUIListeners.size() > 0) { - resourceModelChangedUIListeners.remove(listener); + Iterator itr = metaProperties.iterator(); + MetaProperty prop; + String propName; + String propValue; + while (itr.hasNext()) { + prop = itr.next(); + propName = prop.getPropName(); + propValue = prop.getPropValue(); + if (propName.equals(Constants.PLATFORM_ID)) { + platformInfo.setPlatformID(propValue); + } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) { + platformInfo.setManufacturerName(propValue); + } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) { + platformInfo.setManufacturerUrl(propValue); + } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) { + platformInfo.setModelNumber(propValue); + } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) { + platformInfo.setDateOfManufacture(propValue); + } else if (propName.equals(Constants.PLATFORM_VERSION)) { + platformInfo.setPlatformVersion(propValue); + } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) { + platformInfo.setOperationSystemVersion(propValue); + } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) { + platformInfo.setHardwareVersion(propValue); + } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) { + platformInfo.setFirmwareVersion(propValue); + } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) { + platformInfo.setSupportUrl(propValue); + } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) { + platformInfo.setSystemTime(propValue); } } - } - - public void removeAutomationUIListener(IAutomationUIListener listener) { - synchronized (automationUIListeners) { - if (null != listener && automationUIListeners.size() > 0) { - automationUIListeners.remove(listener); - } + try { + SimulatorManager.setPlatformInfo(platformInfo); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "Error while registering the platform info.\n" + + Utility.getSimulatorErrorString(e, null)); } } - public void removeObserverListChangedUIListener( - IObserverListChangedUIListener listener) { - synchronized (observerUIListeners) { - if (null != listener && observerUIListeners.size() > 0) { - observerUIListeners.remove(listener); + public boolean isPlatformInfoValid(List metaProperties) { + if (null == metaProperties || metaProperties.size() < 1) { + return false; + } + Iterator itr = metaProperties.iterator(); + MetaProperty prop; + String propValue; + while (itr.hasNext()) { + prop = itr.next(); + propValue = prop.getPropValue(); + if (null == propValue || propValue.length() < 1) { + return false; } } + return true; } - public synchronized SimulatorResource getCurrentResourceInSelection() { + public synchronized Resource getCurrentResourceInSelection() { return currentResourceInSelection; } - public synchronized void setCurrentResourceInSelection( - SimulatorResource resource) { + public synchronized void setCurrentResourceInSelection(Resource resource) { this.currentResourceInSelection = resource; } - private void addResourceUriToOrderedMap(String resourceType, - String resourceURI) { - if (null != resourceURI && null != resourceType) { - synchronized (orderedResourceUriMap) { - ArrayList uriListForType = orderedResourceUriMap - .get(resourceType); - if (null == uriListForType) { - uriListForType = new ArrayList(); - orderedResourceUriMap.put(resourceType, uriListForType); + public boolean isResourceExist(String resourceURI) { + return data.isResourceExist(resourceURI); + } + + public boolean isAnyResourceExist() { + return data.isAnyResourceExist(); + } + + public boolean createSingleResource(SingleResource resource, + Map attributes) + throws SimulatorException { + if (null == resource) { + return false; + } + + try { + // Create the resource. + SimulatorResource jSimulatorResource = SimulatorManager + .createResource(SimulatorResource.Type.SINGLE, + resource.getResourceName(), + resource.getResourceURI(), + resource.getResourceType()); + if (null == jSimulatorResource + || !(jSimulatorResource instanceof SimulatorSingleResource)) { + return false; + } + SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource; + resource.setSimulatorResource(jSimulatorSingleResource); + + // Cancel discoverable property if requested by user. + if (!resource.isDiscoverable()) { + jSimulatorSingleResource.setDiscoverable(false); + } + + // Cancel observable property if requested by user. + if (!resource.isObservable()) { + jSimulatorSingleResource.setObservable(false); + } + + // Set the model change listener. + jSimulatorSingleResource + .setResourceModelChangeListener(resourceModelChangeListener); + + // Set the observer listener if the resource is observable. + if (resource.isObservable()) { + jSimulatorSingleResource.setObserverListener(observer); + } + + // Add attributes. + if (null != attributes && !attributes.isEmpty()) { + SimulatorResourceAttribute value; + for (Map.Entry entry : attributes + .entrySet()) { + value = entry.getValue(); + if (null != value) + jSimulatorSingleResource.addAttribute(value); } - uriListForType.add(resourceURI); + + // Get the resource model java object reference. + resource.setResourceModel(jSimulatorSingleResource + .getResourceModel()); + + resource.createResourceRepresentation(jSimulatorSingleResource + .getAttributes()); } + + // Set the resource interfaces. + jSimulatorSingleResource + .setInterface(Utility.convertSetToVectorString(resource + .getResourceInterfaces())); + + // Register the resource with the platform. + jSimulatorSingleResource.start(); + resource.setStarted(true); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), new Date(), + Utility.getSimulatorErrorString(e, null)); + throw e; } + + // Add to local cache. + data.addResource(resource); + + // Update UI listeners + UiListenerHandler.getInstance().resourceCreatedUINotification( + ResourceType.SINGLE); + + return true; } - private void removeResourceUriFromOrderedMap(String resourceType, - String resourceURI) { - synchronized (orderedResourceUriMap) { - if (null != resourceURI && null != resourceType) { - ArrayList uriListForType = orderedResourceUriMap - .get(resourceType); - if (null != uriListForType) { - uriListForType.remove(resourceURI); - if (uriListForType.size() < 1) { - orderedResourceUriMap.remove(resourceType); - } - } - } else if (null != resourceURI) { - orderedResourceUriMap.remove(resourceType); + public Resource createResourceByRAML(String configFilePath) + throws SimulatorException { + Resource resource = null; + try { + // Create the resource + SimulatorResource jSimulatorResource = SimulatorManager + .createResource(configFilePath); + if (null == jSimulatorResource) { + return null; + } + if (jSimulatorResource instanceof SimulatorSingleResource) { + resource = new SingleResource(); } else { - orderedResourceUriMap.clear(); + return null; } - } - } + resource.setSimulatorResource(jSimulatorResource); - private void addResourceToMap(SimulatorResource simulatorResource) { - if (null != simulatorResource) { - synchronized (resourceMap) { - Map resourceTypeMap; - resourceTypeMap = resourceMap.get(simulatorResource - .getResourceType()); - if (null == resourceTypeMap) { - resourceTypeMap = new HashMap(); - resourceMap.put(simulatorResource.getResourceType(), - resourceTypeMap); - } - resourceTypeMap.put(simulatorResource.getResourceURI(), - simulatorResource); + // Fetch and locally store the resource name and uri. + String uri = jSimulatorResource.getURI(); + if (null == uri || uri.trim().isEmpty()) { + return null; + } + resource.setResourceURI(uri.trim()); + + String name = jSimulatorResource.getName(); + if (null == name || name.trim().isEmpty()) { + return null; } + resource.setResourceName(name.trim()); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), new Date(), + Utility.getSimulatorErrorString(e, null)); + throw e; } + return resource; } - private void addResourceToMap(String resourceType, - Map newResourceTypeMap) { - if (null != resourceType && null != newResourceTypeMap) { - synchronized (resourceMap) { - Map resourceTypeMap = resourceMap - .get(resourceType); - if (null != resourceTypeMap) { - resourceTypeMap.putAll(newResourceTypeMap); - } else { - resourceMap.put(resourceType, newResourceTypeMap); + /** + * This method can set/change the resource uri and name of an already + * created resource which is not yet registered with the platform. This + * method registers the model change and observer listeners, registers the + * resource, fetches the resource attributes, updates the local cache and + * notifies the UI listeners. + */ + public boolean completeSingleResourceCreationByRAML(Resource resource, + String uri, String name, boolean multiInstance) + throws SimulatorException { + if (null == resource || !(resource instanceof SingleResource)) { + return false; + } + try { + SingleResource singleRes = (SingleResource) resource; + + SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource + .getSimulatorResource(); + if (null == jSimulatorSingleResource) { + return false; + } + + // Update resource URI and Name if they are changed. + String newUri = uri.trim(); + String newName = name.trim(); + + if (multiInstance) { + singleRes.setResourceURI(newUri); + singleRes.setResourceName(newName); + } else { + if (!singleRes.getResourceURI().equals(newUri)) { + jSimulatorSingleResource.setURI(newUri); + singleRes.setResourceURI(newUri); } + if (!singleRes.getResourceName().equals(newName)) { + jSimulatorSingleResource.setName(newName); + singleRes.setResourceName(newName); + } + } + + // Set the model change listener. + jSimulatorSingleResource + .setResourceModelChangeListener(resourceModelChangeListener); + + // Set the observer listener if the resource is observable. + if (jSimulatorSingleResource.isObservable()) { + jSimulatorSingleResource.setObserverListener(observer); + singleRes.setObservable(true); + } + + // Fetch the resource model. + SimulatorResourceModel jResModel = jSimulatorSingleResource + .getResourceModel(); + if (null == jResModel) { + return false; } + singleRes.setResourceModel(jResModel); + + // Fetch the basic details of the resource. + singleRes.setResourceType(jSimulatorSingleResource + .getResourceType()); + singleRes + .setResourceInterfaces(Utility + .convertVectorToSet(jSimulatorSingleResource + .getInterface())); + + // Fetch the resource attributes. + singleRes.createResourceRepresentation(jSimulatorSingleResource + .getAttributes()); + + // Register the resource with the platform. + jSimulatorSingleResource.start(); + singleRes.setStarted(true); + + // Add to local cache. + data.addResource(singleRes); + + // Update UI listeners for single instance creation + if (!multiInstance) + UiListenerHandler.getInstance().resourceCreatedUINotification( + ResourceType.SINGLE); + } catch (Exception e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), new Date(), + Utility.getSimulatorErrorString(e, null)); + throw e; } + return true; } - private void removeResourceFromMap(String resourceType, String resourceURI) { - if (null != resourceURI && null != resourceType) { - synchronized (resourceMap) { - Map resourceTypeMap = resourceMap - .get(resourceType); - if (null != resourceTypeMap) { - resourceTypeMap.remove(resourceURI); - if (resourceTypeMap.size() < 1) { - resourceMap.remove(resourceType); + public Set createSingleResourceMultiInstances( + String configFile, int count, IProgressMonitor progressMonitor) + throws SimulatorException { + Set resultSet; + try { + resultSet = new HashSet(); + Vector jSimulatorResources = SimulatorManager + .createResource(configFile, count); + if (null == jSimulatorResources || jSimulatorResources.size() < 1) { + return null; + } + SimulatorSingleResource jResource; + SingleResource resource; + boolean result; + for (SimulatorResource jSimulatorResource : jSimulatorResources) { + // If the resource creation progress is canceled, then stop the + // creation and stop/delete + // the resources created already. + if (progressMonitor.isCanceled()) { + removeSingleResources(resultSet); + return null; + } + jResource = (SimulatorSingleResource) jSimulatorResource; + resource = new SingleResource(); + resource.setSimulatorResource(jResource); + try { + result = completeSingleResourceCreationByRAML(resource, + jResource.getURI(), jResource.getName(), true); + if (result) { + resultSet.add(resource); } + } catch (SimulatorException eInner) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + Utility.getSimulatorErrorString(eInner, + null)); } + progressMonitor.worked(1); } + } catch (SimulatorException eOuter) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), new Date(), + Utility.getSimulatorErrorString(eOuter, null)); + throw eOuter; } + return resultSet; } - public boolean isResourceExist(String resourceURI) { - boolean result = false; - if (null != resourceURI) { - SimulatorResource resource = getSimulatorResourceByURI(resourceURI); - if (null != resource) { - result = true; - } + public List getResourceList() { + List resourceList = data.getResources(); + if (null == resourceList) { + return null; } - return result; + // Sort the list + Collections.sort(resourceList, Utility.resourceComparator); + + return resourceList; } - public void addResourceDisplayName(String displayName, String completeURI) { - synchronized (displayNameMap) { - displayNameMap.put(displayName, completeURI); + public List getSingleResourceList() { + List resourceList = data.getSingleResources(); + if (null == resourceList) { + return null; } + // Sort the list + Collections.sort(resourceList, Utility.singleResourceComparator); + + return resourceList; } - public void removeResourceDisplayName(String displayName) { - synchronized (displayNameMap) { - displayNameMap.remove(displayName); + public void removeSingleResources(Set resources) + throws SimulatorException { + if (null == resources) { + return; + } + Iterator itr = resources.iterator(); + while (itr.hasNext()) { + removeResource(itr.next()); } } - public boolean isDisplayName(String displayName) { - boolean exist = false; - synchronized (displayNameMap) { - exist = displayNameMap.containsKey(displayName); + public void removeResource(Resource res) throws SimulatorException { + // Unregister the resource from the platform. + SimulatorResource simRes = res.getSimulatorResource(); + try { + simRes.stop(); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), new Date(), + Utility.getSimulatorErrorString(e, null)); + throw e; } - return exist; + + // Delete this resource + data.deleteResource(res); } - public String getCompleteUriFromDisplayName(String displayName) { - String completeURI = null; - synchronized (displayNameMap) { - completeURI = displayNameMap.get(displayName); + public boolean isUriUnique(List properties) { + if (null == properties) { + return false; } - return completeURI; + MetaProperty prop; + Iterator itr = properties.iterator(); + while (itr.hasNext()) { + prop = itr.next(); + if (prop.getPropName().equals(Constants.RESOURCE_URI)) { + String uri = prop.getPropValue(); + return !data.isResourceExist(uri); + } + } + return false; } - public void createResource(final String configFilePath) { + public void resourceSelectionChanged(final Resource selectedResource) { new Thread() { @Override public void run() { - SimulatorResourceServer resourceServerN; - try { - resourceServerN = SimulatorManager.createResource( - configFilePath, resourceModelChangeListener); - } catch (SimulatorException e) { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() + "]" - + e.code().toString() + "-" - + e.message()); - return; + if (null != selectedResource) { + setCurrentResourceInSelection(selectedResource); + } else { + setCurrentResourceInSelection(null); } - SimulatorResource simulatorResource; - simulatorResource = fetchResourceData(resourceServerN); - if (null != simulatorResource) { - String uri = simulatorResource.getResourceURI(); - addResourceToMap(simulatorResource); - addResourceUriToOrderedMap( - simulatorResource.getResourceType(), uri); - resourceCreatedUINotification(); - - // Form the display URI - String displayName = Utility.uriToDisplayName(uri); - if (null != displayName) { - addResourceDisplayName(displayName, uri); - } else { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "Converting the URI to display format for " - + uri + "failed!"); - } + // Notify all observers for resource selection change event + UiListenerHandler.getInstance() + .resourceSelectionChangedUINotification( + selectedResource); + } + }.start(); + } - // Set the observer for the created resource - try { - resourceServerN.setObserverCallback(observer); - } catch (SimulatorException e) { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() - + "]" + e.code().toString() - + "-" + e.message()); - } + public List getMetaProperties(Resource resource) { + if (null != resource) { + String propName; + String propValue; + + List metaPropertyList = new ArrayList(); - // Print the resource data - simulatorResource.printResourceInfo(); + for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) { + propName = Constants.META_PROPERTIES[index]; + if (propName.equals(Constants.RESOURCE_NAME)) { + propValue = resource.getResourceName(); + } else if (propName.equals(Constants.RESOURCE_URI)) { + propValue = resource.getResourceURI(); + } else if (propName.equals(Constants.RESOURCE_TYPE)) { + propValue = resource.getResourceType(); + } else if (propName.equals(Constants.INTERFACE_TYPES)) { + Set ifTypes = resource.getResourceInterfaces(); + if (null != ifTypes && !ifTypes.isEmpty()) { + propValue = ""; + Iterator itr = ifTypes.iterator(); + while (itr.hasNext()) { + propValue += itr.next(); + if (itr.hasNext()) { + propValue += ", "; + } + } + } else { + propValue = null; + } + } else { + propValue = null; + } + if (null != propValue) { + metaPropertyList.add(new MetaProperty(propName, propValue)); } } - }.start(); + return metaPropertyList; + } + return null; } - public void createResource(final String configFilePath, - final int noOfInstances) { - new Thread() { - @Override - public void run() { - Map resourceTypeMap; - SimulatorResourceServer[] simulatorResourceServers = null; - try { - simulatorResourceServers = SimulatorManager.createResource( - configFilePath, noOfInstances, - resourceModelChangeListener); - } catch (SimulatorException e) { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() + "]" - + e.code().toString() + "-" - + e.message()); - return; - } - if (null == simulatorResourceServers) { - return; - } - resourceTypeMap = new HashMap(); - SimulatorResource resource; - String uri; - String displayName; - for (SimulatorResourceServer resourceServerN : simulatorResourceServers) { - resource = fetchResourceData(resourceServerN); - if (null != resource) { - uri = resource.getResourceURI(); - resourceTypeMap.put(uri, resource); - addResourceUriToOrderedMap(resource.getResourceType(), - uri); - - // Form the display URI - displayName = Utility.uriToDisplayName(uri); - if (null != displayName) { - addResourceDisplayName(displayName, uri); - } else { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "Converting the URI to display format for " - + uri + "failed!"); - } - } - // Set the observer for the created resource - try { - resourceServerN.setObserverCallback(observer); - } catch (SimulatorException e) { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() - + "]" + e.code().toString() - + "-" + e.message()); - } - } - - // Find the resourceType and add it to the local data - // structure and notify UI Listeners - if (resourceTypeMap.size() > 0) { - String resourceType; - Set uriSet = resourceTypeMap.keySet(); - Iterator itr = uriSet.iterator(); - if (itr.hasNext()) { - SimulatorResource simResource = resourceTypeMap.get(itr - .next()); - if (null != simResource) { - resourceType = simResource.getResourceType(); - - addResourceToMap(resourceType, resourceTypeMap); - resourceCreatedUINotification(); - } - } - } - } - }.start(); - } - - private SimulatorResource fetchResourceData( - SimulatorResourceServer resourceServerN) { - SimulatorResource simulatorResource = null; - if (null != resourceServerN) { - simulatorResource = new SimulatorResource(); - simulatorResource.setResourceServer(resourceServerN); - simulatorResource.setResourceURI(resourceServerN.getURI()); - simulatorResource - .setResourceType(resourceServerN.getResourceType()); - simulatorResource.setResourceName(resourceServerN.getName()); - simulatorResource.setResourceInterface(resourceServerN - .getInterfaceType()); - - SimulatorResourceModel resourceModelN; - try { - resourceModelN = resourceServerN.getModel(); - } catch (SimulatorException e) { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() + "]" - + e.code().toString() + "-" - + e.message()); - return null; - } - if (null != resourceModelN) { - simulatorResource.setResourceModel(resourceModelN); - - // Fetch the resource attributes - Map resourceAttributeMap; - resourceAttributeMap = fetchResourceAttributesFromModel(resourceModelN); - if (null != resourceAttributeMap) { - simulatorResource - .setResourceAttributesMap(resourceAttributeMap); - } - } + public boolean startResource(Resource resource) throws SimulatorException { + if (null == resource) { + return false; } - return simulatorResource; - } - - private Map fetchResourceAttributesFromModel( - SimulatorResourceModel resourceModelN) { - Map resourceAttributeMap = null; - if (null != resourceModelN) { - Map attributeMapN; - try { - attributeMapN = resourceModelN.getAttributes(); - } catch (SimulatorException e) { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() + "]" - + e.code().toString() + "-" - + e.message()); - return null; - } - if (null != attributeMapN) { - resourceAttributeMap = new HashMap(); - - Set attNameSet = attributeMapN.keySet(); - String attName; - ResourceAttribute attributeN; - LocalResourceAttribute attribute; - Iterator attNameItr = attNameSet.iterator(); - while (attNameItr.hasNext()) { - attName = attNameItr.next(); - attributeN = attributeMapN.get(attName); - if (null != attributeN) { - attribute = new LocalResourceAttribute(); - attribute.setResourceAttribute(attributeN); - - // Set the attribute value - Object valueObj = attributeN.getValue(); - if (null != valueObj) { - attribute.setAttributeValue(valueObj); - } - - // Set the attribute value list. - attribute.setAttValues(getValueList(attributeN)); - - // Initially disabling the automation - attribute.setAutomationInProgress(false); - - // TODO: Temporarily setting the interval to 500. - // This value should come from the native layer. - // Native implementation is in progress. - attribute - .setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL); - - // Setting the default automation type - attribute - .setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE); - - resourceAttributeMap.put(attName, attribute); - } - } - } + SimulatorResource server = resource.getSimulatorResource(); + if (null == server) { + return false; } - return resourceAttributeMap; + try { + server.start(); + resource.setStarted(true); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "There is an error while starting the resource.\n" + + Utility.getSimulatorErrorString(e, null)); + throw e; + } + return true; } - // This method gives all known possible values of the attribute in string - // format. It takes allowed values or range of values whichever is available - private List getValueList(ResourceAttribute attributeN) { - Object[] allowedValues = attributeN.getAllowedValues(); - List valueList = new ArrayList(); - if (null != allowedValues && allowedValues.length > 0) { - for (Object value : allowedValues) { - if (null != value) { - valueList.add(String.valueOf(value)); - } - } - } else { - Type valueType = attributeN.getBaseType(); - Range range = attributeN.getRange(); - if (null != range) { - Object min = range.getMin(); - Object max = range.getMax(); - if (valueType == Type.INT) { - int minI = (Integer) min; - int maxI = (Integer) max; - for (int value = minI; value <= maxI; value++) { - valueList.add(String.valueOf(value)); - } - } else if (valueType == Type.DOUBLE) { - double minD = (Double) min; - double maxD = (Double) max; - for (double value = minD; value <= maxD; value++) { - valueList.add(String.valueOf(value)); - } - } - } + public boolean stopResource(Resource resource) throws SimulatorException { + if (null == resource) { + return false; } - Object attValue = attributeN.getValue(); - if (valueList.size() < 1 && null != attValue) { - valueList.add(String.valueOf(attValue)); + SimulatorResource server = resource.getSimulatorResource(); + if (null == server) { + return false; } - return valueList; - } - - public void deleteResourceByURI(final String resourceURI) { - if (null != resourceURI) { - new Thread() { - @Override - public void run() { - SimulatorResource resource = getSimulatorResourceByURI(resourceURI); - if (null != resource) { - String resourceType = resource.getResourceType(); - - // Unregister the resource from the platform - deleteResource(resource); - - // Delete from the local data structure - deleteLocalResourceDetails(resourceType, resourceURI); - - // Notify the UI listener for removing this resource - // from UI - resourceDeletedUINotification(); - - if (null != currentResourceInSelection - && resource == currentResourceInSelection) { - // Listeners might query the resource being deleted - // if exists. So set the currently selection to - // null. - setCurrentResourceInSelection(null); - - // Notify all observers for resource selection - // change event - resourceSelectionChangedUINotification(); - } - } - } - }.start(); + try { + server.stop(); + resource.setStarted(false); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "There is an error while stopping the resource.\n" + + Utility.getSimulatorErrorString(e, null)); + throw e; } + return true; } - private SimulatorResource getSimulatorResourceByURI(String resourceURI) { - SimulatorResource resource = null; - if (null != resourceURI) { - synchronized (resourceMap) { - Set typeSet = resourceMap.keySet(); - Iterator typeItr = typeSet.iterator(); - String resourceType; - Map resourceTypeMap; - while (typeItr.hasNext()) { - resourceType = typeItr.next(); - resourceTypeMap = resourceMap.get(resourceType); - if (null != resourceTypeMap) { - resource = resourceTypeMap.get(resourceURI); - if (null != resource) { - break; - } - } - } - } + public boolean changeResourceName(Resource resource, String newName) + throws SimulatorException { + if (null == resource || null == newName) { + return false; } - return resource; - } - private void deleteResource(SimulatorResource resource) { - if (null != resource) { - SimulatorResourceServer resourceServerN = resource - .getResourceServer(); - if (null != resourceServerN) { - try { - SimulatorManager.deleteResource(resourceServerN); - } catch (SimulatorException e) { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() + "]" - + e.code().toString() + "-" - + e.message()); - } - } + if (!stopResource(resource)) { + return false; } - } - public void deleteResourceByType(final String resourceType) { - if (null != resourceType) { - new Thread() { - @Override - public void run() { - // Unregister the resources from the platform - deleteResource(resourceType); - - // Delete from the local data structure - deleteLocalResourceDetails(resourceType, null); - - // Notify the UI listener for removing this resource from UI - resourceDeletedUINotification(); - - if (null != currentResourceInSelection - && resourceType.equals(currentResourceInSelection - .getResourceType())) { - // Listeners might query the resource being deleted if - // exists. So set the currently selection to null. - setCurrentResourceInSelection(null); - - // Notify all observers for resource selection change - // event - resourceSelectionChangedUINotification(); - } - } - }.start(); + SimulatorResource server = resource.getSimulatorResource(); + try { + server.setName(newName); + resource.setResourceName(newName); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "There is an error while changing the resource name.\n" + + Utility.getSimulatorErrorString(e, null)); + throw e; } - } - private void deleteResource(String resourceType) { - if (null != resourceType) { - try { - SimulatorManager.deleteResources(resourceType); - } catch (SimulatorException e) { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() + "]" - + e.code().toString() + "-" - + e.message()); - } + if (!startResource(resource)) { + return false; } - } - public void deleteAllResources() { - new Thread() { - @Override - public void run() { - // Unregister the resources from the platform - deleteResource(); + return true; + } - // Delete from the local data structure - deleteLocalResourceDetails(null, null); + public boolean changeResourceURI(Resource resource, String newURI) + throws SimulatorException { + if (null == resource || null == newURI) { + return false; + } - // Notify the UI listener for removing this resource from UI - resourceDeletedUINotification(); + if (!stopResource(resource)) { + return false; + } - // Listeners might query the resource being deleted if exists. - // So set the currently selection to null. - setCurrentResourceInSelection(null); + String curURI = resource.getResourceURI(); + setResourceURI(resource, newURI); - // Notify all observers for resource selection change event - resourceSelectionChangedUINotification(); + try { + if (!startResource(resource)) { + return false; } - }.start(); + } catch (SimulatorException e) { + setResourceURI(resource, curURI); + } + + return true; } - private void deleteResource() { + public void setResourceURI(Resource resource, String newURI) + throws SimulatorException { + String curURI = resource.getResourceURI(); + SimulatorResource server = resource.getSimulatorResource(); try { - SimulatorManager.deleteResources(null); + server.setURI(newURI); + data.changeResourceURI(resource, curURI, newURI); } catch (SimulatorException e) { Activator .getDefault() .getLogManager() .log(Level.ERROR.ordinal(), new Date(), - "[" + e.getClass().getSimpleName() + "]" - + e.code().toString() + "-" + e.message()); + "There is an error while changing the resource URI.\n" + + Utility.getSimulatorErrorString(e, null)); + throw e; } } - private void deleteLocalResourceDetails(String resourceType, - String resourceURI) { - if (null != resourceType && null != resourceURI) { - removeResourceFromMap(resourceType, resourceURI); - removeResourceUriFromOrderedMap(resourceType, resourceURI); - } else { - synchronized (resourceMap) { - if (null != resourceType) { - removeResourceUriFromOrderedMap(resourceType, null); - resourceMap.remove(resourceType); - } else { - resourceMap.clear(); - removeResourceUriFromOrderedMap(null, null); - } - } - } - // Remove the display name from the map - if (null != resourceURI) { - String dispName = Utility.uriToDisplayName(resourceURI); - if (null != dispName) - removeResourceDisplayName(dispName); + public boolean updateResourceProperties(Resource resource, + List properties, boolean uriChanged, + boolean nameChanged) throws SimulatorException { + if (null == resource || null == properties) { + return false; } - } - private void resourceCreatedUINotification() { - synchronized (resourceListChangedUIListeners) { - if (resourceListChangedUIListeners.size() > 0) { - IResourceListChangedUIListener listener; - Iterator listenerItr = resourceListChangedUIListeners - .iterator(); - while (listenerItr.hasNext()) { - listener = listenerItr.next(); - if (null != listener) { - listener.onResourceCreation(); - } - } + // Updating the properties + Iterator itr = properties.iterator(); + MetaProperty property; + String propName; + String propValue; + String resName = null; + String resURI = null; + while (itr.hasNext()) { + property = itr.next(); + if (null == property) { + continue; } - } - } - - private void resourceDeletedUINotification() { - synchronized (resourceListChangedUIListeners) { - if (resourceListChangedUIListeners.size() > 0) { - IResourceListChangedUIListener listener; - Iterator listenerItr = resourceListChangedUIListeners - .iterator(); - while (listenerItr.hasNext()) { - listener = listenerItr.next(); - if (null != listener) { - listener.onResourceDeletion(); - } - } + propName = property.getPropName(); + propValue = property.getPropValue(); + if (propName.equals(Constants.RESOURCE_NAME)) { + resName = propValue; + } else if (propName.equals(Constants.RESOURCE_URI)) { + resURI = propValue; } } - } - private void resourceSelectionChangedUINotification() { - synchronized (resourceSelectionChangedUIListeners) { - if (resourceSelectionChangedUIListeners.size() > 0) { - IResourceSelectionChangedUIListener listener; - Iterator listenerItr = resourceSelectionChangedUIListeners - .iterator(); - while (listenerItr.hasNext()) { - listener = listenerItr.next(); - if (null != listener) { - listener.onResourceSelectionChange(); - } - } + if (nameChanged) { + if (!changeResourceName(resource, resName)) { + return false; } - } - } - private void resourceModelChangedUINotification( - ModelChangeNotificationType notificationType, String resourceURI, - Set valueChangeSet) { - synchronized (resourceModelChangedUIListeners) { - if (resourceModelChangedUIListeners.size() > 0 - && notificationType != ModelChangeNotificationType.NONE - && null != resourceURI) { - IResourceModelChangedUIListener listener; - Iterator listenerItr = resourceModelChangedUIListeners - .iterator(); - while (listenerItr.hasNext()) { - listener = listenerItr.next(); - if (null != listener) { - listener.onResourceModelChange(notificationType, - resourceURI, valueChangeSet); - } - } - } + // Notify UI Listeners + UiListenerHandler.getInstance().propertiesChangedUINotification( + Resource.class); } - } - private void resourceAutomationStartedUINotification(String resourceURI) { - synchronized (automationUIListeners) { - if (automationUIListeners.size() > 0 && null != resourceURI) { - IAutomationUIListener listener; - Iterator listenerItr = automationUIListeners - .iterator(); - while (listenerItr.hasNext()) { - listener = listenerItr.next(); - if (null != listener) { - listener.onResourceAutomationStart(resourceURI); - } - } + if (uriChanged) { + if (!changeResourceURI(resource, resURI)) { + return false; } } + + return true; } - private void automationCompleteUINotification(String resourceURI, - String attName) { - synchronized (automationUIListeners) { - if (automationUIListeners.size() > 0 && null != resourceURI) { - IAutomationUIListener listener; - Iterator listenerItr = automationUIListeners - .iterator(); - while (listenerItr.hasNext()) { - listener = listenerItr.next(); - if (null != listener) { - listener.onAutomationComplete(resourceURI, attName); - } - } + public boolean updateResourceInterfaces(Resource resource, + Set newIfSet) throws SimulatorException { + if (null == resource || null == newIfSet || newIfSet.isEmpty()) { + return false; + } + SimulatorResource jResource = resource.getSimulatorResource(); + if (null == jResource) { + return false; + } + Set curIfSet = resource.getResourceInterfaces(); + Iterator itr = curIfSet.iterator(); + String interfaceType; + boolean resourceRestartRequired = false; + while (itr.hasNext()) { + interfaceType = itr.next(); + if (!newIfSet.contains(interfaceType)) { + resourceRestartRequired = true; + break; } } - } - private void observerListChangedUINotification(String resourceURI) { - synchronized (observerUIListeners) { - if (observerUIListeners.size() > 0 && null != resourceURI) { - IObserverListChangedUIListener listener; - Iterator listenerItr = observerUIListeners - .iterator(); - while (listenerItr.hasNext()) { - listener = listenerItr.next(); - if (null != listener) { - listener.onObserverListChanged(resourceURI); + try { + // As there is no support from native layer for interface removal, + // supporting it from the simulator requires restarting the resource + // if any existing interfaces are removed. + + if (resourceRestartRequired) { + stopResource(resource); + jResource.setInterface(Utility + .convertSetToVectorString(newIfSet)); + startResource(resource); + } else { + // Existing interfaces are not removed. + itr = newIfSet.iterator(); + while (itr.hasNext()) { + interfaceType = itr.next(); + if (!curIfSet.contains(interfaceType)) { + jResource.addInterface(interfaceType); } } } + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "There is an error while changing the interface types." + + Utility.getSimulatorErrorString(e, null)); + throw e; } - } - public List getResourceTypeList() { - List typeList = null; - synchronized (resourceMap) { - if (resourceMap.size() > 0) { - typeList = new ArrayList(); - Set typeSet = resourceMap.keySet(); - Iterator typeItr = typeSet.iterator(); - while (typeItr.hasNext()) { - typeList.add(typeItr.next()); - } - } - } - return typeList; + // Set the resource interfaces. + resource.setResourceInterfaces(newIfSet); + + return true; } - public boolean isTypeExist(String resType) { - synchronized (resourceMap) { - if (resourceMap.containsKey(resType)) { - return true; + public boolean attributeValueUpdated(SingleResource resource, + String attributeName, AttributeValue value) { + if (null != resource && null != attributeName && null != value) { + SimulatorSingleResource simRes = (SimulatorSingleResource) resource + .getSimulatorResource(); + if (null != simRes) { + try { + simRes.updateAttribute(attributeName, value); + return true; + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), new Date(), + Utility.getSimulatorErrorString(e, null)); + } } } return false; } - public List getURIList() { - List list = null; - synchronized (orderedResourceUriMap) { - Set typeSet = orderedResourceUriMap.keySet(); - List typeList = Utility.convertSetToList(typeSet); - if (null == typeList || typeList.size() < 1) { - return list; - } - list = new ArrayList(); - - // Sort the types - Collections.sort(typeList); - - // Add all URIs to the output list - Iterator typeItr = typeList.iterator(); - while (typeItr.hasNext()) { - List l = orderedResourceUriMap.get(typeItr.next()); - if (null != l) { - list.addAll(l); - } - } + public boolean isResourceStarted(Resource resource) { + if (null == resource) { + return false; } - return list; + return resource.isStarted(); } - public void resourceSelectionChanged(final String selectedItem) { - new Thread() { - @Override - public void run() { - // Check whether the item selected is a resource or resource - // category - if (isTypeExist(selectedItem)) { - // Given item is a resource Type - setCurrentResourceInSelection(null); + public boolean isPropertyValueInvalid(Resource resource, + List properties, String propName) { + if (null == resource || null == properties || null == propName) { + return false; + } + boolean invalid = false; + MetaProperty prop; + Iterator itr = properties.iterator(); + while (itr.hasNext()) { + prop = itr.next(); + if (prop.getPropName().equals(propName)) { + String value = prop.getPropValue(); + if (propName.equals(Constants.RESOURCE_URI)) { + if (!Utility.isUriValid(value)) { + invalid = true; + } } else { - // Given item is a resource URI - SimulatorResource resource = getSimulatorResourceByURI(selectedItem); - if (null != resource) { - setCurrentResourceInSelection(resource); - } else { - setCurrentResourceInSelection(null); + if (null == value || value.trim().isEmpty()) { + invalid = true; } } - // Notify all observers for resource selection change event - resourceSelectionChangedUINotification(); } - }.start(); + } + return invalid; } - public List getMetaProperties(SimulatorResource resource) { - if (null != resource) { - String propName; - String propValue; - - List metaPropertyList = new ArrayList(); - - for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) { - propName = Constants.META_PROPERTIES[index]; - if (propName.equals(Constants.RESOURCE_URI)) { - propValue = resource.getResourceURI(); - } else if (propName.equals(Constants.RESOURCE_TYPE)) { - propValue = resource.getResourceType(); - } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) { - // TODO: Temporarily ignoring till the implementation. - propValue = null; - } else { - propValue = null; - } - if (null != propValue) { - metaPropertyList.add(new MetaProperty(propName, propValue)); + public boolean isPropValueChanged(Resource resource, + List properties, String propName) { + if (null == resource || null == properties || null == propName) { + return false; + } + boolean changed = false; + MetaProperty prop; + String oldValue; + Iterator itr = properties.iterator(); + while (itr.hasNext()) { + prop = itr.next(); + if (prop.getPropName().equals(propName)) { + oldValue = getPropertyValueFromResource(resource, propName); + if (null != oldValue && !prop.getPropValue().equals(oldValue)) { + changed = true; } + break; } - - return metaPropertyList; } - return null; + return changed; } - public List getAttributes(SimulatorResource resource) { - List attList = null; - if (null != resource) { - Map attMap = resource - .getResourceAttributesMap(); - if (null != attMap && attMap.size() > 0) { - attList = new ArrayList(); - Set attNameSet = attMap.keySet(); - String attName; - LocalResourceAttribute attribute; - Iterator attNameItr = attNameSet.iterator(); - while (attNameItr.hasNext()) { - attName = attNameItr.next(); - attribute = attMap.get(attName); - if (null != attribute) { - attList.add(attribute); - } - } - } + private String getPropertyValueFromResource(Resource resource, + String propName) { + if (null == resource || null == propName) { + return null; + } + if (propName.equals(Constants.RESOURCE_URI)) { + return resource.getResourceURI(); + } else if (propName.equals(Constants.RESOURCE_NAME)) { + return resource.getResourceName(); + } else if (propName.equals(Constants.RESOURCE_TYPE)) { + return resource.getResourceType(); + } else { + return null; } - return attList; } - public void attributeValueUpdated(SimulatorResource resource, - String attributeName, String value) { - if (null != resource && null != attributeName && null != value) { - SimulatorResourceServer server = resource.getResourceServer(); - if (null != server) { - LocalResourceAttribute att = resource - .getAttribute(attributeName); - if (null == att) { - return; - } - Type baseType = att.getAttValBaseType(); - try { - if (baseType == Type.STRING) { - server.updateAttributeString(attributeName, value); - } else if (baseType == Type.INT) { - int val; - try { - val = Integer.parseInt(value); - } catch (NumberFormatException nfe) { - return; - } - server.updateAttributeInteger(attributeName, val); - } else if (baseType == Type.DOUBLE) { - double val; - try { - val = Double.parseDouble(value); - } catch (NumberFormatException nfe) { - return; - } - server.updateAttributeDouble(attributeName, val); - } else if (baseType == Type.BOOL) { - boolean val; - val = Boolean.parseBoolean(value); - server.updateAttributeBoolean(attributeName, val); - } - } catch (SimulatorException e) { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() + "]" - + e.code().toString() + "-" - + e.message()); - } - } + public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) { + if (null == att) { + return false; + } + AttributeProperty prop = att.property(); + if (null == prop) { + return false; } - } - private ModelChangeNotificationType compareAndUpdateLocalAttributes( - Map resourceAttributeMapOld, - Map resourceAttributeMapNew, - Set valueChangeSet) { - ModelChangeNotificationType notificationType = ModelChangeNotificationType.NONE; - if (null != resourceAttributeMapOld && null != resourceAttributeMapNew) { - Set oldMapKeySet = resourceAttributeMapOld.keySet(); - Iterator attributeMapOldItr = oldMapKeySet.iterator(); - String attName; - LocalResourceAttribute attributeOld; - LocalResourceAttribute attributeNew; - Object attValueOld; - Object attValueNew; - String oldValueStr; - String newValueStr; - while (attributeMapOldItr.hasNext()) { - attName = attributeMapOldItr.next(); - if (resourceAttributeMapNew.containsKey(attName)) { - attributeOld = resourceAttributeMapOld.get(attName); - attributeNew = resourceAttributeMapNew.get(attName); - // Copy the attribute value from new to old if the value - // has been changed - // Comparing only the attribute's value considering the - // fact that only the value can be changed - if (null != attributeOld && null != attributeNew) { - attValueOld = attributeOld.getAttributeValue(); - attValueNew = attributeNew.getAttributeValue(); - - oldValueStr = String.valueOf(attValueOld); - newValueStr = String.valueOf(attValueNew); - - if (null != oldValueStr && null != newValueStr) { - if (!oldValueStr.equals(newValueStr)) { - attributeOld.setAttributeValue(attValueNew); - notificationType = ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED; - valueChangeSet.add(attributeOld); - } - } - } - resourceAttributeMapNew.remove(attName); - } else { - // Attribute doesn't exist in the new model. Hence - // removing it from the model. - resourceAttributeMapOld.remove(attName); - notificationType = ModelChangeNotificationType.ATTRIBUTE_REMOVED; - } + if (prop.getType() == Type.INTEGER) { + IntegerProperty intProperty = prop.asInteger(); + if (null != intProperty) { + return (intProperty.hasRange() || intProperty.hasValues()); + } else { + return false; } - // Check for new attributes in the new model - if (resourceAttributeMapNew.size() > 0) { - Set remainingAttSet = resourceAttributeMapNew.keySet(); - Iterator remainingAttItr = remainingAttSet.iterator(); - LocalResourceAttribute attribute; - while (remainingAttItr.hasNext()) { - attName = remainingAttItr.next(); - if (null != attName) { - attribute = resourceAttributeMapNew.get(attName); - if (null != attribute) { - resourceAttributeMapOld.put(attName, attribute); - } - } - } - notificationType = ModelChangeNotificationType.ATTRIBUTE_ADDED; + } else if (prop.getType() == Type.DOUBLE) { + DoubleProperty dblProperty = prop.asDouble(); + if (null != dblProperty) { + return (dblProperty.hasRange() || dblProperty.hasValues()); + } else { + return false; + } + } else if (prop.getType() == Type.STRING) { + StringProperty stringProperty = prop.asString(); + if (null != stringProperty) { + return stringProperty.hasValues(); + } else { + return false; } } - return notificationType; + + return true; } - public int startAutomation(SimulatorResource resource, - LocalResourceAttribute attribute, AutomationType autoType, + public int startAutomation(SingleResource resource, + AttributeElement attribute, AutoUpdateType autoType, int autoUpdateInterval) { int autoId = -1; if (null != resource && null != attribute) { - SimulatorResourceServer resourceServerN = resource - .getResourceServer(); - if (null != resourceServerN) { - String attrName = attribute.getAttributeName(); + SimulatorSingleResource server = (SimulatorSingleResource) resource + .getSimulatorResource(); + if (null != server) { + String attrName = attribute.getSimulatorResourceAttribute() + .name(); try { - autoId = resourceServerN.startAttributeAutomation(attrName, - autoType, automationListener); + autoId = server.startAttributeUpdation(attrName, autoType, + autoUpdateInterval, automationListener); } catch (SimulatorException e) { Activator .getDefault() @@ -1362,27 +1305,25 @@ public class ResourceManager { return -1; } if (-1 != autoId) { - attribute.setAutomationId(autoId); - attribute.setAutomationType(autoType); - attribute.setAutomationUpdateInterval(autoUpdateInterval); - attribute.setAutomationInProgress(true); + attribute.setAutoUpdateId(autoId); + attribute.setAutoUpdateType(autoType); + attribute.setAutoUpdateInterval(autoUpdateInterval); + attribute.setAutoUpdateState(true); resource.setAttributeAutomationInProgress(true); - } else { - attribute.setAutomationInProgress(false); } } } return autoId; } - public void stopAutomation(SimulatorResource resource, - LocalResourceAttribute att, int autoId) { + public void stopAutomation(SingleResource resource, AttributeElement att, + int autoId) { if (null != resource) { - SimulatorResourceServer resourceServerN = resource - .getResourceServer(); - if (null != resourceServerN) { + SimulatorSingleResource server = (SimulatorSingleResource) resource + .getSimulatorResource(); + if (null != server) { try { - resourceServerN.stopAutomation(autoId); + server.stopUpdation(autoId); } catch (SimulatorException e) { Activator .getDefault() @@ -1395,119 +1336,58 @@ public class ResourceManager { return; } // Change the automation status - att.setAutomationInProgress(false); + att.setAutoUpdateState(false); resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource)); } } } - private boolean isAnyAttributeInAutomation(SimulatorResource resource) { + public boolean startResourceAutomationUIRequest(AutoUpdateType autoType, + int autoUpdateInterval, final SingleResource resource) { if (null == resource) { return false; } - Map attMap = resource - .getResourceAttributesMap(); - if (null == attMap) { - return false; - } boolean status = false; - Set keySet = attMap.keySet(); - Iterator attItr = keySet.iterator(); - while (attItr.hasNext()) { - LocalResourceAttribute attribute = attMap.get(attItr.next()); - if (attribute.isAutomationInProgress()) { - status = true; - break; + // Invoke the native automation method + SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource + .getSimulatorResource(); + if (null != resourceServer) { + int autoId = -1; + try { + autoId = resourceServer.startResourceUpdation(autoType, + autoUpdateInterval, automationListener); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), new Date(), + Utility.getSimulatorErrorString(e, null)); + autoId = -1; } - } - return status; - } + if (-1 != autoId) { + // Automation request accepted. + changeResourceLevelAutomationStatus(resource, true); - private LocalResourceAttribute getAttributeWithGivenAutomationId( - SimulatorResource resource, int automationId) { - LocalResourceAttribute targetAttribute = null; - if (null != resource) { - Map attributeMap = resource - .getResourceAttributesMap(); - if (null != attributeMap) { - Set attNameSet = attributeMap.keySet(); - Iterator attNameItr = attNameSet.iterator(); - String attName; - LocalResourceAttribute attribute; - while (attNameItr.hasNext()) { - attName = attNameItr.next(); - if (null != attName) { - attribute = attributeMap.get(attName); - if (null != attribute) { - if (attribute.isAutomationInProgress() - && (attribute.getAutomationId() == automationId)) { - targetAttribute = attribute; - break; - } - } - } - } - } - } - return targetAttribute; - } + resource.setAutomationId(autoId); - public boolean startResourceAutomationUIRequest(final String resourceURI) { - if (null == resourceURI) { - return false; - } - boolean status = false; - SimulatorResource resource = getSimulatorResourceByURI(resourceURI); - if (null != resource) { - changeResourceLevelAutomationStatus(resource, true); - - // Invoke the native automation method - SimulatorResourceServer resourceServer = resource - .getResourceServer(); - if (null != resourceServer) { - // TODO: Temporarily handling the normal one-time automation for - // resources - int autoId = -1; - try { - autoId = resourceServer.startResourceAutomation( - AutomationType.NORMAL, automationListener); - } catch (SimulatorException e) { - Activator - .getDefault() - .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() + "]" - + e.code().toString() + "-" - + e.message()); - autoId = -1; - } - if (-1 == autoId) { - // Automation request failed and hence status is being - // rolled back - changeResourceLevelAutomationStatus(resource, false); - } else { - // Automation request accepted. - resource.setAutomationId(autoId); - - // Notify the UI listeners in a different thread. - Thread notifyThread = new Thread() { - public void run() { - resourceAutomationStartedUINotification(resourceURI); - }; + // Notify the UI listeners in a different thread. + Thread notifyThread = new Thread() { + public void run() { + UiListenerHandler.getInstance() + .resourceAutomationStartedUINotification( + resource); }; - notifyThread.setPriority(Thread.MAX_PRIORITY); - notifyThread.start(); + }; + notifyThread.setPriority(Thread.MAX_PRIORITY); + notifyThread.start(); - status = true; - } + status = true; } } return status; } - public boolean stopResourceAutomationUIRequest(final String resourceURI) { - SimulatorResource resource = getSimulatorResourceByURI(resourceURI); + public boolean stopResourceAutomationUIRequest(final SingleResource resource) { if (null == resource) { return false; } @@ -1515,129 +1395,297 @@ public class ResourceManager { if (-1 == autoId) { return false; } - SimulatorResourceServer resourceServer = resource.getResourceServer(); + SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource + .getSimulatorResource(); if (null == resourceServer) { return false; } // Call native method try { - resourceServer.stopAutomation(autoId); + resourceServer.stopUpdation(autoId); } catch (SimulatorException e) { Activator .getDefault() .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() + "]" - + e.code().toString() + "-" + e.message()); + .log(Level.ERROR.ordinal(), new Date(), + Utility.getSimulatorErrorString(e, null)); return false; } // Notify the UI Listeners. Invoke the automation complete callback. Thread stopThread = new Thread() { public void run() { - automationListener.onAutomationComplete(resourceURI, autoId); + automationListener.onUpdateComplete(resource.getResourceURI(), + autoId); } }; stopThread.start(); return true; } + private boolean isAnyAttributeInAutomation(SingleResource resource) { + if (null == resource || null == resource.getResourceRepresentation()) { + return false; + } + + Map attributes = resource + .getResourceRepresentation().getAttributes(); + if (null == attributes || 0 == attributes.size()) + return false; + + for (Map.Entry entry : attributes.entrySet()) { + if (entry.getValue().isAutoUpdateInProgress()) + return true; + } + + return false; + } + // Changes the automation state of the resource and its attributes - private void changeResourceLevelAutomationStatus( - SimulatorResource resource, boolean status) { - - Map attributeMap = resource - .getResourceAttributesMap(); - if (null != attributeMap) { - Set attrNameSet = attributeMap.keySet(); - Iterator attrNameItr = attrNameSet.iterator(); - String attrName; - LocalResourceAttribute attribute; - while (attrNameItr.hasNext()) { - attrName = attrNameItr.next(); - attribute = attributeMap.get(attrName); - if (null != attribute) { - attribute.setAutomationInProgress(status); - } - } + private void changeResourceLevelAutomationStatus(SingleResource resource, + boolean status) { + + if (null == resource || null == resource.getResourceRepresentation()) { + return; } + + Map attributes = resource + .getResourceRepresentation().getAttributes(); + if (null == attributes || 0 == attributes.size()) + return; + + for (Map.Entry entry : attributes.entrySet()) { + entry.getValue().setAutoUpdateState(status); + } + resource.setResourceAutomationInProgress(status); } - public boolean isResourceAutomationStarted(String resourceURI) { - boolean status = false; - if (null == resourceURI) { - return status; + private AttributeElement getAttributeWithGivenAutomationId( + SingleResource resource, int automationId) { + if (null == resource || null == resource.getResourceRepresentation()) { + return null; } - SimulatorResource resource = getSimulatorResourceByURI(resourceURI); - if (null != resource) { - status = resource.isResourceAutomationInProgress(); + Map attributes = resource + .getResourceRepresentation().getAttributes(); + if (null == attributes || 0 == attributes.size()) + return null; + + for (Map.Entry entry : attributes.entrySet()) { + if (automationId == entry.getValue().getAutoUpdateId()) + return entry.getValue(); } - return status; + + return null; } - public boolean isAttributeAutomationStarted(String resourceURI) { + public boolean isResourceAutomationStarted(SingleResource resource) { boolean status = false; - if (null == resourceURI) { - return status; - } - SimulatorResource resource = getSimulatorResourceByURI(resourceURI); if (null != resource) { - status = resource.isAttributeAutomationInProgress(); + status = resource.isResourceAutomationInProgress(); } return status; } - public LocalResourceAttribute getAttributeByResourceURI(String uri, - String attName) { - if (null == uri || null == attName) { - return null; - } - SimulatorResource resource = getSimulatorResourceByURI(uri); + public boolean isAttributeAutomationStarted(SingleResource resource) { if (null == resource) { - return null; - } - Map attMap = resource - .getResourceAttributesMap(); - if (null == attMap) { - return null; + return false; } - return attMap.get(attName); + return resource.isAttributeAutomationInProgress(); } - public void notifyObserverRequest(SimulatorResource res, int observerId) { - if (null == res) { + public void notifyObserverRequest(Resource resource, int observerId) { + if (null == resource) { return; } - SimulatorResourceServer server = res.getResourceServer(); - if (null == server) { + SimulatorResource simulatorResource = resource.getSimulatorResource(); + if (null == simulatorResource) { return; } try { - server.notifyObserver(observerId); + simulatorResource.notifyObserver(observerId); } catch (SimulatorException e) { Activator .getDefault() .getLogManager() - .log(Level.ERROR.ordinal(), - new Date(), - "[" + e.getClass().getSimpleName() + "]" - + e.code().toString() + "-" + e.message()); + .log(Level.ERROR.ordinal(), new Date(), + Utility.getSimulatorErrorString(e, null)); } } - public Image getImage(String resourceURI) { - if (null == resourceURI) { + public List getAllValuesOfAttribute(SimulatorResourceAttribute att) { + if (null == att) { return null; } - SimulatorResource resource = getSimulatorResourceByURI(resourceURI); - if (null == resource) { + + AttributeValue val = att.value(); + if (null == val) { return null; } - return Activator.getDefault().getImageRegistry() - .get(resource.getResourceType()); + + TypeInfo type = val.typeInfo(); + + AttributeProperty prop = att.property(); + if (null == prop) { + return null; + } + + List values = new ArrayList(); + + if (type.mType != ValueType.RESOURCEMODEL) { + if (type.mType == ValueType.ARRAY) { + if (type.mDepth == 1) { + ArrayProperty arrayProperty = prop.asArray(); + if (null != arrayProperty) { + AttributeProperty childProp = arrayProperty + .getElementProperty(); + switch (childProp.getType()) { + case INTEGER: + IntegerProperty intProperty = childProp + .asInteger(); + if (null != intProperty) { + values.addAll(getAllValues(intProperty, + Type.INTEGER)); + } + break; + case DOUBLE: + DoubleProperty dblProperty = childProp + .asDouble(); + if (null != dblProperty) { + values.addAll(getAllValues(dblProperty, + Type.DOUBLE)); + } + break; + case BOOLEAN: + BooleanProperty boolProperty = childProp + .asBoolean(); + if (null != boolProperty) { + values.addAll(getAllValues(boolProperty, + Type.BOOLEAN)); + } + break; + case STRING: + StringProperty stringProperty = childProp + .asString(); + if (null != stringProperty) { + values.addAll(getAllValues(stringProperty, + Type.STRING)); + } + break; + default: + break; + } + } + } + } else { + switch (prop.getType()) { + case INTEGER: + IntegerProperty intProperty = prop.asInteger(); + if (null != intProperty) { + values.addAll(getAllValues(intProperty, + Type.INTEGER)); + } + break; + case DOUBLE: + DoubleProperty dblProperty = prop.asDouble(); + if (null != dblProperty) { + values.addAll(getAllValues(dblProperty, Type.DOUBLE)); + } + break; + case BOOLEAN: + BooleanProperty boolProperty = prop.asBoolean(); + if (null != boolProperty) { + values.addAll(getAllValues(boolProperty, + Type.BOOLEAN)); + } + break; + case STRING: + StringProperty stringProperty = prop.asString(); + if (null != stringProperty) { + values.addAll(getAllValues(stringProperty, + Type.STRING)); + } + break; + default: + break; + } + } + } + + return values; + } + + public List getAllValues(IntegerProperty intProperty, + AttributeProperty.Type type) { + List values = new ArrayList(); + + if (intProperty.hasRange()) { + int min = (int) intProperty.min(); + int max = (int) intProperty.max(); + for (int iVal = min; iVal <= max; iVal++) { + values.add(String.valueOf(iVal)); + } + } else if (intProperty.hasValues()) { + for (Integer val : intProperty.getValues()) { + values.add(String.valueOf(val)); + } + } else { + // Adding the default value. + values.add(String.valueOf(intProperty.getDefaultValue())); + } + return values; + } + + public List getAllValues(DoubleProperty dblProperty, + AttributeProperty.Type type) { + NumberFormat formatter = NumberFormat.getInstance(); + List values = new ArrayList(); + + if (dblProperty.hasRange()) { + double min = (double) dblProperty.min(); + double max = (double) dblProperty.max(); + for (double val = min; val <= max; val += 0.1) { + formatter.setMaximumFractionDigits(1); + formatter.setMinimumFractionDigits(1); + values.add(formatter.format(val)); + } + } else if (dblProperty.hasValues()) { + for (Double val : dblProperty.getValues()) { + values.add(String.valueOf(val)); + } + } else { + // Adding the default value. + values.add(String.valueOf(dblProperty.getDefaultValue())); + } + return values; + } + + public List getAllValues(BooleanProperty boolProperty, + AttributeProperty.Type type) { + List values = new ArrayList(); + values.add("true"); + values.add("false"); + return values; + } + + public List getAllValues(StringProperty stringProperty, + AttributeProperty.Type type) { + List values = new ArrayList(); + + if (stringProperty.hasValues()) { + for (String val : stringProperty.getValues()) { + values.add(String.valueOf(val)); + } + } else { + // Adding the default value. + values.add(String.valueOf(stringProperty.getDefaultValue())); + } + return values; + } + + public int getResourceCount() { + return data.getResourceCount(); } public void shutdown() {