[jboss-cvs] JBossAS SVN: r87185 - in projects/bootstrap/trunk/spi/src: main/java/org/jboss/bootstrap/spi/server and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Apr 13 13:11:42 EDT 2009


Author: ALRubinger
Date: 2009-04-13 13:11:41 -0400 (Mon, 13 Apr 2009)
New Revision: 87185

Added:
   projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/SecurityActions.java
Modified:
   projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/AbstractBasicServerInitializer.java
   projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/AbstractServer.java
   projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/SecurityActions.java
   projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/Server.java
   projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/ServerInitializer.java
   projects/bootstrap/trunk/spi/src/test/java/org/jboss/bootstrap/spi/server/unit/ServerInitializationTestCase.java
Log:
[JBBOOT-33] Add a cleanup phase to server initializers, clean up all system properties set during init

Added: projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/SecurityActions.java
===================================================================
--- projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/SecurityActions.java	                        (rev 0)
+++ projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/SecurityActions.java	2009-04-13 17:11:41 UTC (rev 87185)
@@ -0,0 +1,80 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.bootstrap.spi.config;
+
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * SecurityActions
+ * 
+ * Utility class for security actions, so as to provide
+ * some centralization without leaking privileged actions
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+class SecurityActions
+{
+   //-------------------------------------------------------------------------------------||
+   // Constructor ------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * In place to prevent instanciation 
+    */
+   private SecurityActions()
+   {
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Utility Methods --------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Obtains the URL codesource location of the given class
+    * 
+    * @param clazz
+    * @throws IllegalArgumentException If the class was not specified
+    */
+   static URL getCodeSourceLocation(final Class<?> clazz) throws IllegalArgumentException
+   {
+      // Precondition check
+      if (clazz == null)
+      {
+         throw new IllegalArgumentException("Class for code source is required");
+      }
+
+      // Return codesource location
+      return AccessController.doPrivileged(new PrivilegedAction<URL>()
+      {
+
+         public URL run()
+         {
+            return clazz.getProtectionDomain().getCodeSource().getLocation();
+         }
+
+      });
+   }
+}

Modified: projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/AbstractBasicServerInitializer.java
===================================================================
--- projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/AbstractBasicServerInitializer.java	2009-04-13 16:47:50 UTC (rev 87184)
+++ projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/AbstractBasicServerInitializer.java	2009-04-13 17:11:41 UTC (rev 87185)
@@ -23,6 +23,8 @@
 package org.jboss.bootstrap.spi.server;
 
 import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.jboss.bootstrap.spi.config.ServerConfig;
 import org.jboss.bootstrap.spi.lifecycle.LifecycleState;
@@ -46,6 +48,12 @@
 
    private static final Logger log = Logger.getLogger(AbstractBasicServerInitializer.class);
 
+   /**
+    * Set of all system properties added during initialization.  Synchronized on "this", 
+    * must not be exported.
+    */
+   private static final Set<String> SYS_PROPS_ADDED = new HashSet<String>();
+
    //-------------------------------------------------------------------------------------||
    // Required Implementations -----------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
@@ -53,7 +61,8 @@
    /* (non-Javadoc)
     * @see org.jboss.bootstrap.spi.server.ServerInitializer#initialize(org.jboss.bootstrap.spi.server.Server)
     */
-   public void initialize(final Server<K, T> server) throws IllegalArgumentException, IllegalStateException
+   public synchronized void initialize(final Server<K, T> server) throws IllegalArgumentException,
+         IllegalStateException
    {
       // Log
       if (log.isTraceEnabled())
@@ -90,6 +99,26 @@
 
    }
 
+   /* (non-Javadoc)
+    * @see org.jboss.bootstrap.spi.server.ServerInitializer#cleanup(org.jboss.bootstrap.spi.server.Server)
+    */
+   public synchronized void cleanup(final Server<K, T> server) throws IllegalArgumentException, IllegalStateException
+   {
+      // Log
+      if (log.isTraceEnabled())
+      {
+         log.trace("Preparing for shutdown...");
+      }
+
+      // Clear all system properties set
+      log.debug("Clearing all system properties set in initialization...");
+      for (final String sysProp : SYS_PROPS_ADDED)
+      {
+         SecurityActions.clearSystemProperty(sysProp);
+      }
+
+   }
+
    //-------------------------------------------------------------------------------------||
    // Functional Methods -----------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
@@ -140,6 +169,12 @@
       // Set 
       SecurityActions.setSystemProperty(key, valueToSet);
 
+      // Add to Set 
+      synchronized (this)
+      {
+         SYS_PROPS_ADDED.add(key);
+      }
+
       // Log
       if (log.isTraceEnabled())
       {

Modified: projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/AbstractServer.java
===================================================================
--- projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/AbstractServer.java	2009-04-13 16:47:50 UTC (rev 87184)
+++ projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/AbstractServer.java	2009-04-13 17:11:41 UTC (rev 87185)
@@ -191,6 +191,17 @@
       }
       this.doShutdown();
 
+      // Let the initializer clean up
+      final ServerInitializer<K, T> serverInitializer = this.getServerInitializer();
+      if (serverInitializer != null)
+      {
+         if (log.isTraceEnabled())
+         {
+            log.trace("Calling to clean up for shutdown: " + serverInitializer);
+         }
+         serverInitializer.cleanup(this);
+      }
+
       // Done
       log.info("Stopped: " + this);
       this.setState(LifecycleState.IDLE);
@@ -366,6 +377,9 @@
    public synchronized final void setServerInitializer(final ServerInitializer<K, T> serverInitializer)
          throws IllegalStateException
    {
+      // Precondition check
+      this.checkState(LifecycleState.PRE_INIT, this.getState());
+
       this.serverInitializer = serverInitializer;
       log.debug("Set server initializer to " + serverInitializer);
    }
@@ -383,6 +397,9 @@
     */
    public synchronized final void setConfigInitializer(ConfigurationInitializer<T> configInitializer)
    {
+      // Precondition check
+      this.checkState(LifecycleState.PRE_INIT, this.getState());
+
       this.configInitializer = configInitializer;
       log.debug("Set config initializer to " + configInitializer);
    }
@@ -392,6 +409,9 @@
     */
    public synchronized final void setValidator(final ConfigurationValidator<T> validator)
    {
+      // Precondition check
+      this.checkState(LifecycleState.PRE_INIT, this.getState());
+
       log.debug("Setting validator to: " + validator);
       this.validator = validator;
    }

Modified: projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/SecurityActions.java
===================================================================
--- projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/SecurityActions.java	2009-04-13 16:47:50 UTC (rev 87184)
+++ projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/SecurityActions.java	2009-04-13 17:11:41 UTC (rev 87185)
@@ -25,6 +25,8 @@
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 
+import org.jboss.logging.Logger;
+
 /**
  * SecurityActions
  * 
@@ -37,6 +39,12 @@
 class SecurityActions
 {
    //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   private static final Logger log = Logger.getLogger(SecurityActions.class);
+
+   //-------------------------------------------------------------------------------------||
    // Constructor ------------------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
 
@@ -59,10 +67,7 @@
    static void setSystemProperty(final String key, final String value) throws IllegalArgumentException
    {
       // Precondition check
-      if (key == null)
-      {
-         throw new IllegalArgumentException("Key for System Property was not specified");
-      }
+      ensureSysPropKeySpecified(key);
 
       // Set
       AccessController.doPrivileged(new PrivilegedAction<Void>()
@@ -70,12 +75,68 @@
 
          public Void run()
          {
+            // Set
             System.setProperty(key, value);
 
+            // Log
+            if (log.isTraceEnabled())
+            {
+               log.trace("Set system property \"" + key + "\" > " + value);
+            }
+
             // Return
             return null;
          }
 
       });
    }
+
+   /**
+    * Clears the specified System Property
+    * 
+    * @throws IllegalArgumentException If the key was not specified
+    */
+   static void clearSystemProperty(final String key) throws IllegalArgumentException
+   {
+      // Precondition check
+      ensureSysPropKeySpecified(key);
+
+      // Set
+      AccessController.doPrivileged(new PrivilegedAction<Void>()
+      {
+
+         public Void run()
+         {
+            // Clear
+            String previousValue = System.clearProperty(key);
+
+            // Log
+            if (log.isTraceEnabled())
+            {
+               log.trace("Removed system property \"" + key + "\" which had previous value: " + previousValue);
+            }
+
+            // Return
+            return null;
+         }
+
+      });
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Internal Helper Methods ------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Ensures that the specified key is not null
+    * 
+    * @throws IllegalArgumentException If the specified key was null
+    */
+   private static void ensureSysPropKeySpecified(String key) throws IllegalArgumentException
+   {
+      if (key == null)
+      {
+         throw new IllegalArgumentException("Key for System Property was not specified");
+      }
+   }
 }

Modified: projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/Server.java
===================================================================
--- projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/Server.java	2009-04-13 16:47:50 UTC (rev 87184)
+++ projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/Server.java	2009-04-13 17:11:41 UTC (rev 87185)
@@ -89,8 +89,10 @@
     * 
     * @param configInitializer
     * @return This server
+    * @throws IllegalStateException If the server state is anything aside from
+    * {@link LifecycleState#PRE_INIT}
     */
-   void setConfigInitializer(ConfigurationInitializer<T> configInitializer);
+   void setConfigInitializer(ConfigurationInitializer<T> configInitializer) throws IllegalStateException;
 
    /**
     * Start lifecycle of the Server, optionally invoking upon
@@ -106,7 +108,9 @@
    /**
     * Stop lifecycle of the Server.  During execution the state will be 
     * {@link LifecycleState#STOPPING}, and upon
-    * successful completion will be {@link LifecycleState#IDLE}. 
+    * successful completion will be {@link LifecycleState#IDLE}.  If a 
+    * server initialzer is defined then {@link ServerInitializer#cleanup(Server)}
+    * will be invoked during this call.
     * 
     * @throws IllegalStateException If the Server is not started
     * @throws Exception
@@ -153,8 +157,10 @@
     * 
     * @param validator
     * @return This server
+    * @throws IllegalStateException If the server state is anything aside from
+    * {@link LifecycleState#PRE_INIT}
     */
-   void setValidator(ConfigurationValidator<T> validator);
+   void setValidator(ConfigurationValidator<T> validator) throws IllegalStateException;
 
    /**
     * Adds the specified bootstrap to those to be run on {@link Server#start()}

Modified: projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/ServerInitializer.java
===================================================================
--- projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/ServerInitializer.java	2009-04-13 16:47:50 UTC (rev 87184)
+++ projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/ServerInitializer.java	2009-04-13 17:11:41 UTC (rev 87185)
@@ -48,4 +48,14 @@
     *       {@link LifecycleState#PRE_INIT}
     */
    void initialize(Server<K, T> server) throws IllegalArgumentException, IllegalStateException;
+
+   /**
+    * Cleans up any actions taken during the initialization phase
+    * in preparation for server shutdown
+    * 
+    * @param server
+    * @throws IllegalArgumentException
+    * @throws IllegalStateException If the server's state is not {@link LifecycleState#STOPPING}
+    */
+   void cleanup(Server<K, T> server) throws IllegalArgumentException, IllegalStateException;
 }

Modified: projects/bootstrap/trunk/spi/src/test/java/org/jboss/bootstrap/spi/server/unit/ServerInitializationTestCase.java
===================================================================
--- projects/bootstrap/trunk/spi/src/test/java/org/jboss/bootstrap/spi/server/unit/ServerInitializationTestCase.java	2009-04-13 16:47:50 UTC (rev 87184)
+++ projects/bootstrap/trunk/spi/src/test/java/org/jboss/bootstrap/spi/server/unit/ServerInitializationTestCase.java	2009-04-13 17:11:41 UTC (rev 87185)
@@ -92,12 +92,13 @@
 
    /**
     * Ensures that the properties backing the configuration are set 
-    * both on the configuration itself and in the System props
+    * both on the configuration itself and in the System props.  Upon
+    * cleanup, these properties must be cleared.
     * 
     * @throws Throwable
     */
    @Test
-   public void testPropertiesSetInInitialization() throws Throwable
+   public void testPropertiesInInitializationLifecycle() throws Throwable
    {
       // Get a populated config and server
       TestServerConfig configuration = TestConfigFactory.createConfiguration();
@@ -136,5 +137,22 @@
       TestCase.assertEquals("conf in configuration must match the system property", conf, confFromSystem);
       TestCase.assertEquals("name in configuration must match the config property", name, nameFromConfigProp);
       TestCase.assertEquals("name in configuration must match the system property", name, nameFromSystem);
+
+      // Cleanup
+      serverInitializer.cleanup(server);
+
+      // Get Properties from System
+      final String homeFromSystemAfterCleanup = System.getProperty(ServerConfig.PROP_KEY_BOOTSTRAP_HOME_URL);
+      final String bootstrapURLFromSystemAfterCleanup = System.getProperty(ServerConfig.PROP_KEY_BOOTSTRAP_URL);
+      final String confFromSystemAfterCleanup = System.getProperty(ServerConfig.PROP_KEY_BOOTSTRAP_CONF_URL);
+      final String nameFromSystemAfterCleanup = System.getProperty(ServerConfig.PROP_KEY_BOOTSTRAP_NAME);
+
+      // Ensure all null
+      String failMessage = "System property should be null after cleanup";
+      TestCase.assertNull(failMessage, homeFromSystemAfterCleanup);
+      TestCase.assertNull(failMessage, bootstrapURLFromSystemAfterCleanup);
+      TestCase.assertNull(failMessage, confFromSystemAfterCleanup);
+      TestCase.assertNull(failMessage, nameFromSystemAfterCleanup);
+
    }
 }




More information about the jboss-cvs-commits mailing list