Skip to content
Snippets Groups Projects
Commit 1bd914bc authored by brinn's avatar brinn
Browse files

move: CleanUpCallable & friends to from common.process to hdf5.cleanup

SVN: 10344
parent cd1fb81b
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2007 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;
/**
* A class that implements the logic of cleaning up a resource even in case of an exception but
* re-throws an exception of the clean up procedure only when the main procedure didn't throw one.
* <code>CleanUpRunner</code>s can be stacked.
*
* @author Bernd Rinn
*/
public final class CleanUpCallable
{
private final CleanUpRegistry registry = new CleanUpRegistry();
/**
* Runs a {@link ICallableWithCleanUp} and ensures that all registered clean-ups are performed
* afterwards.
*/
public <T> T call(ICallableWithCleanUp<T> runnable)
{
boolean exceptionThrown = true;
try
{
T result = runnable.call(registry);
exceptionThrown = false;
return result;
} finally
{
registry.cleanUp(exceptionThrown);
}
}
}
/*
* 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.ArrayList;
import java.util.List;
/**
* A class that allows registering items for clean up and that allows to perform the clean up later.
*
* @author Bernd Rinn
*/
public final class CleanUpRegistry implements ICleanUpRegistry
{
private final List<Runnable> cleanUpList = new ArrayList<Runnable>();
public void registerCleanUp(Runnable cleanUp)
{
cleanUpList.add(cleanUp);
}
/**
* Performs all clean-ups registered with {@link #registerCleanUp(Runnable)}.
*
* @param suppressExceptions If <code>true</code>, all exceptions that happen during clean-up
* will be suppressed.
*/
public void cleanUp(boolean suppressExceptions)
{
RuntimeException exceptionDuringCleanUp = null;
for (int i = cleanUpList.size() - 1; i >= 0; --i)
{
final Runnable runnable = cleanUpList.get(i);
try
{
runnable.run();
} catch (RuntimeException ex)
{
if (suppressExceptions == false && exceptionDuringCleanUp == null)
{
exceptionDuringCleanUp = ex;
}
}
}
cleanUpList.clear();
if (exceptionDuringCleanUp != null)
{
throw exceptionDuringCleanUp;
}
}
}
/*
* Copyright 2007 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;
/**
* A role that calls a method which requires one or more clean-up steps that need to be run reliably
* at the end of the method regardless of whether the method is finished normally or whether it
* exits with an exception.
*
* @author Bernd Rinn
*/
public interface ICallableWithCleanUp<T>
{
/** Calls the method requiring clean-up. */
public T call(ICleanUpRegistry registry);
}
/*
* Copyright 2007 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;
/**
* A role that allows to register a clean-up method that is called regardless of whether an
* exception occurs or not.
*
* @author Bernd Rinn
*/
public interface ICleanUpRegistry
{
/**
* Register a clean-up to run when the main {@link Runnable} has been executed.
*/
public void registerCleanUp(Runnable cleanUp);
}
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