org.benow.service
Class LocalServices

java.lang.Object
  extended by org.benow.service.Services
      extended by org.benow.service.LocalServices
All Implemented Interfaces:
org.benow.java.notify.Listener, org.benow.java.shutdown.Deinitialized, org.benow.java.spec.Markup, Service

public class LocalServices
extends Services
implements org.benow.java.notify.Listener, org.benow.java.shutdown.Deinitialized

Loads and exposes discovered services. Service descriptions are saved to a file, which is used in the loading process. To save service descriptions, use the service manager as an application. See main[] for more information.

Author:
andy
See Also:
main(String[])

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.benow.java.spec.Markup
org.benow.java.spec.Markup.ParamName, org.benow.java.spec.Markup.ReturnDescription
 
Field Summary
static org.benow.security.permission.Permission PERM_EDIT
           
 
Constructor Summary
LocalServices(java.util.List<java.lang.Class<? extends Service>> serviceClasses)
           
 
Method Summary
 void addServiceImplementator(java.lang.Class<? extends Service> serviceInterface, java.lang.Class<?> serviceClass)
           
 void addServiceImplementator(java.lang.Class<? extends Service> serviceInterface, java.lang.Class<?> serviceClass, java.lang.String src)
           
protected  void doInit()
          Override to initialize, only ever called once
 java.lang.Class<? extends Service> getServiceInterfaceFromImplementation(java.lang.Class<?> serviceClass)
          Gets the (first) service interace implemented by the given the given service class
 ServiceSpecification getServiceSpecification(java.lang.Class<? extends Service> forClass)
          Gets the service specification for the given class.
 java.util.List<ServiceSpecification> getServiceSpecifications()
          Gets all services the user has permission to access.
static java.lang.Class<?> getStackClass(java.lang.ClassLoader loader, int position)
           
static java.lang.Class<?> getStackClass(int position)
          Gets the class in the given position in the stack trace.
static java.lang.Object invoke(java.lang.Class<? extends Service> serviceClass, java.lang.Object source)
           
static java.lang.Object invokeThisService(java.lang.Object params)
          Called from within a (main) method of a service, this utility invokes the default method within the service class instance with the given object and returns the result.
static boolean isPublished(java.lang.Class<?> serviceClass)
           
 java.util.List<java.lang.Class<? extends Service>> listServiceClassesImplementing(java.lang.Class<?> serviceInterface)
           
 java.util.List<ServiceSpecification> listServices()
           
static void main(java.lang.String[] args)
          Creates a service descriptor over discovered services.
 boolean onEvent(java.lang.Object notifier, org.benow.java.notify.event.Event event)
          Events passed to message bus.
 void onShutDown()
           
 void returnService(Service serviceInstance)
           
 Service takeService(java.lang.Class<? extends Service> serviceClassOrInterface)
           
 java.lang.String toString()
           
 
Methods inherited from class org.benow.service.Services
getInstance, isService, returnAService, setInstance, takeAService
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PERM_EDIT

public static final org.benow.security.permission.Permission PERM_EDIT
Constructor Detail

LocalServices

public LocalServices(java.util.List<java.lang.Class<? extends Service>> serviceClasses)
              throws java.io.InvalidClassException,
                     org.benow.java.spec.SpecificationException
Throws:
java.io.InvalidClassException
org.benow.java.spec.SpecificationException
Method Detail

doInit

protected void doInit()
               throws java.lang.Exception
Description copied from class: Services
Override to initialize, only ever called once

Overrides:
doInit in class Services
Throws:
java.lang.Exception

onShutDown

public void onShutDown()
                throws java.lang.Throwable
Specified by:
onShutDown in interface org.benow.java.shutdown.Deinitialized
Throws:
java.lang.Throwable

addServiceImplementator

public void addServiceImplementator(java.lang.Class<? extends Service> serviceInterface,
                                    java.lang.Class<?> serviceClass)
                             throws org.benow.java.spec.SpecificationException,
                                    java.io.InvalidClassException
Throws:
org.benow.java.spec.SpecificationException
java.io.InvalidClassException

