diff --git a/datamover/source/java/ch/systemsx/cisd/datamover/Main.java b/datamover/source/java/ch/systemsx/cisd/datamover/Main.java index 31b376a2964c11ed09c82239b0e400a5cb70072b..885cd0a6e228b2dbe8cd92ac47f731077fe0eb6e 100644 --- a/datamover/source/java/ch/systemsx/cisd/datamover/Main.java +++ b/datamover/source/java/ch/systemsx/cisd/datamover/Main.java @@ -96,45 +96,31 @@ public final class Main */ private static void selfTest(final Parameters parameters) { - final String msgStart = "Datamover self test failed:"; - try - { - final ArrayList<IFileStore> stores = new ArrayList<IFileStore>(); - final FileSysOperationsFactory factory = new FileSysOperationsFactory(parameters); - stores.add(parameters.getIncomingStore(factory)); - final IFileStore bufferStore = - FileStoreFactory.createLocal(parameters.getBufferDirectoryPath(), - Parameters.BUFFER_KIND_DESC, factory); - stores.add(bufferStore); - stores.add(parameters.getOutgoingStore(factory)); - if (parameters.tryGetManualInterventionDir() != null) - { - final IFileStore dummyStore = - FileStoreFactory.createLocal(parameters.tryGetManualInterventionDir(), - "manual intervention", factory); - stores.add(dummyStore); - } - if (parameters.tryGetExtraCopyDir() != null) - { - final IFileStore dummyStore = - FileStoreFactory.createLocal(parameters.tryGetExtraCopyDir(), "extra-copy", - factory); - stores.add(dummyStore); - } - final IPathCopier copyProcess = factory.getCopier(false); - SelfTest.check(copyProcess, stores.toArray(IFileStore.EMPTY_ARRAY), - FileStoreSelfTestables.getSelfTestables()); - } catch (final HighLevelException e) + final ArrayList<IFileStore> stores = new ArrayList<IFileStore>(); + final FileSysOperationsFactory factory = new FileSysOperationsFactory(parameters); + stores.add(parameters.getIncomingStore(factory)); + final IFileStore bufferStore = + FileStoreFactory.createLocal(parameters.getBufferDirectoryPath(), + Parameters.BUFFER_KIND_DESC, factory); + stores.add(bufferStore); + stores.add(parameters.getOutgoingStore(factory)); + if (parameters.tryGetManualInterventionDir() != null) { - System.err.printf(msgStart + " [%s: %s]\n", e.getClass().getSimpleName(), e - .getMessage()); - System.exit(1); - } catch (final RuntimeException e) + final IFileStore dummyStore = + FileStoreFactory.createLocal(parameters.tryGetManualInterventionDir(), + "manual intervention", factory); + stores.add(dummyStore); + } + if (parameters.tryGetExtraCopyDir() != null) { - System.err.println(msgStart); - e.printStackTrace(); - System.exit(1); + final IFileStore dummyStore = + FileStoreFactory.createLocal(parameters.tryGetExtraCopyDir(), "extra-copy", + factory); + stores.add(dummyStore); } + final IPathCopier copyProcess = factory.getCopier(false); + SelfTest.check(copyProcess, stores.toArray(IFileStore.EMPTY_ARRAY), + FileStoreSelfTestables.getSelfTestables()); } private final static void createShutdownHookTimer(final ITerminable terminable) @@ -176,14 +162,32 @@ public final class Main public static void main(final String[] args) { - initLog(); - final Parameters parameters = new Parameters(args); - printInitialLogMessage(parameters); - startupServer(parameters); - selfTest(parameters); - if (operationLog.isInfoEnabled()) + String msgStart = ""; + try + { + initLog(); + msgStart = "Datamover parameters wrong:"; + final Parameters parameters = new Parameters(args); + msgStart = ""; + printInitialLogMessage(parameters); + msgStart = "Failure in starting up server"; + startupServer(parameters); + msgStart = "Datamover self test failed:"; + selfTest(parameters); + if (operationLog.isInfoEnabled()) + { + operationLog.info("Datamover ready and waiting for data."); + } + } catch (final HighLevelException e) + { + System.err.printf(msgStart + " [%s: %s]\n", e.getClass().getSimpleName(), e + .getMessage()); + System.exit(1); + } catch (final RuntimeException e) { - operationLog.info("Datamover ready and waiting for data."); + System.err.println(msgStart); + e.printStackTrace(); + System.exit(1); } } diff --git a/datamover/source/java/ch/systemsx/cisd/datamover/Parameters.java b/datamover/source/java/ch/systemsx/cisd/datamover/Parameters.java index 077004e3ec47f60b12379c867969e51ff61f0e6b..57a1afecc5a9f71b3005fd426e8d005f10652598 100644 --- a/datamover/source/java/ch/systemsx/cisd/datamover/Parameters.java +++ b/datamover/source/java/ch/systemsx/cisd/datamover/Parameters.java @@ -41,15 +41,12 @@ import ch.systemsx.cisd.args4j.spi.LongOptionHandler; import ch.systemsx.cisd.args4j.spi.OptionHandler; import ch.systemsx.cisd.args4j.spi.Setter; import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; -import ch.systemsx.cisd.common.exceptions.HighLevelException; import ch.systemsx.cisd.common.highwatermark.HostAwareFileWithHighwaterMark; import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.common.utilities.BuildAndEnvironmentInfo; -import ch.systemsx.cisd.common.utilities.IExitHandler; import ch.systemsx.cisd.common.utilities.OSUtilities; import ch.systemsx.cisd.common.utilities.PropertyUtils; -import ch.systemsx.cisd.common.utilities.SystemExit; import ch.systemsx.cisd.datamover.filesystem.FileStoreFactory; import ch.systemsx.cisd.datamover.filesystem.intf.IFileStore; import ch.systemsx.cisd.datamover.filesystem.intf.IFileSysOperationsFactory; @@ -334,57 +331,43 @@ public final class Parameters implements ITimingParameters, IFileSysParameters static final String OUTGOING_KIND_DESC = "outgoing"; Parameters(final String[] args) - { - this(args, SystemExit.SYSTEM_EXIT); - } - - Parameters(final String[] args, final IExitHandler systemExitHandler) { initParametersFromProperties(); - try + parser.parseArgument(args); + if (incomingTarget == null) { - parser.parseArgument(args); - if (incomingTarget == null) - { - throw createConfigurationFailureException(PropertyNames.INCOMING_TARGET); - } else if (incomingTarget.getHighwaterMark() > -1L) + throw createConfigurationFailureException(PropertyNames.INCOMING_TARGET); + } else if (incomingTarget.getHighwaterMark() > -1L) + { + throw ConfigurationFailureException.fromTemplate( + "Can not specify a high water mark for '%s'.", + PropertyNames.INCOMING_TARGET); + } + if (bufferDirectory == null) + { + throw createConfigurationFailureException(PropertyNames.BUFFER_DIR); + } else if (bufferDirectory.tryGetHost() != null) + { + throw ConfigurationFailureException.fromTemplate("Remote '%s' not supported.", + PropertyNames.BUFFER_DIR); + } + if (outgoingTarget == null) + { + throw createConfigurationFailureException(PropertyNames.OUTGOING_TARGET); + } + if (manualInterventionDirectoryOrNull == null && manualInterventionRegex != null) + { + throw ConfigurationFailureException.fromTemplate("No '%s' defined, but '%s'.", + PropertyNames.MANUAL_INTERVENTION_DIR, + PropertyNames.MANUAL_INTERVENTION_REGEX); + } + if (getDataCompletedScript() != null) + { + if (OSUtilities.executableExists(dataCompletedScript) == false) { throw ConfigurationFailureException.fromTemplate( - "Can not specify a high water mark for '%s'.", - PropertyNames.INCOMING_TARGET); - } - if (bufferDirectory == null) - { - throw createConfigurationFailureException(PropertyNames.BUFFER_DIR); - } else if (bufferDirectory.tryGetHost() != null) - { - throw ConfigurationFailureException.fromTemplate("Remote '%s' not supported.", - PropertyNames.BUFFER_DIR); - } - if (outgoingTarget == null) - { - throw createConfigurationFailureException(PropertyNames.OUTGOING_TARGET); - } - if (manualInterventionDirectoryOrNull == null && manualInterventionRegex != null) - { - throw ConfigurationFailureException.fromTemplate("No '%s' defined, but '%s'.", - PropertyNames.MANUAL_INTERVENTION_DIR, - PropertyNames.MANUAL_INTERVENTION_REGEX); - } - if (getDataCompletedScript() != null) - { - if (OSUtilities.executableExists(dataCompletedScript) == false) - { - throw ConfigurationFailureException.fromTemplate( - DATA_COMPLETED_SCRIPT_NOT_FOUND_TEMPLATE, dataCompletedScript); - } + DATA_COMPLETED_SCRIPT_NOT_FOUND_TEMPLATE, dataCompletedScript); } - } catch (final Exception ex) - { - outputException(ex); - systemExitHandler.exit(1); - // Only reached in unit tests. - throw new AssertionError(ex.getMessage()); } } @@ -394,22 +377,6 @@ public final class Parameters implements ITimingParameters, IFileSysParameters return ConfigurationFailureException.fromTemplate("No '%s' defined.", propertyKey); } - private final void outputException(final Exception ex) - { - if (ex instanceof HighLevelException || ex instanceof CmdLineException) - { - System.err.println(ex.getMessage()); - } else - { - System.err.println("An exception occurred."); - ex.printStackTrace(); - } - if (ex instanceof CmdLineException) - { - printHelp(false); - } - } - private final static long toMillis(final long seconds) { return seconds * DateUtils.MILLIS_PER_SECOND; diff --git a/datamover/sourceTest/java/ch/systemsx/cisd/datamover/ParametersTest.java b/datamover/sourceTest/java/ch/systemsx/cisd/datamover/ParametersTest.java index 65f94c14b11bb6da2337fbb60f08056b3d8cd407..0bf662f30bb29b1fac4f6cf70c7e2d0319c8a668 100644 --- a/datamover/sourceTest/java/ch/systemsx/cisd/datamover/ParametersTest.java +++ b/datamover/sourceTest/java/ch/systemsx/cisd/datamover/ParametersTest.java @@ -21,20 +21,17 @@ import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertTrue; -import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.PrintStream; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.time.DateUtils; import org.testng.annotations.AfterClass; -import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import ch.rinn.restrictions.Friend; +import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; import ch.systemsx.cisd.common.highwatermark.HostAwareFileWithHighwaterMark; import ch.systemsx.cisd.common.utilities.AbstractFileSystemTestCase; import ch.systemsx.cisd.common.utilities.SystemExit; @@ -54,21 +51,9 @@ import ch.systemsx.cisd.datamover.intf.IFileSysParameters; { SystemExit.class, Parameters.class, FileStoreFactory.class }) public final class ParametersTest extends AbstractFileSystemTestCase { - private ByteArrayOutputStream logRecorder; - - private PrintStream systemOut; - - private PrintStream systemErr; - private final Parameters parse(final String... args) { - return new Parameters(args, SystemExit.SYSTEM_EXIT); - } - - /** Returns the content of the log recorder. */ - private final String getLogContent() - { - return logRecorder.toString().trim(); + return new Parameters(args); } @BeforeClass @@ -83,30 +68,6 @@ public final class ParametersTest extends AbstractFileSystemTestCase SystemExit.setThrowException(false); } - @Override - @BeforeMethod - public final void setUp() - { - logRecorder = new ByteArrayOutputStream(); - systemOut = System.out; - systemErr = System.err; - System.setErr(new PrintStream(logRecorder)); - System.setOut(new PrintStream(logRecorder)); - } - - @AfterMethod - public void tearDown() - { - if (systemOut != null) - { - System.setOut(systemOut); - } - if (systemErr != null) - { - System.setErr(systemErr); - } - } - @Test public void testSetRsyncExecutableLong() throws Exception { @@ -241,10 +202,10 @@ public final class ParametersTest extends AbstractFileSystemTestCase try { parameters = parse("--" + PropertyNames.DATA_COMPLETED_SCRIPT, scriptName); - } catch (final RuntimeException ex) + } catch (final ConfigurationFailureException ex) { assertEquals(String.format(Parameters.DATA_COMPLETED_SCRIPT_NOT_FOUND_TEMPLATE, - scriptName), getLogContent()); + scriptName), ex.getMessage()); } final File scriptFile = new File(workingDirectory, scriptName); FileUtils.touch(scriptFile);