Permalink
Browse files

#4153 added loading of rsb config from /etc/rsb

  • Loading branch information...
1 parent 31c9500 commit cd95e6dba4bec45e35e7b2273bc3a85d187ac81b @ddossot ddossot committed Jan 11, 2014
@@ -40,86 +40,133 @@
import eu.openanalytics.rsb.config.PersistedConfiguration.PersistedSmtpConfiguration;
/**
- * Verifies that the RSB configuration is loadable and, if not, creates a default one so RSB can start anyway.
+ * Verifies that the RSB configuration is loadable and, if not, creates a default one so RSB can
+ * start anyway.
*
* @author "OpenAnalytics <rsb.development@openanalytics.eu>"
*/
-public class BootstrapConfigurationServletContextListener implements ServletContextListener {
+public class BootstrapConfigurationServletContextListener implements ServletContextListener
+{
public static final String RSB_CONFIGURATION_SERVLET_CONTEXT_PARAM = "rsbConfiguration";
+
private static final Log LOGGER = LogFactory.getLog(BootstrapConfigurationServletContextListener.class);
- public void contextInitialized(final ServletContextEvent sce) {
- if (isConfigurationPresent(sce.getServletContext())) {
- final String configuredConfigurationFileName = getConfiguredConfigurationFileName(sce.getServletContext());
- System.setProperty(Configuration.class.getName(), configuredConfigurationFileName);
- LOGGER.info("Successfully located configured configuration file: " + configuredConfigurationFileName);
+ @Override
+ public void contextInitialized(final ServletContextEvent sce)
+ {
+ if (isConfigurationPresent(sce.getServletContext()))
+ {
return;
}
- final File directoryOnClassPath = getDirectoryOnClassPath(sce);
+ final File configurationDirectory = getWritableConfigurationDirectory(sce);
- if ((directoryOnClassPath != null) && (directoryOnClassPath.isDirectory())) {
- try {
- createDefaultConfigurationFile(new File(directoryOnClassPath, Configuration.DEFAULT_JSON_CONFIGURATION_FILE),
- getWebInfDirectory(sce));
- } catch (final URISyntaxException urie) {
+ if (configurationDirectory != null)
+ {
+ try
+ {
+ createDefaultConfigurationFile(new File(configurationDirectory,
+ Configuration.DEFAULT_JSON_CONFIGURATION_FILE), getWebInfDirectory(sce));
+ }
+ catch (final URISyntaxException urie)
+ {
LOGGER.error("Failed to create a default configuration file", urie);
}
- } else {
+ }
+ else
+ {
LOGGER.error("Configuration file " + Configuration.DEFAULT_JSON_CONFIGURATION_FILE
- + " not found and no way to create a default configuration on the classpath: RSB will not start properly!");
+ + " not found and no way to create a default configuration in "
+ + ConfigurationFactory.RSB_CONFIGURATION_DIRECTORY
+ + " or on the classpath: RSB will not start properly!");
}
}
- public void contextDestroyed(final ServletContextEvent sce) {
+ @Override
+ public void contextDestroyed(final ServletContextEvent sce)
+ {
// NOOP
}
- private static boolean isConfigurationPresent(final ServletContext servletContext) {
+ private static boolean isConfigurationPresent(final ServletContext servletContext)
+ {
final String configurationFile = getConfiguredConfigurationFileName(servletContext);
- Validate.notEmpty(configurationFile, "No configuration specified in web.xml: servlet context parameter "
- + RSB_CONFIGURATION_SERVLET_CONTEXT_PARAM + " is missing");
- return Thread.currentThread().getContextClassLoader().getResource(configurationFile) != null;
+ Validate.notEmpty(configurationFile,
+ "No configuration specified in web.xml: servlet context parameter "
+ + RSB_CONFIGURATION_SERVLET_CONTEXT_PARAM + " is missing");
+
+ System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_NAME_SYSTEM_PROPERTY, configurationFile);
+
+ final boolean configurationPresent = ConfigurationFactory.isConfigurationPresent();
+
+ if (configurationPresent)
+ {
+ LOGGER.info("Successfully located configured configuration file: " + configurationFile);
+ }
+
+ return configurationPresent;
}
- private static String getConfiguredConfigurationFileName(final ServletContext servletContext) {
+ private static String getConfiguredConfigurationFileName(final ServletContext servletContext)
+ {
return servletContext.getInitParameter(RSB_CONFIGURATION_SERVLET_CONTEXT_PARAM);
}
- private File getWebInfDirectory(final ServletContextEvent sce) {
+ private File getWebInfDirectory(final ServletContextEvent sce)
+ {
final String path = sce.getServletContext().getRealPath("/WEB-INF");
- if (StringUtils.isNotBlank(path)) {
+ if (StringUtils.isNotBlank(path))
+ {
return new File(path);
}
return null;
}
- private File getDirectoryOnClassPath(final ServletContextEvent sce) {
+ File getWritableConfigurationDirectory(final ServletContextEvent sce)
+ {
+ File rsbConfigurationDirectory = new File(ConfigurationFactory.RSB_CONFIGURATION_DIRECTORY);
+ if (rsbConfigurationDirectory.isDirectory() && rsbConfigurationDirectory.canWrite())
+ {
+ return rsbConfigurationDirectory;
+ }
+
final String path = sce.getServletContext().getRealPath("/WEB-INF/classes");
- if (StringUtils.isNotBlank(path)) {
- return new File(path);
+ if (StringUtils.isNotBlank(path))
+ {
+ rsbConfigurationDirectory = new File(path);
+ if (rsbConfigurationDirectory.isDirectory() && rsbConfigurationDirectory.canWrite())
+ {
+ return rsbConfigurationDirectory;
+ }
}
+
return null;
}
// exposed for testing
- private static void createDefaultConfigurationFile(final File defaultConfigurationFile, final File webInfDirectory)
- throws URISyntaxException {
+ private static void createDefaultConfigurationFile(final File defaultConfigurationFile,
+ final File webInfDirectory) throws URISyntaxException
+ {
final PersistedConfiguration defaultConfiguration = createDefaultConfiguration(webInfDirectory);
- try {
+ try
+ {
Util.toPrettyJsonFile(defaultConfiguration, defaultConfigurationFile);
LOGGER.warn("Created default RSB configuration: "
- + defaultConfigurationFile
- + ". It is not production grade and will be wiped out in case of RSB redeployment. Please configure properly and move to another non-transient location on the classpath (for example TOMCAT_HOME/lib).");
- } catch (final IOException ioe) {
+ + defaultConfigurationFile
+ + ". It is not production grade and will be wiped out in case of RSB redeployment. Please configure properly and move to another non-transient location on the classpath (for example TOMCAT_HOME/lib).");
+ }
+ catch (final IOException ioe)
+ {
LOGGER.error("Failed to create default RSB configuration file!", ioe);
}
}
// exposed for unit testing
- static PersistedConfiguration createDefaultConfiguration(final File webInfDirectory) throws URISyntaxException {
+ static PersistedConfiguration createDefaultConfiguration(final File webInfDirectory)
+ throws URISyntaxException
+ {
final File defaultRsbHomeDirectory = getDefaultRsbHomeDirectory(webInfDirectory);
final PersistedConfiguration defaultConfiguration = new PersistedConfiguration();
defaultConfiguration.setActiveMqWorkDirectory(new File(defaultRsbHomeDirectory, "activemq"));
@@ -135,29 +182,37 @@ static PersistedConfiguration createDefaultConfiguration(final File webInfDirect
// exposed for unit testing
// by default create all the directories required by RSB under a single parent home directory
// note that this is not a requirement: each directory can be located in different locations
- static File getDefaultRsbHomeDirectory(final File webInfDirectory) {
+ static File getDefaultRsbHomeDirectory(final File webInfDirectory)
+ {
// try in different potential locations
- final File[] potentialRsbHomeParentDirectories = { FileUtils.getUserDirectory(), webInfDirectory, FileUtils.getTempDirectory() };
+ final File[] potentialRsbHomeParentDirectories = {FileUtils.getUserDirectory(), webInfDirectory,
+ FileUtils.getTempDirectory()};
- for (final File potentialRsbHomeParentDirectory : potentialRsbHomeParentDirectories) {
- if (potentialRsbHomeParentDirectory == null) {
+ for (final File potentialRsbHomeParentDirectory : potentialRsbHomeParentDirectories)
+ {
+ if (potentialRsbHomeParentDirectory == null)
+ {
continue;
}
final File result = getOrCreateDefaultRsbHomeDirectory(potentialRsbHomeParentDirectory);
- if (result != null) {
+ if (result != null)
+ {
return result;
}
}
throw new IllegalStateException("Failed to create RSB directories in all attempted locations");
}
- private static File getOrCreateDefaultRsbHomeDirectory(final File rsbHomeParentDirectory) {
+ private static File getOrCreateDefaultRsbHomeDirectory(final File rsbHomeParentDirectory)
+ {
final File rsbHomeDirectory = new File(rsbHomeParentDirectory, ".rsb");
- if (rsbHomeDirectory.isDirectory()) {
+ if (rsbHomeDirectory.isDirectory())
+ {
return rsbHomeDirectory;
}
- if (rsbHomeDirectory.mkdir()) {
+ if (rsbHomeDirectory.mkdir())
+ {
return rsbHomeDirectory;
}
LOGGER.info("Failed to create default RSB home directory under: " + rsbHomeDirectory);
@@ -24,6 +24,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Map.Entry;
@@ -53,23 +54,36 @@
*/
public abstract class ConfigurationFactory
{
+ public static final String RSB_CONFIGURATION_DIRECTORY = "/etc/rsb";
+
+ static final String CONFIGURATION_FILE_NAME_SYSTEM_PROPERTY = Configuration.class.getName();
+
private static final Log LOGGER = LogFactory.getLog(ConfigurationFactory.class);
private ConfigurationFactory()
{
throw new UnsupportedOperationException("do not instantiate");
}
+ static boolean isConfigurationPresent()
+ {
+ return getConfigurationUrl() != null;
+ }
+
public static Configuration loadJsonConfiguration() throws IOException
{
- final String configurationFile = System.getProperty(Configuration.class.getName(),
- Configuration.DEFAULT_JSON_CONFIGURATION_FILE);
- final PersistedConfigurationAdapter pca = load(configurationFile);
+ final PersistedConfigurationAdapter pca = load(getConfigurationUrl());
validateConfiguration(pca);
createMissingDirectories(pca);
return pca;
}
+ private static String getConfigurationFileName()
+ {
+ return System.getProperty(CONFIGURATION_FILE_NAME_SYSTEM_PROPERTY,
+ Configuration.DEFAULT_JSON_CONFIGURATION_FILE);
+ }
+
public static Configuration loadJsonConfiguration(final InputStream is) throws IOException
{
final PersistedConfigurationAdapter pca = loadConfigurationStream(null, is);
@@ -95,17 +109,35 @@ private static void validateConfiguration(final PersistedConfigurationAdapter pc
}
// exposed for testing
- static PersistedConfigurationAdapter load(final String configurationFile) throws IOException
+ static PersistedConfigurationAdapter load(final URL configurationUrl) throws IOException
{
- final URL configurationUrl = Thread.currentThread()
- .getContextClassLoader()
- .getResource(configurationFile);
- Validate.notNull(configurationUrl, "Impossible to find " + configurationFile + " on the classpath");
+ Validate.notNull(configurationUrl, "Impossible to find " + configurationUrl);
final InputStream is = configurationUrl.openStream();
return loadConfigurationStream(configurationUrl, is);
}
+ private static URL getConfigurationUrl()
+ {
+ final File configurationFile = new File(RSB_CONFIGURATION_DIRECTORY, getConfigurationFileName());
+ if (configurationFile.isFile() && configurationFile.canRead())
+ {
+ try
+ {
+ return configurationFile.toURI().toURL();
+ }
+ catch (final MalformedURLException murle)
+ {
+ throw new IllegalStateException("Unexpected toURL failure for file: " + configurationFile,
+ murle);
+ }
+ }
+ else
+ {
+ return Thread.currentThread().getContextClassLoader().getResource(getConfigurationFileName());
+ }
+ }
+
private static PersistedConfigurationAdapter loadConfigurationStream(final URL configurationUrl,
final InputStream is)
throws IOException
@@ -62,7 +62,7 @@
<section name="Configuration">
<p>
RSB is configured with a unique JSON file named
- <b>rsb-configuration.json</b>, which must be located at the root of the JVM classpath (for example, in Tomcat's lib directory).
+ <b>rsb-configuration.json</b>, which must be located either in <code>/etc/rsb</code> or at the root of the JVM classpath (for example, in Tomcat's lib directory).
</p>
<subsection name="Samples">
<p>Here are a few examples of configuration files:</p>
@@ -83,7 +83,7 @@
</subsection>
<subsection name="Default Configuration">
<p>If RSB can't locate a configuration file, it will try to generate one (named rsb-configuration.json, as above)
- and store it WEB-INF/classes. Here is the content of the default
+ and store it either in <code>/etc/rsb</code> or in <code>WEB-INF/classes</code>. Here is the content of the default
configuration:</p>
<pre id="def_conf"></pre>
<p>In this configuration, $RSB_HOME$ is replaced by either, and in order of preference:</p>
@@ -92,7 +92,7 @@
<li>The rsb.war WEB-INF directory, if it is writeable,</li>
<li>The operating system temporary directory</li>
</ul>
- <p>If RSB generates such a default configuration file, it is recommended to review it so it uses the desired values and move it to the root of the JVM classpath, as discussed in the previous section.</p>
+ <p>If RSB generates such a default configuration file in<code>WEB-INF/classes</code>, it is recommended to review it so it uses the desired values and move it to <code>/etc/rsb</code>, as discussed in the previous section.</p>
</subsection>
<subsection name="Description">
<p>Configuration attributes are documented on the <a href="./apidocs/index.html?eu/openanalytics/rsb/config/PersistedConfiguration.html">persisted configuration classes</a>.</p>
Oops, something went wrong.

0 comments on commit cd95e6d

Please sign in to comment.