[jboss-cvs] JBossAS SVN: r87602 - in projects/bootstrap/trunk: impl-base/src/test/java/org/jboss/bootstrap/impl/base/server and 5 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Apr 20 22:39:36 EDT 2009


Author: ALRubinger
Date: 2009-04-20 22:39:36 -0400 (Mon, 20 Apr 2009)
New Revision: 87602

Added:
   projects/bootstrap/trunk/impl-mc/src/test/java/org/jboss/bootstrap/impl/mc/jboot45/
   projects/bootstrap/trunk/impl-mc/src/test/java/org/jboss/bootstrap/impl/mc/jboot45/unit/
   projects/bootstrap/trunk/impl-mc/src/test/java/org/jboss/bootstrap/impl/mc/jboot45/unit/DefaultConfigUnitTestCase.java
Modified:
   projects/bootstrap/trunk/impl-base/src/main/java/org/jboss/bootstrap/impl/base/server/AbstractServer.java
   projects/bootstrap/trunk/impl-base/src/main/java/org/jboss/bootstrap/impl/base/server/SecurityActions.java
   projects/bootstrap/trunk/impl-base/src/test/java/org/jboss/bootstrap/impl/base/server/TestNoOpServer.java
   projects/bootstrap/trunk/impl-mc/pom.xml
   projects/bootstrap/trunk/impl-mc/src/main/java/org/jboss/bootstrap/impl/mc/server/MCServerImpl.java
Log:
[JBBOOT-45] Create a default configuration if none is provided to a Server ctor

Modified: projects/bootstrap/trunk/impl-base/src/main/java/org/jboss/bootstrap/impl/base/server/AbstractServer.java
===================================================================
--- projects/bootstrap/trunk/impl-base/src/main/java/org/jboss/bootstrap/impl/base/server/AbstractServer.java	2009-04-21 00:16:45 UTC (rev 87601)
+++ projects/bootstrap/trunk/impl-base/src/main/java/org/jboss/bootstrap/impl/base/server/AbstractServer.java	2009-04-21 02:39:36 UTC (rev 87602)
@@ -101,23 +101,61 @@
 
    /**
     * Constructor
+    * 
+    * Creates a new Server using an uninitialized, default configuration
+    * (which is an instance of the Class specified by 
+    * {@link AbstractServer#getDefaultServerConfigClass()}
     */
    protected AbstractServer()
    {
+      // Pass it to the other ctor
       this(null);
    }
 
    /**
     * Constructor
     * 
-    * @param configuration The configuration to set
+    * Creates a new Server, specifying the configuration to be used.
+    * 
+    * @param configuration The configuration to set.  If null a new default 
+    *    configuration will be made (which is an instance of the Class specified by 
+    *    {@link AbstractServer#getDefaultServerConfigClass()}
+    * @throws IllegalArgumentException If the configuration has not been supplied
     */
-   protected AbstractServer(final T configuration)
+   protected AbstractServer(final T configuration) throws IllegalArgumentException
    {
-      // Check for valid config and set
-      this.setConfiguration(configuration);
+      // Initialize
+      T configToSet = configuration;
 
+      // Check that a configuration has been supplied
+      if (configToSet == null)
+      {
+
+         /*
+          *  Make a new Configuration
+          */
+         log.debug("No configuration has been supplied, so making a default one");
+         T newConfiguration = null;
+
+         // Get the default config class
+         final Class<? extends T> configClass = this.getDefaultServerConfigClass();
+         try
+         {
+            // Create
+            newConfiguration = SecurityActions.newInstance(configClass);
+            log.debug("Created new default configuration: " + newConfiguration);
+         }
+         catch (Throwable t)
+         {
+            throw new RuntimeException("Could not create default configuration of type " + configClass, t);
+         }
+
+         // Set
+         configToSet = newConfiguration;
+      }
+
       // Set properties
+      this.setConfiguration(configToSet);
       this.setState(LifecycleState.PRE_INIT);
    }
 
@@ -421,6 +459,7 @@
    //-------------------------------------------------------------------------------------||
    // Contracts --------------------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
+
    /**
     * Implementation-specific start hook
     * 
@@ -435,6 +474,14 @@
     */
    protected abstract void doShutdown() throws Exception;
 
