jason.core
Class ServiceContext

java.lang.Object
  |
  +--jason.core.ServiceContext
All Implemented Interfaces:
ServiceContextListener

public class ServiceContext
extends java.lang.Object
implements ServiceContextListener

Created Wed Mar 27 11:33:57 2002

Service context. This class is used by both the ServiceContainer and the Service classes.

Author:
Alexandre Brillant (abrillant@wanadoo.fr)
See Also:
ServiceContainer, Service

Constructor Summary
ServiceContext(ServiceContainer sc)
           
 
Method Summary
 void addServiceContextListener(ServiceContextListener listener)
          Add a listener for the context state
 void alertAll(java.lang.String message)
          Alert all registered user with this message.
 void alertUser(java.lang.String host, java.lang.String to, java.lang.String message)
          Send alert by mail through the Alert service
 void cleanWorkingDirectory(boolean depth)
          Clean the working directory content.
 ServiceResponse fireRequestForType(ServiceType type, ServiceRequest request)
          Get all service for the type and return the response of the first one that can answer to the response.
 ServiceResponse fireRequestForType(ServiceType type, ServiceRequest request, int criteria)
          Get all service for the type and return the response of the first one that can answer to the response.
 Cache getCache()
           
 Domain getDomain()
           
 MessageHandler getMessageHandler()
           
 java.lang.String getName()
           
 java.util.Enumeration getPropertiesName()
           
 java.lang.Object getProperty(java.lang.String name)
           
 ServiceContainer getServiceContainer()
           
 Service[] getServiceForType(ServiceType type)
          This method will return any service matching the good type even bad service
 Service[] getServiceForType(ServiceType type, int criteria)
          This method will return an array of service matching the good type sorting it with the criteria.
 Service[] getServices()
           
 ThreadCapsule getThreadCapsule()
           
 java.io.File getWorkingDirectory()
           
 java.io.File getWorkingDirectoryFile(java.lang.String f)
           
 java.io.FileInputStream getWorkingDirectoryFileInputStream(java.lang.String f)
           
 java.io.FileOutputStream getWorkingDirectoryFileOutputStream(java.lang.String f)
           
 java.io.FileReader getWorkingDirectoryFileReader(java.lang.String f)
           
 java.io.FileWriter getWorkingDirectoryFileWriter(java.lang.String f)
           
 boolean isEnabledCacheForRequestDispatch()
           
 boolean isProcessable()
          Check if the service can be handled in a new process
 boolean isTest()
          Check if a starting test is enabled
 boolean isThreadable()
          Check if the service can be handled in a new thread
 boolean isVerbose()
          Check if verbose is enabled
 void notifyCriticalError(java.lang.Exception exc)
          This method must be called for critical error
 void notifyInit()
          Notify the service context that the Service is initialized
 void notifyRun()
          Notify the service context that the Service is runned.
 void notifyStart()
          Notify the service context that the Service is started
 void notifyStartTask()
          Notify the service context that a Sub task is starting for a service
 void notifyStop()
          Notify the service context that the Service is stopped
 void notifyStopTask()
          Notify the service context that a Sub task is stopping for a service
 void notifyTask()
          Notify the service context that the Service has managed a Task It should be called when a work is managed by the Service, this is used for benchmark
 void removeServiceContextListener(ServiceContextListener listener)
          Remove a listener for the contexte state
 void sendCustomMessage(java.lang.String service, int type, java.lang.String content)
           
 void sendErrorMessage(java.lang.String service, java.lang.String content)
           
 void sendInformationMessage(java.lang.String service, java.lang.String content)
           
 void sendInnerError(java.lang.Exception exc)
          Support for inner class exception
 void sendWarningMessage(java.lang.String service, java.lang.String content)
           
 void setEnabledCacheForRequestDispatch(boolean ok)
          Enable/Disable the inner cache for dispatching a request
 void setProperty(java.lang.String name, java.lang.Object value)
          Update a service property.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServiceContext

public ServiceContext(ServiceContainer sc)
Method Detail

addServiceContextListener

public void addServiceContextListener(ServiceContextListener listener)
Add a listener for the context state


removeServiceContextListener

public void removeServiceContextListener(ServiceContextListener listener)
Remove a listener for the contexte state


getWorkingDirectory

public java.io.File getWorkingDirectory()
Returns:
the service working directory

getWorkingDirectoryFile

public java.io.File getWorkingDirectoryFile(java.lang.String f)
Returns:
the working directory for the file f

getWorkingDirectoryFileInputStream

public java.io.FileInputStream getWorkingDirectoryFileInputStream(java.lang.String f)
                                                           throws java.io.IOException
Returns:
an inputstream for the file f
java.io.IOException

getWorkingDirectoryFileOutputStream

public java.io.FileOutputStream getWorkingDirectoryFileOutputStream(java.lang.String f)
                                                             throws java.io.IOException
Returns:
an outputstream for the file f
java.io.IOException

getWorkingDirectoryFileReader

public java.io.FileReader getWorkingDirectoryFileReader(java.lang.String f)
                                                 throws java.io.IOException
Returns:
a reader for the file f
java.io.IOException

getWorkingDirectoryFileWriter

