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 SysPropertiesRedirector.java
Date Fri, 26 Apr 2002 02:53:21 GMT
donaldp     02/04/25 19:53:21

  Added:       aut/src/java/org/apache/aut/jprocess
                        SysPropertiesRedirector.java
  Log:
  Add in a class that installs redirector
  for System properties and redirects to
  per thread proeprties object.
  
  Revision  Changes    Path
  1.1                  jakarta-ant-myrmidon/aut/src/java/org/apache/aut/jprocess/SysPropertiesRedirector.java
  
  Index: SysPropertiesRedirector.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.IOException;
  import java.util.Properties;
  
  /**
   * This is a utility class that makes it easy to install redirecting
   * Properties objects underlying System.getProeprties() object.
   * The properties object will redirect to the properties object that is
   * associated with the current thread. A properties object becomes
   * associated with the current thread when a user calls one of the
   * bind*() methods or by inheriting the parent threads properties object
   * when thread is initially created. The default  streams will redirect
   * to the default System properties.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/04/26 02:53:21 $
   */
  public final class SysPropertiesRedirector
  {
      private static final String NOT_INSTALLED_ERR =
          "SysPropertiesRedirector not installed";
  
      /**
       * Cached version of original System Properties.
       * (Or at least the properties that were installed in JVM
       * when this class is loaded and resovled).
       */
      private static final Properties c_systemProperties =
          System.getProperties();
  
      private static DemuxProperties c_properties;
  
      /**
       * Private constructor to block instantiation.
       */
      private SysPropertiesRedirector()
      {
      }
  
      /**
       * Install the redirecting Properties object.
       *
       * @return true if installed, false if already installed
       */
      public synchronized static boolean install()
          throws IOException
      {
          if( c_properties == System.getProperties() )
          {
              //Already installed so lets ignore
              return false;
          }
          c_properties = new DemuxProperties( c_systemProperties );
  
          System.setProperties( c_properties );
  
          return true;
      }
  
      /**
       * Uninstall the redirecting streams and
       * replace them with streams that point to
       * FileDescriptor.in, FileDescriptor.out and
       * FileDescriptor.err.
       *
       * @return true if uninstalled, false if already uninstalled
       */
      public synchronized static boolean uninstall()
          throws IOException
      {
          if( c_properties != System.getProperties() )
          {
              //Already uninstalled so lets ignore
              return false;
          }
  
          c_properties = null;
          System.setProperties( c_systemProperties );
  
          return true;
      }
  
      /**
       * Bind the specified Properties object to this thread.
       *
       * @param properties the stream to bind
       * @throws SecurityException if don't have permission to bind to input
       * @throws IllegalStateException if redirector not installed
       */
      public static void bindProperties( final Properties properties )
          throws SecurityException, IllegalStateException
      {
          if( null == properties )
          {
              throw new NullPointerException( "properties" );
          }
          if( null == c_properties )
          {
              throw new IllegalStateException( NOT_INSTALLED_ERR );
          }
  
          final SecurityManager manager = System.getSecurityManager();
          if( null != manager )
          {
              final RuntimePermission permission =
                  new RuntimePermission( "SysPropertiesRedirector.setProperties" );
              manager.checkPermission( permission );
          }
  
          c_properties.bindProperties( properties );
      }
  }
  
  
  

--
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