+   /**
+    * Obtains the Class used in constructing a new default 
+    * Server Configuration if one is not supplied to the constructor 
+    * 
+    * @return
+    */
+   protected abstract Class<? extends T> getDefaultServerConfigClass();
+
    //-------------------------------------------------------------------------------------||
    // Functional Methods -----------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||

Modified: projects/bootstrap/trunk/impl-base/src/main/java/org/jboss/bootstrap/impl/base/server/SecurityActions.java
===================================================================
--- projects/bootstrap/trunk/impl-base/src/main/java/org/jboss/bootstrap/impl/base/server/SecurityActions.java	2009-04-21 00:16:45 UTC (rev 87601)
+++ projects/bootstrap/trunk/impl-base/src/main/java/org/jboss/bootstrap/impl/base/server/SecurityActions.java	2009-04-21 02:39:36 UTC (rev 87602)
@@ -24,10 +24,11 @@
 
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 
 import org.jboss.logging.Logger;
 
-
 /**
  * SecurityActions
  * 
@@ -124,6 +125,59 @@
       });
    }
 
+   /**
+    * Creates a new instance of the specified class using the default ctor
+    * 
+    * @param clazz
+    * @return
+    * @throws IllegalArgumentException If the class is not specified
+    * @throws Throwable If some error occured in creating the instance
+    */
+   static <T> T newInstance(final Class<T> clazz) throws IllegalArgumentException, Throwable
+   {
+      // Precondition check
+      if (clazz == null)
+      {
+         throw new IllegalArgumentException("Class is a required argument");
+      }
+
+      // Create
+      Object newInstance = null;
+      try
+      {
+         // Set
+         newInstance = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>()
+         {
+
+            public Object run() throws Exception
+            {
+               if (log.isTraceEnabled())
+               {
+                  log.trace("Attempting to create new instance of " + clazz);
+               }
+               final Object obj = clazz.newInstance();
+               if (log.isTraceEnabled())
+               {
+                  log.trace("Created " + obj);
+               }
+               return obj;
+            }
+
+         });
+      }
+      catch (PrivilegedActionException pae)
+      {
+         // Throw the cause
+         throw pae.getCause();
+      }
+
+      // Cast
+      T returnValue = clazz.cast(newInstance);
+
+      // Return
+      return returnValue;
+   }
+
    //-------------------------------------------------------------------------------------||
    // Internal Helper Methods ------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||

Modified: projects/bootstrap/trunk/impl-base/src/test/java/org/jboss/bootstrap/impl/base/server/TestNoOpServer.java
===================================================================
--- projects/bootstrap/trunk/impl-base/src/test/java/org/jboss/bootstrap/impl/base/server/TestNoOpServer.java	2009-04-21 00:16:45 UTC (rev 87601)
+++ projects/bootstrap/trunk/impl-base/src/test/java/org/jboss/bootstrap/impl/base/server/TestNoOpServer.java	2009-04-21 02:39:36 UTC (rev 87602)
@@ -26,7 +26,6 @@
 import org.jboss.bootstrap.spi.server.Server;
 import org.jboss.logging.Logger;
 
-
 /**
  * TestServer
  * 
@@ -68,4 +67,13 @@
       log.debug("NOOP doStart");
    }
 
+   /* (non-Javadoc)
+    * @see org.jboss.bootstrap.impl.base.server.AbstractServer#getDefaultServerConfigClass()
+    */
+   @Override
+   protected Class<TestServerConfig> getDefaultServerConfigClass()
+   {
+      return TestServerConfig.class;
+   }
+
 }

Modified: projects/bootstrap/trunk/impl-mc/pom.xml
===================================================================
--- projects/bootstrap/trunk/impl-mc/pom.xml	2009-04-21 00:16:45 UTC (rev 87601)
+++ projects/bootstrap/trunk/impl-mc/pom.xml	2009-04-21 02:39:36 UTC (rev 87602)
@@ -2,7 +2,8 @@
   <!--
   vi:ts=2:sw=2:expandtab:
 -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
   <!-- Parent -->
   <parent>
@@ -25,8 +26,8 @@
   <!-- Properties -->
   <properties>
 
