KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > extensions > actions > ReloadExtensionsAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.extensions.actions;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.struts.Globals;
35 import org.apache.struts.action.ActionForm;
36 import org.apache.struts.action.ActionForward;
37 import org.apache.struts.action.ActionMapping;
38 import org.apache.struts.action.ActionMessage;
39 import org.apache.struts.action.ActionMessages;
40
41 import com.sslexplorer.core.BundleActionMessage;
42 import com.sslexplorer.core.CoreUtil;
43 import com.sslexplorer.core.GlobalWarning;
44 import com.sslexplorer.core.LicenseAgreement;
45 import com.sslexplorer.core.actions.AuthenticatedAction;
46 import com.sslexplorer.extensions.ExtensionBundle;
47 import com.sslexplorer.extensions.ExtensionDescriptor;
48 import com.sslexplorer.extensions.store.ExtensionStore;
49 import com.sslexplorer.extensions.types.PluginType;
50 import com.sslexplorer.policyframework.Permission;
51 import com.sslexplorer.policyframework.PolicyConstants;
52 import com.sslexplorer.security.SessionInfo;
53 import com.sslexplorer.setup.LicenseAgreementCallback;
54
55 /**
56  * Action to reload the extension store.
57  *
58  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
59  */

60 public class ReloadExtensionsAction extends AuthenticatedAction {
61
62     final static Log log = LogFactory.getLog(ReloadExtensionsAction.class);
63
64     /**
65      * Constructor.
66      *
67      */

68     public ReloadExtensionsAction() {
69         super(PolicyConstants.EXTENSIONS_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_CHANGE });
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see com.sslexplorer.core.actions.AuthenticatedAction#onExecute(org.apache.struts.action.ActionMapping,
76      * org.apache.struts.action.ActionForm,
77      * javax.servlet.http.HttpServletRequest,
78      * javax.servlet.http.HttpServletResponse)
79      */

