KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > configmanager > DeploymentPortlet


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.console.configmanager;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager JavaDoc;
29 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
30 import javax.enterprise.deploy.spi.Target JavaDoc;
31 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
32 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
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 JavaDoc;
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 JavaDoc;
54
55 public class DeploymentPortlet extends BasePortlet {
56     private static final String JavaDoc DEPLOY_VIEW = "/WEB-INF/view/configmanager/deploy.jsp";
57     private static final String JavaDoc HELP_VIEW = "/WEB-INF/view/configmanager/deployHelp.jsp";
58     private static final String JavaDoc MIGRATED_PLAN_PARM = "migratedPlan";
59     private static final String JavaDoc ORIGINAL_PLAN_PARM = "originalPlan";
60     private static final String JavaDoc FULL_STATUS_PARM = "fullStatusMessage";
61     private static final String JavaDoc 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 JavaDoc {
67         if (!PortletFileUpload.isMultipartContent(actionRequest)) {
68             throw new PortletException("Expected file upload");
69         }
70
71         File JavaDoc rootDir = new File JavaDoc(System.getProperty("java.io.tmpdir"));
72         PortletFileUpload uploader = new PortletFileUpload(new DiskFileItemFactory(10240, rootDir));
73         File JavaDoc moduleFile = null;
74         File JavaDoc planFile = null;
75         String JavaDoc startApp = null;
76         String JavaDoc redeploy = null;
77         try {
78             List JavaDoc items = uploader.parseRequest(actionRequest);
79             for (Iterator JavaDoc i = items.iterator(); i.hasNext();) {
80                 FileItem item = (FileItem) i.next();
81                 if (!item.isFormField()) {
82                     String JavaDoc fieldName = item.getFieldName();
83                     String JavaDoc name = item.getName().trim();
84                     File JavaDoc file;
85                     if (name.length() == 0) {
86                         file = null;
87                     } else {
88                         // Firefox sends basename, IE sends full path
89
int index = name.lastIndexOf('\\');
90                         if (index != -1) {
91                             name = name.substring(index + 1);
92                         }
93                         file = new File JavaDoc(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 JavaDoc e) {
104                             throw new PortletException(e);
105                         }
106                     }
107                 } else {
108                     // retrieve 'startApp' form field value
109
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 JavaDoc dfm = DeploymentFactoryManager.getInstance();
120         FileInputStream JavaDoc fis = null;
121         try {
122             DeploymentManager JavaDoc 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 JavaDoc[] all = mgr.getTargets();
129                 ProgressObject JavaDoc progress;
130                 if(isRedeploy) {
131                     TargetModuleID JavaDoc[] 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 JavaDoc abbrStatusMessage;
144                 String JavaDoc fullStatusMessage = null;
145                 if(progress.getDeploymentStatus().isCompleted()) {
146                     abbrStatusMessage = "The application was successfully "+(isRedeploy ? "re" : "")+"deployed.<br/>";
147                     // start installed app/s
148
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                     // for the abbreviated status message clip off everything
158
// after the first line, which in most cases means the gnarly stacktrace
159
abbrStatusMessage = "Deployment failed:<br/>"
160                                       + fullStatusMessage.substring(0, fullStatusMessage.indexOf('\n'));
161                     // try to provide an upgraded version of the plan
162
try {
163                         if (planFile != null && planFile.exists()) {
164                             byte[] plan = new byte[(int) planFile.length()];
165                             fis = new FileInputStream JavaDoc(planFile);
166                             fis.read(plan);
167                             DocumentBuilder JavaDoc documentBuilder = XmlUtil.newDocumentBuilderFactory().newDocumentBuilder();
168                             Document JavaDoc doc = documentBuilder.parse(new ByteArrayInputStream JavaDoc(plan));
169                             // v1.1 switched from configId to moduleId
170
String JavaDoc configId = doc.getDocumentElement().getAttribute("configId");
171                             if (configId != null && !("".equals(configId))) {
172                                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
173                                 new Upgrade1_0To1_1().upgrade(new ByteArrayInputStream JavaDoc(plan), sw);
174                                 // have to store the original and upgraded plans in the session
175
// because the buffer size for render parameters is sometimes not
176
// big enough
177
actionRequest.getPortletSession().setAttribute(MIGRATED_PLAN_PARM, sw.getBuffer());
178                                 actionRequest.getPortletSession().setAttribute(ORIGINAL_PLAN_PARM, new String JavaDoc(plan));
179                             }
180                         }
181                     } catch (Exception JavaDoc e) {
182                         // cannot provide a migrated plan in this case, most likely
183
// because the deployment plan would not parse. a valid
184
// status message has already been provided in this case
185
}
186                 }
187                 // have to store the status messages in the portlet session
188
// because the buffer size for render parameters is sometimes not big enough
189
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 JavaDoc e) {
196             throw new PortletException(e);
197         }
198     }
199
200     private TargetModuleID JavaDoc[] identifyTargets(File JavaDoc module, File JavaDoc plan, TargetModuleID JavaDoc[] allModules) throws PortletException {
201         String JavaDoc moduleId = null;
202         List JavaDoc modules = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc[]) modules.toArray(new TargetModuleID JavaDoc[modules.size()]);
229     }
230
231     protected void doView(RenderRequest renderRequest,
232                           RenderResponse renderResponse) throws PortletException, IOException JavaDoc {
233         // The deployment plans and messages from the deployers sometime exceeds
234
// the buffer size for render attributes. To avoid the buffer
235
// overrun the render attributes are temporarily stored in the portlet
236
// session during the processAction phase and then copied into render
237
// attributes here so the JSP has easier access to them. This seems
238
// to only be an issue on tomcat.
239
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 JavaDoc attr) {
247         Object JavaDoc 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 JavaDoc {
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