mcconnell 2002/06/16 07:09:27
Modified: configuration/src/java/org/apache/excalibur/configuration
CascadingConfiguration.java ConfigurationUtil.java
Added: configuration/src/java/org/apache/excalibur/configuration
ContextFactory.java
Log:
replaces excalure/context/ContextUtil
Revision Changes Path
1.3 +2 -2
jakarta-avalon-excalibur/configuration/src/java/org/apache/excalibur/configuration/CascadingConfiguration.java
Index: CascadingConfiguration.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/configuration/src/java/org/apache/excalibur/configuration/CascadingConfiguration.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- CascadingConfiguration.java 13 May 2002 12:17:41 -0000 1.2
+++ CascadingConfiguration.java 16 Jun 2002 14:09:27 -0000 1.3
@@ -303,7 +303,7 @@
/**
* Return the <code>boolean</code> value of the specified parameter
contained
- * in this node.<br>
+ * in this node.
*
* @param paramName The name of the parameter you ask the value of.
* @return boolean value of attribute
@@ -395,7 +395,7 @@
}
/**
- * Return the <code>long</code> value of the node.<br>
+ * Return the <code>long</code> value of the node.
*
* @return the value of the node.
* @exception ConfigurationException If conversion to <code>long</code>
fails.
1.3 +107 -0
jakarta-avalon-excalibur/configuration/src/java/org/apache/excalibur/configuration/ConfigurationUtil.java
Index: ConfigurationUtil.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/configuration/src/java/org/apache/excalibur/configuration/ConfigurationUtil.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ConfigurationUtil.java 13 May 2002 12:17:41 -0000 1.2
+++ ConfigurationUtil.java 16 Jun 2002 14:09:27 -0000 1.3
@@ -10,7 +10,12 @@
package org.apache.excalibur.configuration;
+import java.util.Vector;
+
import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.CascadingRuntimeException;
+import org.apache.avalon.framework.configuration.DefaultConfiguration;
/**
* General utility supporting static operations for generating string
@@ -19,6 +24,8 @@
*/
public class ConfigurationUtil
{
+ private static final Configuration[] EMPTY_CONFS = new Configuration[0];
+
/**
* Returns a simple string representation of the the supplied
configuration.
* @param config a configuration
@@ -68,6 +75,106 @@
buffer.append( "/>" );
}
}
+ }
+
+ /**
+ * Return all occurance of a configuration child containing the supplied
attribute name.
+ * @param config the configuration
+ * @param element the name of child elements to select from the
configuration
+ * @param attribute the attribute name to filter
+ * @param value the attribute value to match (null will match any
attribute value)
+ * @return an array of configuration instances matching the query
+ */
+ public static Configuration[] match( Configuration config, String
element, String attribute )
+ {
+ return match( config, element, attribute, null );
+ }
+
+ /**
+ * Return occurance of a configuration child containing the supplied
attribute name and value.
+ * @param config the configuration
+ * @param element the name of child elements to select from the
configuration
+ * @param attribute the attribute name to filter
+ * @param value the attribute value to match (null will match any
attribute value)
+ * @return an array of configuration instances matching the query
+ */
+ public static Configuration[] match(
+ final Configuration config, final String element, final String
attribute, final String value )
+ {
+ Vector vector = new Vector();
+
+ Configuration[] children = config.getChildren( element );
+ for( int i=0; i<children.length; i++ )
+ {
+ String v = children[i].getAttribute( attribute, null );
+ if( v != null )
+ {
+ if(( value == null ) || v.equals( value ))
+ {
+ // it's a match
+ vector.add( children[i] );
+ }
+ }
+ }
+ return (Configuration[]) vector.toArray( EMPTY_CONFS );
+ }
+
+ /**
+ * Return the first occurance of a configuration child containing the
supplied attribute name and value
+ * or create a new empty configuration if no match found.
+ * @param config the configuration
+ * @param element the name of child elements to select from the
configuration
+ * @param attribute the attribute name to filter
+ * @param value the attribute value to match (null will match any
attribute value)
+ * @return a configuration instances matching the query or empty
configuration
+ */
+ public static Configuration matchFirstOccurance(
+ Configuration config, String element, String attribute, String value )
+ {
+ try
+ {
+ return matchFirstOccurance( config, element, attribute, value,
true );
+ }
+ catch( ConfigurationException e )
+ {
+ // will not happen
+ throw new CascadingRuntimeException("Unexpected exception
condition.", e );
+ }
+ }
+
+
+ /**
+ * Return the first occurance of a configuration child containing the
supplied attribute
+ * name and value. If the supplied creation policy if TRUE and no match
is found, an
+ * empty configuration instance is returned, otherwise a null will
returned.
+ * @param config the configuration
+ * @param element the name of child elements to select from the
configuration
+ * @param attribute the attribute name to filter
+ * @param value the attribute value to match (null will match any
attribute value)
+ * @param create the creation policy if no match
+ * @return a configuration instances matching the query
+ */
+ public static Configuration matchFirstOccurance(
+ Configuration config, String element, String attribute, String value,
boolean create )
+ throws ConfigurationException
+ {
+ Configuration[] children = config.getChildren( element );
+ for( int i=0; i<children.length; i++ )
+ {
+ String v = children[i].getAttribute( attribute, null );
+ if( v != null )
+ {
+ if(( value == null ) || v.equals( value ))
+ {
+ // it's a match
+ return children[i];
+ }
+ }
+ }
+
+ if( create )
+ return new DefaultConfiguration( element, null );
+ return null;
}
}
1.1
jakarta-avalon-excalibur/configuration/src/java/org/apache/excalibur/configuration/ContextFactory.java
Index: ContextFactory.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.TXT file.
*/
package org.apache.excalibur.configuration;
import java.lang.reflect.Constructor;
import java.util.Hashtable;
import java.util.Map;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.logger.Logger;
/**
* ContextFactory is a utility class that provides support for the creation
* context instances based on a XML context desciption.
*
* @version $Id: ContextFactory.java,v 1.1 2002/06/16 14:09:27 mcconnell Exp $
* @author Stephen McConnell <[EMAIL PROTECTED]>
*/
public class ContextFactory
{
//==========================================================
// context utilities
//==========================================================
/**
* Create context-attributes from entrys within <context/>-tag in
config
* @param config the context configuration
* @return Context a context instance
* @exception ConfigurationException if a context related error occurs
*/
public static Context createContextFromConfiguration( Configuration
config )
throws ConfigurationException
{
return createContextFromConfiguration( null, config );
}
/**
* Create context-attributes from entrys within <context/>-tag in
config
* @param parent the parent context
* @param config the configuration element describing the context
parameters
* @return Context a context instance
* @exception ConfigurationException if a context related error occurs
*/
public static Context createContextFromConfiguration(
Context parent, Configuration config )
throws ConfigurationException
{
return createContextFromConfiguration( parent, config, null );
}
/**
* Create context-attributes from entrys within <context/>-tag in
config
* @param parent the parent context
* @param config the configuration element describing the context
parameters
* @param log a logging channel
* @return Context a context instance
* @exception ConfigurationException if a context related error occurs
*/
public static Context createContextFromConfiguration(
Context parent, Configuration config, Logger log )
throws ConfigurationException
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String contextClassName = config.getAttribute(
"class",
"org.apache.avalon.framework.context.DefaultContext" );
Class contextClass = null;
try
{
contextClass = loader.loadClass( contextClassName );
}
catch( ClassNotFoundException cnfe )
{
throw new ConfigurationException(
"Could not find context class " + contextClassName, cnfe );
}
Map map = new Hashtable();
Context context = null;
try
{
Constructor constructor = contextClass.getConstructor(
new Class[]{Map.class, Context.class} );
context = (Context)constructor.newInstance( new Object[]{map,
parent} );
}
catch( Throwable e )
{
throw new ConfigurationException(
"Unexpected exception while creating custom context form "
+ contextClassName, e );
}
final Configuration[] entrys = config.getChildren( "entry" );
for( int i = 0; i < entrys.length; i++ )
{
final String className = entrys[ i ].getAttribute(
"type", "java.lang.String" );
final String paramName = entrys[ i ].getAttribute(
"name", null );
if( paramName == null )
{
throw new ConfigurationException(
"missing name for context-entry" );
}
try
{
Class[] params;
Object[] values;
Configuration entry = entrys[ i ];
if( entry.getAttribute( "value", null ) != null )
{
// Single argument String-constructor
params = new Class[ 1 ];
params[ 0 ] = Class.forName( "java.lang.String" );
Class[] consObjects = {Class.forName( "java.lang.String"
)};
Constructor cons = params[ 0 ].getConstructor(
consObjects );
values = new Object[ 1 ];
Object[] consValues = {
getContextValue( map, entry.getAttribute( "value" ) )
};
values[ 0 ] = cons.newInstance( consValues );
if( log != null )
{
log.debug( "add context-attr '" + paramName
+ "' class '" + className
+ "' with value '" + consValues[ 0 ] + "'"
);
}
}
else
{
// Multiple argument constructor
Configuration[] entryChilds = entry.getChildren(
"parameter" );
params = new Class[ entryChilds.length ];
values = new Object[ entryChilds.length ];
if( log != null )
{
log.debug( "add context-attr '" + paramName
+ "' class '" + className + "' with "
+ entryChilds.length + " values" );
}
for( int p = 0; p < entryChilds.length; p++ )
{
String paramClassName = entryChilds[ p ].getAttribute(
"type", "java.lang.String" );
String paramValue = entryChilds[ p ].getAttribute(
"value", null );
if( paramValue == null )
{
if( log != null )
{
log.debug( "value" + ( p + 1 ) + ": class '"
+ paramClassName + "' no value" );
}
}
else
{
paramValue = getContextValue( map, paramValue );
if( log != null )
{
log.debug( "value" + ( p + 1 ) + ": class '"
+ paramClassName + "' value '" +
paramValue + "'" );
}
}
try
{
params[ p ] = loader.loadClass( paramClassName );
if( paramValue == null )
{
values[ p ] = params[ p ].newInstance();
}
else
{
Class[] consObjects = {Class.forName(
"java.lang.String" )};
Constructor cons = params[ p
].getConstructor( consObjects );
Object[] consValues = {paramValue};
values[ p ] = cons.newInstance( consValues );
}
}
catch( ClassNotFoundException e )
{
// Class not found
// -> perhaps a primitve class?
if( paramClassName.equals( "int" ) )
{
params[ p ] = int.class;
values[ p ] = new Integer( paramValue );
}
else if( paramClassName.equals( "short" ) )
{
params[ p ] = short.class;
values[ p ] = new Short( paramValue );
}
else if( paramClassName.equals( "long" ) )
{
params[ p ] = long.class;
values[ p ] = new Long( paramValue );
}
else if( paramClassName.equals( "byte" ) )
{
params[ p ] = byte.class;
values[ p ] = new Byte( paramValue );
}
else if( paramClassName.equals( "double" ) )
{
params[ p ] = double.class;
values[ p ] = new Double( paramValue );
}
else if( paramClassName.equals( "float" ) )
{
params[ p ] = float.class;
values[ p ] = new Float( paramValue );
}
else if( paramClassName.equals( "char" ) )
{
params[ p ] = char.class;
values[ p ] = new Character(
paramValue.charAt( 0 ) );
}
else if( paramClassName.equals( "boolean" ) )
{
params[ p ] = boolean.class;
values[ p ] = new Boolean( paramValue );
}
else
{
throw new ConfigurationException(
"incorrect type '" + paramClassName
+ "' for context-attribute '" + paramName
+ "'", e );
}
}
}
}
Class paramClass;
try
{
paramClass = loader.loadClass( className );
}
catch( final ClassNotFoundException e )
{
throw new ConfigurationException(
"incorrect type '" + className
+ "' for context-attribute '" + paramName + "'",
e );
}
Object paramInstance;
if( params.length > 0 )
{
// using param contructor
Constructor cons = paramClass.getConstructor( params );
paramInstance = cons.newInstance( values );
}
else
{
// using default constructor
paramInstance = paramClass.newInstance();
}
map.put( paramName, paramInstance );
}
catch( ConfigurationException e )
{
throw e;
}
catch( Exception e )
{
throw new ConfigurationException(
"Error add context-attribute '" + paramName
+ "' from Configuration", e );
}
}
return context;
}
/**
* Resolving an attribute value by replacing ${context-param} with
* the corresponding param out of current context.
* @param map a map
* @param rawValue a raw value
* @return String the context attribute value
* @exception ConfigurationException if context-param does not exists
*/
private static String getContextValue( Map map, String rawValue )
throws ConfigurationException
{
StringBuffer result = new StringBuffer( "" );
int i = 0;
int j = -1;
while( ( j = rawValue.indexOf( "${", i ) ) > -1 )
{
if( i < j )
{
result.append( rawValue.substring( i, j ) );
}
int k = rawValue.indexOf( '}', j );
final String ctxName = rawValue.substring( j + 2, k );
final Object ctx = map.get( ctxName );
if( ctx == null )
{
throw new ConfigurationException(
"missing entry '" + ctxName + "' in Context" );
}
result.append( ctx.toString() );
i = k + 1;
}
if( i < rawValue.length() )
{
result.append( rawValue.substring( i, rawValue.length() ) );
}
return result.toString();
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>