jason.core
Class AbstractService

java.lang.Object
  |
  +--jason.core.AbstractService
All Implemented Interfaces:
java.io.Serializable, Service, ServiceDataListener
Direct Known Subclasses:
DebugService, EmptyLog, LogImpl, NopService, SchedulableService, ThreadableService, ToolkitService

public abstract class AbstractService
extends java.lang.Object
implements Service, java.io.Serializable, ServiceDataListener

This class is the common class for services implementation. Its role is to managed the service state (START/STOP). It takes into consideration the XML service configuration file by using the ServiceContext. So user can retreive properties for handling correctly the service. The container calls the init method and the start method automatically. The start method will call by default the run method that should contains the user core function.

The AbstractService doesn't behave excatly like a Thread. For the Thread support, please look at the ThreadableService class

Developer can made its service persistent thanks to the setPersistent method. This method must be changed before the start method. Then during the init the setPersistent wish is delegated to the ServiceContext. User can change the behavior in the notifyInit method. Then, if the service is set persistent, the container will call the getServiceState method during the stop method for storing the service state. On the contrary, when starting a service, the container will reload the old service state. A current implementation tried to get all value for each get starting method name, writing the state is writing for all tied set method.

Since:
1.0
Author:
"Alexandre Brillant"
See Also:
Service, ThreadableService, SocketService, ServiceContext, ServiceVersion, Serialized Form

Field Summary
static int INITIALIZED
           
protected  java.lang.String name
           
static int NOSTATE
           
protected  ServiceContext serviceContext
           
static int STARTED
           
static int STOPPED
           
 
Fields inherited from interface jason.core.Service
ERROR_MESSAGE, INFORMATION_MESSAGE, VERBOSE_MESSAGE, WARNING_MESSAGE
 
Constructor Summary
AbstractService()
          Default constructor, nothing to do
 
Method Summary
protected  void actionForStart()
          This method is called at the end of the start method.
 boolean canRestartOnError()
           
protected  void checkForContext()
          Check that the context is not empty, else it throws a RuntimeException, because this is an illegal access
protected  void fireCriticalErrorDetected(java.lang.Exception exc)
          This method must be called for critical error, it will avoid the service to start
 long getAutoRestartCounter()
           
 boolean getBooleanProperty(java.lang.String name)
           
 ServiceInfo getInfo()
           
 int getIntProperty(java.lang.String name)
           
 java.lang.String getName()
          This name should distinguish your service from another one.
 java.util.Enumeration getPropertiesName()
           
 java.lang.Object getProperty(java.lang.String name)
           
 ServiceContext getServiceContext()
           
 ServiceState getServiceState()
           
 int getState()
           
 java.lang.String getStringProperty(java.lang.String name)
           
 ServiceType getType()
          This is an important function for cooperation, it says what it the role of a service such as a Web service.
 ServiceVersion getVersion()
           
 boolean hasServiceContext()
           
 void init(ServiceContext sc)
          Initialize the current service.
protected  boolean isAutoStop()
           
 boolean isEnabled()
          This method is used only when starting the first time this service
 boolean isInitialized()
          this method return true when the service has a ServiceContext
 boolean isOverride()
          By default it returns false
 boolean isPersistent()
           
 boolean isPrivate()
          Design the accessibility level for this service
 boolean isProcessable()
          Check for a loadbalancing for each new process
 boolean isStarted()
           
 boolean isStopped()
           
protected  boolean isTest()
           
 boolean isThreadable()
          Check for the thread support from a thread pooling system
 boolean isVerbose()
          This method will avoid to show the Information message type Look at the ServiceContect for more information
 ServiceResponse manageRequest(ServiceRequest request)
          Manage a request
protected  ServiceResponse manageRequestForType(ServiceType type, ServiceRequest request)
           
 void notifyData(java.lang.String key, java.lang.Object value)
           
protected  void notifyInitReady()
          This method is called when the init method is runned
protected  void notifyStartReady()
          This method is called at the end of the start method
protected  void notifyStopReady()
          This method is called at the end of the stop method
protected  void postStop()
          Action before stopping the service
 void restart()
          restart the service
protected  void run()
          User should override this method for running a task.
 void sendCustomMessage(int type, java.lang.String message)
          Send a custom message with a tied type
 void sendErrorMessage(java.lang.String message)
          Send an error message
 void sendInformationMessage(java.lang.String message)
          Send an information message, this message will not be taken into account if the verbose value is false
 void sendInnerError(java.lang.Exception exc)
          Send an inner error exception
 void sendWarningMessage(java.lang.String message)
          Send a warning message
 void setAutoRestartCounter(long counter)
          Reset the counter for trying again to start the service on error reported.
 void setAutoRestartOnError(boolean restart)
          Set a flag for restarting automatically the current service on runtime error.
