Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ControllerShutdownThread.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2005 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Emmanuel Cecchet. 
00022  * Contributor(s): Mathieu Peltier, Nicolas Modrzyk.
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.core.shutdown;
00026 
00027 import java.io.File;
00028 import java.util.ArrayList;
00029 
00030 import org.objectweb.cjdbc.common.exceptions.ShutdownException;
00031 import org.objectweb.cjdbc.common.i18n.Translate;
00032 import org.objectweb.cjdbc.controller.core.Controller;
00033 import org.objectweb.cjdbc.controller.core.ControllerConstants;
00034 import org.objectweb.cjdbc.controller.core.ControllerServerThread;
00035 import org.objectweb.cjdbc.controller.core.ReportManager;
00036 import org.objectweb.cjdbc.controller.jmx.MBeanServerManager;
00037 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase;
00038 
00039 /**
00040  * Abstract class for all implementations of controller shutdown strategies.
00041  * 
00042  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00043  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00044  * @version 1.2
00045  */
00046 public abstract class ControllerShutdownThread extends ShutdownThread
00047 {
00048   protected Controller controller;
00049 
00050   /**
00051    * Prepare the thread for shutting down.
00052    * 
00053    * @param controller the controller to shutdown
00054    * @param level Constants.SHUTDOWN_WAIT, Constants.SHUTDOWN_SAFE or
00055    *          Constants.SHUTDOWN_FORCE
00056    */
00057   public ControllerShutdownThread(Controller controller, int level)
00058   {
00059     super(level);
00060     this.controller = controller;
00061   }
00062 
00063   /**
00064    * Shutdown the JMX Agent.
00065    */
00066   protected void shutdownJmxAgent()
00067   {
00068     logger.info("Shutting down Jmx Agent");
00069     try
00070     {
00071       if (controller.getJmxEnable())
00072         MBeanServerManager.setJmxEnabled(false);
00073     }
00074     catch (Exception jme)
00075     {
00076       logger.error(Translate.get("controller.shutdown.jmx.error", jme
00077           .getMessage()), jme);
00078       //throw new ShutdownException(jme);
00079     }
00080   }
00081 
00082   /**
00083    * Shutdown all databases of this controller using the current shutdown level.
00084    */
00085   protected void shutdownDatabases()
00086   {
00087     logger.info("Shutting down databases");
00088     try
00089     {
00090       //Shutdown each virtual database with proper level
00091       ArrayList listvb = controller.getVirtualDatabases();
00092       int nbvb = listvb.size();
00093       for (int i = 0; i < nbvb; i++)
00094       {
00095         logger.info("Shutting down database:"
00096             + ((VirtualDatabase) listvb.get(i)).getVirtualDatabaseName()
00097             + " with level:" + this.shutdownLevel);
00098         ((VirtualDatabase) listvb.get(i)).shutdown(this.shutdownLevel);
00099         logger.info("Database:"
00100             + ((VirtualDatabase) listvb.get(i)).getVirtualDatabaseName()
00101             + " is shutdown");
00102       }
00103     }
00104     catch (Exception e)
00105     {
00106       logger.error(Translate.get("controller.shutdown.database.error", e));
00107     }
00108   }
00109 
00110   /**
00111    * Shutdown the ControllerServerThread and its attached connection to reject
00112    * new incoming connections.
00113    * 
00114    * @param joinTimeoutInMillis timeout in milliseconds to wait for controller
00115    *          server thread termination. A timeout of 0 means wait forever.
00116    * @throws ShutdownException if an error occurs
00117    */
00118   protected void shutdownServerConnectionThread(int joinTimeoutInMillis)
00119       throws ShutdownException
00120   {
00121     if (logger.isDebugEnabled())
00122       logger.debug("Shutting down ControllerServerThread");
00123     try
00124     {
00125       // Shutdown Server Connections Thread
00126       ControllerServerThread thread = controller.getConnectionThread();
00127       if (thread != null && !thread.isShuttingDown())
00128       {
00129         thread.shutdown();
00130         logger.info("Waiting for controller thread termination.");
00131         thread.join(joinTimeoutInMillis);
00132       }
00133     }
00134     catch (Exception e)
00135     {
00136       throw new ShutdownException(e);
00137     }
00138   }
00139 
00140   /**
00141    * Generate a controller report if it has been enabled in the config file.
00142    */
00143   protected void generateReportIfNeeded()
00144   {
00145     ReportManager report = controller.getReport();
00146     if (report != null && report.isGenerateOnShutdown())
00147     {
00148       report.startReport();
00149       report.generate();
00150       logger.info(Translate.get("fatal.report.generated", report
00151           .getReportLocation()
00152           + File.separator + ControllerConstants.REPORT_FILE));
00153     }
00154   }
00155 
00156 }

Generated on Mon Apr 11 22:01:30 2005 for C-JDBC by  doxygen 1.3.9.1