-    <version.org.jboss.bootstrap_jboss.bootstrap.impl.base>0.1.1</version.org.jboss.bootstrap_jboss.bootstrap.impl.base>
-    <version.org.jboss.bootstrap_jboss.bootstrap.spi.mc>0.1.1</version.org.jboss.bootstrap_jboss.bootstrap.spi.mc>
+    <version.org.jboss.bootstrap_jboss.bootstrap.impl.base>0.1.2-SNAPSHOT</version.org.jboss.bootstrap_jboss.bootstrap.impl.base>
+    <version.org.jboss.bootstrap_jboss.bootstrap.spi.mc>0.1.2-SNAPSHOT</version.org.jboss.bootstrap_jboss.bootstrap.spi.mc>
     <version.org.jboss.man_jboss.managed>2.0.0.GA</version.org.jboss.man_jboss.managed>
 
   </properties>

Modified: projects/bootstrap/trunk/impl-mc/src/main/java/org/jboss/bootstrap/impl/mc/server/MCServerImpl.java
===================================================================
--- projects/bootstrap/trunk/impl-mc/src/main/java/org/jboss/bootstrap/impl/mc/server/MCServerImpl.java	2009-04-21 00:16:45 UTC (rev 87601)
+++ projects/bootstrap/trunk/impl-mc/src/main/java/org/jboss/bootstrap/impl/mc/server/MCServerImpl.java	2009-04-21 02:39:36 UTC (rev 87602)
@@ -22,6 +22,7 @@
 
 package org.jboss.bootstrap.impl.mc.server;
 
+import org.jboss.bootstrap.impl.mc.config.BasicMCServerConfig;
 import org.jboss.bootstrap.impl.mc.config.MCConfigurationInitializer;
 import org.jboss.bootstrap.impl.mc.config.MCConfigurationValidator;
 import org.jboss.bootstrap.spi.mc.config.MCServerConfig;
@@ -130,4 +131,17 @@
          }
       }
    }
+
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /* (non-Javadoc)
+    * @see org.jboss.bootstrap.impl.base.server.AbstractServer#getDefaultServerConfigClass()
+    */
+   @Override
+   protected Class<? extends MCServerConfig> getDefaultServerConfigClass()
+   {
+      return BasicMCServerConfig.class;
+   }
 }

Added: projects/bootstrap/trunk/impl-mc/src/test/java/org/jboss/bootstrap/impl/mc/jboot45/unit/DefaultConfigUnitTestCase.java
===================================================================
--- projects/bootstrap/trunk/impl-mc/src/test/java/org/jboss/bootstrap/impl/mc/jboot45/unit/DefaultConfigUnitTestCase.java	                        (rev 0)
+++ projects/bootstrap/trunk/impl-mc/src/test/java/org/jboss/bootstrap/impl/mc/jboot45/unit/DefaultConfigUnitTestCase.java	2009-04-21 02:39:36 UTC (rev 87602)
@@ -0,0 +1,78 @@
+/*
+ * 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.impl.mc.jboot45.unit;
+
+import junit.framework.TestCase;
+
+import org.jboss.bootstrap.impl.mc.config.BasicMCServerConfig;
+import org.jboss.bootstrap.impl.mc.server.MCServerImpl;
+import org.jboss.bootstrap.spi.mc.config.MCServerConfig;
+import org.jboss.bootstrap.spi.mc.server.MCServer;
+import org.jboss.logging.Logger;
+import org.junit.Test;
+
+/**
+ * DefaultConfigUnitTestCase
+ * 
+ * Ensures that a default configuration is always provided, 
+ * even if one is not explicitly specified in creation
+ * 
+ * JIRA: JBBOOT-45
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class DefaultConfigUnitTestCase
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   private static final Logger log = Logger.getLogger(DefaultConfigUnitTestCase.class);
+
+   //-------------------------------------------------------------------------------------||
+   // Tests ------------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Ensures that creating a new server without specifying a 
+    * configuration results in an implicit setting of a new
+    * default config
+    */
+   @Test
+   public void testDefaultConfigurationWhenNoneSupplied()
+   {
+      // Make a server
+      MCServer server = new MCServerImpl();
+
+      // Get out the config
+      MCServerConfig config = server.getConfiguration();
+      log.info("Got configuration: " + config);
+
+      // Test
+      TestCase.assertNotNull("Configuration must not be null", config);
+      TestCase.assertTrue("Config did not default to expected type", config instanceof BasicMCServerConfig);
+   }
+
+}




More information about the jboss-cvs-commits mailing list