org.fourthline.cling.model
Class DefaultServiceManager<T>

java.lang.Object
  extended by org.fourthline.cling.model.DefaultServiceManager<T>
All Implemented Interfaces:
ServiceManager<T>

public class DefaultServiceManager<T>
extends Object
implements ServiceManager<T>

Default implementation, creates and manages a single instance of a plain Java bean.

Creates instance of the defined service class when it is first needed (acts as a factory), manages the instance in a field (it's shared), and synchronizes (locks) all multi-threaded access. A locking attempt will timeout after 500 milliseconds with a runtime exception if another operation is already in progress. Override getLockTimeoutMillis() to customize this behavior, e.g. if your service bean is slow and requires more time for typical action executions or state variable reading.

Author:
Christian Bauer

Nested Class Summary
protected  class DefaultServiceManager.DefaultPropertyChangeListener
           
 
Field Summary
protected  ReentrantLock lock
           
protected  PropertyChangeSupport propertyChangeSupport
           
protected  LocalService<T> service
           
protected  Class<T> serviceClass
           
protected  T serviceImpl
           
 
Fields inherited from interface org.fourthline.cling.model.ServiceManager
EVENTED_STATE_VARIABLES
 
Constructor Summary
protected DefaultServiceManager(LocalService<T> service)
           
  DefaultServiceManager(LocalService<T> service, Class<T> serviceClass)
           
 
Method Summary
protected  PropertyChangeListener createPropertyChangeListener(T serviceImpl)
           
protected  PropertyChangeSupport createPropertyChangeSupport(T serviceImpl)
           
protected  T createServiceInstance()
           
 void execute(Command<T> cmd)
          Double-dispatch of arbitrary commands, used by action executors and state variable accessors.
 Collection<StateVariableValue> getCurrentState()
          Reading the state of a service manually.
protected  Collection<StateVariableValue> getCurrentState(String[] variableNames)
           
 T getImplementation()
           
protected  int getLockTimeoutMillis()
           
 PropertyChangeSupport getPropertyChangeSupport()
          Provides the capability to monitor the service for state changes.
 LocalService<T> getService()
           
protected  void init()
           
protected  void lock()
           
protected  Collection<StateVariableValue> readInitialEventedStateVariableValues()
           
 String toString()
           
protected  void unlock()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

service

protected final LocalService<T> service

serviceClass

protected final Class<T> serviceClass

lock

protected final ReentrantLock lock

serviceImpl

protected T serviceImpl

propertyChangeSupport

protected PropertyChangeSupport propertyChangeSupport
Constructor Detail

DefaultServiceManager

protected DefaultServiceManager(LocalService<T> service)

DefaultServiceManager

public DefaultServiceManager(LocalService<T> service,
                             Class<T> serviceClass)
Method Detail

lock

protected void lock()

unlock

protected void unlock()

getLockTimeoutMillis

protected int getLockTimeoutMillis()

getService

public LocalService<T> getService()
Specified by:
getService in interface ServiceManager<T>
Returns:
The metadata of the service to which this manager is assigned.

getImplementation

public T getImplementation()
Specified by:
getImplementation in interface ServiceManager<T>
Returns:
An instance with the interface expected by the bound ActionExecutors and StateVariableAccessors.

getPropertyChangeSupport

public PropertyChangeSupport getPropertyChangeSupport()
Description copied from interface: ServiceManager
Provides the capability to monitor the service for state changes.

The GENA subsystem expects that this adapter will notify its listeners whenever any evented UPnP state variable of the service has changed its state. The following change event is expected:

The collection has to include values for all state variables, no matter what state variable was updated. Any other event is ignored (e.g. individual property changes).

Specified by:
getPropertyChangeSupport in interface ServiceManager<T>
Returns:
An adapter that will notify its listeners whenever any evented state variable changes.

execute

public void execute(Command<T> cmd)
             throws Exception
Description copied from interface: ServiceManager
Double-dispatch of arbitrary commands, used by action executors and state variable accessors.

The service manager will execute the given Command and it might decorate the execution, for example, by locking/unlocking access to a shared service implementation before and after the execution.

Specified by:
execute in interface ServiceManager<T>
Parameters:
cmd - The command to execute.
Throws:
Exception - Any exception, without wrapping, as thrown by Command.execute(ServiceManager)

getCurrentState

public Collection<StateVariableValue> getCurrentState()
                                               throws Exception
Description copied from interface: ServiceManager
Reading the state of a service manually.

Specified by:
getCurrentState in interface ServiceManager<T>
Returns:
A Collection of StateVariableValue, representing the current state of the service, that is, all evented state variable values.
Throws:
Exception - Any error that occurred when the service's state was accessed.

getCurrentState

protected Collection<StateVariableValue> getCurrentState(String[] variableNames)
                                                  throws Exception
Throws:
Exception

init

protected void init()

createServiceInstance

protected T createServiceInstance()
                           throws Exception
Throws:
Exception

createPropertyChangeSupport

protected PropertyChangeSupport createPropertyChangeSupport(T serviceImpl)
                                                     throws Exception
Throws:
Exception

createPropertyChangeListener

protected PropertyChangeListener createPropertyChangeListener(T serviceImpl)
                                                       throws Exception
Throws:
Exception

readInitialEventedStateVariableValues

protected Collection<StateVariableValue> readInitialEventedStateVariableValues()
                                                                        throws Exception
Throws:
Exception

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2013 4th Line GmbH, Switzerland. All Rights Reserved.