org.locomotive.directory
Class NameService

java.lang.Object
  |
  +--org.locomotive.directory.NameService

public class NameService
extends java.lang.Object
implements DirectoryEventTypes

A naming service for Java components. This is the core functionality provided by NameService :

The configurations is organized in a tree structure of DirectoryContexts.

Synopsis

Open Issues

Version:
$Id: NameService.java,v 1.4 1999/05/05 10:19:32 gunnar Exp $
Author:
Gunnar Rønning (gunnar@candleweb.no).

Fields inherited from interface org.locomotive.directory.DirectoryEventTypes
DEBUG, ERROR, WARNING
 
Constructor Summary
NameService()
           
NameService(BeanParser beanParser, java.lang.String[] configPath)
          Create a new NameService.
NameService(java.lang.String[] configPath)
          Create a new NameService.
 
Method Summary
 void addDirectoryEventListener(DirectoryEventListener listener)
          Add a directory event listener.
 void addPropertyConverter(java.lang.Class clazz, PropertyConverter converter)
          Add property converter.
 void dispatchErrorEvent(java.lang.Object source, java.lang.String message, BeanConfiguration beanConfig, BeanProperty beanProperty, java.lang.Throwable throwable)
          Convenience method for creating and dispatching an event event.
 void dispatchEvent(DirectoryEvent event)
          Dispatch a DirectoryEvent to event listeners.
 void dispatchEvent(int type, java.lang.Object source, java.lang.String message, BeanConfiguration beanConfig, BeanProperty beanProperty, java.lang.Throwable throwable)
          Convenience method for creating and dispatching an event event.
protected  BeanConfiguration getBeanConfiguration(DirectoryContext context, java.lang.String name)
          Get the BeanConfiguration for a given component.
 BeanParser getBeanParser()
          The parser to use when parsing bean configuration files.
 int getCheckIntervalSeconds()
          Get the check interval seconds property.
 boolean getLogDebug()
          Control logging of debug messages for the bean directory.
 PropertyConverter getPropertyConverter(java.lang.Class clazz)
          Get the property converter for a specific class.
 void logDebug(BeanConfiguration beanConfig, java.lang.String message)
          Log a debug message for the bean directory.
protected  BeanConfiguration readBeanConfiguration(DirectoryContext context, java.lang.String name)
          Search the config path for a bean configuration file matching a name, and return a BeanConfiguration object describing the configuration.
 void registerComponent(ScopeLookup scopeLookup, java.lang.String scopeType, java.lang.String name, java.lang.Object component)
          Register a component with a given name and scope.
 void removeDirectoryEventListener(DirectoryEventListener listener)
          Remove a directory event listener.
 void removeScope(java.lang.Object scope)
          Remove a scope from the NameService.
 java.lang.Object resolve(ScopeLookup scopeLookup, DirectoryContext context, java.lang.String name)
          Resolve a component relative to a given directory context.
 java.lang.Object resolve(ScopeLookup scopeLookup, java.lang.String name)
          Resolve a component with a given scope.
 void setBeanParser(BeanParser beanParser)
          Set the parser to use when parsing bean configuration files.
 void setCheckIntervalSeconds(int checkIntervalSeconds)
          Check seconds defines how many seconds a BeanConfiguration read from file is valid before it will be reread if changed.
 void setConfigPath(java.lang.String[] configPath)
           
 void setLogDebug(boolean logDebug)
          Control logging of debug messages for the bean directory.
protected  void updateBeanConfigFromDir(BeanConfiguration beanConfig, java.lang.String configDir, java.lang.String name)
          Search one directory in the filesystem for a bean configuration file, and update the associated with the values in this file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NameService

public NameService(java.lang.String[] configPath)
Create a new NameService. Relative entries in the config path are computed relative to the current directory of the Java application.
Parameters:
configPath - A String array containing the configuration path.

NameService

public NameService(BeanParser beanParser,
                   java.lang.String[] configPath)
Create a new NameService. Relative entries in the config path are computed relative to the current directory of the Java application.
Parameters:
beanParser - The parser to use when parsing configuration files.
configPath - A String array containing the configuration path.

NameService

public NameService()
Method Detail

setConfigPath

public void setConfigPath(java.lang.String[] configPath)

resolve

public java.lang.Object resolve(ScopeLookup scopeLookup,
                                DirectoryContext context,
                                java.lang.String name)
Resolve a component relative to a given directory context.
Parameters:
scopeLookup - The object responsible for looking up the scope for this component.
context - The directory context to resolve this name relative to.
name - The name of the component.
Returns:
Returns the resolved component or null if the component not could be resolved.

resolve

public java.lang.Object resolve(ScopeLookup scopeLookup,
                                java.lang.String name)
Resolve a component with a given scope.
Parameters:
scopeLookup - The object responsible for looking up the scope for this component.
name - The name of the component.
Returns:
Returns the resolved component or null if the component not could be resolved.

registerComponent

public void registerComponent(ScopeLookup scopeLookup,
                              java.lang.String scopeType,
                              java.lang.String name,
                              java.lang.Object component)
Register a component with a given name and scope. This method is useful in two instances :
  1. When bootstrapping the NameService, you want it to know about itself.
  2. Generally when you want to register components that are created by other means than configuration files.
