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 protected long mNativeHandle;
38 protected void finalize() throws Throwable {
41 } catch (Throwable t) {
49 * Enum to represent the type of resource.
56 * Enum to represent the update type of automation.
58 public enum AutoUpdateType {
63 * API which indicates whether the resource is collection or single
66 * @return True if the resource is collection, otherwise false.
68 * @throws SimulatorException
69 * This exception will be thrown if the native resource object
70 * does not exist or for some general errors.
72 public boolean isCollection() {
73 return (this instanceof SimulatorCollectionResource);
77 * API to get the type which indicates whether resource is single or
78 * collection resource.
80 * @return Type of resource.
82 * @throws SimulatorException
83 * This exception will be thrown if the native resource object
84 * does not exist or for some general errors.
86 public Type getType() {
87 if (this instanceof SimulatorSingleResource) {
90 return Type.COLLECTION;
95 * API to get the name of the resource.
97 * @return Name of the resource.
99 * @throws SimulatorException
100 * This exception will be thrown if the native resource object
101 * does not exist or for some general errors.
103 public String getName() throws SimulatorException {
104 return nativeGetName();
108 * API to get the resource URI.
110 * @return Resource URI.
112 * @throws SimulatorException
113 * This exception will be thrown if the native resource object
114 * does not exist or for some general errors.
116 public String getURI() throws SimulatorException {
117 return nativeGetURI();
121 * API to get the resource type.
123 * @return Resource type.
125 * @throws SimulatorException
126 * This exception will be thrown if the native resource object
127 * does not exist or for some general errors.
129 public String getResourceType() throws SimulatorException {
130 return nativeGetResourceType();
134 * API to get the interfaces resource is bound with.
136 * @return Interface types.
138 * @throws SimulatorException
139 * This exception will be thrown if the native resource object
140 * does not exist or for some general errors.
142 public Vector<String> getInterface() throws SimulatorException {
143 return nativeGetInterface();
147 * API to get the observable state of resource.
149 * @return Observable state - true if resource is observable, otherwise
152 * @throws SimulatorException
153 * This exception will be thrown if the native resource object
154 * does not exist or for some general errors.
156 public boolean isObservable() throws SimulatorException {
157 return nativeIsObservable();
161 * API to get the discoverable state of resource.
163 * @return Discoverable state - true if resource is discoverable, otherwise
166 * @throws SimulatorException
167 * This exception will be thrown if the native resource object
168 * does not exist or for some general errors.
170 public boolean isDiscoverable() throws SimulatorException {
171 return nativeIsDiscoverable();
175 * API to get the start state of resource.
177 * @return Start state - true if resource is started, otherwise false.
179 * @throws SimulatorException
180 * This exception will be thrown if the native resource object
181 * does not exist or for some general errors.
183 public boolean isStarted() throws SimulatorException {
184 return nativeIsStarted();
188 * API to set the name of the resource.
193 * @throws InvalidArgsException
194 * This exception will be thrown if the resource name is
196 * @throws SimulatorException
197 * This exception will be thrown if the native resource object
198 * does not exist or for some general errors.
200 public void setName(String name) throws InvalidArgsException,
206 * API to set the resource URI.
211 * @throws InvalidArgsException
212 * This exception will be thrown if the resource URI is invalid.
213 * @throws SimulatorException
214 * A resource needs to be stopped by calling the stop() before
215 * calling this API. This exception will be thrown if the native
216 * resource object has not yet been stopped.
218 public void setURI(String uri) throws InvalidArgsException,
224 * API to set the resource type.
226 * @param resourceType
227 * Resource type string.
229 * @throws InvalidArgsException
230 * This exception will be thrown if the resource type is
232 * @throws SimulatorException
233 * This exception will be thrown if the native resource object
234 * does not exist or for some general errors.
236 public void setResourceType(String resourceType)
237 throws InvalidArgsException, SimulatorException {
238 nativeSetResourceType(resourceType);
242 * API to set interface to resource. Resource should be stopped before
245 * @param interfaceType
246 * Interface to be set.
248 * @throws InvalidArgsException
249 * This exception will be thrown if the interface type is
251 * @throws SimulatorException
252 * This exception will be thrown if the native resource object
253 * does not exist or resource is still running or for some
256 public void setInterface(String interfaceType) throws InvalidArgsException,
258 nativeSetInterface(interfaceType);
262 * API to set a list of interfaces to resource. Resource should be stopped
263 * before calling this API.
265 * @param interfaceTypes
266 * Interfaces to be set.
268 * @throws InvalidArgsException
269 * This exception will be thrown if the interface type is
271 * @throws SimulatorException
272 * This exception will be thrown if the native resource object
273 * does not exist or resource is still running or for some
276 public void setInterface(Vector<String> interfaceTypes)
277 throws InvalidArgsException, SimulatorException {
278 nativeSetInterfaces(interfaceTypes);
282 * API to add interface type for resource.
284 * @param interfaceType
285 * Interface to be added for resource.
287 * @throws InvalidArgsException
288 * This exception will be thrown if the interface type is
290 * @throws SimulatorException
291 * This exception will be thrown if the native resource object
292 * does not exist or for some general errors.
294 public void addInterface(String interfaceType) throws InvalidArgsException,
295 NoSupportException, SimulatorException {
296 nativeAddInterface(interfaceType);
300 * API to make the resource observable or not observable.
303 * True makes the resource observable, otherwise non-observable.
305 * @throws SimulatorException
306 * This exception will be thrown if the native resource object
307 * does not exist or for some general errors.
309 public void setObservable(boolean state) throws SimulatorException {
310 nativeSetObservable(state);
314 * API to make the resource discoverable or not discoverable.
317 * True makes the resource discoverable, otherwise
320 * @throws SimulatorException
321 * This exception will be thrown if the native resource object
322 * does not exist or for some general errors.
324 public void setDiscoverable(boolean state) throws SimulatorException {
325 nativeSetDiscoverable(state);
329 * API to set the listener for receiving the notifications when observer is
330 * registered or unregistered with resource.
333 * Callback to be set for receiving the notifications.
335 * @throws InvalidArgsException
336 * This exception will be thrown if the listener is invalid.
337 * @throws SimulatorException
338 * This exception will be thrown if the native resource object
339 * does not exist or for some general errors.
341 public void setObserverListener(ObserverListener listener)
342 throws InvalidArgsException, SimulatorException {
343 nativeSetObserverListener(listener);
347 * API to set listener for receiving notifications when resource's model
351 * {@link ResourceModelChangeListener}.
353 * @throws InvalidArgsException
354 * This exception will be thrown on invalid input.
355 * @throws SimulatorException
356 * This exception will be thrown for other errors.
358 public void setResourceModelChangeListener(
359 ResourceModelChangeListener listener) throws InvalidArgsException,
361 nativeSetResourceModelChangeListener(listener);
365 * API to start(register) the resource.
367 * @throws SimulatorException
368 * This exception will be thrown if the native resource object
369 * does not exist or for some general errors.
371 public void start() throws SimulatorException {
376 * API to stop(unregister) the resource.
378 * @throws SimulatorException
379 * This exception will be thrown if the native resource object
380 * does not exist or for some general errors.
382 public void stop() throws SimulatorException {
387 * API to get the {@link SimulatorResourceModel} of the simulated resource.
389 * @return {@link SimulatorResourceModel} object on success, otherwise null.
391 * @throws SimulatorException
392 * This exception will be thrown if simulated resource is not
395 public SimulatorResourceModel getResourceModel() throws SimulatorException {
396 return nativeGetResourceModel();
400 * API to get observers which are registered with resource.
402 * @return observers as an array of {@link Observer}.
404 * @throws SimulatorException
405 * This exception will be thrown if the native resource object
406 * does not exist or for some general errors.
408 public Vector<Observer> getObservers() throws SimulatorException {
409 return nativeGetObservers();
413 * API to notify current resource model to a specific observer.
416 * Observer ID to notify.
418 * @throws SimulatorException
419 * This exception will be thrown if the native resource object
420 * does not exist or for some general errors.
422 public void notifyObserver(int observerId) throws SimulatorException {
423 nativeNotify(observerId);
427 * API to notify current resource model to all registered observers.
429 * @throws SimulatorException
430 * This exception will be thrown if the native resource object
431 * does not exist or for some general errors.
433 public void notifyAllObservers() throws SimulatorException {
438 * Listener for receiving notification when observer is registered or
439 * unregistered with the resource.
441 public interface ObserverListener {
443 * Method will be invoked when an observer is registered with resource.
446 * URI of the resource.
448 * {@link Observer} object containing the details of
451 public void onObserverAdded(String resourceURI, Observer observer);
454 * Method will be invoked when an observer is unregistered from the
458 * URI of the resource.
460 * {@link Observer} object containing the details of
463 public void onObserverRemoved(String resourceURI, Observer observer);
467 * Listener for receiving notification on completion of automatically
468 * updating attribute value from its range or value set property.
470 public interface AutoUpdateListener {
472 * Method for receiving automation complete notifications.
477 * Identifier for auto resource/attribute update session.
479 public void onUpdateComplete(String uri, int id);
483 * Listener for receiving notifications whenever there is a change in the
486 public interface ResourceModelChangeListener {
488 * Method will be invoked to notify about the changes in the resource
493 * @param resourceModel
494 * {@link SimulatorResourceModel} of the resource.
496 public void onResourceModelChanged(String uri,
497 SimulatorResourceModel resourceModel);
500 protected SimulatorResource() {
503 private native String nativeGetName();
505 private native String nativeGetURI();
507 private native String nativeGetResourceType();
509 private native Vector<String> nativeGetInterface();
511 private native boolean nativeIsObservable();
513 private native boolean nativeIsDiscoverable();
515 private native boolean nativeIsStarted();
517 private native void nativeSetName(String name);
519 private native void nativeSetURI(String uri);
521 private native void nativeSetResourceType(String resourceType);
523 private native void nativeSetInterface(String interfaceType);
525 private native void nativeSetInterfaces(Vector<String> interfaceTypes);
527 private native void nativeAddInterface(String interfaceType);
529 private native void nativeSetObservable(boolean state);
531 private native void nativeSetDiscoverable(boolean state);
533 private native void nativeSetObserverListener(ObserverListener listener);
535 private native void nativeSetResourceModelChangeListener(
536 ResourceModelChangeListener listener);
538 private native void nativeStart();
540 private native void nativeStop();
542 private native SimulatorResourceModel nativeGetResourceModel();
544 private native Vector<Observer> nativeGetObservers();
546 private native void nativeNotify(int observerId);
548 private native void nativeNotifyAll();
550 private native void nativeDispose();