1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 package org.iotivity.resourcecontainer.bundle.api;
23 import java.util.List;
24 import java.util.Vector;
27 * The BaseActivator implements the native interface to the resource container.
28 * It loads the resource container library and provies native methods that can
29 * be used to register and unregister resources.
31 public class BaseActivator implements BundleActivator {
32 private String bundleId;
33 private Vector<BundleResource> bundleResources = new Vector<BundleResource>();
36 * Creates an instance of the BaseActivator
39 * unique bundle identifier (e.g., oic.bundle.hue)
41 public BaseActivator(String bundleId) {
42 this.bundleId = bundleId;
47 System.loadLibrary("rcs_container");
48 } catch (Exception e) {
54 * Bundle activation needs to be provided by the subclass.
56 public void activateBundle() {
61 * Deactivates the bundle and unregisters its resources.
63 public void deactivateBundle() {
64 System.out.println("Deactivating bundle (Base Activator).");
65 for (BundleResource bundleResource : bundleResources) {
66 unregisterResource(bundleResource);
71 * Registers a bundle resource at the resource container.
74 * bundle resource instance that should be made available as OIC
77 public void registerResource(BundleResource resource) {
78 bundleResources.add(resource);
79 registerJavaResource(resource, resource.getAttributeKeys(), bundleId,
80 resource.getURI(), resource.getResourceType(),
85 * Wrapper to retrieve the resource configuration of the bundle resources.
87 * @return List of resource configurations.
89 public List<ResourceConfig> getConfiguredBundleResources() {
90 int configuredResources = getNumberOfConfiguredResources(bundleId);
92 Vector<ResourceConfig> configs = new Vector<ResourceConfig>();
94 for (int i = 0; i < configuredResources; i++) {
95 String[] resourceParams = getConfiguredResourceParams(bundleId, i);
96 ResourceConfig config = new ResourceConfig(resourceParams);
104 * Unregisters a resource from the resource container.
106 public void unregisterResource(BundleResource resource) {
107 bundleResources.remove(resource);
108 unregisterJavaResource(resource, resource.getURI());
112 * Native method that calls to the resource container.
115 * String array of attribute names
117 * unique bundle identifier
119 * Uri that should be used to register the resource
121 private native void registerJavaResource(BundleResource resource,
122 String[] attributes, String bundleId, String uri,
123 String resourceType, String name);
126 * Native method that calls to the resource container.
131 private native void unregisterJavaResource(BundleResource resource,
135 * Native method to retrieve the number of configured resources.
138 * unique bundle identifier
140 private native int getNumberOfConfiguredResources(String bundleId);
143 * Native method to retrieve the configured resource parameters.
146 * unique bundle identifier
148 * get the resource params for a certain resource
150 private native String[] getConfiguredResourceParams(String bundleId,