You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/04/26 08:29:41 UTC
cvs commit: jakarta-avalon-excalibur/threadcontext/src/java/org/apache/excalibur/threadcontext/impl ContextEntry.java DefaultThreadContextPolicy.java
donaldp 02/04/25 23:29:41
Modified: threadcontext/src/java/org/apache/excalibur/threadcontext/impl
DefaultThreadContextPolicy.java
Added: threadcontext/src/java/org/apache/excalibur/threadcontext/impl
ContextEntry.java
Log:
Refactor DefaultThreadContextPolicy to make it easier to extend by other users.
Revision Changes Path
1.3 +76 -23 jakarta-avalon-excalibur/threadcontext/src/java/org/apache/excalibur/threadcontext/impl/DefaultThreadContextPolicy.java
Index: DefaultThreadContextPolicy.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/threadcontext/src/java/org/apache/excalibur/threadcontext/impl/DefaultThreadContextPolicy.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DefaultThreadContextPolicy.java 26 Apr 2002 05:38:49 -0000 1.2
+++ DefaultThreadContextPolicy.java 26 Apr 2002 06:29:40 -0000 1.3
@@ -7,9 +7,10 @@
*/
package org.apache.excalibur.threadcontext.impl;
-import org.apache.excalibur.threadcontext.ThreadContextPolicy;
-import org.apache.excalibur.threadcontext.ThreadContextAccessor;
+import java.util.HashMap;
import java.util.Map;
+import org.apache.excalibur.threadcontext.ThreadContextAccessor;
+import org.apache.excalibur.threadcontext.ThreadContextPolicy;
/**
* Default <code>ThreadContextPolicy</code> that just maintains the
@@ -17,11 +18,56 @@
* class to extend for those wanting to write their own Policy.
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
- * @version $Revision: 1.2 $ $Date: 2002/04/26 05:38:49 $
+ * @version $Revision: 1.3 $ $Date: 2002/04/26 06:29:40 $
*/
public class DefaultThreadContextPolicy
implements ThreadContextPolicy
{
+ private final HashMap m_contextEntrys = new HashMap();
+
+ public DefaultThreadContextPolicy()
+ {
+ addEntry( CLASSLOADER, ClassLoader.class, true );
+ }
+
+ /**
+ * Add a context entry with specified key, type and
+ * whether null is valid.
+ *
+ * @param key the key of context entry
+ * @param type the type of value
+ * @param isNullValid true if value can be null, false
+ * otherwise
+ */
+ protected final void addEntry( final String key,
+ final Class type,
+ final boolean isNullValid )
+ {
+ final ContextEntry entry = new ContextEntry( key, type, isNullValid );
+ addEntry( entry );
+ }
+
+ /**
+ * Add a context entry to set of valid entrys.
+ *
+ * @param entry the entry
+ */
+ protected final void addEntry( final ContextEntry entry )
+ {
+ m_contextEntrys.put( entry.getKey(), entry );
+ }
+
+ /**
+ * Return entry associated with key if any.
+ *
+ * @param key the key to look for
+ * @return the entry associated with key if any.
+ */
+ protected final ContextEntry getEntry( final String key )
+ {
+ return (ContextEntry)m_contextEntrys.get( key );
+ }
+
/**
* The activate method is called when the ThreadContext
* is associated with a thread. This method sets the ContextClassLoader
@@ -39,7 +85,7 @@
store.put( CLASSLOADER, oldLoader );
final ClassLoader newLoader =
- (ClassLoader)get( accessor, CLASSLOADER, null, ClassLoader.class );
+ (ClassLoader)get( accessor, CLASSLOADER, null );
Thread.currentThread().setContextClassLoader( newLoader );
}
}
@@ -72,12 +118,26 @@
public void verifyKeyValue( final String key, final Object value )
throws IllegalArgumentException
{
- if( key.equals( CLASSLOADER ) &&
- null != value &&
- !( value instanceof ClassLoader ) )
+ final ContextEntry entry = getEntry( key );
+ if( null == entry )
+ {
+ final String message = "Unkown key named " + key;
+ throw new IllegalArgumentException( message );
+ }
+
+ if( !entry.isNullValid() && null == value )
+ {
+ final String message = "Key " + key + " must not have a null value";
+ throw new IllegalArgumentException( message );
+ }
+
+ final Class type = entry.getType();
+ if( null != value &&
+ !type.isInstance( value ) )
{
- throw new IllegalArgumentException( "Key " + key + " must be of type " +
- ClassLoader.class.getName() );
+ final String message = "Key named '" + key +
+ "' must be of type " + type.getName();
+ throw new IllegalArgumentException( message );
}
}
@@ -87,28 +147,21 @@
*
* @param key the key used to lookup value
* @param defaultValue the default value if the key does not specify value
- * @param type the expected type of value
* @return the value
*/
protected Object get( final ThreadContextAccessor accessor,
final String key,
- final Object defaultValue,
- final Class type )
+ final Object defaultValue )
{
- Object result = defaultValue;
-
- if( accessor.containsKey( key ) )
+ final ContextEntry entry = getEntry( key );
+ if( null == entry || !accessor.containsKey( key ) )
{
- result = accessor.get( key );
+ return defaultValue;
}
-
- if( null != result && !type.isInstance( result ) )
+ else
{
- throw new IllegalArgumentException( "Key " + key + " expected to access " +
- type.getName() + " but got " +
- result.getClass().getName() );
+ return accessor.get( key );
}
-
- return result;
}
+
}
1.1 jakarta-avalon-excalibur/threadcontext/src/java/org/apache/excalibur/threadcontext/impl/ContextEntry.java
Index: ContextEntry.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.threadcontext.impl;
/**
* Class represents possible context entries that will
* be handled.
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
* @version $Revision: 1.1 $ $Date: 2002/04/26 06:29:40 $
*/
public class ContextEntry
{
private final String m_key;
private final Class m_type;
private final boolean m_isNullValid;
/**
* Construct a ContextEntry with specified key, type and
* whether null is valid.
*
* @param key the key of context entry
* @param type the type of value
* @param isNullValid true if value can be null, false
* otherwise
*/
public ContextEntry( final String key,
final Class type,
final boolean isNullValid )
{
m_key = key;
m_type = type;
m_isNullValid = isNullValid;
}
/**
* Return the key used to look up Context value.
*
* @return the key used to look up Context value.
*/
public String getKey()
{
return m_key;
}
/**
* Return the required class of value (if not null).
*
* @return the required class of value (if not null).
*/
public Class getType()
{
return m_type;
}
/**
* Return true if the value for key be null.
*
* @return true if the value for key be null, false otherwise.
*/
public boolean isNullValid()
{
return m_isNullValid;
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>