[jboss-cvs] JBossAS SVN: r87175 - in projects/bootstrap/trunk/spi/src: test/java/org/jboss/bootstrap/spi/config/unit and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Sun Apr 12 05:08:43 EDT 2009
Author: ALRubinger
Date: 2009-04-12 05:08:42 -0400 (Sun, 12 Apr 2009)
New Revision: 87175
Modified:
projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/AbstractBasicConfigurationInitializer.java
projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/AbstractBasicServerConfig.java
projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/ServerConfig.java
projects/bootstrap/trunk/spi/src/test/java/org/jboss/bootstrap/spi/config/unit/ConfigInitializationTestCase.java
projects/bootstrap/trunk/spi/src/test/java/org/jboss/bootstrap/spi/config/unit/ConfigValidationTestCase.java
Log:
[JBBOOT-30] Honor system properties as overrides to configuration
Modified: projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/AbstractBasicConfigurationInitializer.java
===================================================================
--- projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/AbstractBasicConfigurationInitializer.java 2009-04-12 08:00:17 UTC (rev 87174)
+++ projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/AbstractBasicConfigurationInitializer.java 2009-04-12 09:08:42 UTC (rev 87175)
@@ -24,6 +24,7 @@
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.Map;
import org.jboss.logging.Logger;
@@ -62,6 +63,10 @@
{
log.trace("Initializing: " + config);
}
+
+ // Apply overrides
+ this.applySystemPropertyOverrides(config);
+
/*
* Default the config
*/
@@ -73,10 +78,10 @@
/*
* If bootstrapURL is not directly defined, construct it from
- * home+name
+ * home+name (assuming they both *are* defined)
*/
- if (bootstrapUrl == null)
+ if (bootstrapUrl == null && (home != null && name != null))
{
// Log
if (log.isTraceEnabled())
@@ -85,8 +90,6 @@
}
// Construct
- assert home != null : "Bootstrap home should not be null, and should have failed validation if so";
- assert name != null : "Bootstrap name should not be null, and should have failed validation if so";
URL newBootstrapUrl = null;
try
{
@@ -133,8 +136,8 @@
{
try
{
- String bootstrapUrlString = bootstrapUrl.toExternalForm();
- String bootstrapUrlBase = bootstrapUrlString.substring(0, bootstrapUrlString.lastIndexOf("/") + 1);
+ final String bootstrapUrlString = bootstrapUrl.toExternalForm();
+ final String bootstrapUrlBase = bootstrapUrlString.substring(0, bootstrapUrlString.lastIndexOf("/") + 1);
newConf = new URL(bootstrapUrlBase);
log.debug("Defaulted bootstrapConf from bootstrapURL's base: " + bootstrapUrlBase);
}
@@ -153,4 +156,95 @@
}
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods -----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Sets the values of contracted system properties upon the supplied configuration
+ *
+ * @throws IllegalArgumentException If the config was not specified
+ */
+ protected void applySystemPropertyOverrides(final T config) throws IllegalArgumentException
+ {
+ // Log
+ if (log.isTraceEnabled())
+ {
+ log.trace("Applying system property overrides...");
+ }
+
+ // Precondition check
+ if (config == null)
+ {
+ throw new IllegalArgumentException("Supplied configuration is null");
+ }
+
+ // Get Properties
+ final Map<String, String> properties = config.getProperties();
+
+ /*
+ * Apply overrides if present
+ */
+
+ // Home
+ final String overrideHome = this.getOverrideValue(ServerConfig.PROP_KEY_BOOTSTRAP_HOME_URL, properties);
+ if (overrideHome != null)
+ {
+ config.bootstrapHome(overrideHome);
+ }
+
+ // Conf
+ final String overrideConf = this.getOverrideValue(ServerConfig.PROP_KEY_BOOTSTRAP_CONF_URL, properties);
+ if (overrideConf != null)
+ {
+ config.bootstrapConfLocation(overrideConf);
+ }
+
+ // URL
+ final String overrideUrl = this.getOverrideValue(ServerConfig.PROP_KEY_BOOTSTRAP_URL, properties);
+ if (overrideUrl != null)
+ {
+ config.bootstrapUrl(overrideUrl);
+ }
+
+ // Name
+ final String overrideName = this.getOverrideValue(ServerConfig.PROP_KEY_BOOTSTRAP_NAME, properties);
+ if (overrideName != null)
+ {
+ config.bootstrapName(overrideName);
+ }
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Returns the override value for the specified property name in the specified
+ * properties, additionally logging if found. If not found, null will be returned.
+ *
+ * @throws IllegalArgumentException If either argument is null
+ */
+ private String getOverrideValue(final String propertyName, final Map<String, String> properties)
+ throws IllegalArgumentException
+ {
+ // Get the override value
+ String override = properties.get(propertyName);
+
+ // Adjust to null if empty
+ if (override.length() == 0)
+ {
+ override = null;
+ }
+
+ // Log if we'll use it
+ if (override != null && override.length() > 0)
+ {
+ log.debug("Got system property " + propertyName + ", so using as override: " + override);
+ }
+
+ // Return
+ return override;
+ }
+
}
Modified: projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/AbstractBasicServerConfig.java
===================================================================
--- projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/AbstractBasicServerConfig.java 2009-04-12 08:00:17 UTC (rev 87174)
+++ projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/AbstractBasicServerConfig.java 2009-04-12 09:08:42 UTC (rev 87175)
@@ -293,7 +293,7 @@
}
/* (non-Javadoc)
- * @see org.jboss.bootstrap.spi.config.ServerConfig#setProperty(java.lang.String, java.lang.String)
+ * @see org.jboss.bootstrap.spi.config.ServerConfig#property(java.lang.String, java.lang.String)
*/
public T property(final String key, final String value) throws IllegalArgumentException
{
@@ -323,6 +323,42 @@
return this.frozen.get();
}
+ /* (non-Javadoc)
+ * @see org.jboss.bootstrap.spi.config.ServerConfig#bootstrapConfLocation(java.lang.String)
+ */
+ public T bootstrapConfLocation(String confLocation) throws IllegalArgumentException, IllegalStateException
+ {
+ // Set
+ this.bootstrapConfLocation(this.urlFromString(confLocation));
+
+ // Return
+ return this.covarientReturn();
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.bootstrap.spi.config.ServerConfig#bootstrapHome(java.lang.String)
+ */
+ public T bootstrapHome(String bootstrapHome) throws IllegalArgumentException, IllegalStateException
+ {
+ // Set
+ this.bootstrapHome(this.urlFromString(bootstrapHome));
+
+ // Return
+ return this.covarientReturn();
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.bootstrap.spi.config.ServerConfig#bootstrapUrl(java.lang.String)
+ */
+ public T bootstrapUrl(String bootstrapUrl) throws IllegalArgumentException, IllegalStateException
+ {
+ // Set
+ this.bootstrapUrl(this.urlFromString(bootstrapUrl));
+
+ // Return
+ return this.covarientReturn();
+ }
+
//-------------------------------------------------------------------------------------||
// Helper Methods ---------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -388,7 +424,7 @@
*
* @return
*/
- protected T covarientReturn() throws ClassCastException
+ protected final T covarientReturn() throws ClassCastException
{
try
{
@@ -409,7 +445,7 @@
* @param url
* @return
*/
- private URL copyURL(final URL url)
+ protected final URL copyURL(final URL url)
{
// If null, return
if (url == null)
@@ -427,4 +463,27 @@
throw new RuntimeException("Error in copying URL", e);
}
}
+
+ /**
+ * Constructs a URL from the specified String, throwing
+ * {@link IllegalArgumentException} to wrap the
+ * {@link MalformedURLException} if unable to do so. Null arguments
+ * will be ignored.
+ *
+ * @param url
+ * @return
+ * @throws IllegalArgumentException
+ */
+ protected URL urlFromString(String url) throws IllegalArgumentException
+ {
+ try
+ {
+ return url != null ? new URL(url) : null;
+ }
+ catch (MalformedURLException e)
+ {
+ throw new IllegalArgumentException("Could not construct " + URL.class.getSimpleName()
+ + " from the supplied argument: " + url, e);
+ }
+ }
}
Modified: projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/ServerConfig.java
===================================================================
--- projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/ServerConfig.java 2009-04-12 08:00:17 UTC (rev 87174)
+++ projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/config/ServerConfig.java 2009-04-12 09:08:42 UTC (rev 87175)
@@ -22,6 +22,7 @@
package org.jboss.bootstrap.spi.config;
+import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
@@ -65,15 +66,15 @@
//-------------------------------------------------------------------------------------||
/**
- * Obtains the location of the bootstrap directory. This will mirror the value
- * of {@link ServerConfig#PROP_KEY_BOOTSTRAP_HOME_URL}.
+ * Obtains the location of the bootstrap directory. Corresponds to
+ * {@link ServerConfig#PROP_KEY_BOOTSTRAP_HOME_URL}.
*
* @return
*/
URL getBootstrapHome();
/**
- * Sets the location of the bootstrap directory. Will additionally set
+ * Sets the location of the bootstrap directory. Corresponds to
* {@link ServerConfig#PROP_KEY_BOOTSTRAP_HOME_URL}.
*
* @param bootstrapHome
@@ -84,15 +85,27 @@
T bootstrapHome(URL bootstrapHome) throws IllegalArgumentException, IllegalStateException;
/**
- * Obtains the location of the bootstrap file. This will mirror the
- * value of {@link ServerConfig#PROP_KEY_BOOTSTRAP_URL}.
+ * Sets the location of the bootstrap directory. Corresponds to
+ * {@link ServerConfig#PROP_KEY_BOOTSTRAP_HOME_URL}.
*
+ * @param bootstrapHome
+ * @return This configuration
+ * @throws IllegalArgumentException If the bootstrap home location was not specified,
+ * or could not be assigned to a URL (ie. {@link MalformedURLException}
+ * @throws IllegalStateException If the configuration has been frozen
+ */
+ T bootstrapHome(String bootstrapHome) throws IllegalArgumentException, IllegalStateException;
+
+ /**
+ * Obtains the location of the bootstrap file. Corresponds to
+ * {@link ServerConfig#PROP_KEY_BOOTSTRAP_URL}.
+ *
* @return
*/
URL getBootstrapUrl();
/**
- * Sets the location of the bootstrap file. Will additionally set
+ * Sets the location of the bootstrap file. Corresponds to
* {@link ServerConfig#PROP_KEY_BOOTSTRAP_URL}.
*
* @param bootstrapLocation
@@ -103,15 +116,27 @@
T bootstrapUrl(URL bootstrapLocation) throws IllegalArgumentException, IllegalStateException;
/**
- * Obtains the name of the bootstrap configuration. This will
- * mirror the value of {@link ServerConfig#PROP_KEY_BOOTSTRAP_NAME}.
+ * Sets the location of the bootstrap file. Corresponds to
+ * {@link ServerConfig#PROP_KEY_BOOTSTRAP_URL}.
*
+ * @param bootstrapUrl
+ * @return This configuration
+ * @throws IllegalArgumentException If the bootstrap URL location was not specified,
+ * or could not be assigned to a URL (ie. {@link MalformedURLException}
+ * @throws IllegalStateException If the configuration has been frozen
+ */
+ T bootstrapUrl(String bootstrapUrl) throws IllegalArgumentException, IllegalStateException;
+
+ /**
+ * Obtains the name of the bootstrap configuration. Corresponds to
+ * {@link ServerConfig#PROP_KEY_BOOTSTRAP_NAME}.
+ *
* @return
*/
String getBootstrapName();
/**
- * Sets the name of the bootstrap configuration file. Will additionally set
+ * Sets the name of the bootstrap configuration file. Corresponds to
* {@link ServerConfig#PROP_KEY_BOOTSTRAP_NAME}.
*
* @param name
@@ -122,15 +147,15 @@
T bootstrapName(String name) throws IllegalArgumentException, IllegalStateException;
/**
- * Obtains the location of the bootstrap configuration location. This will mirror the
- * value of {@link ServerConfig#PROP_KEY_BOOTSTRAP_CONF_URL}.
+ * Obtains the location of the bootstrap configuration location. Corresponds to
+ * {@link ServerConfig#PROP_KEY_BOOTSTRAP_CONF_URL}.
*
* @return
*/
URL getBootstrapConfLocation();
/**
- * Sets the name of the bootstrap configuration location. Will additionally set
+ * Sets the name of the bootstrap configuration location. Corresponds to
* {@link ServerConfig#PROP_KEY_BOOTSTRAP_CONF_URL}.
*
* @param confLocation
@@ -141,6 +166,18 @@
T bootstrapConfLocation(URL confLocation) throws IllegalArgumentException, IllegalStateException;
/**
+ * Sets the location of the bootstrap file. Corresponds to
+ * {@link ServerConfig#PROP_KEY_BOOTSTRAP_CONF_URL}.
+ *
+ * @param confLocation
+ * @return This configuration
+ * @throws IllegalArgumentException If the location was not specified,
+ * or could not be assigned to a URL (ie. {@link MalformedURLException}
+ * @throws IllegalStateException If the configuration has been frozen
+ */
+ T bootstrapConfLocation(String confLocation) throws IllegalArgumentException, IllegalStateException;
+
+ /**
* Returns an immutable copy of the properties used in configuring the server
*
* @return
Modified: projects/bootstrap/trunk/spi/src/test/java/org/jboss/bootstrap/spi/config/unit/ConfigInitializationTestCase.java
===================================================================
--- projects/bootstrap/trunk/spi/src/test/java/org/jboss/bootstrap/spi/config/unit/ConfigInitializationTestCase.java 2009-04-12 08:00:17 UTC (rev 87174)
+++ projects/bootstrap/trunk/spi/src/test/java/org/jboss/bootstrap/spi/config/unit/ConfigInitializationTestCase.java 2009-04-12 09:08:42 UTC (rev 87175)
@@ -26,6 +26,7 @@
import junit.framework.TestCase;
+import org.jboss.bootstrap.spi.config.ServerConfig;
import org.jboss.bootstrap.spi.config.TestConfigFactory;
import org.jboss.bootstrap.spi.config.TestConfigurationInitializer;
import org.jboss.bootstrap.spi.config.TestServerConfig;
@@ -81,11 +82,11 @@
// Log
log.info("testBootstrapConfDefaultedInInitializationFromHome");
- // Get a populated config and server
+ // Get a populated config
final TestServerConfig configuration = TestConfigFactory.createConfiguration();
// Remove conf and set on server
- configuration.bootstrapConfLocation(null);
+ configuration.bootstrapConfLocation((String) null);
// Initialize
configurationInitializer.initialize(configuration);
@@ -110,11 +111,11 @@
@Test
public void testBootstrapConfDefaultedInInitializationFromBootstrapUrlBase() throws Throwable
{
- // Get a populated config and server
+ // Get a populated config
final TestServerConfig configuration = TestConfigFactory.createConfiguration();
// Remove conf and home
- configuration.bootstrapHome(null).bootstrapConfLocation(null);
+ configuration.bootstrapHome((String) null).bootstrapConfLocation((String) null);
// Initialize
configurationInitializer.initialize(configuration);
@@ -139,11 +140,11 @@
@Test
public void testBootstrapUrlDefaultedInInitialization() throws Throwable
{
- // Get a populated config and server
+ // Get a populated config
final TestServerConfig configuration = TestConfigFactory.createConfiguration();
// Remove bootstrapURL
- configuration.bootstrapUrl(null);
+ configuration.bootstrapUrl((String) null);
// Initialize
configurationInitializer.initialize(configuration);
@@ -160,4 +161,42 @@
TestCase.assertNotNull("Bootstrap URL was not initialized", bootstrapUrl);
TestCase.assertEquals("Initialized bootstrap is to incorrect location", expectedBootstrapURL, bootstrapUrl);
}
+
+ /**
+ * Ensures that each configuration value may be overridden
+ * by using the corresponding system property
+ */
+ @Test
+ public void testSystemPropertyOverrides() throws Throwable
+ {
+ // Log
+ log.info("testSystemPropertyOverrides");
+
+ // Get a populated config
+ final TestServerConfig configuration = TestConfigFactory.createConfiguration();
+
+ // Define some property values
+ final String expectedBootstrapName = "overrideBootstrapName";
+ final String expectedBootstrapHome = "file://overrideBootstrapHome";
+ final String expectedBootstrapConf = "file://overrideBootstrapConf";
+ final String expectedBootstrapUrl = "file://overrideBootstrapUrl";
+
+ // Set as overrides
+ configuration.property(ServerConfig.PROP_KEY_BOOTSTRAP_NAME, expectedBootstrapName).property(
+ ServerConfig.PROP_KEY_BOOTSTRAP_HOME_URL, expectedBootstrapHome).property(
+ ServerConfig.PROP_KEY_BOOTSTRAP_CONF_URL, expectedBootstrapConf).property(
+ ServerConfig.PROP_KEY_BOOTSTRAP_URL, expectedBootstrapUrl);
+
+ // Initialize
+ configurationInitializer.initialize(configuration);
+
+ // Test
+ final String failMessage = "Override was not honored";
+ TestCase.assertEquals(failMessage, expectedBootstrapName, configuration.getBootstrapName());
+ TestCase.assertEquals(failMessage, expectedBootstrapHome, configuration.getBootstrapHome().toExternalForm());
+ TestCase.assertEquals(failMessage, expectedBootstrapConf, configuration.getBootstrapConfLocation()
+ .toExternalForm());
+ TestCase.assertEquals(failMessage, expectedBootstrapUrl, configuration.getBootstrapUrl().toExternalForm());
+
+ }
}
Modified: projects/bootstrap/trunk/spi/src/test/java/org/jboss/bootstrap/spi/config/unit/ConfigValidationTestCase.java
===================================================================
--- projects/bootstrap/trunk/spi/src/test/java/org/jboss/bootstrap/spi/config/unit/ConfigValidationTestCase.java 2009-04-12 08:00:17 UTC (rev 87174)
+++ projects/bootstrap/trunk/spi/src/test/java/org/jboss/bootstrap/spi/config/unit/ConfigValidationTestCase.java 2009-04-12 09:08:42 UTC (rev 87175)
@@ -126,7 +126,7 @@
final TestServerConfig configuration = TestConfigFactory.createConfiguration();
// Remove home and name
- configuration.bootstrapHome(null).bootstrapName(null);
+ configuration.bootstrapHome((String) null).bootstrapName((String) null);
// Validate
try
@@ -158,7 +158,7 @@
final TestServerConfig configuration = TestConfigFactory.createConfiguration();
// Remove home, name, bootstrapURL
- configuration.bootstrapHome(null).bootstrapName(null).bootstrapUrl(null);
+ configuration.bootstrapHome((String) null).bootstrapName((String) null).bootstrapUrl((String) null);
// Validate
boolean validationFailed = false;
More information about the jboss-cvs-commits
mailing list