addServiceImplementator

public void addServiceImplementator(java.lang.Class<? extends Service> serviceInterface,
                                    java.lang.Class<?> serviceClass,
                                    java.lang.String src)
                             throws org.benow.java.spec.SpecificationException,
                                    java.io.InvalidClassException
Throws:
org.benow.java.spec.SpecificationException
java.io.InvalidClassException

getServiceSpecification

public ServiceSpecification getServiceSpecification(java.lang.Class<? extends Service> forClass)
                                             throws org.benow.java.spec.SpecificationException
Gets the service specification for the given class. If is interface, specification returned directly, if is implementation, specification for the first service interface is returned.

Specified by:
getServiceSpecification in class Services
Throws:
org.benow.java.spec.SpecificationException

takeService

public Service takeService(java.lang.Class<? extends Service> serviceClassOrInterface)
Specified by:
takeService in class Services

returnService

public void returnService(Service serviceInstance)
Specified by:
returnService in class Services

listServiceClassesImplementing

public java.util.List<java.lang.Class<? extends Service>> listServiceClassesImplementing(java.lang.Class<?> serviceInterface)
Specified by:
listServiceClassesImplementing in class Services
Returns:
list of services implementing given service interface

main

public static void main(java.lang.String[] args)
Creates a service descriptor over discovered services. Works by scanning a given class directory recursively... it loads each class and tests to see if it is a service (ie implements Service interface). If found, the service is added to the descriptor. This descriptor is used to load services by the ServiceManager. This process saves having to scan all classes each time (and having to know where they were loaded from, etc). Once services are registered, providing a service is as easy as adding a jar to the classpath (seriously).

Parameters:
args -

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

invokeThisService

public static java.lang.Object invokeThisService(java.lang.Object params)
                                          throws java.lang.IllegalArgumentException,
                                                 java.lang.IllegalAccessException,
                                                 java.lang.InstantiationException,
                                                 java.lang.Throwable
Called from within a (main) method of a service, this utility invokes the default method within the service class instance with the given object and returns the result. A context is created over the given object for the calling service and the default method of that service is invoked.

Returns:
the value returned from the method invoked
Throws:
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.InstantiationException
java.lang.Throwable

invoke

public static java.lang.Object invoke(java.lang.Class<? extends Service> serviceClass,
                                      java.lang.Object source)
                               throws org.benow.java.spec.SpecificationException,
                                      java.lang.IllegalArgumentException,
                                      java.lang.IllegalAccessException,
                                      java.lang.InstantiationException,
                                      java.lang.Throwable
Throws:
org.benow.java.spec.SpecificationException
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.InstantiationException
java.lang.Throwable

getStackClass

public static java.lang.Class<?> getStackClass(int position)
Gets the class in the given position in the stack trace. The position should be relative to the calling location, ie not including Util.getStackClass(). getStackClass(1) refers to calling class.

NOTE: this is an expensive operation, and should be confined to rare calling, such as from static blocks.

Parameters:
position - the position of the class in the stace
Returns:
class as given in the stack in the given position

getStackClass

public static java.lang.Class<?> getStackClass(java.lang.ClassLoader loader,
                                               int position)

isPublished

public static boolean isPublished(java.lang.Class<?> serviceClass)

getServiceInterfaceFromImplementation

public java.lang.Class<? extends Service> getServiceInterfaceFromImplementation(java.lang.Class<?> serviceClass)
Gets the (first) service interace implemented by the given the given service class

Parameters:
serviceClass -
Returns:
service interface or null if no interface (not a service)

onEvent

public boolean onEvent(java.lang.Object notifier,
                       org.benow.java.notify.event.Event event)
Events passed to message bus. Subscribe to know.

Specified by:
onEvent in interface org.benow.java.notify.Listener

getServiceSpecifications

public java.util.List<ServiceSpecification> getServiceSpecifications()
Gets all services the user has permission to access.

Specified by:
getServiceSpecifications in class Services
Returns:

listServices

public java.util.List<ServiceSpecification> listServices()
Specified by:
listServices in class Services
Returns:
list of available services (interfaces)