1 17 18 package org.apache.geronimo.console.configmanager; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.IOException ; 24 import java.io.StringWriter ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager ; 29 import javax.enterprise.deploy.spi.DeploymentManager ; 30 import javax.enterprise.deploy.spi.Target ; 31 import javax.enterprise.deploy.spi.TargetModuleID ; 32 import javax.enterprise.deploy.spi.status.ProgressObject ; 33 import javax.portlet.ActionRequest; 34 import javax.portlet.ActionResponse; 35 import javax.portlet.PortletConfig; 36 import javax.portlet.PortletException; 37 import javax.portlet.PortletRequestDispatcher; 38 import javax.portlet.RenderRequest; 39 import javax.portlet.RenderResponse; 40 import javax.xml.parsers.DocumentBuilder ; 41 42 import org.apache.commons.fileupload.FileItem; 43 import org.apache.commons.fileupload.FileUploadException; 44 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 45 import org.apache.commons.fileupload.portlet.PortletFileUpload; 46 import org.apache.geronimo.console.BasePortlet; 47 import org.apache.geronimo.deployment.plugin.jmx.JMXDeploymentManager; 48 import org.apache.geronimo.deployment.plugin.ConfigIDExtractor; 49 import org.apache.geronimo.common.DeploymentException; 50 import org.apache.geronimo.kernel.repository.Artifact; 51 import org.apache.geronimo.kernel.util.XmlUtil; 52 import org.apache.geronimo.upgrade.Upgrade1_0To1_1; 53 import org.w3c.dom.Document ; 54 55 public class DeploymentPortlet extends BasePortlet { 56 private static final String DEPLOY_VIEW = "/WEB-INF/view/configmanager/deploy.jsp"; 57 private static final String HELP_VIEW = "/WEB-INF/view/configmanager/deployHelp.jsp"; 58 private static final String MIGRATED_PLAN_PARM = "migratedPlan"; 59 private static final String ORIGINAL_PLAN_PARM = "originalPlan"; 60 private static final String FULL_STATUS_PARM = "fullStatusMessage"; 61 private static final String ABBR_STATUS_PARM = "abbrStatusMessage"; 62 private PortletRequestDispatcher deployView; 63 private PortletRequestDispatcher helpView; 64 65 public void processAction(ActionRequest actionRequest, 66 ActionResponse actionResponse) throws PortletException, IOException { 67 if (!PortletFileUpload.isMultipartContent(actionRequest)) { 68 throw new PortletException("Expected file upload"); 69 } 70 71 File rootDir = new File (System.getProperty("java.io.tmpdir")); 72 PortletFileUpload uploader = new PortletFileUpload(new DiskFileItemFactory(10240, rootDir)); 73 File moduleFile = null; 74 File planFile = null; 75 String startApp = null; 76 String redeploy = null; 77 try { 78 List items = uploader.parseRequest(actionRequest); 79 for (Iterator i = items.iterator(); i.hasNext();) { 80 FileItem item = (FileItem) i.next(); 81 if (!item.isFormField()) { 82 String fieldName = item.getFieldName(); 83 String name = item.getName().trim(); 84 File file; 85 if (name.length() == 0) { 86 file = null; 87 } else { 88 int index = name.lastIndexOf('\\'); 90 if (index != -1) { 91 name = name.substring(index + 1); 92 } 93 file = new File (rootDir, name); 94 } 95 if ("module".equals(fieldName)) { 96 moduleFile = file; 97 } else if ("plan".equals(fieldName)) { 98 planFile = file; 99 } 100 if (file != null) { 101 try { 102 item.write(file); 103 } catch (Exception e) { 104 throw new PortletException(e); 105 } 106 } 107 } else { 108 if ("startApp".equalsIgnoreCase(item.getFieldName())) { 110 startApp = item.getString(); 111 } else if ("redeploy".equalsIgnoreCase(item.getFieldName())) { 112 redeploy = item.getString(); 113 } 114 } 115 } 116 } catch (FileUploadException e) { 117 throw new PortletException(e); 118 } 119 DeploymentFactoryManager dfm = DeploymentFactoryManager.getInstance(); 120 FileInputStream fis = null; 121 try { 122 DeploymentManager mgr = dfm.getDeploymentManager("deployer:geronimo:inVM", null, null); 123 try { 124 boolean isRedeploy = redeploy != null && !redeploy.equals(""); 125 if(mgr instanceof JMXDeploymentManager) { 126 ((JMXDeploymentManager)mgr).setLogConfiguration(false, true); 127 } 128 Target [] all = mgr.getTargets(); 129 ProgressObject progress; 130 if(isRedeploy) { 131 TargetModuleID [] targets = identifyTargets(moduleFile, planFile, mgr.getAvailableModules(null, all)); 132 if(targets.length == 0) { 133 throw new PortletException("Unable to identify modules to replace. Please include a Geronimo deployment plan or use the command-line deployment tool."); 134 } 135 progress = mgr.redeploy(targets, moduleFile, planFile); 136 } else { 137 progress = mgr.distribute(all, moduleFile, planFile); 138 } 139 while(progress.getDeploymentStatus().isRunning()) { 140 Thread.sleep(100); 141 } 142 143 String abbrStatusMessage; 144 String fullStatusMessage = null; 145 if(progress.getDeploymentStatus().isCompleted()) { 146 abbrStatusMessage = "The application was successfully "+(isRedeploy ? "re" : "")+"deployed.<br/>"; 147 if (!isRedeploy && startApp != null && !startApp.equals("")) { 149 progress = mgr.start(progress.getResultTargetModuleIDs()); 150 while(progress.getDeploymentStatus().isRunning()) { 151 Thread.sleep(100); 152 } 153 abbrStatusMessage+="The application was successfully started"; 154 } 155 } else { 156 fullStatusMessage = progress.getDeploymentStatus().getMessage(); 157 abbrStatusMessage = "Deployment failed:<br/>" 160 + fullStatusMessage.substring(0, fullStatusMessage.indexOf('\n')); 161 try { 163 if (planFile != null && planFile.exists()) { 164 byte[] plan = new byte[(int) planFile.length()]; 165 fis = new FileInputStream (planFile); 166 fis.read(plan); 167 DocumentBuilder documentBuilder = XmlUtil.newDocumentBuilderFactory().newDocumentBuilder(); 168 Document doc = documentBuilder.parse(new ByteArrayInputStream (plan)); 169 String configId = doc.getDocumentElement().getAttribute("configId"); 171 if (configId != null && !("".equals(configId))) { 172 StringWriter sw = new StringWriter (); 173 new Upgrade1_0To1_1().upgrade(new ByteArrayInputStream (plan), sw); 174 actionRequest.getPortletSession().setAttribute(MIGRATED_PLAN_PARM, sw.getBuffer()); 178 actionRequest.getPortletSession().setAttribute(ORIGINAL_PLAN_PARM, new String (plan)); 179 } 180 } 181 } catch (Exception e) { 182 } 186 } 187 actionRequest.getPortletSession().setAttribute(FULL_STATUS_PARM, fullStatusMessage); 190 actionRequest.getPortletSession().setAttribute(ABBR_STATUS_PARM, abbrStatusMessage); 191 } finally { 192 mgr.release(); 193 if (fis!=null) fis.close(); 194 } 195 } catch (Exception e) { 196 throw new PortletException(e); 197 } 198 } 199 200 private TargetModuleID [] identifyTargets(File module, File plan, TargetModuleID [] allModules) throws PortletException { 201 String moduleId = null; 202 List modules = new ArrayList (); 203 try { 204 if(plan != null) { 205 moduleId = ConfigIDExtractor.extractModuleIdFromPlan(plan); 206 } else if(module != null) { 207 moduleId = ConfigIDExtractor.extractModuleIdFromArchive(module); 208 if(moduleId == null) { 209 int pos = module.getName().lastIndexOf('.'); 210 moduleId = pos > -1 ? module.getName().substring(0, pos) : module.getName(); 211 } 212 } 213 if(moduleId != null) { 214 modules.addAll(ConfigIDExtractor.identifyTargetModuleIDs(allModules, moduleId, true)); 215 } else { 216 String name = module != null ? module.getName() : plan.getName(); 217 int pos = name.lastIndexOf('.'); 218 if(pos > -1) { 219 name = name.substring(0, pos); 220 } 221 modules.addAll(ConfigIDExtractor.identifyTargetModuleIDs(allModules, Artifact.DEFAULT_GROUP_ID+"/"+name+"//", true)); 222 } 223 } catch (IOException e) { 224 throw new PortletException("Unable to read input files: "+e.getMessage()); 225 } catch (DeploymentException e) { 226 throw new PortletException(e.getMessage(), e); 227 } 228 return (TargetModuleID []) modules.toArray(new TargetModuleID [modules.size()]); 229 } 230 231 protected void doView(RenderRequest renderRequest, 232 RenderResponse renderResponse) throws PortletException, IOException { 233 copyRenderAttribute(renderRequest, FULL_STATUS_PARM); 240 copyRenderAttribute(renderRequest, ABBR_STATUS_PARM); 241 copyRenderAttribute(renderRequest, MIGRATED_PLAN_PARM); 242 copyRenderAttribute(renderRequest, ORIGINAL_PLAN_PARM); 243 deployView.include(renderRequest, renderResponse); 244 } 245 246 private void copyRenderAttribute(RenderRequest renderRequest, String attr) { 247 Object value = renderRequest.getPortletSession().getAttribute(attr); 248 renderRequest.getPortletSession().removeAttribute(attr); 249 renderRequest.setAttribute(attr, value); 250 } 251 252 protected void doHelp(RenderRequest renderRequest, 253 RenderResponse renderResponse) throws PortletException, IOException { 254 helpView.include(renderRequest, renderResponse); 255 } 256 257 public void init(PortletConfig portletConfig) throws PortletException { 258 super.init(portletConfig); 259 deployView = portletConfig.getPortletContext().getRequestDispatcher(DEPLOY_VIEW); 260 helpView = portletConfig.getPortletContext().getRequestDispatcher(HELP_VIEW); 261 } 262 263 public void destroy() { 264 deployView = null; 265 helpView = null; 266 super.destroy(); 267 } 268 } 269 | Popular Tags |