protected  void setAutoStop(boolean autoStop)
          Decide to stop the service automatically after the run invocakation, by default the value is false
protected  void setEnabled(boolean enabled)
          Change the enabled mode.
 void setName(java.lang.String name)
          Reset the name of the service
 void setPersistent(boolean persistent)
          Make the current service persistent through the getServiceState
protected  void setPrivate(boolean privateState)
          This method lets the user change the accessibility for service
protected  void setProperty(java.lang.String name, java.lang.Object value)
          Reset the starting property. it can be only called when the init method has been called.
 void setServiceContext(ServiceContext serviceContext)
           
protected  void setTest(boolean test)
          This method shows for the first run call a message that control your service is really running
protected  void setType(ServiceType type)
           
protected  void setVerbose(boolean verbose)
          Decide for the verbosity
 void setVersion(ServiceVersion version)
          Reset the version of the service
 void start()
          Start the service.
 void stop()
          stop the service
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NOSTATE

public static final int NOSTATE
See Also:
Constant Field Values

INITIALIZED

public static final int INITIALIZED
See Also:
Constant Field Values

STOPPED

public static final int STOPPED
See Also:
Constant Field Values

STARTED

public static final int STARTED
See Also:
Constant Field Values

serviceContext

protected transient ServiceContext serviceContext

name

protected java.lang.String name
Constructor Detail

AbstractService

public AbstractService()
Default constructor, nothing to do

Method Detail

init

public void init(ServiceContext sc)
Initialize the current service. You shouldn't override this method, if you want to be notified when this method if done, please use the notifyServiceInitReady rather

Specified by:
init in interface Service

setTest

protected void setTest(boolean test)
This method shows for the first run call a message that control your service is really running


isTest

protected boolean isTest()
Returns:
true> is the test mode is supported

getPropertiesName

public java.util.Enumeration getPropertiesName()
Returns:
an enumeration of all property name

setProperty

protected void setProperty(java.lang.String name,
                           java.lang.Object value)
                    throws ServiceException
Reset the starting property. it can be only called when the init method has been called. So, be careful and called it rather in notifiyInitReadt called. If you invoke it before a RuntimeException will be called

ServiceException

getStringProperty

public java.lang.String getStringProperty(java.lang.String name)
Specified by:
getStringProperty in interface Service
Returns:
a Service property as a string

getIntProperty

public int getIntProperty(java.lang.String name)
Specified by:
getIntProperty in interface Service
Returns:
a Service property as an integer

getBooleanProperty

public boolean getBooleanProperty(java.lang.String name)
Specified by:
getBooleanProperty in interface Service
Returns:
a Service property as a boolean

getProperty

public java.lang.Object getProperty(java.lang.String name)
Specified by:
getProperty in interface Service
Returns:
a property service

checkForContext

protected void checkForContext()
Check that the context is not empty, else it throws a RuntimeException, because this is an illegal access


notifyInitReady

protected void notifyInitReady()
This method is called when the init method is runned


notifyStartReady

protected void notifyStartReady()
This method is called at the end of the start method


notifyStopReady

protected void notifyStopReady()
This method is called at the end of the stop method


notifyData

public void notifyData(java.lang.String key,
                       java.lang.Object value)
Specified by:
notifyData in interface ServiceDataListener

restart

public void restart()
Description copied from interface: Service
restart the service

Specified by:
restart in interface Service

stop

public void stop()
Description copied from interface: Service
stop the service

Specified by:
stop in interface Service

setAutoStop

protected void setAutoStop(boolean autoStop)
Decide to stop the service automatically after the run invocakation, by default the value is false


isAutoStop

protected boolean isAutoStop()
Returns:
true if the service is automatically stop after the run method

setPersistent

public void setPersistent(boolean persistent)
Make the current service persistent through the getServiceState


isPersistent

public boolean isPersistent()
Returns:
true if the service state can be store

postStop

protected void postStop()
Action before stopping the service


getServiceState

public ServiceState getServiceState()
Specified by:
getServiceState in interface Service
Returns:
the current service state

start

public void start()
Start the service. By default this method will call the run method

Specified by:
start in interface Service

actionForStart

protected void actionForStart()
This method is called at the end of the start method. By default it tries to call the run method


setAutoRestartOnError

