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: chris@1: /** This variable is write synchronized through setRunning */ chris@1: private boolean isRunning = false; chris@1: chris@1: /** chris@1: * Protected constructor. Will be called by derived classes. chris@1: */ chris@1: protected AbstractDaemon() chris@1: { chris@1: setDaemon(true); // VM will exit when all threads are daemons chris@1: setName(getClass().getSimpleName()); chris@1: } chris@1: chris@1: /** chris@1: * @return true if shutdown() was not yet called. chris@1: */ chris@1: public boolean isRunning() chris@1: { chris@1: synchronized(this) chris@1: { chris@1: return this.isRunning; chris@1: } chris@1: } chris@1: chris@1: /** chris@3: * Marks this thread to exit soon. Closes the associated JDBCDatabase connection chris@1: * if available. chris@1: * @throws java.sql.SQLException chris@1: */ chris@3: public void shutdownNow() chris@1: throws SQLException chris@1: { chris@1: synchronized(this) chris@1: { chris@1: this.isRunning = false; chris@3: StorageManager.disableProvider(); chris@1: } chris@1: } chris@1: chris@1: /** chris@1: * Calls shutdownNow() but catches SQLExceptions if occurring. chris@1: */ chris@1: public void shutdown() chris@1: { chris@1: try chris@1: { chris@1: shutdownNow(); chris@1: } chris@1: catch(SQLException ex) chris@1: { chris@1: Log.msg(ex, true); chris@1: } chris@1: } chris@1: chris@1: /** chris@1: * Starts this daemon. chris@1: */ chris@1: @Override chris@1: public void start() chris@1: { chris@1: synchronized(this) chris@1: { chris@1: this.isRunning = true; chris@1: } chris@1: super.start(); chris@1: } chris@1: chris@1: }