ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant-myrmidon/aut/src/java/org/apache/aut/jprocess JavaProcessContextPolicy.java
Date Fri, 26 Apr 2002 07:49:55 GMT
donaldp     02/04/26 00:49:55

  Added:       aut/src/java/org/apache/aut/jprocess
                        JavaProcessContextPolicy.java
  Log:
  Implement a ContextPolicy that binds the
  following items to a thread;
  
  * Properties accessed from System.getProperties()
  * System.in, System.out, System.err
  
  In the future we will also isolate other things
  such as
  * URLStreamHandlerFactory
  * JNDI InitialContextFactory
  
  However given how rare it is for a task to use these
  things (currently only 2 use/set URLStreamHandlerFactory
  and none of core ant tasks set InitialContextFactory) this
  will be a lower priority.
  
  Revision  Changes    Path
  1.1                  jakarta-ant-myrmidon/aut/src/java/org/apache/aut/jprocess/JavaProcessContextPolicy.java
  
  Index: JavaProcessContextPolicy.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.aut.jprocess;
  
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.URLStreamHandlerFactory;
  import java.util.Map;
  import java.util.Properties;
  import javax.naming.spi.InitialContextFactory;
  import org.apache.excalibur.threadcontext.ThreadContextAccessor;
  import org.apache.excalibur.threadcontext.impl.DefaultThreadContextPolicy;
  
  /**
   * This is a basic extension of ThreadContextPolicy that
   * just defines new constants that will be handled.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/04/26 07:49:55 $
   */
  public class JavaProcessContextPolicy
      extends DefaultThreadContextPolicy
  {
      public static final String INPUT = InputStream.class.getName();
      public static final String OUTPUT = OutputStream.class.getName();
      public static final String ERROR = OutputStream.class.getName() + "/Error";
      public static final String PROPERTIES = Properties.class.getName();
      public static final String URL_FACTORY = URLStreamHandlerFactory.class.getName();
      public static final String JNDI_FACTORY = InitialContextFactory.class.getName();
  
      /**
       * Construct the policy object and register the types
       * that it is capable of handling.
       */
      public JavaProcessContextPolicy()
      {
          addEntry( INPUT, InputStream.class, true );
          addEntry( OUTPUT, OutputStream.class, true );
          addEntry( ERROR, OutputStream.class, true );
          addEntry( PROPERTIES, Properties.class, true );
          addEntry( URL_FACTORY, URLStreamHandlerFactory.class, true );
          addEntry( JNDI_FACTORY, InitialContextFactory.class, true );
      }
  
      /**
       * 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.
       *
       * @param accessor the accessor to retrieve values from ThreadContext
       */
      public void activate( final ThreadContextAccessor accessor,
                            final Map store )
      {
          super.activate( accessor, store );
  
          if( accessor.containsKey( INPUT ) )
          {
              final InputStream newStream =
                  (InputStream)get( accessor, INPUT, null );
  
              final InputStream oldStream =
                  StdioRedirector.bindInput( newStream );
              store.put( INPUT, oldStream );
          }
  
          if( accessor.containsKey( OUTPUT ) )
          {
              final OutputStream newStream =
                  (OutputStream)get( accessor, OUTPUT, null );
  
              final OutputStream oldStream =
                  StdioRedirector.bindOutput( newStream );
              store.put( OUTPUT, oldStream );
          }
  
          if( accessor.containsKey( ERROR ) )
          {
              final OutputStream newStream =
                  (OutputStream)get( accessor, ERROR, null );
  
              final OutputStream oldStream =
                  StdioRedirector.bindError( newStream );
              store.put( OUTPUT, oldStream );
          }
  
          if( accessor.containsKey( PROPERTIES ) )
          {
              final Properties newProperties =
                  (Properties)get( accessor, PROPERTIES, null );
  
              final Properties oldProperties =
                  SysPropertiesRedirector.bindProperties( newProperties );
              store.put( PROPERTIES, oldProperties );
          }
      }
  
      /**
       * The deactivate method is called when the ThreadContext is
       * dis-associated with a thread.
       *
       * @param store the store contianign data required to
       *        deactivate context
       */
      public void deactivate( final ThreadContextAccessor accessor,
                              final Map store )
      {
          super.deactivate( accessor, store );
  
          if( accessor.containsKey( INPUT ) )
          {
              final InputStream oldStream =
                  (InputStream)store.get( INPUT );
              StdioRedirector.bindInput( oldStream );
          }
  
          if( accessor.containsKey( OUTPUT ) )
          {
              final OutputStream oldStream =
                  (OutputStream)store.get( OUTPUT );
              StdioRedirector.bindOutput( oldStream );
          }
  
          if( accessor.containsKey( ERROR ) )
          {
              final OutputStream oldStream =
                  (OutputStream)store.get( ERROR );
              StdioRedirector.bindError( oldStream );
          }
  
          if( accessor.containsKey( PROPERTIES ) )
          {
              final Properties oldProperties =
                  (Properties)store.get( PROPERTIES );
              SysPropertiesRedirector.bindProperties( oldProperties );
          }
      }
  }
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message