Skip to content
Snippets Groups Projects
Commit 3cdba332 authored by ribeaudc's avatar ribeaudc
Browse files

[LMS-579] change: - Move 'loadProperties' to 'PropertyUtils'.

SVN: 8301
parent 6f1594d5
No related branches found
No related tags found
No related merge requests found
...@@ -16,10 +16,13 @@ ...@@ -16,10 +16,13 @@
package ch.systemsx.cisd.common.utilities; package ch.systemsx.cisd.common.utilities;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Properties; import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang.math.NumberUtils;
...@@ -400,10 +403,39 @@ public final class PropertyUtils ...@@ -400,10 +403,39 @@ public final class PropertyUtils
{ {
assert properties != null : "Unspecified properties"; assert properties != null : "Unspecified properties";
for (final Enumeration<String> enumeration = for (final Enumeration<String> enumeration =
(Enumeration<String>) properties.propertyNames(); enumeration.hasMoreElements(); ) (Enumeration<String>) properties.propertyNames(); enumeration.hasMoreElements(); /**/)
{ {
final String key = enumeration.nextElement(); final String key = enumeration.nextElement();
properties.setProperty(key, StringUtils.trim(properties.getProperty(key))); properties.setProperty(key, StringUtils.trim(properties.getProperty(key)));
} }
} }
/**
* Loads and returns {@link Properties} found in given <var>propertiesFilePath</var>.
*
* @throws ConfigurationFailureException If an exception occurs when loading the properties.
* @return never <code>null</code> but could return empty properties.
*/
public final static Properties loadProperties(final String propertiesFilePath)
{
assert propertiesFilePath != null : "Unspecified file";
final Properties properties = new Properties();
InputStream is = null;
try
{
is = new FileInputStream(propertiesFilePath);
properties.load(is);
trimProperties(properties);
return properties;
} catch (final Exception ex)
{
final String msg =
String.format("Could not load the properties from given resource '%s'.",
propertiesFilePath);
throw new ConfigurationFailureException(msg, ex);
} finally
{
IOUtils.closeQuietly(is);
}
}
} }
...@@ -17,15 +17,12 @@ ...@@ -17,15 +17,12 @@
package ch.systemsx.cisd.datamover; package ch.systemsx.cisd.datamover;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.time.DateUtils; import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang.time.DurationFormatUtils; import org.apache.commons.lang.time.DurationFormatUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
...@@ -396,7 +393,8 @@ public final class Parameters implements ITimingParameters, IFileSysParameters ...@@ -396,7 +393,8 @@ public final class Parameters implements ITimingParameters, IFileSysParameters
private final void initParametersFromProperties() private final void initParametersFromProperties()
{ {
final Properties serviceProperties = loadServiceProperties(); final Properties serviceProperties =
PropertyUtils.loadProperties(DatamoverConstants.SERVICE_PROPERTIES_FILE);
dataCompletedScript = dataCompletedScript =
tryCreateFile(serviceProperties, PropertyNames.DATA_COMPLETED_SCRIPT, tryCreateFile(serviceProperties, PropertyNames.DATA_COMPLETED_SCRIPT,
dataCompletedScript); dataCompletedScript);
...@@ -503,36 +501,6 @@ public final class Parameters implements ITimingParameters, IFileSysParameters ...@@ -503,36 +501,6 @@ public final class Parameters implements ITimingParameters, IFileSysParameters
} }
} }
/**
* Returns the service property.
*
* @throws ConfigurationFailureException If an exception occurs when loading the service
* properties.
*/
private final static Properties loadServiceProperties()
{
final Properties properties = new Properties();
try
{
final InputStream is = new FileInputStream(DatamoverConstants.SERVICE_PROPERTIES_FILE);
try
{
properties.load(is);
return properties;
} finally
{
IOUtils.closeQuietly(is);
}
} catch (final Exception ex)
{
final String msg =
"Could not load the service properties from resource '"
+ DatamoverConstants.SERVICE_PROPERTIES_FILE + "'.";
operationLog.warn(msg, ex);
throw new ConfigurationFailureException(msg, ex);
}
}
public final File getDataCompletedScript() public final File getDataCompletedScript()
{ {
return dataCompletedScript; return dataCompletedScript;
...@@ -570,8 +538,8 @@ public final class Parameters implements ITimingParameters, IFileSysParameters ...@@ -570,8 +538,8 @@ public final class Parameters implements ITimingParameters, IFileSysParameters
} }
/** /**
* @return <code>true</code>, if rsync is called in such a way to files that already exist are * @return <code>true</code>, if rsync is called in such a way to files that already exist
* overwritten rather than appended to. * are overwritten rather than appended to.
*/ */
public final boolean isRsyncOverwrite() public final boolean isRsyncOverwrite()
{ {
...@@ -701,8 +669,8 @@ public final class Parameters implements ITimingParameters, IFileSysParameters ...@@ -701,8 +669,8 @@ public final class Parameters implements ITimingParameters, IFileSysParameters
/** /**
* @return The directory where we create an additional copy of incoming data or * @return The directory where we create an additional copy of incoming data or
* <code>null</code> if it is not specified. Note that this directory needs to be on the * <code>null</code> if it is not specified. Note that this directory needs to be on
* same file system as {@link #getBufferDirectoryPath}. * the same file system as {@link #getBufferDirectoryPath}.
*/ */
public final File tryGetExtraCopyDir() public final File tryGetExtraCopyDir()
{ {
......
...@@ -16,12 +16,10 @@ ...@@ -16,12 +16,10 @@
package ch.systemsx.cisd.openbis.datasetdownload; package ch.systemsx.cisd.openbis.datasetdownload;
import java.io.FileInputStream; import java.io.File;
import java.io.InputStream;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Properties; import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.mortbay.jetty.Server; import org.mortbay.jetty.Server;
import org.mortbay.jetty.security.SslSocketConnector; import org.mortbay.jetty.security.SslSocketConnector;
...@@ -55,36 +53,71 @@ public class DatasetDownloadService ...@@ -55,36 +53,71 @@ public class DatasetDownloadService
private static final Logger operationLog = private static final Logger operationLog =
LogFactory.getLogger(LogCategory.OPERATION, DatasetDownloadService.class); LogFactory.getLogger(LogCategory.OPERATION, DatasetDownloadService.class);
public static void main(final String[] args) throws Exception private static Server server;
public static final void start()
{ {
LogInitializer.init(); assert server == null : "Server already started";
ServiceRegistry.setLIMSServiceFactory(RMIBasedLIMSServiceFactory.INSTANCE); ServiceRegistry.setLIMSServiceFactory(RMIBasedLIMSServiceFactory.INSTANCE);
final ApplicationContext applicationContext = createApplicationContext(); final ApplicationContext applicationContext = createApplicationContext();
server = createServer(applicationContext);
try
{
server.start();
selfTest(applicationContext);
if (operationLog.isInfoEnabled())
{
operationLog.info("Data set download server ready on port "
+ applicationContext.getConfigParameters().getPort());
}
} catch (final Exception ex)
{
operationLog.error("Failed to start server.", ex);
}
}
public static final void stop()
{
assert server != null : "Server has not been started.";
if (server.isRunning())
{
try
{
server.stop();
} catch (final Exception ex)
{
operationLog.error("Failed to stop server.", ex);
}
}
server = null;
}
public static void main(final String[] args)
{
LogInitializer.init();
start();
}
private final static Server createServer(final ApplicationContext applicationContext)
{
final ConfigParameters configParameters = applicationContext.getConfigParameters(); final ConfigParameters configParameters = applicationContext.getConfigParameters();
final int port = configParameters.getPort(); final int port = configParameters.getPort();
final Server server = new Server(); final Server thisServer = new Server();
final SslSocketConnector socketConnector = new SslSocketConnector(); final SslSocketConnector socketConnector = new SslSocketConnector();
socketConnector.setPort(port); socketConnector.setPort(port);
socketConnector.setMaxIdleTime(30000); socketConnector.setMaxIdleTime(30000);
socketConnector.setKeystore(configParameters.getKeystorePath()); socketConnector.setKeystore(configParameters.getKeystorePath());
socketConnector.setPassword(configParameters.getKeystorePassword()); socketConnector.setPassword(configParameters.getKeystorePassword());
socketConnector.setKeyPassword(configParameters.getKeystoreKeyPassword()); socketConnector.setKeyPassword(configParameters.getKeystoreKeyPassword());
server.addConnector(socketConnector); thisServer.addConnector(socketConnector);
final Context context = new Context(server, "/", Context.SESSIONS); final Context context = new Context(thisServer, "/", Context.SESSIONS);
context.setAttribute(APPLICATION_CONTEXT_KEY, applicationContext); context.setAttribute(APPLICATION_CONTEXT_KEY, applicationContext);
context.addServlet(DatasetDownloadServlet.class, "/" context.addServlet(DatasetDownloadServlet.class, "/"
+ applicationContext.getApplicationName() + "/*"); + applicationContext.getApplicationName() + "/*");
server.start(); return thisServer;
selfTest(applicationContext);
if (operationLog.isInfoEnabled())
{
operationLog.info("Data set download server ready on port " + port);
}
} }
private static void selfTest(final ApplicationContext applicationContext) private final static void selfTest(final ApplicationContext applicationContext)
{ {
final int version = applicationContext.getDataSetService().getVersion(); final int version = applicationContext.getDataSetService().getVersion();
if (IWebService.VERSION != version) if (IWebService.VERSION != version)
...@@ -99,7 +132,7 @@ public class DatasetDownloadService ...@@ -99,7 +132,7 @@ public class DatasetDownloadService
} }
} }
private static ApplicationContext createApplicationContext() private final static ApplicationContext createApplicationContext()
{ {
final ConfigParameters configParameters = getConfigParameters(); final ConfigParameters configParameters = getConfigParameters();
final IDataSetService dataSetService = new DataSetService(configParameters); final IDataSetService dataSetService = new DataSetService(configParameters);
...@@ -108,9 +141,16 @@ public class DatasetDownloadService ...@@ -108,9 +141,16 @@ public class DatasetDownloadService
return applicationContext; return applicationContext;
} }
private static ConfigParameters getConfigParameters() private final static ConfigParameters getConfigParameters()
{ {
final Properties properties = loadProperties(); final Properties properties;
if (new File(SERVICE_PROPERTIES_FILE).exists() == false)
{
properties = new Properties();
} else
{
properties = PropertyUtils.loadProperties(SERVICE_PROPERTIES_FILE);
}
final Properties systemProperties = System.getProperties(); final Properties systemProperties = System.getProperties();
final Enumeration<?> propertyNames = systemProperties.propertyNames(); final Enumeration<?> propertyNames = systemProperties.propertyNames();
while (propertyNames.hasMoreElements()) while (propertyNames.hasMoreElements())
...@@ -126,31 +166,4 @@ public class DatasetDownloadService ...@@ -126,31 +166,4 @@ public class DatasetDownloadService
configParameters.log(); configParameters.log();
return configParameters; return configParameters;
} }
private static Properties loadProperties()
{
final Properties properties = new Properties();
try
{
final InputStream is = new FileInputStream(SERVICE_PROPERTIES_FILE);
try
{
properties.load(is);
PropertyUtils.trimProperties(properties);
return properties;
} finally
{
IOUtils.closeQuietly(is);
}
} catch (final Exception ex)
{
final String msg =
"Could not load the service properties from resource '"
+ SERVICE_PROPERTIES_FILE + "'.";
operationLog.warn(msg, ex);
throw new ConfigurationFailureException(msg, ex);
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment