diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/PropertyUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/PropertyUtils.java index 88af44f3dd749bbf8f0fb3ec9a2ffa374986dd9d..d04eb59207f1fde8515b421b53a1f6671fc01734 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/PropertyUtils.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/PropertyUtils.java @@ -16,10 +16,13 @@ package ch.systemsx.cisd.common.utilities; +import java.io.FileInputStream; +import java.io.InputStream; import java.util.Collections; import java.util.Enumeration; import java.util.Properties; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; @@ -400,10 +403,39 @@ public final class PropertyUtils { assert properties != null : "Unspecified properties"; for (final Enumeration<String> enumeration = - (Enumeration<String>) properties.propertyNames(); enumeration.hasMoreElements(); ) + (Enumeration<String>) properties.propertyNames(); enumeration.hasMoreElements(); /**/) { final String key = enumeration.nextElement(); 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); + } + } } diff --git a/datamover/source/java/ch/systemsx/cisd/datamover/Parameters.java b/datamover/source/java/ch/systemsx/cisd/datamover/Parameters.java index a47217d26113a7bb9f709e52be7c348836c15328..9dacbb6230f37677f0c5811802822872e63e56aa 100644 --- a/datamover/source/java/ch/systemsx/cisd/datamover/Parameters.java +++ b/datamover/source/java/ch/systemsx/cisd/datamover/Parameters.java @@ -17,15 +17,12 @@ package ch.systemsx.cisd.datamover; import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Properties; import java.util.regex.Pattern; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.time.DateUtils; import org.apache.commons.lang.time.DurationFormatUtils; import org.apache.log4j.Logger; @@ -396,7 +393,8 @@ public final class Parameters implements ITimingParameters, IFileSysParameters private final void initParametersFromProperties() { - final Properties serviceProperties = loadServiceProperties(); + final Properties serviceProperties = + PropertyUtils.loadProperties(DatamoverConstants.SERVICE_PROPERTIES_FILE); dataCompletedScript = tryCreateFile(serviceProperties, PropertyNames.DATA_COMPLETED_SCRIPT, dataCompletedScript); @@ -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() { return dataCompletedScript; @@ -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 - * overwritten rather than appended to. + * @return <code>true</code>, if rsync is called in such a way to files that already exist + * are overwritten rather than appended to. */ public final boolean isRsyncOverwrite() { @@ -701,8 +669,8 @@ public final class Parameters implements ITimingParameters, IFileSysParameters /** * @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 - * same file system as {@link #getBufferDirectoryPath}. + * <code>null</code> if it is not specified. Note that this directory needs to be on + * the same file system as {@link #getBufferDirectoryPath}. */ public final File tryGetExtraCopyDir() { diff --git a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/DatasetDownloadService.java b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/DatasetDownloadService.java index 3e0d4e6abbb8ad0b294b103ab2a418a3e89fc4e9..146d4b07830852fac5069cd559f93267e452ea8b 100644 --- a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/DatasetDownloadService.java +++ b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/DatasetDownloadService.java @@ -16,12 +16,10 @@ package ch.systemsx.cisd.openbis.datasetdownload; -import java.io.FileInputStream; -import java.io.InputStream; +import java.io.File; import java.util.Enumeration; import java.util.Properties; -import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import org.mortbay.jetty.Server; import org.mortbay.jetty.security.SslSocketConnector; @@ -55,36 +53,71 @@ public class DatasetDownloadService private static final Logger operationLog = 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); - 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 int port = configParameters.getPort(); - final Server server = new Server(); + final Server thisServer = new Server(); final SslSocketConnector socketConnector = new SslSocketConnector(); socketConnector.setPort(port); socketConnector.setMaxIdleTime(30000); socketConnector.setKeystore(configParameters.getKeystorePath()); socketConnector.setPassword(configParameters.getKeystorePassword()); socketConnector.setKeyPassword(configParameters.getKeystoreKeyPassword()); - server.addConnector(socketConnector); - final Context context = new Context(server, "/", Context.SESSIONS); + thisServer.addConnector(socketConnector); + final Context context = new Context(thisServer, "/", Context.SESSIONS); context.setAttribute(APPLICATION_CONTEXT_KEY, applicationContext); context.addServlet(DatasetDownloadServlet.class, "/" + applicationContext.getApplicationName() + "/*"); - server.start(); - - selfTest(applicationContext); - if (operationLog.isInfoEnabled()) - { - operationLog.info("Data set download server ready on port " + port); - } + return thisServer; } - private static void selfTest(final ApplicationContext applicationContext) + private final static void selfTest(final ApplicationContext applicationContext) { final int version = applicationContext.getDataSetService().getVersion(); if (IWebService.VERSION != version) @@ -99,7 +132,7 @@ public class DatasetDownloadService } } - private static ApplicationContext createApplicationContext() + private final static ApplicationContext createApplicationContext() { final ConfigParameters configParameters = getConfigParameters(); final IDataSetService dataSetService = new DataSetService(configParameters); @@ -108,9 +141,16 @@ public class DatasetDownloadService 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 Enumeration<?> propertyNames = systemProperties.propertyNames(); while (propertyNames.hasMoreElements()) @@ -126,31 +166,4 @@ public class DatasetDownloadService configParameters.log(); 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); - } - - } }