All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.day.util.FinalizerHandler Maven / Gradle / Ivy

/*
 * $Id: FinalizerHandler.java 12345 2004-08-22 04:56:09Z fielding $
 *
 * Copyright 1997-2004 Day Management AG
 * Barfuesserplatz 6, 4001 Basel, Switzerland
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Day Management AG, ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Day.
 */
package com.day.util;

import java.util.Stack;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The FinalizerHandler class implements the functionality to
 * register {@link Finalizer}s and call them at some point in time, such as
 * the end of the current ExecutionContext.
 * 

* The registered {@link Finalizer}s will be called in the reverse sequence * of their registration. That is the last {@link Finalizer} registered will * get called first. * * @version $Revision: 1.13 $, $Date: 2004-08-22 06:56:09 +0200 (Sun, 22 Aug 2004) $ * @author fmeschbe * @since antbear * @audience dev */ public class FinalizerHandler { /** default log */ private static final Logger log = LoggerFactory.getLogger(FinalizerHandler.class); /** Keep the list of registered objects in this Stack. */ private final Stack registered = new Stack(); /** * Registers a Finalizer. * @param object the finalzier to register */ public void registerObject(Finalizer object) { registered.push(object); } /** * Unregisters a Finalizer object. After unregistering the * objects doFinalize() method is not called anymore. * * @param object The Finalizer object to unregister. */ public void unregister(Finalizer object) { registered.remove(object); } /** * Unregisters all registered Finalizer objects so that a * subsequent call to {@link #callFinalizers()} effectively does nothing. */ public void unregisterAll() { registered.clear(); } /** * Returns true if no {@link Finalizer} objects are registered * yet. */ public boolean isEmpty() { return registered.isEmpty(); } /** * Calls all registered Finalizer {@link Finalizer#doFinalize()} * method, and removes them from the internal list. *

* The call to the {@link Finalizer#doFinalize()} is not expected to throw * any exceptions or even errors. In case a Throwable is thrown * this is logged as an error and processing continiues with the next * Finalizer to be finalized. */ public void callFinalizers() { while (!registered.isEmpty()) { Finalizer fin = (Finalizer) registered.pop(); try { fin.doFinalize(); } catch (Throwable t) { // ignore anything thrown log.error("callFinalizers: Unexpected Exception/Error: {}", t.toString()); log.debug("dump", t); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy