Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceContainer / bundle-java-api / src / main / java / org / iotivity / resourcecontainer / bundle / api / BaseActivator.java
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 package org.iotivity.resourcecontainer.bundle.api;
22
23 import java.util.List;
24 import java.util.Vector;
25
26 /**
27  * The BaseActivator implements the native interface to the resource container. 
28  * It loads the resource container library and provies native methods
29  * that can be used to register and unregister resources.
30  */
31 public class BaseActivator implements BundleActivator {
32     private String bundleId;
33     private Vector<BundleResource> bundleResources = new Vector<BundleResource>();
34
35     /**
36      * Creates an instance of the BaseActivator
37      * @param bundleId unique bundle identifier (e.g., oic.bundle.hue)
38      */
39     public BaseActivator(String bundleId) {
40         this.bundleId = bundleId;
41         
42     }
43
44     static {
45         try {
46             System.loadLibrary("ResContainerLib");
47         } catch (Exception e) {
48             e.printStackTrace();
49         }
50     }
51
52     /**
53      * Bundle activation needs to be provided by the subclass.
54      */
55     public void activateBundle() {
56
57     }
58
59     /**
60      * Deactivates the bundle and unregisters its resources.
61      */
62     public void deactivateBundle() {
63         System.out.println("Deactivating bundle (Base Activator).");
64         for(BundleResource bundleResource : bundleResources){
65             unregisterResource(bundleResource);
66         }
67     }
68
69     /**
70      * Registers a bundle resource at the resource container.
71      * @param resource bundle resource instance that should be made available as OIC resource 
72      */
73     public void registerResource(BundleResource resource) {
74         bundleResources.add(resource);
75         registerJavaResource(resource, resource.getAttributeKeys(), bundleId,
76                 resource.getURI(), resource.getResourceType(),
77                 resource.getName());
78     }
79
80     /**
81      * Wrapper to retrieve the resource configuration of the bundle resources.
82      * @return List of resource configurations.     
83      */
84     public List<ResourceConfig> getConfiguredBundleResources() {
85         int configuredResources = getNumberOfConfiguredResources(bundleId);
86
87         Vector<ResourceConfig> configs = new Vector<ResourceConfig>();
88
89         for (int i = 0; i < configuredResources; i++) {
90             String[] resourceParams = getConfiguredResourceParams(bundleId, i);
91             ResourceConfig config = new ResourceConfig(resourceParams);
92             configs.add(config);
93
94         }
95         return configs;
96     }
97
98     /**
99      * Unregisters a resource from the resource container.
100      */
101     public void unregisterResource(BundleResource resource) {
102         bundleResources.remove(resource);
103         unregisterJavaResource(resource, resource.getURI());
104     }
105
106     /**
107      * Native method that calls to the resource container.
108      * @param attributes String array of attribute names
109      * @param bundleId unique bundle identifier
110      * @param uri Uri that should be used to register the resource
111      */
112     private native void registerJavaResource(BundleResource resource,
113             String[] attributes, String bundleId, String uri,
114             String resourceType, String name);
115
116     /**
117      * Native method that calls to the resource container.
118      * @param resource 
119      * @param uri
120      */
121     private native void unregisterJavaResource(BundleResource resource, String uri);
122
123     /**
124      * Native method to retrieve the number of configured resources.
125      * @param bundleId unique bundle identifier
126      */
127     private native int getNumberOfConfiguredResources(String bundleId);
128
129     /**
130      * Native method to retrieve the configured resource parameters.
131      * @param bundleId unique bundle identifier 
132      * @param resId get the resource params for a certain resource
133      */
134     private native String[] getConfiguredResourceParams(String bundleId,
135             int resId);
136
137 }