public void setAutoRestartOnError(boolean restart)
Set a flag for restarting automatically the current service on runtime error. By default this feature is disabled. It can be used for case where known exception cannot alter the runtime process. In all the cases, the error notification is maintained, so by enabling this feature you don't remove error notification system. By enabling this feature, the service can be restarted infinite times, such behavior can be a bad thing for service that crash each time it is restarted, so a counter for trying is enabled. Set the Setautorestartcounter


canRestartOnError

public boolean canRestartOnError()
Returns:
true to restart the service if an unknown exception is thrown. This is a secure way to maintain a running state for the service as possible

setAutoRestartCounter

public void setAutoRestartCounter(long counter)
Reset the counter for trying again to start the service on error reported. This counter is useful only if the AutoRestartOnError property is enabled. By default the value is '-1' meaning try it infinite times


getAutoRestartCounter

public long getAutoRestartCounter()
Returns:
the counter for trying to restart the service. By default this is -1 meaning try it infinite times

isStarted

public boolean isStarted()

isStopped

public boolean isStopped()

isInitialized

public boolean isInitialized()
this method return true when the service has a ServiceContext


getState

public int getState()

setVersion

public void setVersion(ServiceVersion version)
Reset the version of the service


getVersion

public ServiceVersion getVersion()
Specified by:
getVersion in interface Service
Returns:
the version of the service

isOverride

public boolean isOverride()
By default it returns false

Specified by:
isOverride in interface Service
Returns:
true if the service can be replaced by another service with the same type

getInfo

public ServiceInfo getInfo()
Specified by:
getInfo in interface Service
Returns:
Information about the service

setName

public void setName(java.lang.String name)
Reset the name of the service


getName

public java.lang.String getName()
Description copied from interface: Service
This name should distinguish your service from another one. This is better for multiple service for the same service type to not introduce generic name like HTTP service or MAIL service here.

Specified by:
getName in interface Service
Returns:
the service name

run

protected void run()
User should override this method for running a task. This is the core of the service running state. Note that you shouldn't use a main loop like for standard thread. You just have to notify that this service use a main loop for the setThreadLoopMode method


setServiceContext

public void setServiceContext(ServiceContext serviceContext)

getServiceContext

public ServiceContext getServiceContext()

hasServiceContext

public boolean hasServiceContext()
Returns:
true if the serviceContext is available

setVerbose

protected void setVerbose(boolean verbose)
Decide for the verbosity


isVerbose

public boolean isVerbose()
This method will avoid to show the Information message type Look at the ServiceContect for more information

Specified by:
isVerbose in interface Service
Returns:
true if the service should write running information

isThreadable

public boolean isThreadable()
Check for the thread support from a thread pooling system


isProcessable

public boolean isProcessable()
Check for a loadbalancing for each new process


sendErrorMessage

public void sendErrorMessage(java.lang.String message)
Send an error message


sendInnerError

public void sendInnerError(java.lang.Exception exc)
Send an inner error exception


sendWarningMessage

public void sendWarningMessage(java.lang.String message)
Send a warning message


sendInformationMessage

public void sendInformationMessage(java.lang.String message)
Send an information message, this message will not be taken into account if the verbose value is false


sendCustomMessage

public void sendCustomMessage(int type,
                              java.lang.String message)
Send a custom message with a tied type


isPrivate

public boolean isPrivate()
Design the accessibility level for this service

Specified by:
isPrivate in interface Service
Returns:
true if you don't want this service cooperate with other service

setPrivate

protected void setPrivate(boolean privateState)
This method lets the user change the accessibility for service


getType

public ServiceType getType()
Description copied from interface: Service
This is an important function for cooperation, it says what it the role of a service such as a Web service. Defining a service type will help the JASon container to tied services

Specified by:
getType in interface Service
Returns:
the Service type

fireCriticalErrorDetected

protected void fireCriticalErrorDetected(java.lang.Exception exc)
This method must be called for critical error, it will avoid the service to start


setType

protected void setType(ServiceType type)
Parameters:
type - Reset the service type

manageRequest

public ServiceResponse manageRequest(ServiceRequest request)
                              throws ServiceException
Manage a request

Specified by:
manageRequest in interface Service
Parameters:
request - A request for the service
Returns:
the request's response
Throws:
ServiceException - when the request is not managed

manageRequestForType

protected ServiceResponse manageRequestForType(ServiceType type,
                                               ServiceRequest request)
                                        throws ServiceException
ServiceException

isEnabled

public boolean isEnabled()
This method is used only when starting the first time this service

Specified by:
isEnabled in interface Service
Returns:
true is the service can be run

setEnabled

protected void setEnabled(boolean enabled)
Change the enabled mode. This method must be called before the start method such as in the constructor

Parameters:
enabled - can start or not this service