KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > core > shutdown > ControllerShutdownThread


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Copyright (C) 2006 Continuent, Inc.
7  * Contact: sequoia@continuent.org
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Mathieu Peltier, Nicolas Modrzyk.
23  */

24
25 package org.continuent.sequoia.controller.core.shutdown;
26
27 import java.io.File JavaDoc;
28
29 import org.continuent.sequoia.common.exceptions.ShutdownException;
30 import org.continuent.sequoia.common.i18n.Translate;
31 import org.continuent.sequoia.common.util.Constants;
32 import org.continuent.sequoia.controller.core.Controller;
33 import org.continuent.sequoia.controller.core.ControllerConstants;
34 import org.continuent.sequoia.controller.core.ControllerServerThread;
35 import org.continuent.sequoia.controller.core.ReportManager;
36 import org.continuent.sequoia.controller.jmx.MBeanServerManager;
37 import org.continuent.sequoia.controller.jmx.RmiConnector;
38
39 /**
40  * Class to shut down a controller
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  */

45 public class ControllerShutdownThread extends ShutdownThread
46 {
47   protected Controller controller;
48
49   /**
50    * Prepare the thread for shutting down.
51    *
52    * @param controller the controller to shutdown
53    */

54   public ControllerShutdownThread(Controller controller)
55   {
56     super(Constants.SHUTDOWN_SAFE);
57     this.controller = controller;
58   }
59
60   /**
61    * @see org.continuent.sequoia.controller.core.shutdown.ShutdownThread#shutdown()
62    */

63   public void shutdown() throws ShutdownException
64   {
65     logger.info("Starting controller shutdown");
66     generateReportIfNeeded();
67     shutdownJmxAgent();
68     shutdownServerConnectionThread(0);
69     logger.info("Controller shutdown completed");
70   }
71
72   /**
73    * Shutdown the JMX Agent.
74    */

75   protected void shutdownJmxAgent()
76   {
77     logger.info("Shutting down Jmx Agent");
78     try
79     {
80       RmiConnector rmiConnector = controller.getRmiConnector();
81       if (rmiConnector != null)
82         rmiConnector.stop();
83       MBeanServerManager.stopMBeanServer();
84     }
85     catch (Exception JavaDoc jme)
86     {
87       logger.error(Translate.get("controller.shutdown.jmx.error", jme
88           .getMessage()), jme);
89       // throw new ShutdownException(jme);
90
}
91   }
92
93   /**
94    * Shutdown the ControllerServerThread and its attached connection to reject
95    * new incoming connections.
96    *
97    * @param joinTimeoutInMillis timeout in milliseconds to wait for controller
98    * server thread termination. A timeout of 0 means wait forever.
99    * @throws ShutdownException if an error occurs
100    */

101   protected void shutdownServerConnectionThread(int joinTimeoutInMillis)
102       throws ShutdownException
103   {
104     if (logger.isDebugEnabled())
105       logger.debug("Shutting down ControllerServerThread");
106     try
107     {
108       // Shutdown Server Connections Thread
109
ControllerServerThread thread = controller.getConnectionThread();
110       if (thread != null && !thread.isShuttingDown())
111       {
112         thread.shutdown();
113         logger.info("Waiting for controller thread termination.");
114         thread.join(joinTimeoutInMillis);
115       }
116     }
117     catch (Exception JavaDoc e)
118     {
119       throw new ShutdownException(e);
120     }
121   }
122
123   /**
124    * Generate a controller report if it has been enabled in the config file.
125    */

126   protected void generateReportIfNeeded()
127   {
128     ReportManager report = controller.getReport();
129     if (report != null && report.isGenerateOnShutdown())
130     {
131       report.generate(true);
132       logger.info(Translate.get("fatal.report.generated", report
133           .getReportLocation()
134           + File.separator + ControllerConstants.REPORT_FILE));
135     }
136   }
137
138 }
Popular Tags