80     public ActionForward onExecute(ActionMapping mapping, ActionForm form, final HttpServletRequest JavaDoc request,
81                                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
82         try {
83             List JavaDoc errors = new ArrayList JavaDoc();
84
85             // Get a list of the currently loaded plugin definitions. If this
86
// changes display a restart warning
87
Map JavaDoc currentlyLoadedPlugins = getLoadedPlugins();
88             Map JavaDoc currentlyLoadedExtensions = getLoadedExtensions();
89
90             // If there are any pending removals or installations, we cannot
91
// reload
92
boolean pending = false;
93             for (Iterator JavaDoc i = currentlyLoadedExtensions.entrySet().iterator(); !pending && i.hasNext();) {
94                 Map.Entry JavaDoc ent = (Map.Entry JavaDoc) i.next();
95                 final ExtensionBundle bundle = (ExtensionBundle) ent.getValue();
96                 pending = bundle.getType() == ExtensionBundle.TYPE_PENDING_INSTALLATION || bundle.getType() == ExtensionBundle.TYPE_PENDING_REMOVAL
97                     || bundle.getType() == ExtensionBundle.TYPE_PENDING_UPDATE
98                     || bundle.getType() == ExtensionBundle.TYPE_PENDING_STATE_CHANGE;
99             }
100             if (pending) {
101                 throw new Exception JavaDoc("There are pending installations / removals / updates. You must restart the server.");
102             }
103
104             // Reload
105

106             if (request.getParameter("id") == null) {
107                 ExtensionStore.getInstance().reload();
108             } else {
109                 String JavaDoc application = request.getParameter("id");
110                 ExtensionStore.getInstance().reload(application);
111             }
112
113             /*
114              * Build up an error message from any exceptions that may have
115              * occured during reloading
116              */

117             if (errors != null && errors.size() > 0) {
118                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
119                 for (ExtensionBundle bundle : ExtensionStore.getInstance().getExtensionBundles()) {
120                     if (bundle.getError() != null) {
121                         if (buf.length() > 0) {
122                             buf.append(". ");
123                         }
124                         buf.append(bundle.getError().getMessage());
125                     }
126                 }
127                 throw new Exception JavaDoc(buf.toString());
128             }
129
130             // Look for new plugins
131
Map JavaDoc newLoadedPlugins = getLoadedPlugins();
132
133             // Look for new plugins
134
boolean newPluginsFound = false;
135             for (Iterator JavaDoc i = newLoadedPlugins.entrySet().iterator(); i.hasNext();) {
136                 Map.Entry JavaDoc ent = (Map.Entry JavaDoc) i.next();
137                 if (!currentlyLoadedPlugins.containsKey(ent.getKey())) {
138                     ExtensionDescriptor des = (ExtensionDescriptor) newLoadedPlugins.get(ent.getKey());
139                     des.getApplicationBundle().setType(ExtensionBundle.TYPE_PENDING_INSTALLATION);
140                     newPluginsFound = true;
141                 }
142             }
143             if (newPluginsFound) {
144                 CoreUtil.addMultipleGlobalWarning(GlobalWarning.MANAGEMENT_USERS, new BundleActionMessage("extensions",
145                                 "extensionStore.message.pluginInstalledRestartRequired"));
146             }
147
148             // Look for new extensions
149
Map JavaDoc newLoadedExtensions = getLoadedExtensions();
150
151             // Look for extension updates
152
File JavaDoc updatedExtensionsDir = ExtensionStore.getInstance().getUpdatedExtensionsDirectory();
153             boolean updatesFound = false;
154             for (Iterator JavaDoc i = newLoadedExtensions.entrySet().iterator(); i.hasNext();) {
155                 Map.Entry JavaDoc ent = (Map.Entry JavaDoc) i.next();
156                 if (new File JavaDoc(updatedExtensionsDir, (String JavaDoc) ent.getKey()).exists()) {
157                     final ExtensionBundle bundle = (ExtensionBundle) ent.getValue();
158                     for (Iterator JavaDoc j = bundle.iterator(); j.hasNext();) {
159                         ExtensionDescriptor des = (ExtensionDescriptor) j.next();
160                         des.getApplicationBundle().setType(ExtensionBundle.TYPE_PENDING_UPDATE);
161                     }
162                     updatesFound = true;
163                 }
164             }
165             if (updatesFound) {
166                 CoreUtil.addMultipleGlobalWarning(GlobalWarning.MANAGEMENT_USERS, new BundleActionMessage("extensions",
167                                 "extensionStore.message.extensionUpdatedRestartRequired"));
168             }
169
170             // Check for new extensions
171
for (Iterator JavaDoc i = newLoadedExtensions.entrySet().iterator(); i.hasNext();) {
172                 Map.Entry JavaDoc ent = (Map.Entry JavaDoc) i.next();
173                 if (!currentlyLoadedExtensions.containsKey(ent.getKey())) {
174                     final ExtensionBundle bundle = (ExtensionBundle) ent.getValue();
175
176                     // If installing, there may be a license agreement to handle
177
File JavaDoc licenseFile = bundle.getLicenseFile();
178                     if (licenseFile != null && licenseFile.exists()) {
179                         final boolean fNewPluginsFound = newPluginsFound;
180                         CoreUtil.requestLicenseAgreement(request.getSession(), new LicenseAgreement(bundle.getName(),
181                                         licenseFile,
182                                         new LicenseAgreementCallback() {
183                                             public void licenseAccepted(HttpServletRequest JavaDoc request) {
184                                                 // Dont care
185
}
186
187                                             public void licenseRejected(HttpServletRequest JavaDoc request) {
188                                                 try {
189                                                     ExtensionStore.getInstance().removeExtensionBundle(bundle);
190                                                 } catch (Exception JavaDoc e) {
191                                                 }
192                                                 if (fNewPluginsFound) {
193                                                     CoreUtil.removeGlobalWarning(request.getSession(),
194                                                         "extensionStore.message.pluginInstalledRestartRequired");
195                                                     CoreUtil.addMultipleGlobalWarning(GlobalWarning.MANAGEMENT_USERS,
196                                                         new BundleActionMessage("extensions",
197                                                                         "extensionStore.message.pluginLicenseRejectedRestartRequired"));
198                                                 }
199                                             }
200
201                                         },
202                                         new ActionForward("/showExtensionStore.do", true)));
203                     }
204                 }
205             }
206         } catch (Exception JavaDoc e) {
207             log.error("Failed to reload extension store.", e);
208             ActionMessages errs = new ActionMessages();
209             errs.add(Globals.ERROR_KEY, new ActionMessage("extensionStore.error.reloadFailed", e.getMessage()));
210             saveErrors(request, errs);
211         }
212
213         return mapping.findForward("success");
214     }
215
216     private Map JavaDoc<String JavaDoc,ExtensionDescriptor> getLoadedPlugins() {
217         Map JavaDoc<String JavaDoc,ExtensionDescriptor> map = new HashMap JavaDoc<String JavaDoc,ExtensionDescriptor>();
218         for (Iterator JavaDoc i = getLoadedExtensions().entrySet().iterator(); i.hasNext();) {
219             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
220             ExtensionBundle b = (ExtensionBundle) entry.getValue();
221             for (Iterator JavaDoc j = b.iterator(); j.hasNext();) {
222                 ExtensionDescriptor des = (ExtensionDescriptor) j.next();
223                 if (des.getExtensionType() instanceof PluginType) {
224                     map.put(des.getId(), des);
225                 }
226             }
227         }
228         return map;
229     }
230
231     private Map JavaDoc<String JavaDoc,ExtensionBundle> getLoadedExtensions() {
232         Map JavaDoc<String JavaDoc,ExtensionBundle> map = new HashMap JavaDoc<String JavaDoc,ExtensionBundle>();
233         for (Iterator JavaDoc i = ExtensionStore.getInstance().getExtensionBundles().iterator(); i.hasNext();) {
234             ExtensionBundle bundle = (ExtensionBundle) i.next();
235             map.put(bundle.getId(), bundle);
236         }
237         return map;
238     }
239
240     /*
241      * (non-Javadoc)
242      *
243      * @see com.sslexplorer.core.actions.AuthenticatedAction#getNavigationContext(org.apache.struts.action.ActionMapping,
244      * org.apache.struts.action.ActionForm,
245      * javax.servlet.http.HttpServletRequest,
246      * javax.servlet.http.HttpServletResponse)
247      */

248     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
249         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
250     }
251 }
Popular Tags