KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > exec > RuntimeExecShutdownBean


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util.exec;
18
19 import java.util.Collections JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.alfresco.util.exec.RuntimeExec.ExecutionResult;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.springframework.context.ApplicationEvent;
26 import org.springframework.context.ApplicationListener;
27 import org.springframework.context.event.ContextClosedEvent;
28 import org.springframework.context.event.ContextRefreshedEvent;
29
30 /**
31  * This bean executes a list of shutdown commands when either the VM shuts down
32  * or the application context closes. In both cases, the commands are only
33  * executed if the application context was started.
34  *
35  * @author Derek Hulley
36  */

37 public class RuntimeExecShutdownBean implements ApplicationListener
38 {
39     private static Log logger = LogFactory.getLog(RuntimeExecShutdownBean.class);
40     
41     /** the commands to execute on context closure or VM shutdown */
42     private List JavaDoc<RuntimeExec> shutdownCommands;
43     /** the registered shutdown hook */
44     private Thread JavaDoc shutdownHook;
45     /** ensures that commands don't get executed twice */
46     private boolean executed;
47
48     /**
49      * Initializes the bean with empty defaults, i.e. it will do nothing
50      */

51     public RuntimeExecShutdownBean()
52     {
53         this.shutdownCommands = Collections.emptyList();
54         this.executed = false;
55     }
56
57     /**
58      * Set the commands to execute, in sequence, when the application context
59      * is initialized.
60      *
61      * @param startupCommands list of commands
62      */

63     public void setShutdownCommands(List JavaDoc<RuntimeExec> startupCommands)
64     {
65         this.shutdownCommands = startupCommands;
66     }
67
68     /**
69      * Listens for the the context refresh and executes the startup commands.
70      * Any failure of the commands will lead to context initialization failure.
71      */

72     public synchronized void onApplicationEvent(ApplicationEvent event)
73     {
74         if (event instanceof ContextRefreshedEvent)
75         {
76             // register shutdown hook
77
shutdownHook = new ShutdownThread();
78             Runtime.getRuntime().addShutdownHook(shutdownHook);
79
80             if (logger.isDebugEnabled())
81             {
82                 logger.debug("Registered shutdown hook");
83             }
84         }
85         else if (event instanceof ContextClosedEvent)
86         {
87             // remove shutdown hook and execute
88
if (shutdownHook != null)
89             {
90                 // execute
91
execute();
92                 // remove hook
93
try
94                 {
95                     Runtime.getRuntime().removeShutdownHook(shutdownHook);
96                 }
97                 catch (IllegalStateException JavaDoc e)
98                 {
99                     // VM is already shutting down
100
}
101                 shutdownHook = null;
102                 
103                 if (logger.isDebugEnabled())
104                 {
105                     logger.debug("Deregistered shutdown hook");
106                 }
107             }
108         }
109     }
110     
111     private synchronized void execute()
112     {
113         // have we already done this?
114
if (executed)
115         {
116             return;
117         }
118         executed = true;
119         for (RuntimeExec command : shutdownCommands)
120         {
121             ExecutionResult result = command.execute();
122             // check for failure
123
if (!result.getSuccess())
124             {
125                 logger.error("Shutdown command execution failed. Continuing with other commands.: \n" + result);
126             }
127         }
128         // done
129
if (logger.isDebugEnabled())
130         {
131             logger.debug("Executed shutdown commands");
132         }
133     }
134     
135     /**
136      * The thread that will call the shutdown commands.
137      *
138      * @author Derek Hulley
139      */

140     private class ShutdownThread extends Thread JavaDoc
141     {
142         private ShutdownThread()
143         {
144             this.setDaemon(true);
145         }
146
147         @Override JavaDoc
148         public void run()
149         {
150             execute();
151         }
152     }
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
Popular Tags