Parameters:
scopeLookup - The object used to lookup the scope for this component.
scopeType - The scope type for this component. This is only set if no configuration file is found for the component.
name - The full name of the component.
component - The component to register.

removeScope

public void removeScope(java.lang.Object scope)
Remove a scope from the NameService. This removes references to objects associated with this scope.
Parameters:
scope - The scope to remove.

getBeanConfiguration

protected BeanConfiguration getBeanConfiguration(DirectoryContext context,
                                                 java.lang.String name)
Get the BeanConfiguration for a given component.
Parameters:
context - The DirectoryContext to get this component is in.
name - The name of the component.
Returns:
The BeanConfiguration object for this component.

updateBeanConfigFromDir

protected void updateBeanConfigFromDir(BeanConfiguration beanConfig,
                                       java.lang.String configDir,
                                       java.lang.String name)
Search one directory in the filesystem for a bean configuration file, and update the associated with the values in this file. If a configuration file for the bean was not found, the beanConfig object will not be modified.
Parameters:
beanConfig - The BeanConfiguration object to update.
configDir - The filesystem directory used as the root when looking up this component.
name - The name of the bean configuration.

readBeanConfiguration

protected BeanConfiguration readBeanConfiguration(DirectoryContext context,
                                                  java.lang.String name)
Search the config path for a bean configuration file matching a name, and return a BeanConfiguration object describing the configuration.

Subclassing

This method could be overridden by subclasses to read bean configurations from sources other than the local filesystem.
Parameters:
context - The directory context of the bean configuration.
name - The name of the bean configuration.
Returns:
Returns a BeanConfiguration object, or null if the configuration was not found.

setCheckIntervalSeconds

public void setCheckIntervalSeconds(int checkIntervalSeconds)
Check seconds defines how many seconds a BeanConfiguration read from file is valid before it will be reread if changed. When deploying an application it is recommended that you set this parameter to -1(the default) to improve performance on component lookups. In a development situtation you might want to set this property to a low so changes in your configuration files are reflected without restarting the application.
Parameters:
checkSeconds - How often a BeanConfiguration should be checked for changes on file. -1 is the default value means that configuration files not will be reparsed. If the value is 0 the configuration file will always be reread if changed.

getCheckIntervalSeconds

public int getCheckIntervalSeconds()
Get the check interval seconds property. See setCheckIntervalSeconds for a detailed description.

getBeanParser

public BeanParser getBeanParser()
The parser to use when parsing bean configuration files.

setBeanParser

public void setBeanParser(BeanParser beanParser)
Set the parser to use when parsing bean configuration files.
Parameters:
beanParser - Object implementing the BeanParser interface.

addPropertyConverter

public void addPropertyConverter(java.lang.Class clazz,
                                 PropertyConverter converter)
Add property converter. Registers a new PropertyConverter object for a specific class.
Parameters:
clazz - The class of objects this PropertyConverter creates.
converter - An object implementing the PropertyConverter interface.

getPropertyConverter

public PropertyConverter getPropertyConverter(java.lang.Class clazz)
Get the property converter for a specific class.
Parameters:
clazz - The class the class to get the property converter for.
Returns:
The PropertyConverter object for this clazz, or null if there no property converter is registered.

addDirectoryEventListener

public void addDirectoryEventListener(DirectoryEventListener listener)
Add a directory event listener. A DirectoryEventListener will receive event notifications on all events in this directory.
Parameters:
listener - The DirectoryEventListener to add.

removeDirectoryEventListener

public void removeDirectoryEventListener(DirectoryEventListener listener)
Remove a directory event listener.
Parameters:
listener - The DirectoryEventListener to remove.

dispatchEvent

public void dispatchEvent(DirectoryEvent event)
Dispatch a DirectoryEvent to event listeners.

TODO: Implement an event queue, so we can have asynchronous event logging.

Parameters:
event - The event to dispatch.

dispatchEvent

public void dispatchEvent(int type,
                          java.lang.Object source,
                          java.lang.String message,
                          BeanConfiguration beanConfig,
                          BeanProperty beanProperty,
                          java.lang.Throwable throwable)
Convenience method for creating and dispatching an event event.
Parameters:
type - The type of this event.
source - The object that generated the event.
message - Descriptive message of the event.
beanConfig - The BeanConfiguration processed when the event occured, or null.
beanProperty - The BeanProperty processed when the event occured, or null.
throwable - The Throwable that triggered the event, or null.

dispatchErrorEvent

public void dispatchErrorEvent(java.lang.Object source,
                               java.lang.String message,
                               BeanConfiguration beanConfig,
                               BeanProperty beanProperty,
                               java.lang.Throwable throwable)
Convenience method for creating and dispatching an event event.
Parameters:
source - The object that generated the event.
message - Descriptive message of the event.
beanConfig - The BeanConfiguration processed when the event occured, or null.
beanProperty - The BeanProperty processed when the event occured, or null.
throwable - The Throwable that triggered the event, or null.

setLogDebug

public void setLogDebug(boolean logDebug)
Control logging of debug messages for the bean directory.
Parameters:
logDebug - True if debug messages should be logged, false otherwise.

getLogDebug

public boolean getLogDebug()
Control logging of debug messages for the bean directory.
Returns:
Returns True if debug messages should be logged, false otherwise.

logDebug

public void logDebug(BeanConfiguration beanConfig,
                     java.lang.String message)
Log a debug message for the bean directory.