chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: chris@1: package org.sonews.daemon; chris@1: chris@1: import java.sql.SQLException; chris@3: import org.sonews.storage.StorageManager; chris@1: import org.sonews.util.Log; chris@1: chris@1: /** chris@1: * Base class of all sonews threads. chris@1: * Instances of this class will be automatically registered at the ShutdownHook chris@1: * to be cleanly exited when the server is forced to exit. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ chris@1: public abstract class AbstractDaemon extends Thread chris@1: { chris@1: cli@37: /** This variable is write synchronized through setRunning */ cli@37: private boolean isRunning = false; chris@1: cli@37: /** cli@37: * Protected constructor. Will be called by derived classes. cli@37: */ cli@37: protected AbstractDaemon() cli@37: { cli@37: setDaemon(true); // VM will exit when all threads are daemons cli@37: setName(getClass().getSimpleName()); cli@37: } cli@37: cli@37: /** cli@37: * @return true if shutdown() was not yet called. cli@37: */ cli@37: public boolean isRunning() cli@37: { cli@37: synchronized (this) { cli@37: return this.isRunning; cli@37: } cli@37: } cli@37: cli@37: /** cli@37: * Marks this thread to exit soon. Closes the associated JDBCDatabase connection cli@37: * if available. cli@37: * @throws java.sql.SQLException cli@37: */ cli@37: public void shutdownNow() cli@37: throws SQLException cli@37: { cli@37: synchronized (this) { cli@37: this.isRunning = false; cli@37: StorageManager.disableProvider(); cli@37: } cli@37: } cli@37: cli@37: /** cli@37: * Calls shutdownNow() but catches SQLExceptions if occurring. cli@37: */ cli@37: public void shutdown() cli@37: { cli@37: try { cli@37: shutdownNow(); cli@37: } catch (SQLException ex) { cli@37: Log.get().warning(ex.toString()); cli@37: } cli@37: } cli@37: cli@37: /** cli@37: * Starts this daemon. cli@37: */ cli@37: @Override cli@37: public void start() cli@37: { cli@37: synchronized (this) { cli@37: this.isRunning = true; cli@37: } cli@37: super.start(); cli@37: } chris@1: }