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 public boolean isCollection() {
69 return (this instanceof SimulatorCollectionResource);
73 * API to get the type which indicates whether resource is single or
74 * collection resource.
76 * @return Type of resource.
78 public Type getType() {
79 if (this instanceof SimulatorSingleResource) {
82 return Type.COLLECTION;
87 * API to get the name of the resource.
89 * @return Name of the resource.
91 * @throws SimulatorException
92 * This exception will be thrown if the native resource object
93 * does not exist or for some general errors.
95 public String getName() throws SimulatorException {
96 return nativeGetName();
100 * API to get the resource URI.
102 * @return Resource URI.
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 String getURI() throws SimulatorException {
109 return nativeGetURI();
113 * API to get the resource type.
115 * @return Resource type.
117 * @throws SimulatorException
118 * This exception will be thrown if the native resource object
119 * does not exist or for some general errors.
121 public String getResourceType() throws SimulatorException {
122 return nativeGetResourceType();
126 * API to get the interfaces resource is bound with.
128 * @return Interface types.
130 * @throws SimulatorException
131 * This exception will be thrown if the native resource object
132 * does not exist or for some general errors.
134 public Vector<String> getInterface() throws SimulatorException {
135 return nativeGetInterface();
139 * API to get the observable state of resource.
141 * @return Observable state - true if resource is observable, otherwise
144 * @throws SimulatorException
145 * This exception will be thrown if the native resource object
146 * does not exist or for some general errors.
148 public boolean isObservable() throws SimulatorException {
149 return nativeIsObservable();
153 * API to get the discoverable state of resource.
155 * @return Discoverable state - true if resource is discoverable, otherwise
158 * @throws SimulatorException
159 * This exception will be thrown if the native resource object
160 * does not exist or for some general errors.
162 public boolean isDiscoverable() throws SimulatorException {
163 return nativeIsDiscoverable();
167 * API to get the start state of resource.
169 * @return Start state - true if resource is started, otherwise false.
171 * @throws SimulatorException
172 * This exception will be thrown if the native resource object
173 * does not exist or for some general errors.
175 public boolean isStarted() throws SimulatorException {
176 return nativeIsStarted();
180 * API to set the name of the resource.
185 * @throws InvalidArgsException
186 * This exception will be thrown if the resource name is
188 * @throws SimulatorException
189 * This exception will be thrown if the native resource object
190 * does not exist or for some general errors.
192 public void setName(String name) throws InvalidArgsException,
198 * API to set the resource URI.
203 * @throws InvalidArgsException
204 * This exception will be thrown if the resource URI is invalid.
205 * @throws SimulatorException
206 * A resource needs to be stopped by calling the stop() before
207 * calling this API. This exception will be thrown if the native
208 * resource object has not yet been stopped.
210 public void setURI(String uri) throws InvalidArgsException,
216 * API to set the resource type.
218 * @param resourceType
219 * Resource type string.
221 * @throws InvalidArgsException
222 * This exception will be thrown if the resource type is
224 * @throws SimulatorException
225 * This exception will be thrown if the native resource object
226 * does not exist or for some general errors.
228 public void setResourceType(String resourceType)
229 throws InvalidArgsException, SimulatorException {
230 nativeSetResourceType(resourceType);
234 * API to set interface to resource. Resource should be stopped before
237 * @param interfaceType
238 * Interface to be set.
240 * @throws InvalidArgsException
241 * This exception will be thrown if the interface type is
243 * @throws SimulatorException
244 * This exception will be thrown if the native resource object
245 * does not exist or resource is still running or for some
248 public void setInterface(String interfaceType) throws InvalidArgsException,
250 nativeSetInterface(interfaceType);
254 * API to set a list of interfaces to resource. Resource should be stopped
255 * before calling this API.
257 * @param interfaceTypes
258 * Interfaces to be set.
260 * @throws InvalidArgsException
261 * This exception will be thrown if the interface type is
263 * @throws SimulatorException
264 * This exception will be thrown if the native resource object
265 * does not exist or resource is still running or for some
268 public void setInterface(Vector<String> interfaceTypes)
269 throws InvalidArgsException, SimulatorException {
270 nativeSetInterfaces(interfaceTypes);
274 * API to add interface type for resource.
276 * @param interfaceType
277 * Interface to be added for resource.
279 * @throws InvalidArgsException
280 * This exception will be thrown if the interface type is
282 * @throws NoSupportException
283 * This exception will be thrown if the interface type is not
284 * supported by the resource.
285 * @throws SimulatorException
286 * This exception will be thrown if the native resource object
287 * does not exist or for some general errors.
289 public void addInterface(String interfaceType) throws InvalidArgsException,
290 NoSupportException, SimulatorException {
291 nativeAddInterface(interfaceType);
295 * API to make the resource observable or not observable.
298 * True makes the resource observable, otherwise non-observable.
300 * @throws SimulatorException
301 * This exception will be thrown if the native resource object
302 * does not exist or for some general errors.
304 public void setObservable(boolean state) throws SimulatorException {
305 nativeSetObservable(state);
309 * API to make the resource discoverable or not discoverable.
312 * True makes the resource discoverable, otherwise
315 * @throws SimulatorException
316 * This exception will be thrown if the native resource object
317 * does not exist or for some general errors.
319 public void setDiscoverable(boolean state) throws SimulatorException {
320 nativeSetDiscoverable(state);
324 * API to set the listener for receiving the notifications when observer is
325 * registered or unregistered with resource.
328 * Callback to be set for receiving the notifications.
330 * @throws InvalidArgsException
331 * This exception will be thrown if the listener is invalid.
332 * @throws SimulatorException
333 * This exception will be thrown if the native resource object
334 * does not exist or for some general errors.
336 public void setObserverListener(ObserverListener listener)
337 throws InvalidArgsException, SimulatorException {
338 nativeSetObserverListener(listener);
342 * API to set listener for receiving notifications when resource's model
346 * {@link ResourceModelChangeListener}.
348 * @throws InvalidArgsException
349 * This exception will be thrown on invalid input.
350 * @throws SimulatorException
351 * This exception will be thrown for other errors.
353 public void setResourceModelChangeListener(
354 ResourceModelChangeListener listener) throws InvalidArgsException,
356 nativeSetResourceModelChangeListener(listener);
360 * API to start(register) the resource.
362 * @throws SimulatorException
363 * This exception will be thrown if the native resource object
364 * does not exist or for some general errors.
366 public void start() throws SimulatorException {
371 * API to stop(unregister) the resource.
373 * @throws SimulatorException
374 * This exception will be thrown if the native resource object
375 * does not exist or for some general errors.
377 public void stop() throws SimulatorException {
382 * API to get the {@link SimulatorResourceModel} of the simulated resource.
384 * @return {@link SimulatorResourceModel} object on success, otherwise null.
386 * @throws SimulatorException
387 * This exception will be thrown if simulated resource is not
390 public SimulatorResourceModel getResourceModel() throws SimulatorException {
391 return nativeGetResourceModel();
395 * API to get observers which are registered with resource.
397 * @return observers as an array of {@link Observer}.
399 * @throws SimulatorException
400 * This exception will be thrown if the native resource object
401 * does not exist or for some general errors.
403 public Vector<Observer> getObservers() throws SimulatorException {
404 return nativeGetObservers();
408 * API to notify current resource model to a specific observer.
411 * Observer ID to notify.
413 * @throws SimulatorException
414 * This exception will be thrown if the native resource object
415 * does not exist or for some general errors.
417 public void notifyObserver(int observerId) throws SimulatorException {
418 nativeNotify(observerId);
422 * API to notify current resource model to all registered observers.
424 * @throws SimulatorException
425 * This exception will be thrown if the native resource object
426 * does not exist or for some general errors.
428 public void notifyAllObservers() throws SimulatorException {
433 * Listener for receiving notification when observer is registered or
434 * unregistered with the resource.
436 public interface ObserverListener {
438 * Method will be invoked when an observer is registered with resource.
441 * URI of the resource.
443 * {@link Observer} object containing the details of
446 public void onObserverAdded(String resourceURI, Observer observer);
449 * Method will be invoked when an observer is unregistered from the
453 * URI of the resource.
455 * {@link Observer} object containing the details of
458 public void onObserverRemoved(String resourceURI, Observer observer);
462 * Listener for receiving notification on completion of automatically
463 * updating attribute value from its range or value set property.
465 public interface AutoUpdateListener {
467 * Method for receiving automation complete notifications.
472 * Identifier for auto resource/attribute update session.
474 public void onUpdateComplete(String uri, int id);
478 * Listener for receiving notifications whenever there is a change in the
481 public interface ResourceModelChangeListener {
483 * Method will be invoked to notify about the changes in the resource
488 * @param resourceModel
489 * {@link SimulatorResourceModel} of the resource.
491 public void onResourceModelChanged(String uri,
492 SimulatorResourceModel resourceModel);
495 protected SimulatorResource() {
498 private native String nativeGetName();
500 private native String nativeGetURI();
502 private native String nativeGetResourceType();
504 private native Vector<String> nativeGetInterface();
506 private native boolean nativeIsObservable();
508 private native boolean nativeIsDiscoverable();
510 private native boolean nativeIsStarted();
512 private native void nativeSetName(String name);
514 private native void nativeSetURI(String uri);
516 private native void nativeSetResourceType(String resourceType);
518 private native void nativeSetInterface(String interfaceType);
520 private native void nativeSetInterfaces(Vector<String> interfaceTypes);
522 private native void nativeAddInterface(String interfaceType);
524 private native void nativeSetObservable(boolean state);
526 private native void nativeSetDiscoverable(boolean state);
528 private native void nativeSetObserverListener(ObserverListener listener);
530 private native void nativeSetResourceModelChangeListener(
531 ResourceModelChangeListener listener);
533 private native void nativeStart();
535 private native void nativeStop();
537 private native SimulatorResourceModel nativeGetResourceModel();
539 private native Vector<Observer> nativeGetObservers();
541 private native void nativeNotify(int observerId);
543 private native void nativeNotifyAll();
545 private native void nativeDispose();