org.gcube.common.core.contexts
Class GCUBEContext

java.lang.Object
  extended by org.gcube.common.core.contexts.GCUBEContext
Direct Known Subclasses:
GCUBEPortTypeContext, GCUBERemotePortTypeContext, GCUBEServiceContext, GHNContext

public abstract class GCUBEContext
extends Object

Base implementation for contexts, i.e. centralised information managers and utility providers for key implementation components (e.g. services, porttypes, nodes). From this implementation, contexts inherit facilities for handling component configuration. Configuration may be accessed from files in the file system or the classpath and then stored in local state or in a JNDI naming service (typically that of the gCore container). Storage may occur at any point of a context's lifetime, but it is always transient. Its persistence is largely responsibility of subclasses, though this implementation offers some transparencies for the management of file backups.

Author:
Fabio Simeoni (University of Strathclyde), Manuele Simi (ISTI-CNR)

Field Summary
protected  GCUBELog logger
          Object logger.
protected  Map<Thread,Long> timers
          Timers, indexed by thread
 
Constructor Summary
GCUBEContext()
          Creates and initialises an instance.
 
Method Summary
static void debugContext(Context ctxt, String... indent)
          Convenience method for testing.
 File getFile(String path, boolean... writeMode)
          Gives read or write access to a File.
protected  Context getJNDIContext()
          Returns the initial JNDI context of the naming service.
 String getName()
          Returns the name of the context as used in log entries.
 Object getProperty(String prop, boolean... required)
          Resolve a configuration property against the naming service.
 InputStream getResource(String resourceName)
          Returns a classpath resource given its path.
 float getTiming()
          Returns the timing for the current thread.
static void printContext(Context context)
           
 void resetTimer()
          Reset the timer for the current thread.
protected  void setJNDIContext(Context jndiContext)
          Sets the initial JNDI context of the naming service.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final GCUBELog logger
Object logger.


timers

protected Map<Thread,Long> timers
Timers, indexed by thread

Constructor Detail

GCUBEContext

public GCUBEContext()
Creates and initialises an instance.

Method Detail

getJNDIContext

protected Context getJNDIContext()
Returns the initial JNDI context of the naming service.

Returns:
the naming context.

setJNDIContext

protected void setJNDIContext(Context jndiContext)
Sets the initial JNDI context of the naming service.

Parameters:
jndiContext - the naming context.

getProperty

public Object getProperty(String prop,
                          boolean... required)
                   throws RuntimeException
Resolve a configuration property against the naming service. Resolution is relative to the root JNDI context of the naming service and the JNDI name associated with the context.

Parameters:
prop - the property.
required - (optional) true if the property is required, false or omitted if property is optional. The only implication is a different handling of failure and logging. Failure in resolving optional properties results in a runtime exception and fatal log entry, whereas failure in resolving optional properties results in a null result and warning in the log.
Returns:
the value of the property, or null if the property was optional and could not be resolved.
Throws:
RuntimeException - if the property is required but cannot be resolved.

getName

public String getName()
Returns the name of the context as used in log entries. By default, it returns the simple name of the context implementation. Override to specialize the log (e.g. with the name of the component associated with the context).

Returns:
the name.

getResource

public InputStream getResource(String resourceName)
Returns a classpath resource given its path. By default, a relative path is resolved with respect to the context implementation.

Returns:
the resource, or null if the path could not be resolved.

getFile

public File getFile(String path,
                    boolean... writeMode)
             throws IllegalArgumentException
Gives read or write access to a File. Write access induces backups and read access relies on backups to recover from failures.

Parameters:
path - the file path.
writeMode - (optional) the access mode, true for write access and false for read access (default).
Returns:
the file.
Throws:
IllegalArgumentException - if access is in write mode and the path is to a folder.

debugContext

public static void debugContext(Context ctxt,
                                String... indent)
                         throws Exception
Convenience method for testing. Shows the content of the naming service, recursively and starting from a given context.

Parameters:
ctxt - the starting context.
indent - used internally to format debug information in line with the context hierarchy.
Throws:
Exception - if the content could not be shown.

printContext

public static void printContext(Context context)

getTiming

public float getTiming()
Returns the timing for the current thread.

Returns:
the timing.

resetTimer

public void resetTimer()
Reset the timer for the current thread.



Copyright © 2012. All Rights Reserved.