src/org/sonews/daemon/AbstractDaemon.java
author cli
Sun, 29 Aug 2010 17:28:58 +0200
changeset 35 ed84c8bdd87b
parent 15 org/sonews/daemon/AbstractDaemon.java@f2293e8566f5
child 37 74139325d305
permissions -rw-r--r--
Moving source files into src/-subdir.
     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     {
    54       return this.isRunning;
    55     }
    56   }
    57   
    58   /**
    59    * Marks this thread to exit soon. Closes the associated JDBCDatabase connection
    60    * if available.
    61    * @throws java.sql.SQLException
    62    */
    63   public void shutdownNow()
    64     throws SQLException
    65   {
    66     synchronized(this)
    67     {
    68       this.isRunning = false;
    69       StorageManager.disableProvider();
    70     }
    71   }
    72   
    73   /**
    74    * Calls shutdownNow() but catches SQLExceptions if occurring.
    75    */
    76   public void shutdown()
    77   {
    78     try
    79     {
    80       shutdownNow();
    81     }
    82     catch(SQLException ex)
    83     {
    84       Log.get().warning(ex.toString());
    85     }
    86   }
    87   
    88   /**
    89    * Starts this daemon.
    90    */
    91   @Override
    92   public void start()
    93   {
    94     synchronized(this)
    95     {
    96       this.isRunning = true;
    97     }
    98     super.start();
    99   }
   100   
   101 }