org/sonews/daemon/AbstractDaemon.java
author chris <chris@marvin>
Wed, 01 Jul 2009 10:48:22 +0200
changeset 2 1090e2141798
parent 1 6fceb66e1ad7
child 3 2fdc9cc89502
permissions -rw-r--r--
sonews/0.5.1 fixes merged
     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.daemon.storage.Database;
    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 Database connection
    60    * if available.
    61    * @throws java.sql.SQLException
    62    */
    63   void shutdownNow()
    64     throws SQLException
    65   {
    66     synchronized(this)
    67     {
    68       this.isRunning = false;
    69       Database db = Database.getInstance(false);
    70       if(db != null)
    71       {
    72         db.shutdown();
    73       }
    74     }
    75   }
    76   
    77   /**
    78    * Calls shutdownNow() but catches SQLExceptions if occurring.
    79    */
    80   public void shutdown()
    81   {
    82     try
    83     {
    84       shutdownNow();
    85     }
    86     catch(SQLException ex)
    87     {
    88       Log.msg(ex, true);
    89     }
    90   }
    91   
    92   /**
    93    * Starts this daemon.
    94    */
    95   @Override
    96   public void start()
    97   {
    98     synchronized(this)
    99     {
   100       this.isRunning = true;
   101     }
   102     super.start();
   103   }
   104   
   105 }