KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > core > shutdown > ControllerShutdownThread


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Mathieu Peltier, Nicolas Modrzyk.
23  */

24
25 package org.objectweb.cjdbc.controller.core.shutdown;
26
27 import java.io.File JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.objectweb.cjdbc.common.exceptions.ShutdownException;
31 import org.objectweb.cjdbc.common.i18n.Translate;
32 import org.objectweb.cjdbc.controller.core.Controller;
33 import org.objectweb.cjdbc.controller.core.ControllerConstants;
34 import org.objectweb.cjdbc.controller.core.ControllerServerThread;
35 import org.objectweb.cjdbc.controller.core.ReportManager;
36 import org.objectweb.cjdbc.controller.jmx.MBeanServerManager;
37 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase;
38
39 /**
40  * Abstract class for all implementations of controller shutdown strategies.
41  *
42  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
43  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
44  * @version 1.2
45  */

46 public abstract class ControllerShutdownThread extends ShutdownThread
47 {
48   protected Controller controller;
49
50   /**
51    * Prepare the thread for shutting down.
52    *
53    * @param controller the controller to shutdown
54    * @param level Constants.SHUTDOWN_WAIT, Constants.SHUTDOWN_SAFE or
55    * Constants.SHUTDOWN_FORCE
56    */

57   public ControllerShutdownThread(Controller controller, int level)
58   {
59     super(level);
60     this.controller = controller;
61   }
62
63   /**
64    * Shutdown the JMX Agent.
65    */

66   protected void shutdownJmxAgent()
67   {
68     logger.info("Shutting down Jmx Agent");
69     try
70     {
71       if (controller.getJmxEnable())
72         MBeanServerManager.setJmxEnabled(false);
73     }
74     catch (Exception JavaDoc jme)
75     {
76       logger.error(Translate.get("controller.shutdown.jmx.error", jme
77           .getMessage()), jme);
78       //throw new ShutdownException(jme);
79
}
80   }
81
82   /**
83    * Shutdown all databases of this controller using the current shutdown level.
84    */

85   protected void shutdownDatabases()
86   {
87     logger.info("Shutting down databases");
88     try
89     {
90       //Shutdown each virtual database with proper level
91
ArrayList JavaDoc listvb = controller.getVirtualDatabases();
92       int nbvb = listvb.size();
93       for (int i = 0; i < nbvb; i++)
94       {
95         logger.info("Shutting down database:"
96             + ((VirtualDatabase) listvb.get(i)).getVirtualDatabaseName()
97             + " with level:" + this.shutdownLevel);
98         ((VirtualDatabase) listvb.get(i)).shutdown(this.shutdownLevel);
99         logger.info("Database:"
100             + ((VirtualDatabase) listvb.get(i)).getVirtualDatabaseName()
101             + " is shutdown");
102       }
103     }
104     catch (Exception JavaDoc e)
105     {
106       logger.error(Translate.get("controller.shutdown.database.error", e));
107     }
108   }
109
110   /**
111    * Shutdown the ControllerServerThread and its attached connection to reject
112    * new incoming connections.
113    *
114    * @param joinTimeoutInMillis timeout in milliseconds to wait for controller
115    * server thread termination. A timeout of 0 means wait forever.
116    * @throws ShutdownException if an error occurs
117    */

118   protected void shutdownServerConnectionThread(int joinTimeoutInMillis)
119       throws ShutdownException
120   {
121     if (logger.isDebugEnabled())
122       logger.debug("Shutting down ControllerServerThread");
123     try
124     {
125       // Shutdown Server Connections Thread
126
ControllerServerThread thread = controller.getConnectionThread();
127       if (thread != null && !thread.isShuttingDown())
128       {
129         thread.shutdown();
130         logger.info("Waiting for controller thread termination.");
131         thread.join(joinTimeoutInMillis);
132       }
133     }
134     catch (Exception JavaDoc e)
135     {
136       throw new ShutdownException(e);
137     }
138   }
139
140   /**
141    * Generate a controller report if it has been enabled in the config file.
142    */

143   protected void generateReportIfNeeded()
144   {
145     ReportManager report = controller.getReport();
146     if (report != null && report.isGenerateOnShutdown())
147     {
148       report.startReport();
149       report.generate();
150       logger.info(Translate.get("fatal.report.generated", report
151           .getReportLocation()
152           + File.separator + ControllerConstants.REPORT_FILE));
153     }
154   }
155
156 }
Popular Tags