src/org/sonews/daemon/AbstractDaemon.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 07 Nov 2011 17:35:43 +0100
changeset 117 79ce65d63cce
parent 37 74139325d305
permissions -rwxr-xr-x
ACL: uživatel v Article bude jako User ne jen jako String.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.daemon;
    20 
    21 import java.sql.SQLException;
    22 import org.sonews.storage.StorageManager;
    23 import org.sonews.util.Log;
    24 
    25 /**
    26  * Base class of all sonews threads.
    27  * Instances of this class will be automatically registered at the ShutdownHook
    28  * to be cleanly exited when the server is forced to exit.
    29  * @author Christian Lins
    30  * @since sonews/0.5.0
    31  */
    32 public abstract class AbstractDaemon extends Thread
    33 {
    34 
    35 	/** This variable is write synchronized through setRunning */
    36 	private boolean isRunning = false;
    37 
    38 	/**
    39 	 * Protected constructor. Will be called by derived classes.
    40 	 */
    41 	protected AbstractDaemon()
    42 	{
    43 		setDaemon(true); // VM will exit when all threads are daemons
    44 		setName(getClass().getSimpleName());
    45 	}
    46 
    47 	/**
    48 	 * @return true if shutdown() was not yet called.
    49 	 */
    50 	public boolean isRunning()
    51 	{
    52 		synchronized (this) {
    53 			return this.isRunning;
    54 		}
    55 	}
    56 
    57 	/**
    58 	 * Marks this thread to exit soon. Closes the associated JDBCDatabase connection
    59 	 * if available.
    60 	 * @throws java.sql.SQLException
    61 	 */
    62 	public void shutdownNow()
    63 		throws SQLException
    64 	{
    65 		synchronized (this) {
    66 			this.isRunning = false;
    67 			StorageManager.disableProvider();
    68 		}
    69 	}
    70 
    71 	/**
    72 	 * Calls shutdownNow() but catches SQLExceptions if occurring.
    73 	 */
    74 	public void shutdown()
    75 	{
    76 		try {
    77 			shutdownNow();
    78 		} catch (SQLException ex) {
    79 			Log.get().warning(ex.toString());
    80 		}
    81 	}
    82 
    83 	/**
    84 	 * Starts this daemon.
    85 	 */
    86 	@Override
    87 	public void start()
    88 	{
    89 		synchronized (this) {
    90 			this.isRunning = true;
    91 		}
    92 		super.start();
    93 	}
    94 }