From 597658406ff7e03dfc8ed5f4f72126254dc138bc Mon Sep 17 00:00:00 2001 From: Minji Park Date: Fri, 25 Sep 2015 13:29:39 +0900 Subject: [PATCH] Align resource container Java bundle APIs with new naming scheme Updating Java bundle resource to follow the new naming scheme (handleSetAttributeRequest, handleGetAttributeRequest). Latest changes in the resource container APIs are now also updated in the Java related code. Change-Id: I7b28c830c2dc6f2dd1c7ad31405851b6d94def54 Signed-off-by: Markus Jung Signed-off-by: Minji Park Reviewed-on: https://gerrit.iotivity.org/gerrit/3031 Tested-by: jenkins-iotivity Reviewed-by: Hun-je Yeon Reviewed-by: Madan Lanka (cherry picked from commit ba2c9a84f7700c4778645bfd07e59d9bd8f40395) Reviewed-on: https://gerrit.iotivity.org/gerrit/3201 --- .../bundle/api/BaseActivator.java | 230 +++++++++++--------- .../bundle/api/BundleResource.java | 242 ++++++++++++--------- .../resourceContainer/examples/ContainerSample.cpp | 16 +- .../iotivity/bundle/hue/HueBundleActivator.java | 62 +++--- .../org/iotivity/bundle/hue/HueLightResource.java | 104 +++++---- .../src/HueSampleBundleActivator.cpp | 1 - .../examples/ResourceContainerConfig.xml | 6 +- .../resourceContainer/include/JavaBundleResource.h | 20 +- .../src/resourceContainer/src/BaseActivator.cpp | 8 +- .../resourceContainer/src/JavaBundleResource.cpp | 58 +++-- .../src/ResourceContainerImpl.cpp | 37 ++-- 11 files changed, 433 insertions(+), 351 deletions(-) diff --git a/service/resource-encapsulation/src/resourceContainer/bundle-java-api/src/main/java/org/iotivity/resourcecontainer/bundle/api/BaseActivator.java b/service/resource-encapsulation/src/resourceContainer/bundle-java-api/src/main/java/org/iotivity/resourcecontainer/bundle/api/BaseActivator.java index 11e3310..b8e2e88 100644 --- a/service/resource-encapsulation/src/resourceContainer/bundle-java-api/src/main/java/org/iotivity/resourcecontainer/bundle/api/BaseActivator.java +++ b/service/resource-encapsulation/src/resourceContainer/bundle-java-api/src/main/java/org/iotivity/resourcecontainer/bundle/api/BaseActivator.java @@ -24,114 +24,130 @@ import java.util.List; import java.util.Vector; /** - * The BaseActivator implements the native interface to the resource container. - * It loads the resource container library and provies native methods - * that can be used to register and unregister resources. + * The BaseActivator implements the native interface to the resource container. + * It loads the resource container library and provies native methods that can + * be used to register and unregister resources. */ public class BaseActivator implements BundleActivator { - private String bundleId; - private Vector bundleResources = new Vector(); - - /** - * Creates an instance of the BaseActivator - * @param bundleId unique bundle identifier (e.g., oic.bundle.hue) - */ - public BaseActivator(String bundleId) { - this.bundleId = bundleId; - - } - - static { - try { - System.loadLibrary("ResContainerLib"); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Bundle activation needs to be provided by the subclass. - */ - public void activateBundle() { - - } - - /** - * Deactivates the bundle and unregisters its resources. - */ - public void deactivateBundle() { - System.out.println("Deactivating bundle (Base Activator)."); - for(BundleResource bundleResource : bundleResources){ - unregisterResource(bundleResource); - } - } - - /** - * Registers a bundle resource at the resource container. - * @param resource bundle resource instance that should be made available as OIC resource - */ - public void registerResource(BundleResource resource) { - bundleResources.add(resource); - registerJavaResource(resource, resource.getAttributeKeys(), bundleId, - resource.getURI(), resource.getResourceType(), - resource.getName()); - } - - /** - * Wrapper to retrieve the resource configuration of the bundle resources. - * @return List of resource configurations. - */ - public List getConfiguredBundleResources() { - int configuredResources = getNumberOfConfiguredResources(bundleId); - - Vector configs = new Vector(); - - for (int i = 0; i < configuredResources; i++) { - String[] resourceParams = getConfiguredResourceParams(bundleId, i); - ResourceConfig config = new ResourceConfig(resourceParams); - configs.add(config); - - } - return configs; - } - - /** - * Unregisters a resource from the resource container. - */ - public void unregisterResource(BundleResource resource) { - bundleResources.remove(resource); - unregisterJavaResource(resource, resource.getURI()); - } - - /** - * Native method that calls to the resource container. - * @param attributes String array of attribute names - * @param bundleId unique bundle identifier - * @param uri Uri that should be used to register the resource - */ - private native void registerJavaResource(BundleResource resource, - String[] attributes, String bundleId, String uri, - String resourceType, String name); - - /** - * Native method that calls to the resource container. - * @param resource - * @param uri - */ - private native void unregisterJavaResource(BundleResource resource, String uri); - - /** - * Native method to retrieve the number of configured resources. - * @param bundleId unique bundle identifier - */ - private native int getNumberOfConfiguredResources(String bundleId); - - /** - * Native method to retrieve the configured resource parameters. - * @param bundleId unique bundle identifier - * @param resId get the resource params for a certain resource - */ - private native String[] getConfiguredResourceParams(String bundleId, - int resId); + private String bundleId; + private Vector bundleResources = new Vector(); + + /** + * Creates an instance of the BaseActivator + * + * @param bundleId + * unique bundle identifier (e.g., oic.bundle.hue) + */ + public BaseActivator(String bundleId) { + this.bundleId = bundleId; + } + + static { + try { + System.loadLibrary("rcs_container"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Bundle activation needs to be provided by the subclass. + */ + public void activateBundle() { + + } + + /** + * Deactivates the bundle and unregisters its resources. + */ + public void deactivateBundle() { + System.out.println("Deactivating bundle (Base Activator)."); + for (BundleResource bundleResource : bundleResources) { + unregisterResource(bundleResource); + } + } + + /** + * Registers a bundle resource at the resource container. + * + * @param resource + * bundle resource instance that should be made available as OIC + * resource + */ + public void registerResource(BundleResource resource) { + bundleResources.add(resource); + registerJavaResource(resource, resource.getAttributeKeys(), bundleId, + resource.getURI(), resource.getResourceType(), + resource.getName()); + } + + /** + * Wrapper to retrieve the resource configuration of the bundle resources. + * + * @return List of resource configurations. + */ + public List getConfiguredBundleResources() { + int configuredResources = getNumberOfConfiguredResources(bundleId); + + Vector configs = new Vector(); + + for (int i = 0; i < configuredResources; i++) { + String[] resourceParams = getConfiguredResourceParams(bundleId, i); + ResourceConfig config = new ResourceConfig(resourceParams); + configs.add(config); + + } + return configs; + } + + /** + * Unregisters a resource from the resource container. + */ + public void unregisterResource(BundleResource resource) { + bundleResources.remove(resource); + unregisterJavaResource(resource, resource.getURI()); + } + + /** + * Native method that calls to the resource container. + * + * @param attributes + * String array of attribute names + * @param bundleId + * unique bundle identifier + * @param uri + * Uri that should be used to register the resource + */ + private native void registerJavaResource(BundleResource resource, + String[] attributes, String bundleId, String uri, + String resourceType, String name); + + /** + * Native method that calls to the resource container. + * + * @param resource + * @param uri + */ + private native void unregisterJavaResource(BundleResource resource, + String uri); + + /** + * Native method to retrieve the number of configured resources. + * + * @param bundleId + * unique bundle identifier + */ + private native int getNumberOfConfiguredResources(String bundleId); + + /** + * Native method to retrieve the configured resource parameters. + * + * @param bundleId + * unique bundle identifier + * @param resId + * get the resource params for a certain resource + */ + private native String[] getConfiguredResourceParams(String bundleId, + int resId); } diff --git a/service/resource-encapsulation/src/resourceContainer/bundle-java-api/src/main/java/org/iotivity/resourcecontainer/bundle/api/BundleResource.java b/service/resource-encapsulation/src/resourceContainer/bundle-java-api/src/main/java/org/iotivity/resourcecontainer/bundle/api/BundleResource.java index 411a428..a4ed5e6 100644 --- a/service/resource-encapsulation/src/resourceContainer/bundle-java-api/src/main/java/org/iotivity/resourcecontainer/bundle/api/BundleResource.java +++ b/service/resource-encapsulation/src/resourceContainer/bundle-java-api/src/main/java/org/iotivity/resourcecontainer/bundle/api/BundleResource.java @@ -24,107 +24,149 @@ import java.util.HashMap; import java.util.Set; /** - * Basic BundleResource that should be used as a base class - * by a bundle resources. A concrete technology has - * to override the setAttribute and getAttribute method - * and map the according reads and writes to the technology specific - * messages. + * Basic BundleResource that should be used as a base class by a bundle + * resources. A concrete technology has to override the setAttribute and + * getAttribute method and map the according reads and writes to the technology + * specific messages. */ public abstract class BundleResource { - protected String m_name, m_uri, m_resourceType, m_address; - - protected HashMap m_attributes; - - /** - * Initialize the internal attribute structure. - */ - protected abstract void initAttributes(); - - /** - * Set the attribute (map to a send command for the according protocol) - * @param key name of the attribute to be set - * @param value new value of the attribute - */ - public abstract void setAttribute(String key, String value); - - /** - * Retrieve the attribute (map to read command for the according protocol) - * @param key name of the attribute to be read - * @return Value of the attribute - */ - public abstract String getAttribute(String key); - - /** - * Attribute keys provided through by the bundle resource. - * @return Name of attribute keys as string array - */ - public String[] getAttributeKeys() { - Set keys = m_attributes.keySet(); - return keys.toArray(new String[keys.size()]); - } - - /** - * Setter for the uri property - * @param uri URI of the resource - */ - public void setURI(String uri) { - this.m_uri = uri; - } - - /** - * Returns the URI of the resource - * @return Resource URI - */ - public String getURI() { - return m_uri; - } - - /** - * Sets the resource type property - * @param resourceType OIC resource type - */ - public void setResourceType(String resourceType) { - this.m_resourceType = resourceType; - } - - /** - * Getter for the resource type - * @return OIC resource type - */ - public String getResourceType() { - return m_resourceType; - } - - /** - * Sets the technology specific address information (e.g., ZigBee short or long identifier) - * @param address Resource address - */ - public void setAddress(String address) { - this.m_address = address; - } - - /** - * Returns the technology specific address information - * @return Resource address - */ - public String getAddress() { - return m_address; - } - - /** - * Sets the name property of the resource - * @param name Resource name - */ - public void setName(String name) { - this.m_name = name; - } - - /** - * Returns the name property of the resource - * @return Resource name - */ - public String getName() { - return m_name; - } + protected String m_name, m_uri, m_resourceType, m_address; + + protected HashMap m_attributes = new HashMap(); + + /** + * Initialize the internal attribute structure. + */ + protected abstract void initAttributes(); + + /** + * Set the attribute (map to a send command for the according protocol) + * + * @param key + * name of the attribute to be set + * @param value + * new value of the attribute + */ + protected final void setAttribute(String key, String value) { + m_attributes.put(key, value); + } + + /** + * Set the attribute (map to a send command for the according protocol) + * + * @param key + * name of the attribute to be set + * @param value + * new value of the attribute + */ + public abstract void handleSetAttributeRequest(String key, String value); + + /** + * Retrieve the attribute (only data) + * + * @param key + * name of the attribute to be read + * @return Value of the attribute + */ + protected final String getAttribute(String key) { + return m_attributes.get(key); + } + + /** + * Retrieve the attribute (map to read command) + * + * @param key + * name of the attribute to be set + * @param value + * new value of the attribute + */ + public abstract String handleGetAttributeRequest(String key); + + /** + * Attribute keys provided through by the bundle resource. + * + * @return Name of attribute keys as string array + */ + public String[] getAttributeKeys() { + Set keys = m_attributes.keySet(); + return keys.toArray(new String[keys.size()]); + } + + /** + * Setter for the uri property + * + * @param uri + * URI of the resource + */ + public void setURI(String uri) { + this.m_uri = uri; + } + + /** + * Returns the URI of the resource + * + * @return Resource URI + */ + public String getURI() { + return m_uri; + } + + /** + * Sets the resource type property + * + * @param resourceType + * OIC resource type + */ + public void setResourceType(String resourceType) { + this.m_resourceType = resourceType; + } + + /** + * Getter for the resource type + * + * @return OIC resource type + */ + public String getResourceType() { + return m_resourceType; + } + + /** + * Sets the technology specific address information (e.g., ZigBee short or + * long identifier) + * + * @param address + * Resource address + */ + public void setAddress(String address) { + this.m_address = address; + } + + /** + * Returns the technology specific address information + * + * @return Resource address + */ + public String getAddress() { + return m_address; + } + + /** + * Sets the name property of the resource + * + * @param name + * Resource name + */ + public void setName(String name) { + this.m_name = name; + } + + /** + * Returns the name property of the resource + * + * @return Resource name + */ + public String getName() { + return m_name; + } } diff --git a/service/resource-encapsulation/src/resourceContainer/examples/ContainerSample.cpp b/service/resource-encapsulation/src/resourceContainer/examples/ContainerSample.cpp index 3d9febc..af2dae4 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/ContainerSample.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/ContainerSample.cpp @@ -149,11 +149,11 @@ int main() getchar(); bundleParams["libraryPath"] = "."; std::string activator = "org.iotivity.bundle.hue.HueBundleActivator"; - container->addBundle("oic.bundle.hueJavaSample", "/hueJava", - "../../../../../../../../service/resource-encapsulation/src/resourceContainer/" \ - "examples/HueJavaSampleBundle/hue/target/hue-0.1-jar-with-dependencies.jar", - activator, - bundleParams); + container->addBundle("oic.bundle.hueJavaSample2", "/hueJava", + "../../../../../../../../service/resource-encapsulation/src/resourceContainer/" \ + "examples/HueJavaSampleBundle/hue/target/hue-0.1-jar-with-dependencies.jar", + activator, + bundleParams); bundles = container->listBundles(); cout << "\t>>> bundle list size : " << bundles.size() << endl; @@ -164,15 +164,15 @@ int main() cout << "\nPress enter to start java bundle " << endl; getchar(); - container->startBundle("oic.bundle.hueJavaSample"); + container->startBundle("oic.bundle.hueJavaSample2"); cout << "Press enter to stop java Bundle " << endl; getchar(); - container->stopBundle("oic.bundle.hueJavaSample"); + container->stopBundle("oic.bundle.hueJavaSample2"); cout << "Press enter to test remove java Bundle " << endl; getchar(); - container->removeBundle("oic.bundle.hueJavaSample"); + container->removeBundle("oic.bundle.hueJavaSample2"); bundles = container->listBundles(); cout << "\t>>> bundle list size : " << bundles.size() << endl; diff --git a/service/resource-encapsulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/src/main/java/org/iotivity/bundle/hue/HueBundleActivator.java b/service/resource-encapsulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/src/main/java/org/iotivity/bundle/hue/HueBundleActivator.java index 7be201f..6fc1fe8 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/src/main/java/org/iotivity/bundle/hue/HueBundleActivator.java +++ b/service/resource-encapsulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/src/main/java/org/iotivity/bundle/hue/HueBundleActivator.java @@ -6,35 +6,35 @@ import org.iotivity.resourcecontainer.bundle.api.BaseActivator; import org.iotivity.resourcecontainer.bundle.api.ResourceConfig; public class HueBundleActivator extends BaseActivator { - private HueConnector connector; - - public HueBundleActivator(String bundleId) { - super(bundleId); - } - - public void activateBundle() { - super.activateBundle(); - connector = new HueConnector(); - List resourceConfig = getConfiguredBundleResources(); - - for (ResourceConfig config : resourceConfig) { - - HueLightResource hueLightResource = new HueLightResource(connector, - config.getName(), config.getURI(), - config.getResourceType(), config.getAddress()); - - System.out.println("Registration of HueLightresource"); - registerResource(hueLightResource); - } - } - - public void deactivateBundle() { - System.out.println("Deactivate bundle called."); - super.deactivateBundle(); - } - - // test call - public static void main(String[] args) { - HueBundleActivator activator = new HueBundleActivator("oic.hue.bundle"); - } + private HueConnector connector; + + public HueBundleActivator(String bundleId) { + super(bundleId); + } + + public void activateBundle() { + super.activateBundle(); + connector = new HueConnector(); + List resourceConfig = getConfiguredBundleResources(); + + for (ResourceConfig config : resourceConfig) { + + HueLightResource hueLightResource = new HueLightResource(connector, + config.getName(), config.getURI(), + config.getResourceType(), config.getAddress()); + + System.out.println("Registration of Hue light resource " + config); + registerResource(hueLightResource); + } + } + + public void deactivateBundle() { + System.out.println("Deactivate bundle called."); + super.deactivateBundle(); + } + + // test call + public static void main(String[] args) { + HueBundleActivator activator = new HueBundleActivator("oic.hue.bundle"); + } } diff --git a/service/resource-encapsulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/src/main/java/org/iotivity/bundle/hue/HueLightResource.java b/service/resource-encapsulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/src/main/java/org/iotivity/bundle/hue/HueLightResource.java index 32f3fd5..d65b5a7 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/src/main/java/org/iotivity/bundle/hue/HueLightResource.java +++ b/service/resource-encapsulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/src/main/java/org/iotivity/bundle/hue/HueLightResource.java @@ -10,67 +10,65 @@ import org.iotivity.resourcecontainer.bundle.api.BundleResource; * @author iotivity */ public class HueLightResource extends BundleResource { - private HueConnector m_hueConnector; + private HueConnector m_hueConnector; - public HueLightResource() { - initAttributes(); - m_resourceType = "oic.light.control"; - - } + public HueLightResource() { + initAttributes(); + m_resourceType = "oic.r.light.control"; - public HueLightResource(HueConnector hueConnector, String name, String uri, - String resourceType, String address) { - this(); - this.m_hueConnector = hueConnector; - m_name = name; - m_uri = uri; - m_resourceType = resourceType; - m_address = address; - } + } - protected void initAttributes() { - m_attributes = new HashMap(); - m_attributes.put("on-off", "true"); - m_attributes.put("color", "0"); - m_attributes.put("dim", "0"); - } + public HueLightResource(HueConnector hueConnector, String name, String uri, + String resourceType, String address) { + this(); + this.m_hueConnector = hueConnector; + m_name = name; + m_uri = uri; + m_resourceType = resourceType; + m_address = address; + } - public void setAttribute(String key, String value) { - System.out.println("Set attribute called - key: " + key + ", value: " - + value + " transmitting now."); + protected void initAttributes() { + m_attributes.put("on-off", "true"); + m_attributes.put("color", "0"); + m_attributes.put("dim", "0"); + } - if ("on-off".equals(value)) { - m_hueConnector.transmit(m_address + "/state", "{\"on\":" + value - + "}"); - } + public void handleSetAttributeRequest(String key, String value) { + System.out.println("Set attribute called - key: " + key + ", value: " + + value + " transmitting now."); - if ("dim".equals(value)) { - m_hueConnector.transmit(m_address + "/state", "{\"bri\":" + value - + "}"); - } + if ("on-off".equals(value)) { + m_hueConnector.transmit(m_address + "/state", "{\"on\":" + value + + "}"); + } - if ("color".equals(value)) { - m_hueConnector.transmit(m_address + "/state", "{\"hue\":" + value - + "}"); - } - m_attributes.put(key, value); - } + if ("dim".equals(value)) { + m_hueConnector.transmit(m_address + "/state", "{\"bri\":" + value + + "}"); + } - public String getAttribute(String key) { - - // map key to hue address - // read from Hue gateway, parse resource representation and return - // attribute - //m_hueConnector.read(m_address); - return m_attributes.get(key); - } + if ("color".equals(value)) { + m_hueConnector.transmit(m_address + "/state", "{\"hue\":" + value + + "}"); + } + this.setAttribute(key, value); + } - @Override - public String toString() { - return "HueLightResource [m_hueConnector=" + m_hueConnector - + ", m_name=" + m_name + ", m_uri=" + m_uri - + ", m_resourceType=" + m_resourceType + ", m_address=" - + m_address + ", m_attributes=" + m_attributes + "]"; - } + public String handleGetAttributeRequest(String key) { + // map key to hue address + // read from Hue gateway, parse resource representation and return + // attribute + // m_hueConnector.read(m_address); + return this.getAttribute(key); + } + + @Override + public String toString() { + return "HueLightResource [m_hueConnector=" + m_hueConnector + + ", m_name=" + m_name + ", m_uri=" + m_uri + + ", m_resourceType=" + m_resourceType + ", m_address=" + + m_address + ", m_attributes=" + m_attributes + "]"; + } } diff --git a/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/src/HueSampleBundleActivator.cpp b/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/src/HueSampleBundleActivator.cpp index 2cb696f..903f6ba 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/src/HueSampleBundleActivator.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/src/HueSampleBundleActivator.cpp @@ -80,7 +80,6 @@ void HueSampleBundleActivator::createResource(resourceInfo resourceInfo) static int lightCount = 1; BundleResource::Ptr hueLight = std::make_shared< HueLight >(m_connector, resourceInfo.address); resourceInfo.uri = "/hue/light/" + std::to_string(lightCount++); - std::cout << "Registering resource " << resourceInfo.uri << std::endl; hueLight->m_bundleId = m_bundleId; hueLight->m_uri = resourceInfo.uri; hueLight->m_resourceType = resourceInfo.resourceType; diff --git a/service/resource-encapsulation/src/resourceContainer/examples/ResourceContainerConfig.xml b/service/resource-encapsulation/src/resourceContainer/examples/ResourceContainerConfig.xml index a8548a1..347e737 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/ResourceContainerConfig.xml +++ b/service/resource-encapsulation/src/resourceContainer/examples/ResourceContainerConfig.xml @@ -85,7 +85,7 @@ - + \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/include/JavaBundleResource.h b/service/resource-encapsulation/src/resourceContainer/include/JavaBundleResource.h index 54cb40e..0dc750b 100644 --- a/service/resource-encapsulation/src/resourceContainer/include/JavaBundleResource.h +++ b/service/resource-encapsulation/src/resourceContainer/include/JavaBundleResource.h @@ -42,19 +42,23 @@ namespace OIC jobjectArray attributes); virtual ~JavaBundleResource(); - virtual RCSResourceAttributes& getAttributes(); + void handleSetAttributeRequest(const std::string& key, + RCSResourceAttributes::Value&&); - virtual void setAttribute(std::string key, RCSResourceAttributes::Value&&); + RCSResourceAttributes::Value handleGetAttributeRequest(const std::string& key); + + virtual void handleSetAttributesRequest(RCSResourceAttributes &attrs); + + virtual RCSResourceAttributes& handleGetAttributesRequest(); - virtual RCSResourceAttributes::Value getAttribute(const std::string& key); virtual void initAttributes(); private: // needs to be a GlobalRef - jobject bundleResource; - jobjectArray attributes; - jclass bundleResourceClass; - jmethodID attributeSetter; - jmethodID attributeGetter; + jobject m_bundleResource; + jobjectArray m_attributes; + jclass m_bundleResourceClass; + jmethodID m_attributeSetRequestHandler; + jmethodID m_attributeGetRequestHandler; string m_bundleId; }; } diff --git a/service/resource-encapsulation/src/resourceContainer/src/BaseActivator.cpp b/service/resource-encapsulation/src/resourceContainer/src/BaseActivator.cpp index 69917fc..e5cf71c 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/BaseActivator.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/BaseActivator.cpp @@ -22,11 +22,10 @@ #include #include "org_iotivity_resourcecontainer_bundle_api_BaseActivator.h" #include "JavaBundleResource.h" -#include "ResourceContainerImpl.h" using namespace OIC::Service; -std::map< string, JavaBundleResource * > java_resources; +std::map< string, BundleResource::Ptr > java_resources; /* * Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator @@ -43,8 +42,8 @@ Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_registerJavaResourc const char *str_resourceType = env->GetStringUTFChars(resourceType, 0); const char *str_res_name = env->GetStringUTFChars(res_name, 0); - JavaBundleResource *javaBundleResource = new JavaBundleResource(env, obj, bundleResource, - str_bundleId, attributes); + BundleResource::Ptr javaBundleResource = std::make_shared< JavaBundleResource > + (env, obj, bundleResource, str_bundleId, attributes); ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance(); javaBundleResource->m_uri = string(str_uri, strlen(str_uri)); @@ -53,7 +52,6 @@ Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_registerJavaResourc container->registerResource(javaBundleResource); java_resources[str_uri] = javaBundleResource; - } /* diff --git a/service/resource-encapsulation/src/resourceContainer/src/JavaBundleResource.cpp b/service/resource-encapsulation/src/resourceContainer/src/JavaBundleResource.cpp index d4adf66..47228ee 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/JavaBundleResource.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/JavaBundleResource.cpp @@ -57,15 +57,15 @@ JavaBundleResource::JavaBundleResource(JNIEnv *env, jobject obj, jobject bundleR m_bundleId = bundleId; - this->bundleResource = env->NewGlobalRef(bundleResource); + this->m_bundleResource = env->NewGlobalRef(bundleResource); - bundleResourceClass = env->GetObjectClass(bundleResource); + m_bundleResourceClass = env->GetObjectClass(bundleResource); - attributeSetter = env->GetMethodID(bundleResourceClass, "setAttribute", - "(Ljava/lang/String;Ljava/lang/String;)V"); + m_attributeSetRequestHandler = env->GetMethodID(m_bundleResourceClass, + "handleSetAttributeRequest", "(Ljava/lang/String;Ljava/lang/String;)V"); - attributeGetter = env->GetMethodID(bundleResourceClass, "getAttribute", - "(Ljava/lang/String;)Ljava/lang/String;"); + m_attributeGetRequestHandler = env->GetMethodID(m_bundleResourceClass, + "handleGetAttributeRequest", "(Ljava/lang/String;)Ljava/lang/String;"); } @@ -74,12 +74,8 @@ JavaBundleResource::~JavaBundleResource() } -RCSResourceAttributes &JavaBundleResource::getAttributes() -{ - return BundleResource::getAttributes(); -} - -RCSResourceAttributes::Value JavaBundleResource::getAttribute(const std::string &attributeName) +RCSResourceAttributes::Value JavaBundleResource::handleGetAttributeRequest( + const std::string &attributeName) { JavaVM *vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId); @@ -90,18 +86,20 @@ RCSResourceAttributes::Value JavaBundleResource::getAttribute(const std::string { if (vm->AttachCurrentThread((void **) &env, NULL) != 0) { - OC_LOG_V(ERROR, CONTAINER_TAG, "[JavaBundleResource::getAttribute] Failed to attach "); + OC_LOG_V(ERROR, CONTAINER_TAG, + "[JavaBundleResource::handleGetAttributeRequest] Failed to attach "); } } else if (envStat == JNI_EVERSION) { - OC_LOG_V(ERROR, CONTAINER_TAG, "[JavaBundleResource::getAttribute] Env: version not supported"); + OC_LOG_V(ERROR, CONTAINER_TAG, + "[JavaBundleResource::handleGetAttributeRequest] Env: version not supported"); } jstring attrName = env->NewStringUTF(attributeName.c_str()); - jstring returnString = (jstring) env->CallObjectMethod(bundleResource, attributeGetter, - attrName); + jstring returnString = (jstring) env->CallObjectMethod(m_bundleResource, + m_attributeGetRequestHandler, attrName); const char *js = env->GetStringUTFChars(returnString, NULL); std::string val(js); @@ -111,7 +109,7 @@ RCSResourceAttributes::Value JavaBundleResource::getAttribute(const std::string return BundleResource::getAttribute(attributeName); } -void JavaBundleResource::setAttribute(std::string attributeName, +void JavaBundleResource::handleSetAttributeRequest(const std::string &attributeName, RCSResourceAttributes::Value &&value) { JavaVM *vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId); @@ -123,19 +121,39 @@ void JavaBundleResource::setAttribute(std::string attributeName, { if (vm->AttachCurrentThread((void **) &env, NULL) != 0) { - OC_LOG_V(ERROR, CONTAINER_TAG, "[JavaBundleResource::setAttribute] Failed to attach "); + OC_LOG_V(ERROR, CONTAINER_TAG, + "[JavaBundleResource::handleSetAttributeRequest] Failed to attach "); } } else if (envStat == JNI_EVERSION) { - OC_LOG_V(ERROR, CONTAINER_TAG, "[JavaBundleResource::setAttribute] Env: version not supported "); + OC_LOG_V(ERROR, CONTAINER_TAG, + "[JavaBundleResource::handleSetAttributeRequest] Env: version not supported "); } jstring attrName = env->NewStringUTF(attributeName.c_str()); jstring val = env->NewStringUTF(value.toString().c_str()); - env->CallObjectMethod(bundleResource, attributeSetter, attrName, val); + env->CallObjectMethod(m_bundleResource, m_attributeSetRequestHandler, attrName, val); BundleResource::setAttribute(attributeName, std::move(value)); } + + +void JavaBundleResource::handleSetAttributesRequest(RCSResourceAttributes &attrs){ + for (RCSResourceAttributes::iterator it = attrs.begin(); it != attrs.end(); ++it) + { + handleSetAttributeRequest(it->key(),std::move(it->value())); + } +} + +RCSResourceAttributes & JavaBundleResource::handleGetAttributesRequest(){ + RCSResourceAttributes ret; + std::list attrsNames = getAttributeNames(); + for(std::list::iterator iterator = attrsNames.begin(); + iterator != attrsNames.end(); ++iterator ){ + ret[*iterator] = handleGetAttributeRequest(*iterator); + } + return ret; +} #endif diff --git a/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp b/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp index cc761ab..364d27d 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp @@ -92,8 +92,10 @@ namespace OIC bundles[i][BUNDLE_LIBRARY_PATH]); } - OC_LOG_V(INFO, CONTAINER_TAG, "Init Bundle:(%s)", std::string(bundles[i][BUNDLE_ID] + ";" + - bundles[i][BUNDLE_PATH]).c_str()); + OC_LOG_V(INFO, CONTAINER_TAG, "Init Bundle:(%s)", + std::string(bundles[i][BUNDLE_ID] + ";" + + bundles[i][BUNDLE_PATH]).c_str()); + registerBundle(bundleInfo); activateBundle(bundleInfo); } @@ -284,8 +286,9 @@ namespace OIC std::bind(&ResourceContainerImpl::setRequestHandler, this, std::placeholders::_1, std::placeholders::_2)); - OC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)", std::string(strUri + ", " + - strResourceType).c_str()); + OC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)", + std::string(strUri + ", " + + strResourceType).c_str()); if (m_config->isHasInput(resource->m_bundleId)) { @@ -407,7 +410,8 @@ namespace OIC void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri) { - OC_LOG_V(INFO, CONTAINER_TAG, "notification from (%s)", std::string(strResourceUri + ".").c_str()); + OC_LOG_V(INFO, CONTAINER_TAG, + "notification from (%s)", std::string(strResourceUri + ".").c_str()); if (m_mapServers.find(strResourceUri) != m_mapServers.end()) { @@ -424,7 +428,8 @@ namespace OIC RCSResourceObject::Ptr ResourceContainerImpl::buildResourceObject(const std::string &strUri, const std::string &strResourceType) { - return RCSResourceObject::Builder(strUri, strResourceType, "oic.if.baseline").setObservable( + return RCSResourceObject::Builder(strUri, strResourceType, + "oic.if.baseline").setObservable( true).setDiscoverable(true).build(); } @@ -487,8 +492,9 @@ namespace OIC ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params[BUNDLE_LIBRARY_PATH]); } - OC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)", std::string(bundleInfo->getID() + "; " + - bundleInfo->getPath()).c_str()); + OC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)", + std::string(bundleInfo->getID() + "; " + + bundleInfo->getPath()).c_str()); registerBundle(bundleInfo); } @@ -819,7 +825,6 @@ namespace OIC { OC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)", std::string(bundleInfo->getID()).c_str()); - JavaVM *jvm; JNIEnv *env; JavaVMInitArgs vm_args; @@ -831,8 +836,9 @@ namespace OIC { fclose(file); - OC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)", std::string(bundleInfo->getPath() + - " available.").c_str()); + OC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)", + std::string(bundleInfo->getPath() + + " available.").c_str()); } else { @@ -848,7 +854,8 @@ namespace OIC strcpy(classpath, "-Djava.class.path="); strcat(classpath, bundleInfo->getPath().c_str()); - OC_LOG(INFO, CONTAINER_TAG, std::string("Configured classpath: ").append(classpath).c_str()); + OC_LOG(INFO, CONTAINER_TAG, + std::string("Configured classpath: ").append(classpath).c_str()); options[1].optionString = classpath; @@ -857,7 +864,8 @@ namespace OIC strcat(libraryPath, bundleInfo->getLibraryPath().c_str()); options[2].optionString = libraryPath; - OC_LOG(INFO, CONTAINER_TAG, std::string("Configured library path: ").append(libraryPath).c_str()); + OC_LOG(INFO, CONTAINER_TAG, + std::string("Configured library path: ").append(libraryPath).c_str()); vm_args.version = JNI_VERSION_1_4; vm_args.options = options; @@ -903,7 +911,6 @@ namespace OIC OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)", std::string( bundleInfoInternal->getID() + " activate bundle method not found ").c_str()); - return; } bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod); @@ -916,7 +923,6 @@ namespace OIC OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)", std::string( bundleInfoInternal->getID() + " deactivate bundle method not found ").c_str()); - return; } @@ -936,6 +942,7 @@ namespace OIC m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo); + OC_LOG(INFO, CONTAINER_TAG, "Bundle registered"); } -- 2.7.4