2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.oic.simulator.server;
19 import java.util.Vector;
21 import org.oic.simulator.InvalidArgsException;
22 import org.oic.simulator.NoSupportException;
23 import org.oic.simulator.SimulatorException;
24 import org.oic.simulator.SimulatorResourceModel;
27 * This class serves as a base class type for all the resources in the
28 * simulator. It provides APIs to get and update the resource details, and
29 * register listeners to get notifications for resource model changes and
30 * observe requests from clients, and APIs to register/ unregister the resource
33 public class SimulatorResource {
35 private native void dispose();
37 protected long mNativeHandle;
39 protected SimulatorResource() {
43 protected void finalize() throws Throwable {
46 } catch (Throwable t) {
54 * Enum to represent the type of resource.
61 * Enum to represent the update type of automation.
63 public enum AutoUpdateType {
68 * API which indicates whether the resource is collection or single
71 * @return True if the resource is collection, otherwise false.
73 * @throws SimulatorException
74 * This exception will be thrown if the native resource object
75 * does not exist or for some general errors.
77 public boolean isCollection() {
78 return (this instanceof SimulatorCollectionResource);
82 * API to get the type which indicates whether resource is single or
83 * collection resource.
85 * @return Type of resource.
87 * @throws SimulatorException
88 * This exception will be thrown if the native resource object
89 * does not exist or for some general errors.
91 public Type getType() {
92 if (this instanceof SimulatorSingleResource) {
95 return Type.COLLECTION;
100 * API to get the name of the resource.
102 * @return Name of the resource.
104 * @throws SimulatorException
105 * This exception will be thrown if the native resource object
106 * does not exist or for some general errors.
108 public native String getName() throws SimulatorException;
111 * API to get the resource URI.
113 * @return Resource URI.
115 * @throws SimulatorException
116 * This exception will be thrown if the native resource object
117 * does not exist or for some general errors.
119 public native String getURI() throws SimulatorException;
122 * API to get the resource type.
124 * @return Resource type.
126 * @throws SimulatorException
127 * This exception will be thrown if the native resource object
128 * does not exist or for some general errors.
130 public native String getResourceType() throws SimulatorException;
133 * API to get the interfaces resource is bound with.
135 * @return Interface types.
137 * @throws SimulatorException
138 * This exception will be thrown if the native resource object
139 * does not exist or for some general errors.
141 public native Vector<String> getInterface() throws SimulatorException;
144 * API to get the observable state of resource.
146 * @return Observable state - true if resource is observable, otherwise
149 * @throws SimulatorException
150 * This exception will be thrown if the native resource object
151 * does not exist or for some general errors.
153 public native boolean isObservable() throws SimulatorException;
156 * API to get the discoverable state of resource.
158 * @return Discoverable state - true if resource is discoverable, otherwise
161 * @throws SimulatorException
162 * This exception will be thrown if the native resource object
163 * does not exist or for some general errors.
165 public native boolean isDiscoverable() throws SimulatorException;
168 * API to get the start state of resource.
170 * @return Start state - true if resource is started, otherwise false.
172 * @throws SimulatorException
173 * This exception will be thrown if the native resource object
174 * does not exist or for some general errors.
176 public native boolean isStarted() throws SimulatorException;
179 * API to set the name of the resource.
184 * @throws InvalidArgsException
185 * This exception will be thrown if the resource name is
187 * @throws SimulatorException
188 * This exception will be thrown if the native resource object
189 * does not exist or for some general errors.
191 public native void setName(String name) throws InvalidArgsException,
195 * API to set the resource URI.
200 * @throws InvalidArgsException
201 * This exception will be thrown if the resource URI is invalid.
202 * @throws SimulatorException
203 * A resource needs to be stopped by calling the stop() before
204 * calling this API. This exception will be thrown if the native
205 * resource object has not yet been stopped.
207 public native void setURI(String uri) throws InvalidArgsException,
211 * API to set the resource type.
213 * @param resourceType
214 * Resource type string.
216 * @throws InvalidArgsException
217 * This exception will be thrown if the resource type is
219 * @throws SimulatorException
220 * This exception will be thrown if the native resource object
221 * does not exist or for some general errors.
223 public native void setResourceType(String resourceType)
224 throws InvalidArgsException, SimulatorException;
227 * API to set interface to resource. Resource should be stopped before
230 * @param interfaceType
231 * Interface to be set.
233 * @throws InvalidArgsException
234 * This exception will be thrown if the interface type is
236 * @throws SimulatorException
237 * This exception will be thrown if the native resource object
238 * does not exist or resource is still running or for some
241 public native void setInterface(String interfaceType)
242 throws InvalidArgsException, SimulatorException;
245 * API to set a list of interfaces to resource. Resource should be stopped
246 * before calling this API.
248 * @param interfaceTypes
249 * Interfaces to be set.
251 * @throws InvalidArgsException
252 * This exception will be thrown if the interface type is
254 * @throws SimulatorException
255 * This exception will be thrown if the native resource object
256 * does not exist or resource is still running or for some
259 public void setInterface(Vector<String> interfaceTypes)
260 throws InvalidArgsException, SimulatorException {
261 setInterfaces(interfaceTypes);
264 private native void setInterfaces(Vector<String> interfaceTypes)
265 throws InvalidArgsException, SimulatorException;
268 * API to add interface type for resource.
270 * @param interfaceType
271 * Interface to be added for resource.
273 * @throws InvalidArgsException
274 * This exception will be thrown if the interface type is
276 * @throws SimulatorException
277 * This exception will be thrown if the native resource object
278 * does not exist or for some general errors.
280 public native void addInterface(String interfaceType)
281 throws InvalidArgsException, NoSupportException, SimulatorException;
284 * API to make the resource observable or not observable.
287 * True makes the resource observable, otherwise non-observable.
289 * @throws SimulatorException
290 * This exception will be thrown if the native resource object
291 * does not exist or for some general errors.
293 public native void setObservable(boolean state) throws SimulatorException;
296 * API to make the resource discoverable or not discoverable.
299 * True makes the resource discoverable, otherwise
302 * @throws SimulatorException
303 * This exception will be thrown if the native resource object
304 * does not exist or for some general errors.
306 public native void setDiscoverable(boolean state) throws SimulatorException;
309 * API to set the listener for receiving the notifications when observer is
310 * registered or unregistered with resource.
313 * Callback to be set for receiving the notifications.
315 * @throws InvalidArgsException
316 * This exception will be thrown if the listener is invalid.
317 * @throws SimulatorException
318 * This exception will be thrown if the native resource object
319 * does not exist or for some general errors.
321 public native void setObserverListener(ObserverListener listener)
322 throws InvalidArgsException, SimulatorException;
325 * API to set listener for receiving notifications when resource's model
329 * {@link ResourceModelChangeListener}.
331 * @throws InvalidArgsException
332 * This exception will be thrown on invalid input.
333 * @throws SimulatorException
334 * This exception will be thrown for other errors.
336 public native void setResourceModelChangeListener(
337 ResourceModelChangeListener listener) throws InvalidArgsException,
341 * API to start(register) the resource.
343 * @throws SimulatorException
344 * This exception will be thrown if the native resource object
345 * does not exist or for some general errors.
347 public native void start() throws SimulatorException;
350 * API to stop(unregister) the resource.
352 * @throws SimulatorException
353 * This exception will be thrown if the native resource object
354 * does not exist or for some general errors.
356 public native void stop() throws SimulatorException;
359 * API to get the {@link SimulatorResourceModel} of the simulated resource.
361 * @return {@link SimulatorResourceModel} object on success, otherwise null.
363 * @throws SimulatorException
364 * This exception will be thrown if simulated resource is not
367 public native SimulatorResourceModel getResourceModel()
368 throws SimulatorException;
371 * API to get observers which are registered with resource.
373 * @return observers as an array of {@link Observer}.
375 * @throws SimulatorException
376 * This exception will be thrown if the native resource object
377 * does not exist or for some general errors.
379 public native Vector<Observer> getObservers() throws SimulatorException;
382 * API to notify current resource model to a specific observer.
385 * Observer ID to notify.
387 * @throws SimulatorException
388 * This exception will be thrown if the native resource object
389 * does not exist or for some general errors.
391 public native void notifyObserver(int observerId) throws SimulatorException;
394 * API to notify current resource model to all registered observers.
396 * @throws SimulatorException
397 * This exception will be thrown if the native resource object
398 * does not exist or for some general errors.
400 public native void notifyAllObservers() throws SimulatorException;
403 * Listener for receiving notification when observer is registered or
404 * unregistered with the resource.
406 public interface ObserverListener {
408 * Method will be invoked when an observer is registered with resource.
411 * URI of the resource.
413 * {@link Observer} object containing the details of
416 public void onObserverAdded(String resourceURI, Observer observer);
419 * Method will be invoked when an observer is unregistered from the
423 * URI of the resource.
425 * {@link Observer} object containing the details of
428 public void onObserverRemoved(String resourceURI, Observer observer);
432 * Listener for receiving notification on completion of automatically
433 * updating attribute value from its range or value set property.
435 public interface AutoUpdateListener {
437 * Method for receiving automation complete notifications.
442 * Identifier for auto resource/attribute update session.
444 public void onUpdateComplete(String uri, int id);
448 * Listener for receiving notifications whenever there is a change in the
451 public interface ResourceModelChangeListener {
453 * Method will be invoked to notify about the changes in the resource
458 * @param resourceModel
459 * {@link SimulatorResourceModel} of the resource.
461 public void onResourceModelChanged(String uri,
462 SimulatorResourceModel resourceModel);