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:33:29 UTC

cvs commit: jakarta-avalon-excalibur/threadcontext/src/java/org/apache/excalibur/threadcontext/impl AbstractThreadContextPolicy.java DefaultThreadContextPolicy.java

donaldp     02/04/25 23:33:29

  Modified:    threadcontext/src/java/org/apache/excalibur/threadcontext/impl
                        DefaultThreadContextPolicy.java
  Added:       threadcontext/src/java/org/apache/excalibur/threadcontext/impl
                        AbstractThreadContextPolicy.java
  Log:
  Extract super class for policy to make it easier to create new implementations.
  
  Revision  Changes    Path
  1.4       +2 -100    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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DefaultThreadContextPolicy.java	26 Apr 2002 06:29:40 -0000	1.3
  +++ DefaultThreadContextPolicy.java	26 Apr 2002 06:33:28 -0000	1.4
  @@ -7,10 +7,8 @@
    */
   package org.apache.excalibur.threadcontext.impl;
   
  -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
  @@ -18,12 +16,11 @@
    * class to extend for those wanting to write their own Policy.
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version $Revision: 1.3 $ $Date: 2002/04/26 06:29:40 $
  + * @version $Revision: 1.4 $ $Date: 2002/04/26 06:33:28 $
    */
   public class DefaultThreadContextPolicy
  -    implements ThreadContextPolicy
  +    extends AbstractThreadContextPolicy
   {
  -    private final HashMap m_contextEntrys = new HashMap();
   
       public DefaultThreadContextPolicy()
       {
  @@ -31,44 +28,6 @@
       }
   
       /**
  -     * 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
        * if CLASSLOADER key is present in context.
  @@ -107,61 +66,4 @@
               Thread.currentThread().setContextClassLoader( classLoader );
           }
       }
  -
  -    /**
  -     * Verify that the key/value pair is valid.
  -     *
  -     * @param key The key
  -     * @param value the value
  -     * @exception IllegalArgumentException if pair is not valid
  -     */
  -    public void verifyKeyValue( final String key, final Object value )
  -        throws IllegalArgumentException
  -    {
  -        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 ) )
  -        {
  -            final String message = "Key named '" + key +
  -                "' must be of type " + type.getName();
  -            throw new IllegalArgumentException( message );
  -        }
  -    }
  -
  -    /**
  -     * Get a value for specified key, using specified default if none present
  -     * and making sure value is of specified type.
  -     *
  -     * @param key the key used to lookup value
  -     * @param defaultValue the default value if the key does not specify value
  -     * @return the value
  -     */
  -    protected Object get( final ThreadContextAccessor accessor,
  -                          final String key,
  -                          final Object defaultValue )
  -    {
  -        final ContextEntry entry = getEntry( key );
  -        if( null == entry || !accessor.containsKey( key ) )
  -        {
  -            return defaultValue;
  -        }
  -        else
  -        {
  -            return accessor.get( key );
  -        }
  -    }
  -
   }
  
  
  
  1.1                  jakarta-avalon-excalibur/threadcontext/src/java/org/apache/excalibur/threadcontext/impl/AbstractThreadContextPolicy.java
  
  Index: AbstractThreadContextPolicy.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;
  
  import org.apache.excalibur.threadcontext.ThreadContextPolicy;
  import org.apache.excalibur.threadcontext.ThreadContextAccessor;
  import java.util.HashMap;
  
  /**
   * Abstract class to make it easier to implement
   * {@link ThreadContextPolicy} objects.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/04/26 06:33:28 $
   */
  public abstract class AbstractThreadContextPolicy
      implements ThreadContextPolicy
  {
      private final HashMap m_contextEntrys = new HashMap();
  
      /**
       * 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 );
      }
  
      /**
       * Verify that the key/value pair is valid.
       *
       * @param key The key
       * @param value the value
       * @exception IllegalArgumentException if pair is not valid
       */
      public void verifyKeyValue( final String key, final Object value )
          throws IllegalArgumentException
      {
          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 ) )
          {
              final String message = "Key named '" + key +
                  "' must be of type " + type.getName();
              throw new IllegalArgumentException( message );
          }
      }
  
      /**
       * Get a value for specified key, using specified default if none present
       * and making sure value is of specified type.
       *
       * @param key the key used to lookup value
       * @param defaultValue the default value if the key does not specify value
       * @return the value
       */
      protected Object get( final ThreadContextAccessor accessor,
                            final String key,
                            final Object defaultValue )
      {
          final ContextEntry entry = getEntry( key );
          if( null == entry || !accessor.containsKey( key ) )
          {
              return defaultValue;
          }
          else
          {
              return accessor.get( key );
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>