KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > admin > patch > PatchExecuter


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.repo.admin.patch;
18
19 import java.util.Date JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.alfresco.error.AlfrescoRuntimeException;
23 import org.alfresco.i18n.I18NUtil;
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  * This component is responsible for ensuring that patches are applied
32  * at the appropriate time.
33  *
34  * @author Derek Hulley
35  */

36 public class PatchExecuter implements ApplicationListener
37 {
38     private static final String JavaDoc MSG_CHECKING = "patch.executer.checking";
39     private static final String JavaDoc MSG_NO_PATCHES_REQUIRED = "patch.executer.no_patches_required";
40     private static final String JavaDoc MSG_NOT_EXECUTED = "patch.executer.not_executed";
41     private static final String JavaDoc MSG_EXECUTED = "patch.executer.executed";
42     private static final String JavaDoc MSG_FAILED = "patch.executer.failed";
43     
44     private static Log logger = LogFactory.getLog(PatchExecuter.class);
45     
46     private PatchService patchService;
47
48     /**
49      * @param patchService the server that actually executes the patches
50      */

51     public void setPatchService(PatchService patchService)
52     {
53         this.patchService = patchService;
54     }
55     
56     /**
57      * Ensures that all outstanding patches are applied.
58      */

59     public void applyOutstandingPatches()
60     {
61         logger.info(I18NUtil.getMessage(MSG_CHECKING));
62         
63         Date JavaDoc before = new Date JavaDoc(System.currentTimeMillis() - 20000L); // 20 seconds ago
64
patchService.applyOutstandingPatches();
65         Date JavaDoc after = new Date JavaDoc(System .currentTimeMillis() + 20000L); // 20 seconds ahead
66

67         // get all the patches executed in the time
68
List JavaDoc<PatchInfo> appliedPatches = patchService.getPatches(before, after);
69         
70         // don't report anything if nothing was done
71
if (appliedPatches.size() == 0)
72         {
73             logger.info(I18NUtil.getMessage(MSG_NO_PATCHES_REQUIRED));
74         }
75         else
76         {
77             boolean succeeded = true;
78             // list all patches applied, including failures
79
for (PatchInfo patchInfo : appliedPatches)
80             {
81                 if (!patchInfo.getWasExecuted())
82                 {
83                     // the patch was not executed
84
logger.debug(I18NUtil.getMessage(MSG_NOT_EXECUTED, patchInfo.getId(), patchInfo.getReport()));
85                 }
86                 else if (patchInfo.getSucceeded())
87                 {
88                     logger.info(I18NUtil.getMessage(MSG_EXECUTED, patchInfo.getId(), patchInfo.getReport()));
89                 }
90                 else
91                 {
92                     succeeded = false;
93                     logger.error(I18NUtil.getMessage(MSG_FAILED, patchInfo.getId(), patchInfo.getReport()));
94                }
95             }
96             // generate an error if there was a failure
97
if (!succeeded)
98             {
99                 throw new AlfrescoRuntimeException("Not all patches could be applied");
100             }
101         }
102     }
103
104     /*
105      * (non-Javadoc)
106      * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
107      */

108     public void onApplicationEvent(ApplicationEvent event)
109     {
110         if (event instanceof ContextRefreshedEvent)
111         {
112             applyOutstandingPatches();
113         }
114     }
115
116 }
117
Popular Tags