diff --git a/common/source/java/ch/systemsx/cisd/common/concurrent/ConcurrencyUtilities.java b/common/source/java/ch/systemsx/cisd/common/concurrent/ConcurrencyUtilities.java index dc3d68aa068a3cbfcc6417db44e09859e0e4e352..b554f789895183437ebd82f2703ac63d3dfc43e7 100644 --- a/common/source/java/ch/systemsx/cisd/common/concurrent/ConcurrencyUtilities.java +++ b/common/source/java/ch/systemsx/cisd/common/concurrent/ConcurrencyUtilities.java @@ -22,6 +22,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; +import ch.systemsx.cisd.common.exceptions.StopException; import ch.systemsx.cisd.common.logging.ISimpleLogger; import ch.systemsx.cisd.common.logging.LogLevel; diff --git a/common/source/java/ch/systemsx/cisd/common/concurrent/TimerUtilities.java b/common/source/java/ch/systemsx/cisd/common/concurrent/TimerUtilities.java index 8e3b5421e034541bbbb58d881a29506c0445ee53..09ce5a3d48d6938cfc3dc9d1d07f2f90b6b150a5 100644 --- a/common/source/java/ch/systemsx/cisd/common/concurrent/TimerUtilities.java +++ b/common/source/java/ch/systemsx/cisd/common/concurrent/TimerUtilities.java @@ -20,6 +20,8 @@ import java.lang.reflect.Field; import java.util.Timer; import java.util.TimerTask; +import ch.systemsx.cisd.common.exceptions.StopException; + /** * Utilities for {@link Timer}. * diff --git a/common/source/java/ch/systemsx/cisd/common/exceptions/CheckedExceptionTunnel.java b/common/source/java/ch/systemsx/cisd/common/exceptions/CheckedExceptionTunnel.java index ec843f27f5ea9c5e3f215c53efc7a56d8269f26f..0b17e1d906b0550171b0f28446ffcfb0faa08ff5 100644 --- a/common/source/java/ch/systemsx/cisd/common/exceptions/CheckedExceptionTunnel.java +++ b/common/source/java/ch/systemsx/cisd/common/exceptions/CheckedExceptionTunnel.java @@ -51,10 +51,12 @@ public final class CheckedExceptionTunnel extends RuntimeException if (exception instanceof RuntimeException) { return (RuntimeException) exception; - } else + } + if (exception instanceof InterruptedException) { - return new CheckedExceptionTunnel(exception); + return new StopException(exception); } + return new CheckedExceptionTunnel(exception); } /** @@ -66,7 +68,7 @@ public final class CheckedExceptionTunnel extends RuntimeException assert exception != null : "Exception not specified."; if (exception instanceof CheckedExceptionTunnel) { - // We are sur that the wrapped exception is an 'Exception'. + // We are sure that the wrapped exception is an 'Exception'. return (Exception) exception.getCause(); } else { diff --git a/common/source/java/ch/systemsx/cisd/common/concurrent/StopException.java b/common/source/java/ch/systemsx/cisd/common/exceptions/StopException.java similarity index 83% rename from common/source/java/ch/systemsx/cisd/common/concurrent/StopException.java rename to common/source/java/ch/systemsx/cisd/common/exceptions/StopException.java index 591715ab852a2428feeb7b957d0fc63a6cda5a3a..77a93f826ad0a561ca25113707a83bab21a61cb8 100644 --- a/common/source/java/ch/systemsx/cisd/common/concurrent/StopException.java +++ b/common/source/java/ch/systemsx/cisd/common/exceptions/StopException.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ch.systemsx.cisd.common.concurrent; +package ch.systemsx.cisd.common.exceptions; /** * Exception that signals that whoever gets it should stop its current work. @@ -24,26 +24,30 @@ package ch.systemsx.cisd.common.concurrent; * * @author Bernd Rinn */ -public class StopException extends RuntimeException +public final class StopException extends RuntimeException { private static final long serialVersionUID = 1L; + public StopException() + { + super(); + } + + public StopException(final Throwable cause) + { + super(cause); + } + /** * Checks whether the current thread has been interrupted and, if it has, throw a * {@link StopException}. */ - public static void check() throws StopException + public final static void check() throws StopException { if (Thread.interrupted()) { throw new StopException(); } } - - public StopException() - { - super(); - } - } diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/AlwaysAboveFreeSpaceProvider.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/AlwaysAboveFreeSpaceProvider.java index 6e455aa6902c28738ddfcea96dd90ff32e5c4d73..ff2d196b74c71ef8947e63642fa174c3607c7d8c 100644 --- a/common/source/java/ch/systemsx/cisd/common/highwatermark/AlwaysAboveFreeSpaceProvider.java +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/AlwaysAboveFreeSpaceProvider.java @@ -17,7 +17,6 @@ package ch.systemsx.cisd.common.highwatermark; import java.io.File; -import java.io.IOException; import ch.systemsx.cisd.common.highwatermark.HighwaterMarkWatcher.IFreeSpaceProvider; @@ -45,7 +44,7 @@ public final class AlwaysAboveFreeSpaceProvider implements IFreeSpaceProvider // IFreeSpaceProvider // - public final long freeSpaceKb(final File path) throws IOException + public final long freeSpaceKb(final File path) { return Long.MAX_VALUE; } diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/DelegateFreeSpaceProvider.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/DelegateFreeSpaceProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..f5a93d7999e3d72bffdf4fe8236354330f6f3910 --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/DelegateFreeSpaceProvider.java @@ -0,0 +1,53 @@ +/* + * Copyright 2008 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.highwatermark; + +import java.io.File; +import java.io.IOException; + +import ch.systemsx.cisd.common.highwatermark.HighwaterMarkWatcher.IFreeSpaceProvider; + +/** + * A <code>IFreeSpaceProvider</code> implementation which delegates its job to the encapsulated + * {@link IFreeSpaceProvider} implementation. + * + * @author Christian Ribeaud + */ +public class DelegateFreeSpaceProvider implements IFreeSpaceProvider +{ + + private final IFreeSpaceProvider freeSpaceProvider; + + public DelegateFreeSpaceProvider(final IFreeSpaceProvider freeSpaceProvider) + { + this.freeSpaceProvider = freeSpaceProvider; + } + + protected final IFreeSpaceProvider getFreeSpaceProvider() + { + return freeSpaceProvider; + } + + // + // IFreeSpaceProvider + // + + public long freeSpaceKb(final File path) throws IOException + { + return freeSpaceProvider.freeSpaceKb(path); + } +} diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcher.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcher.java index 9cadcc4664e64257e2bd6218d4c34f3aff91b452..c4226ac31014a77236c0714f05d5c2fc97403013 100644 --- a/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcher.java +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcher.java @@ -24,7 +24,6 @@ import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.event.EventListenerList; -import org.apache.commons.io.DestroyableFileSystemUtils; import org.apache.commons.io.FileSystemUtils; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; @@ -48,7 +47,7 @@ public final class HighwaterMarkWatcher implements Runnable private static final String UNSPECIFIED = "unspecified"; private final static IFreeSpaceProvider DEFAULT_FREE_SPACE_PROVIDER = - new DefaultFreeSpaceProvider(); + new NonHangingFreeSpaceProvider(new SimpleFreeSpaceProvider()); private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, HighwaterMarkWatcher.class); @@ -224,20 +223,6 @@ public final class HighwaterMarkWatcher implements Runnable // Helper classes // - private final static class DefaultFreeSpaceProvider implements IFreeSpaceProvider - { - - // - // IFreeSpaceProvider - // - - public final long freeSpaceKb(final File path) throws IOException - { - final String canonicalPath = FileUtilities.getCanonicalPath(path); - return DestroyableFileSystemUtils.freeSpaceKb(canonicalPath); - } - } - public final static class HighwaterMarkState implements Serializable { private static final long serialVersionUID = 1L; @@ -246,7 +231,7 @@ public final class HighwaterMarkWatcher implements Runnable private final long freeSpace; - HighwaterMarkState(final FileWithHighwaterMark fileWithHighwaterMark, final long freeSpace) + HighwaterMarkState(final FileWithHighwaterMark fileWithHighwaterMark, final Long freeSpace) { this.fileWithHighwaterMark = fileWithHighwaterMark; this.freeSpace = freeSpace; @@ -285,7 +270,7 @@ public final class HighwaterMarkWatcher implements Runnable } /** - * Whether the free space is below or reaches the high water mark. + * Whether the free space is below the high water mark. */ public final boolean isBelow() { @@ -318,13 +303,10 @@ public final class HighwaterMarkWatcher implements Runnable final static class NotificationLogChangeListener implements ChangeListener { static final String INFO_LOG_FORMAT = - "The amount of available space (%s) on '%s' " - + "is again sufficient (greater than the specified high water mark: %s)."; + "Low space condition resolved on '%s', required: %s, found: %s, resuming operation."; static final String WARNING_LOG_FORMAT = - "The amount of available space (%s) on '%s' " - + "is lower than the specified high water mark (%s). " - + "Missing space is %s."; + "Highwater mark reached on '%s', required: %s, found: %s, missing: %s, suspending operation."; private static final Logger notificationLog = LogFactory.getLogger(LogCategory.NOTIFY, NotificationLogChangeListener.class); @@ -347,12 +329,12 @@ public final class HighwaterMarkWatcher implements Runnable { final String missingSpace = displayKilobyteValue(event.getHighwaterMark() - event.getFreeSpace()); - notificationLog.warn(String.format(WARNING_LOG_FORMAT, freeSpaceDisplayed, path, - highwaterMarkDisplayed, missingSpace)); + notificationLog.warn(String.format(WARNING_LOG_FORMAT, path, + highwaterMarkDisplayed, freeSpaceDisplayed, missingSpace)); } else { - notificationLog.info(String.format(INFO_LOG_FORMAT, freeSpaceDisplayed, path, - highwaterMarkDisplayed)); + notificationLog.info(String.format(INFO_LOG_FORMAT, path, highwaterMarkDisplayed, + freeSpaceDisplayed)); } } } diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/NonHangingFreeSpaceProvider.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/NonHangingFreeSpaceProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..4174acdc9ad648c2584a403f43a4b643c69812a7 --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/NonHangingFreeSpaceProvider.java @@ -0,0 +1,94 @@ +/* + * Copyright 2008 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.highwatermark; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import ch.systemsx.cisd.common.Constants; +import ch.systemsx.cisd.common.concurrent.ConcurrencyUtilities; +import ch.systemsx.cisd.common.concurrent.ExecutionResult; +import ch.systemsx.cisd.common.highwatermark.HighwaterMarkWatcher.IFreeSpaceProvider; + +/** + * A <code>DelegateFreeSpaceProvider</code> which does not block, computing the free space in its + * own thread. + * + * @author Christian Ribeaud + */ +public final class NonHangingFreeSpaceProvider extends DelegateFreeSpaceProvider +{ + private final ExecutorService executorService = Executors.newSingleThreadExecutor(); + + public NonHangingFreeSpaceProvider(final IFreeSpaceProvider freeSpaceProvider) + { + super(freeSpaceProvider); + } + + // + // DelegateFreeSpaceProvider + // + + @Override + public final long freeSpaceKb(final File path) throws IOException + { + final Future<Long> future = + executorService.submit(new FreeSpaceCallable(getFreeSpaceProvider(), path)); + final ExecutionResult<Long> executionResult = + ConcurrencyUtilities.getResult(future, Constants.MILLIS_TO_WAIT_BEFORE_TIMEOUT); + final Long resultOrNull = executionResult.tryGetResult(); + if (resultOrNull != null) + { + return resultOrNull; + } else + { + final Throwable cause = executionResult.tryGetException(); + throw new IOException(cause == null ? String.format( + "Computing free space on '%s' failed.", path) : cause.getMessage()); + } + } + + // + // Helper classes + // + + private final static class FreeSpaceCallable implements Callable<Long> + { + private final IFreeSpaceProvider freeSpaceProvider; + + private final File path; + + FreeSpaceCallable(final IFreeSpaceProvider freeSpaceProvider, final File path) + { + this.freeSpaceProvider = freeSpaceProvider; + this.path = path; + } + + // + // Callable + // + + public final Long call() throws Exception + { + return freeSpaceProvider.freeSpaceKb(path); + } + } +} diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/SimpleFreeSpaceProvider.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/SimpleFreeSpaceProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..338961b9f9bf432c4ce42730aedcc6f2b98050fc --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/SimpleFreeSpaceProvider.java @@ -0,0 +1,44 @@ +/* + * Copyright 2008 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.highwatermark; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.io.FileSystemUtils; + +import ch.systemsx.cisd.common.highwatermark.HighwaterMarkWatcher.IFreeSpaceProvider; +import ch.systemsx.cisd.common.utilities.FileUtilities; + +/** + * A simple <code>IFreeSpaceProvider</code> implementation based on {@link FileSystemUtils}. + * + * @author Christian Ribeaud + */ +public final class SimpleFreeSpaceProvider implements IFreeSpaceProvider +{ + + // + // IFreeSpaceProvider + // + + public final long freeSpaceKb(final File path) throws IOException + { + final String canonicalPath = FileUtilities.getCanonicalPath(path); + return FileSystemUtils.freeSpaceKb(canonicalPath); + } +} \ No newline at end of file diff --git a/common/source/java/ch/systemsx/cisd/common/process/ProcessExecutionHelper.java b/common/source/java/ch/systemsx/cisd/common/process/ProcessExecutionHelper.java index 00e2967c84737c918f8318a18268a1a3dca7accb..6a82dc3d270081d073f9af09c12d516126036b49 100644 --- a/common/source/java/ch/systemsx/cisd/common/process/ProcessExecutionHelper.java +++ b/common/source/java/ch/systemsx/cisd/common/process/ProcessExecutionHelper.java @@ -37,7 +37,7 @@ import ch.systemsx.cisd.common.concurrent.ExecutionResult; import ch.systemsx.cisd.common.concurrent.ExecutionStatus; import ch.systemsx.cisd.common.concurrent.NamedCallable; import ch.systemsx.cisd.common.concurrent.NamingThreadPoolExecutor; -import ch.systemsx.cisd.common.concurrent.StopException; +import ch.systemsx.cisd.common.exceptions.StopException; /** * Utility to execute a command from a command line and log all events. diff --git a/common/source/java/ch/systemsx/cisd/common/process/ProcessRunner.java b/common/source/java/ch/systemsx/cisd/common/process/ProcessRunner.java index 5e6575c03c56518a7683028d571d3faa4301a1ab..0895b0ceef38fcad203ab19571368cb25ceadfeb 100644 --- a/common/source/java/ch/systemsx/cisd/common/process/ProcessRunner.java +++ b/common/source/java/ch/systemsx/cisd/common/process/ProcessRunner.java @@ -16,8 +16,8 @@ package ch.systemsx.cisd.common.process; -import ch.systemsx.cisd.common.concurrent.StopException; import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; +import ch.systemsx.cisd.common.exceptions.StopException; /** * This class takes cares of running encapsulated {@link IProcess}. diff --git a/common/source/java/ch/systemsx/cisd/common/process/ProcessWatchdog.java b/common/source/java/ch/systemsx/cisd/common/process/ProcessWatchdog.java deleted file mode 100644 index acebe1ebe8ec38e2b68d786b8a7dc205a543a960..0000000000000000000000000000000000000000 --- a/common/source/java/ch/systemsx/cisd/common/process/ProcessWatchdog.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright 2008 ETH Zuerich, CISD - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.systemsx.cisd.common.process; - -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - -/** - * Destroys a process running for too long. - * - * @author Christian Ribeaud - */ -@Deprecated -public final class ProcessWatchdog implements Runnable -{ - private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); - - private final TimeUnit timeUnit; - - private final long timeOut; - - private Process process; - - /** say whether or not the watchdog is currently monitoring a process */ - private boolean watch; - - /** say whether or not the process was killed due to running overtime */ - private boolean processKilled; - - private ScheduledFuture<?> scheduledFuture; - - public ProcessWatchdog(final long timeOutInMillis) - { - this(TimeUnit.MILLISECONDS, timeOutInMillis); - } - - public ProcessWatchdog(final TimeUnit timeUnit, final long timeOut) - { - assert timeOut > 0L : "Unspecified time out"; - this.timeUnit = timeUnit; - this.timeOut = timeOut; - } - - /** - * Watches the given process and terminates it, if it runs for too long. All information from - * the previous run are reset. - * - * @param p the process to monitor. It cannot be <tt>null</tt>. * - * @throws IllegalStateException if a process is still being monitored. - */ - public synchronized void start(final Process p) - { - assert p != null : "Unspecified process"; - if (this.process != null) - { - throw new IllegalStateException("Already running."); - } - this.process = p; - this.processKilled = false; - this.watch = true; - this.process = p; - scheduledFuture = executorService.schedule(this, timeOut, timeUnit); - } - - /** - * Stops the watcher. - */ - public final synchronized void stop() - { - if (scheduledFuture != null) - { - scheduledFuture.cancel(true); - scheduledFuture = null; - } - watch = false; - process = null; - } - - /** - * reset the monitor flag and the process. - */ - protected void cleanUp() - { - watch = false; - process = null; - } - - /** - * Indicates whether or not the watchdog is still monitoring the process. - * - * @return <tt>true</tt> if the process is still running, otherwise <tt>false</tt>. - */ - public final boolean isWatching() - { - return watch; - } - - /** - * Indicates whether the last process run was killed on timeout or not. - * - * @return <tt>true</tt> if the process was killed otherwise <tt>false</tt>. - */ - public final boolean isProcessKilled() - { - return processKilled; - } - - // - // Runnable - // - - public final synchronized void run() - { - try - { - try - { - // We must check if the process was not stopped - // before being here - process.exitValue(); - } catch (final IllegalThreadStateException itse) - { - // the process is not terminated, if this is really - // a timeout and not a manual stop then kill it. - if (watch) - { - processKilled = true; - process.destroy(); - } - } - } finally - { - cleanUp(); - } - } -} diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/RecursiveHardLinkMaker.java b/common/source/java/ch/systemsx/cisd/common/utilities/RecursiveHardLinkMaker.java index e14f247bca6124132bfcc3f49d12b0fb188942c9..73b8435c96c4bbf9b2200b27df3b352523bf5ed6 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/RecursiveHardLinkMaker.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/RecursiveHardLinkMaker.java @@ -24,7 +24,7 @@ import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; -import ch.systemsx.cisd.common.concurrent.StopException; +import ch.systemsx.cisd.common.exceptions.StopException; import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.common.process.IProcess; diff --git a/common/source/java/org/apache/commons/io/DestroyableFileSystemUtils.java b/common/source/java/org/apache/commons/io/DestroyableFileSystemUtils.java deleted file mode 100644 index ae71fcbd6d62a68cce3d013248bf496e75829dde..0000000000000000000000000000000000000000 --- a/common/source/java/org/apache/commons/io/DestroyableFileSystemUtils.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright 2008 ETH Zuerich, CISD - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.io; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import ch.systemsx.cisd.common.Constants; -import ch.systemsx.cisd.common.process.ProcessWatchdog; - -/** - * A <code>FileSystemUtils</code> extension which puts a watch dog to the process created. - * - * @author Christian Ribeaud - */ -@Deprecated -public final class DestroyableFileSystemUtils extends FileSystemUtils -{ - - private final static DestroyableFileSystemUtils INSTANCE = new DestroyableFileSystemUtils(); - - private final static int OS; - - static - { - int os = -1; - try - { - final Field osField = FileSystemUtils.class.getDeclaredField("OS"); - osField.setAccessible(true); - os = osField.getInt(FileSystemUtils.class); - } catch (final Exception ex) - { - } - OS = os; - } - - final ProcessWatchdog watchdog = new ProcessWatchdog(Constants.MILLIS_TO_WAIT_BEFORE_TIMEOUT); - - private DestroyableFileSystemUtils() - { - // Can not be instantiated. - } - - /** - * Waits for a given process. - * - * @param process the process one wants to wait for. - */ - protected final void waitFor(final Process process, final String[] cmdAttribs) - throws IOException - { - try - { - process.waitFor(); - watchdog.stop(); - } catch (final InterruptedException e) - { - process.destroy(); - throw new IOException("Command line threw an InterruptedException '" + e.getMessage() - + "' for command " + Arrays.asList(cmdAttribs)); - } - } - - /** - * Returns the free space on a drive or volume in kilobytes by invoking the command line. - */ - public static long freeSpaceKb(final String path) throws IOException - { - return INSTANCE.freeSpaceOS(path, OS, true); - } - - // - // FileSystemUtils - // - - @Override - final List<String> performCommand(final String[] cmdAttribs, final int max) throws IOException - { - final List<String> lines = new ArrayList<String>(20); - Process proc = null; - InputStream in = null; - OutputStream out = null; - InputStream err = null; - BufferedReader inr = null; - try - { - proc = openProcess(cmdAttribs); - watchdog.start(proc); - in = proc.getInputStream(); - out = proc.getOutputStream(); - err = proc.getErrorStream(); - inr = new BufferedReader(new InputStreamReader(in)); - String line = inr.readLine(); - while (line != null && lines.size() < max) - { - line = line.toLowerCase().trim(); - lines.add(line); - line = inr.readLine(); - } - waitFor(proc, cmdAttribs); - if (proc.exitValue() != 0) - { - // os command problem, throw exception - throw new IOException("Command line returned OS error code '" + proc.exitValue() - + "' for command " + Arrays.asList(cmdAttribs)); - } - if (lines.size() == 0) - { - // unknown problem, throw exception - throw new IOException("Command line did not return any info " + "for command " - + Arrays.asList(cmdAttribs)); - } - return lines; - } finally - { - IOUtils.closeQuietly(in); - IOUtils.closeQuietly(out); - IOUtils.closeQuietly(err); - IOUtils.closeQuietly(inr); - if (proc != null) - { - proc.destroy(); - } - } - } -} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/concurrent/ConcurrencyUtilitiesTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/concurrent/ConcurrencyUtilitiesTest.java index 807ba598dd5edc0c9dacbd8dd470c697d1c1d58d..0470de2f4eb379fb856946281ee23b1a7e8051d3 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/concurrent/ConcurrencyUtilitiesTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/concurrent/ConcurrencyUtilitiesTest.java @@ -28,6 +28,7 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; +import ch.systemsx.cisd.common.exceptions.StopException; import ch.systemsx.cisd.common.logging.LogInitializer; /** diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/concurrent/TimerUtilitiesTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/concurrent/TimerUtilitiesTest.java index 42ff35d44d8e517af4392ebca3dfb9729953fe63..ff40e6723e315c9fd7be36dfbf88576fa221812d 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/concurrent/TimerUtilitiesTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/concurrent/TimerUtilitiesTest.java @@ -25,6 +25,8 @@ import java.util.concurrent.TimeUnit; import org.testng.annotations.Test; +import ch.systemsx.cisd.common.exceptions.StopException; + /** * Test cases for the {@link TimerUtilities}. * <p> diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcherTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcherTest.java index 14c94971305ae25b215ce4453e49d5784c727482..ca6b6f6e52e768d8446251591bfe856aa7372d3a 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcherTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcherTest.java @@ -200,8 +200,8 @@ public final class HighwaterMarkWatcherTest final long missingSpace = DEFAULT_WATERMARK - freeSpaces[i]; assertEquals(String.format( HighwaterMarkWatcher.NotificationLogChangeListener.WARNING_LOG_FORMAT, - HighwaterMarkWatcher.displayKilobyteValue(freeSpaces[i]), DEFAULT_PATH, - HighwaterMarkWatcher.displayKilobyteValue(DEFAULT_WATERMARK), HighwaterMarkWatcher + DEFAULT_PATH, HighwaterMarkWatcher.displayKilobyteValue(DEFAULT_WATERMARK), + HighwaterMarkWatcher.displayKilobyteValue(freeSpaces[i]), HighwaterMarkWatcher .displayKilobyteValue(missingSpace)), logRecorder.getLogContent()); // Space still "red". Do not inform the administrator. He already knows it. logRecorder.resetLogContent(); @@ -212,10 +212,9 @@ public final class HighwaterMarkWatcherTest logRecorder.resetLogContent(); highwaterMarkWatcher.setPathAndRun(DEFAULT_PATH); assertEquals(String.format( - HighwaterMarkWatcher.NotificationLogChangeListener.INFO_LOG_FORMAT, - HighwaterMarkWatcher.displayKilobyteValue(freeSpaces[i]), DEFAULT_PATH, - HighwaterMarkWatcher.displayKilobyteValue(DEFAULT_WATERMARK)), logRecorder - .getLogContent()); + HighwaterMarkWatcher.NotificationLogChangeListener.INFO_LOG_FORMAT, DEFAULT_PATH, + HighwaterMarkWatcher.displayKilobyteValue(DEFAULT_WATERMARK), HighwaterMarkWatcher + .displayKilobyteValue(freeSpaces[i])), logRecorder.getLogContent()); // Space still OK. Do not inform the administrator. logRecorder.resetLogContent(); highwaterMarkWatcher.setPathAndRun(DEFAULT_PATH); diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/process/ProcessExecutionHelperTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/process/ProcessExecutionHelperTest.java index 488fa56be237d0d1758573de5029aadbc65c7271..23b124803c8d79b7fd6c56663c99e48cc3ebaaad 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/process/ProcessExecutionHelperTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/process/ProcessExecutionHelperTest.java @@ -32,7 +32,7 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import ch.systemsx.cisd.common.collections.CollectionIO; -import ch.systemsx.cisd.common.concurrent.StopException; +import ch.systemsx.cisd.common.exceptions.StopException; import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.common.logging.LogInitializer;