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 DemuxPolicy.java PolicyRedirector.java
Date Sun, 28 Apr 2002 04:50:32 GMT
donaldp     02/04/27 21:50:32

  Added:       aut/src/java/org/apache/aut/jprocess DemuxPolicy.java
                        PolicyRedirector.java
  Log:
  Add in a Policy redirector so that you can
  bind per-thread Policy
  
  Revision  Changes    Path
  1.1                  jakarta-ant-myrmidon/aut/src/java/org/apache/aut/jprocess/DemuxPolicy.java
  
  Index: DemuxPolicy.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.security.CodeSource;
  import java.security.PermissionCollection;
  import java.security.Policy;
  
  /**
   * This is a Policy object that delegates to a Policy objects
   * associated with the current thread.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/04/28 04:50:32 $
   */
  public class DemuxPolicy
      extends Policy
  {
      /**
       * The Policy associated with each thread.
       */
      private final InheritableThreadLocal m_policies =
          new InheritableThreadLocal();
  
      /**
       * The default Policy to fall back on if no Policy
       * object bound.
       */
      private final Policy m_defaultPolicy;
  
      /**
       * Create a Policy object that delegates to per-thread
       * Policy objects.
       *
       * @param defaultPolicy the default Policy object
       */
      public DemuxPolicy( final Policy defaultPolicy )
      {
          m_defaultPolicy = defaultPolicy;
      }
  
      /**
       * Bind the specified Policy object to the current thread.
       *
       * @param policy the Policy object to bind
       */
      public Policy bindPolicy( final Policy policy )
      {
          final Policy oldPolicy = (Policy)m_policies.get();
          m_policies.set( policy );
          return oldPolicy;
      }
  
      public PermissionCollection getPermissions( final CodeSource codesource )
      {
          return getCurrentPolicy().getPermissions( codesource );
      }
  
      public void refresh()
      {
          getCurrentPolicy().refresh();
      }
  
      /**
       * Retrieve Policy currently in operation.
       *
       * @return the current policy
       */
      private Policy getCurrentPolicy()
      {
          final Policy policy = (Policy)m_policies.get();
          if( null == policy )
          {
              return m_defaultPolicy;
          }
          else
          {
              return policy;
          }
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/aut/src/java/org/apache/aut/jprocess/PolicyRedirector.java
  
  Index: PolicyRedirector.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.security.Policy;
  
  /**
   * This is a utility class that makes it easy to install redirecting
   * Policy objects. The Policy object will redirect to the Policy object
   * that is associated with the current thread. A Policy becomes
   * associated with the current thread when a user calls the
   * {@link #bindPolicy} method or by inheriting the parent threads Policy
   * when thread is initially created. The default Policy will redirect
   * to the default Policy.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/04/28 04:50:32 $
   */
  public final class PolicyRedirector
  {
      private static final String NOT_INSTALLED_ERR =
          "PolicyRedirector not installed";
  
      /**
       * Cached version of original System Policy.
       * (Or at least the Policy that were installed in JVM
       * when this class is loaded and resovled).
       */
      private static final Policy c_systemPolicy = Policy.getPolicy();
  
      private static DemuxPolicy c_policy;
  
      /**
       * Private constructor to block instantiation.
       */
      private PolicyRedirector()
      {
      }
  
      /**
       * Install the Redirector.
       *
       * @return true if installed, false if already installed
       */
      public synchronized static boolean install()
          throws IOException
      {
          if( c_policy == Policy.getPolicy() )
          {
              //Already installed so lets ignore
              return false;
          }
          c_policy = new DemuxPolicy( c_systemPolicy );
  
          Policy.setPolicy( c_policy );
  
          return true;
      }
  
      /**
       * Uninstall the redirector.
       *
       * @return true if uninstalled, false if already uninstalled
       */
      public synchronized static boolean uninstall()
          throws IOException
      {
          if( c_policy != Policy.getPolicy() )
          {
              //Already uninstalled so lets ignore
              return false;
          }
  
          c_policy = null;
          Policy.setPolicy( c_systemPolicy );
  
          return true;
      }
  
      /**
       * Bind the specified Policy object to this thread.
       *
       * @param policy the Policy to bind
       * @throws SecurityException if don't have permission to bind
       * @throws IllegalStateException if redirector not installed
       */
      public static Policy bindPolicy( final Policy policy )
          throws SecurityException, IllegalStateException
      {
          if( null == policy )
          {
              throw new NullPointerException( "policy" );
          }
          if( null == c_policy )
          {
              throw new IllegalStateException( NOT_INSTALLED_ERR );
          }
  
          final SecurityManager manager = System.getSecurityManager();
          if( null != manager )
          {
              final RuntimePermission permission =
                  new RuntimePermission( "PolicyRedirector.setPolicy" );
              manager.checkPermission( permission );
          }
  
          return c_policy.bindPolicy( policy );
      }
  }
  
  
  

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