KickJava   Java API By Example, From Geeks To Geeks.

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


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.error.AlfrescoRuntimeException;
23 import org.alfresco.util.exec.RuntimeExec.ExecutionResult;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.springframework.context.ApplicationEvent;
27 import org.springframework.context.ApplicationListener;
28 import org.springframework.context.event.ContextRefreshedEvent;
29
30 /**
31  * Application bootstrap bean that is able to execute one or more
32  * native executable statements upon startup and shutdown.
33  *
34  * @author Derek Hulley
35  */

36 public class RuntimeExecBootstrapBean implements ApplicationListener
37 {
38     private static Log logger = LogFactory.getLog(RuntimeExecBootstrapBean.class);
39     
40     private List JavaDoc<RuntimeExec> startupCommands;
41
42     /**
43      * Initializes the bean with empty defaults, i.e. it will do nothing
44      */

45     public RuntimeExecBootstrapBean()
46     {
47         this.startupCommands = Collections.emptyList();
48     }
49
50     /**
51      * Set the commands to execute, in sequence, when the application context
52      * is initialized.
53      *
54      * @param startupCommands list of commands
55      */

56     public void setStartupCommands(List JavaDoc<RuntimeExec> startupCommands)
57     {
58         this.startupCommands = startupCommands;
59     }
60
61     /**
62      * Listens for the the context refresh and executes the startup commands.
63      * Any failure of the commands will lead to context initialization failure.
64      */

65     public void onApplicationEvent(ApplicationEvent event)
66     {
67         if (!(event instanceof ContextRefreshedEvent))
68         {
69             return;
70         }
71         // execute
72
for (RuntimeExec command : startupCommands)
73         {
74             ExecutionResult result = command.execute();
75             // check for failure
76
if (!result.getSuccess())
77             {
78                 throw new AlfrescoRuntimeException("Bootstrap command failed: \n" + result);
79             }
80         }
81         // done
82
if (logger.isDebugEnabled())
83         {
84             logger.debug("Bootstrap execution of " + startupCommands.size() + " commands was successful");
85         }
86     }
87 }
88
Popular Tags