Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-container / 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 that can
29  * 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          * 
38          * @param bundleId
39          *            unique bundle identifier (e.g., oic.bundle.hue)
40          */
41         public BaseActivator(String bundleId) {
42                 this.bundleId = bundleId;
43         }
44
45         static {
46                 try {
47                         System.loadLibrary("rcs_container");
48                 } catch (Exception e) {
49                         e.printStackTrace();
50                 }
51         }
52
53         /**
54          * Bundle activation needs to be provided by the subclass.
55          */
56         public void activateBundle() {
57
58         }
59
60         /**
61          * Deactivates the bundle and unregisters its resources.
62          */
63         public void deactivateBundle() {
64                 System.out.println("Deactivating bundle (Base Activator).");
65                 for (BundleResource bundleResource : bundleResources) {
66                         unregisterResource(bundleResource);
67                 }
68         }
69
70         /**
71          * Registers a bundle resource at the resource container.
72          * 
73          * @param resource
74          *            bundle resource instance that should be made available as OIC
75          *            resource
76          * @return int
77          *       0        in case of an success
78          *       -EEXIST  when the resource already exists and was not registered
79          *       -EINVAL  when it was not possible to create such a resource
80          */
81         public int registerResource(BundleResource resource) {
82                 bundleResources.add(resource);
83                 return registerJavaResource(resource, resource.getAttributeKeys(), bundleId,
84                                 resource.getURI(), resource.getResourceType(),
85                                 resource.getName());
86         }
87
88         /**
89          * Wrapper to retrieve the resource configuration of the bundle resources.
90          * 
91          * @return List of resource configurations.
92          */
93         public List<ResourceConfig> getConfiguredBundleResources() {
94                 int configuredResources = getNumberOfConfiguredResources(bundleId);
95
96                 Vector<ResourceConfig> configs = new Vector<ResourceConfig>();
97
98                 for (int i = 0; i < configuredResources; i++) {
99                         String[] resourceParams = getConfiguredResourceParams(bundleId, i);
100                         ResourceConfig config = new ResourceConfig(resourceParams);
101                         configs.add(config);
102
103                 }
104                 return configs;
105         }
106
107         /**
108          * Unregisters a resource from the resource container.
109          */
110         public void unregisterResource(BundleResource resource) {
111                 bundleResources.remove(resource);
112                 unregisterJavaResource(resource, resource.getURI());
113         }
114
115         /**
116          * Native method that calls to the resource container.
117          * 
118          * @param attributes
119          *            String array of attribute names
120          * @param bundleId
121          *            unique bundle identifier
122          * @param uri
123          *            Uri that should be used to register the resource
124          *
125          * @return int
126          *       0        in case of an success
127          *       -EEXIST  when the resource already exists and was not registered
128          *       -EINVAL  when it was not possible to create such a resource
129          */
130         private native int registerJavaResource(BundleResource resource,
131                         String[] attributes, String bundleId, String uri,
132                         String resourceType, String name);
133
134         /**
135          * Native method that calls to the resource container.
136          * 
137          * @param resource
138          * @param uri
139          */
140         private native void unregisterJavaResource(BundleResource resource,
141                         String uri);
142
143         /**
144          * Native method to retrieve the number of configured resources.
145          * 
146          * @param bundleId
147          *            unique bundle identifier
148          */
149         private native int getNumberOfConfiguredResources(String bundleId);
150
151         /**
152          * Native method to retrieve the configured resource parameters.
153          * 
154          * @param bundleId
155          *            unique bundle identifier
156          * @param resId
157          *            get the resource params for a certain resource
158          */
159         private native String[] getConfiguredResourceParams(String bundleId,
160                         int resId);
161
162 }