1 19 20 package com.sslexplorer.extensions.actions; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 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 60 public class ReloadExtensionsAction extends AuthenticatedAction { 61 62 final static Log log = LogFactory.getLog(ReloadExtensionsAction.class); 63 64 68 public ReloadExtensionsAction() { 69 super(PolicyConstants.EXTENSIONS_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_CHANGE }); 70 } 71 72 80 public ActionForward onExecute(ActionMapping mapping, ActionForm form, final HttpServletRequest request, 81 HttpServletResponse response) throws Exception { 82 try { 83 List errors = new ArrayList (); 84 85 Map currentlyLoadedPlugins = getLoadedPlugins(); 88 Map currentlyLoadedExtensions = getLoadedExtensions(); 89 90 boolean pending = false; 93 for (Iterator i = currentlyLoadedExtensions.entrySet().iterator(); !pending && i.hasNext();) { 94 Map.Entry ent = (Map.Entry ) 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 ("There are pending installations / removals / updates. You must restart the server."); 102 } 103 104 106 if (request.getParameter("id") == null) { 107 ExtensionStore.getInstance().reload(); 108 } else { 109 String application = request.getParameter("id"); 110 ExtensionStore.getInstance().reload(application); 111 } 112 113 117 if (errors != null && errors.size() > 0) { 118 StringBuffer buf = new StringBuffer (); 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 (buf.toString()); 128 } 129 130 Map newLoadedPlugins = getLoadedPlugins(); 132 133 boolean newPluginsFound = false; 135 for (Iterator i = newLoadedPlugins.entrySet().iterator(); i.hasNext();) { 136 Map.Entry ent = (Map.Entry ) 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 Map newLoadedExtensions = getLoadedExtensions(); 150 151 File updatedExtensionsDir = ExtensionStore.getInstance().getUpdatedExtensionsDirectory(); 153 boolean updatesFound = false; 154 for (Iterator i = newLoadedExtensions.entrySet().iterator(); i.hasNext();) { 155 Map.Entry ent = (Map.Entry ) i.next(); 156 if (new File (updatedExtensionsDir, (String ) ent.getKey()).exists()) { 157 final ExtensionBundle bundle = (ExtensionBundle) ent.getValue(); 158 for (Iterator 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 for (Iterator i = newLoadedExtensions.entrySet().iterator(); i.hasNext();) { 172 Map.Entry ent = (Map.Entry ) i.next(); 173 if (!currentlyLoadedExtensions.containsKey(ent.getKey())) { 174 final ExtensionBundle bundle = (ExtensionBundle) ent.getValue(); 175 176 File 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 request) { 184 } 186 187 public void licenseRejected(HttpServletRequest request) { 188 try { 189 ExtensionStore.getInstance().removeExtensionBundle(bundle); 190 } catch (Exception 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 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 <String ,ExtensionDescriptor> getLoadedPlugins() { 217 Map <String ,ExtensionDescriptor> map = new HashMap <String ,ExtensionDescriptor>(); 218 for (Iterator i = getLoadedExtensions().entrySet().iterator(); i.hasNext();) { 219 Map.Entry entry = (Map.Entry ) i.next(); 220 ExtensionBundle b = (ExtensionBundle) entry.getValue(); 221 for (Iterator 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 <String ,ExtensionBundle> getLoadedExtensions() { 232 Map <String ,ExtensionBundle> map = new HashMap <String ,ExtensionBundle>(); 233 for (Iterator 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 248 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 249 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT; 250 } 251 } | Popular Tags |