mcconnell 02/02/06 01:35:07
Added: apps/enterprise/orb/src/java/org/apache/orb ORBContext.java
Log:
replaces org.apache.orb.Properties
Revision Changes Path
1.1
jakarta-avalon-cornerstone/apps/enterprise/orb/src/java/org/apache/orb/ORBContext.java
Index: ORBContext.java
===================================================================
/**
* File: ORBContext.java
* License: etc/LICENSE.TXT
* Copyright: Copyright (C) The Apache Software Foundation. All rights
reserved.
* Copyright: OSM SARL 2001-2002, All Rights Reserved.
*/
package org.apache.orb;
import java.util.Map;
import java.io.InputStream;
import java.io.IOException;
import java.io.File;
import java.net.URL;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.DefaultContext;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.avalon.framework.CascadingRuntimeException;
import org.apache.avalon.framework.logger.Logger;
/**
* The ORBContext is an interface facilitating access to the runtime ORB. The
* ORB instance exposed by the <code>getOrb</code> operation is an ORB
supporting
* the CORBA 2.3 portability specification.
* @author Stephen McConnell <[EMAIL PROTECTED]>
*/
public class ORBContext extends java.util.Properties implements Context
{
//=============================================================================
// state
//=============================================================================
private final Context m_parent;
private boolean m_readOnly = false;
private Configuration m_config;
private Configuration m_default_config;
private Logger m_logger;
//=============================================================================
// constructors
//=============================================================================
/**
* Create a Context with specified data and parent.
*
* @param data the initial context data
* @param logger the logger to be used for ORB logging
* @param context the context to be used during contextualization of
* ORB subsystems
* @param config optional configuration argument containing initializer
* configuation information (may be null)
* @param defaults optional fallback configuration argument containing
initializer
* configuation information (may be null)
*/
public ORBContext( final java.util.Properties data, final Logger logger,
final Context context, final Configuration config, final Configuration
defaults )
{
super( data );
m_logger = logger;
m_parent = context;
m_config = config;
m_default_config = defaults;
System.out.println( "setting app config to: " + m_config.getName() +
", " + config.getChildren().length );
System.out.println( "setting defaults config to: " +
m_default_config.getName() + ", " + defaults.getChildren().length );
}
//=============================================================================
// ORBContext
//=============================================================================
/**
* Returns the logger to be used for ORB logging and
* log enablement of ORB subsytems.
*/
public Logger getBaseLogger()
{
return m_logger;
}
/**
* Returns a Context object to be applied during the contextualization
* phase of ORB subsytems.
*
* @param name the name of the initalizer class
*/
public Context getBaseContext()
{
return m_parent;
}
/**
* Returns an initializer configuration based on a supplied
* initializer class name.
* @param name the name of the initalizer class
*/
public Configuration getConfigurationByClassName( String name )
{
return getConfigurationByClassName( name, true );
}
/**
* Returns an initializer configuration based on a supplied
* initializer class name.
* @param name the name of the initalizer class
* @param policy if no initializer configaration
* matching the supplied name can be found, and the policy argument
* is TRUE (the default), an non-null empty configuration will be
* returned, otherwise if policy is FALSE implementation will return
* a null value
*/
public Configuration getConfigurationByClassName( String name, boolean
policy )
{
if( m_config != null )
{
System.out.println("looking up app initializer config for : " +
name + " in " + m_config.getName() );
Configuration config = getByClassName( m_config, name );
if( config != null ) return config;
}
System.out.println("\tapplication profile return null" );
if( m_default_config != null )
{
System.out.println("looking up default initializer config for : "
+ name + " in " + m_default_config.getName() );
Configuration def = getByClassName( m_default_config, name );
if( def != null ) return def;
}
System.out.println("\tdefault profile return null" );
if( policy ) return new DefaultConfiguration( name, "-" );
return null;
}
/**
* Locate an initializer element based on a supplied class name.
*/
private Configuration getByClassName( Configuration config, String name )
{
System.out.println("\tlooking in " + config.getName() + ", " +
config.getChildren().length );
Configuration[] children = config.getChildren("initializer");
for( int i=0; i<children.length; i++ )
{
try
{
if( children[i].getAttribute("class").equals( name ) ) return
children[i];
}
catch( Exception e )
{
final String error = "Configuration " + m_config.getName()
+ " contains an initializer element with a missing 'class'
attribute.";
throw new CascadingRuntimeException( error, e );
}
}
return null;
}
//=============================================================================
// Context
//=============================================================================
/**
* Retrieve an item from the Context.
*
* @param key the key of item
* @return the item stored in context
* @exception ContextException if item not present
*/
public Object get( final Object key )
{
// handle special keys
if( key instanceof String )
{
final String name = ((String)key);
if( name.equalsIgnoreCase("LOGGER") ) return m_logger;
if( name.equalsIgnoreCase("CONTEXT") ) return m_parent;
if( name.equalsIgnoreCase("CONFIGURATION") ) return m_config;
}
// handle general keys
final Object data = super.get( key );
if( null != data ) return data;
// check the parent
if( null == m_parent ) return null;
try
{
return m_parent.get( key );
}
catch( Exception e )
{
return null;
}
}
//=============================================================================
// implementation
//=============================================================================
/**
* Helper method for adding items to Context.
*
* @param key the items key
* @param value the item
* @exception IllegalStateException if context is read only
* @exception IllegalArgumentException if the key is a string with
* conflicting with the reserved names "LOGGER", "CONTEXT" or
* "CONFIGURATION"
*/
public Object put( final Object key, final Object value )
throws IllegalStateException
{
if( key instanceof String )
{
final String name = ((String)key);
if(( name.equalsIgnoreCase("LOGGER") ) || (
name.equalsIgnoreCase("CONTEXT") )
|| ( name.equalsIgnoreCase("CONFIGURATION") ) ) throw new
IllegalArgumentException(
"Cannot put value under reserved key: " + key );
}
checkWriteable();
return put( key, value );
}
/**
* Make the context read-only.
* Any attempt to write to the context via put()
* will result in an IllegalStateException.
*/
public void makeReadOnly()
{
m_readOnly = true;
}
/**
* Utility method to check if context is writeable and if not throw
exception.
*
* @exception IllegalStateException if context is read only
*/
protected final void checkWriteable()
throws IllegalStateException
{
if( m_readOnly )
{
throw new IllegalStateException( "Context is read only and can
not be modified" );
}
}
/**
* Add properties to the instance based on a supplied configuration.
Property
* declarations with a file attribute will be expanded relative to the
current
* working directory.
*
* @see #addProperties( Configuration, File )
* @param configuration a configuration containing 'property' element
declarations
*/
public void addProperties( Configuration configuration ) throws Exception
{
addProperties( configuration, new File(
System.getProperty("user.work")) );
}
/**
* Add properties to the instance based on a supplied configuration.
* Any configuration elements of the following form will be translated
* to property values and added to the property set.
*
* <pre>
*
* <property name="myProperty" value="the-property-value"/>
* <property name="info" file="eggs.xml"/>
*
* </pre>
*
* The return value is suitable for passing to an ORB.init() method as a
* <code>java.util.Properties</code> instance.
*
* @param configuration a configuration containing 'property' element
declarations
* @param root the file path to be used in resolution of property
* element in the configuration that contain 'file' attributes as
* the property value
*/
public void addProperties( Configuration configuration, File root )
throws Exception
{
Configuration[] props = configuration.getChildren("property");
for( int i = 0; i< props.length; i++ )
{
Configuration child = props[i];
//
// every property must have a name
//
String name = "";
try
{
name = child.getAttribute("name");
}
catch( ConfigurationException noName )
{
final String error = "encountered a property without a
name";
throw new Exception ( error, noName );
}
//
// The value of a property is either declared directly under a
value attribute,
// or indirectory under a 'file' attribute. In the case of
'file' attributes
// we need to resolve this relative to this file before setting
the
// property value.
//
String value = "";
try
{
value = child.getAttribute("value");
}
catch( ConfigurationException noValueAttribute )
{
try
{
final String s = child.getAttribute("file");
File f = new File( root, s );
value = f.getAbsolutePath();
}
catch( ConfigurationException noFileAttribute )
{
String s = null;
try
{
s = child.getAttribute("url");
}
catch( Exception noURL )
{
final String error = "Found a property without
a 'value', 'file' or 'url' attribute";
throw new Exception( error, noURL );
}
if( s.startsWith("file:"))
{
try
{
URL base = root.toURL();
URL url = new URL( base, s );
value = url.toString();
}
catch( Exception unknown )
{
final String error = "Unexpected exception
while creating file:// URL value.";
throw new Exception( error, unknown );
}
}
else
{
try
{
URL url = new URL( s );
value = url.toString();
}
catch( Exception unknown )
{
final String error = "Unexpected exception
while creating URL value.";
throw new Exception( error
+ "\n" + "cause: " +
unknown.getClass().getName() + ", "
+ "\n" + unknown.getMessage(), unknown
);
}
}
}
}
setProperty( name, value );
}
}
//=============================================================================
// override properties operations
//=============================================================================
public void load(InputStream input ) throws IOException
{
checkWriteable();
super.load( input );
}
public synchronized Object setProperty(String key, String value) {
checkWriteable();
return super.put(key, value);
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>