public java.io.FileWriter getWorkingDirectoryFileWriter(java.lang.String f)
                                                 throws java.io.IOException
Returns:
a writer for the file f
java.io.IOException

cleanWorkingDirectory

public void cleanWorkingDirectory(boolean depth)
Clean the working directory content.

Parameters:
depth - true to clean all the directory content with all sub-directory, else it cleans only the conctent at the first level

isVerbose

public boolean isVerbose()
Check if verbose is enabled


isTest

public boolean isTest()
Check if a starting test is enabled


isThreadable

public boolean isThreadable()
Check if the service can be handled in a new thread


isProcessable

public boolean isProcessable()
Check if the service can be handled in a new process


getServiceContainer

public ServiceContainer getServiceContainer()

getMessageHandler

public MessageHandler getMessageHandler()

getName

public java.lang.String getName()

getServices

public Service[] getServices()
Returns:
available services

getServiceForType

public Service[] getServiceForType(ServiceType type)
This method will return any service matching the good type even bad service

Returns:
available services matching the type

getServiceForType

public Service[] getServiceForType(ServiceType type,
                                   int criteria)
This method will return an array of service matching the good type sorting it with the criteria. This criteria are SERVICE_NO_SKILL_CRITERIA, SERVICE_HOST_SKILL_CRITERIA, SERVICE_REQUEST_SKILL_CRITERIA from the ServicesManager class.

Returns:
available services matching the type

fireRequestForType

public ServiceResponse fireRequestForType(ServiceType type,
                                          ServiceRequest request)
                                   throws ServiceException
Get all service for the type and return the response of the first one that can answer to the response. Note that this method will try any service for getting the response even the same previous bad service.

Parameters:
type - A Service type
request - a Service request
Throws:
ServiceException - for invalid service request

setEnabledCacheForRequestDispatch

public void setEnabledCacheForRequestDispatch(boolean ok)
Enable/Disable the inner cache for dispatching a request


isEnabledCacheForRequestDispatch

public boolean isEnabledCacheForRequestDispatch()
Returns:
the state for enabling/disabling the inner cache

fireRequestForType

public ServiceResponse fireRequestForType(ServiceType type,
                                          ServiceRequest request,
                                          int criteria)
                                   throws ServiceException
Get all service for the type and return the response of the first one that can answer to the response. Note that this method will try to find the best service depending on your criteria choice. This criteria are SERVICE_NO_SKILL_CRITERIA, SERVICE_HOST_SKILL_CRITERIA, SERVICE_REQUEST_SKILL_CRITERIA from the ServicesManager class.

Parameters:
type - A Service type
request - a Service request
criteria - SERVICE_NO_SKILL_CRITERIA, SERVICE_HOST_SKILL_CRITERIA, SERVICE_REQUEST_SKILL_CRITERIA
Throws:
ServiceException - for invalid service request

getPropertiesName

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

getProperty

public java.lang.Object getProperty(java.lang.String name)
Returns:
a service property

setProperty

public void setProperty(java.lang.String name,
                        java.lang.Object value)
                 throws ServiceException
Update a service property. This change will be illegal for private service

ServiceException

getDomain

public Domain getDomain()
Returns:
the current service domain

getThreadCapsule

public ThreadCapsule getThreadCapsule()
Returns:
a ThreadCapsule for Thread Pooling

getCache

public Cache getCache()
Returns:
a cache. By default this is the BasicCacheImpl

sendErrorMessage

public void sendErrorMessage(java.lang.String service,
                             java.lang.String content)

alertAll

public void alertAll(java.lang.String message)
Alert all registered user with this message. This alert doesn't take into account the type of alert : warning, error or exception

Parameters:
message - a String value

sendInnerError

public void sendInnerError(java.lang.Exception exc)
Support for inner class exception


alertUser

public void alertUser(java.lang.String host,
                      java.lang.String to,
                      java.lang.String message)
Send alert by mail through the Alert service

Parameters:
host - host name (like smtp.server)
to - user mail
message - Text to send

sendWarningMessage

public void sendWarningMessage(java.lang.String service,
                               java.lang.String content)

sendInformationMessage

public void sendInformationMessage(java.lang.String service,
                                   java.lang.String content)

sendCustomMessage

public void sendCustomMessage(java.lang.String service,
                              int type,
                              java.lang.String content)

notifyInit

public void notifyInit()
Notify the service context that the Service is initialized

Specified by:
notifyInit in interface ServiceContextListener

notifyStart

public void notifyStart()
Notify the service context that the Service is started

Specified by:
notifyStart in interface ServiceContextListener

notifyStop

public void notifyStop()
Notify the service context that the Service is stopped

Specified by:
notifyStop in interface ServiceContextListener

notifyRun

public void notifyRun()
Notify the service context that the Service is runned. Please call this method each time the run method is called


notifyTask

public void notifyTask()
Notify the service context that the Service has managed a Task It should be called when a work is managed by the Service, this is used for benchmark


notifyStartTask

public void notifyStartTask()
Notify the service context that a Sub task is starting for a service


notifyStopTask

public void notifyStopTask()
Notify the service context that a Sub task is stopping for a service


notifyCriticalError

public void notifyCriticalError(java.lang.Exception exc)
This method must be called for critical error