001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.manager;
018:
019: import java.io.CharArrayWriter;
020: import java.io.File;
021: import java.io.IOException;
022: import java.io.PrintWriter;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.servlet.ServletException;
027: import javax.servlet.http.HttpServlet;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import org.apache.commons.fileupload.DiskFileUpload;
032: import org.apache.commons.fileupload.FileItem;
033: import org.apache.commons.fileupload.FileUpload;
034: import org.apache.jetspeed.Jetspeed;
035: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
036: import org.apache.jetspeed.components.portletregistry.RegistryException;
037: import org.apache.jetspeed.deployment.DeploymentManager;
038: import org.apache.jetspeed.deployment.DeploymentStatus;
039: import org.apache.jetspeed.factory.PortletFactory;
040: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
041: import org.apache.jetspeed.om.common.portlet.PortletApplication;
042: import org.apache.jetspeed.tools.pamanager.servletcontainer.ApplicationServerManager;
043: import org.apache.jetspeed.tools.pamanager.servletcontainer.ApplicationServerManagerResult;
044:
045: /**
046: * ManagerServlet ala Tomcat ManagerServlet
047: *
048: * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
049: * @version $Id: ManagerServlet.java 517719 2007-03-13 15:05:48Z ate $
050: */
051: public class ManagerServlet extends HttpServlet {
052: private static int OK = 0;
053: private static int ERROR_NO_DATA = 1;
054: private static int ERROR_UNKNOWN_COMMAND = 2;
055: private static int ERROR_UNKNOWN_PA = 3;
056: private static int ERROR_INVALID = 4;
057: private static int ERROR_UNSUPPORTED = 5;
058: private static int ERROR_UNAVAILABLE = 6;
059: private static int ERROR_SERVER = 7;
060: private static int ERROR_UNEXPECTED = 8;
061: private static int ERROR_IGNORED = 9;
062:
063: private ApplicationServerManager asm;
064: private PortletRegistry registry;
065: private PortletFactory portletFactory;
066: private DeploymentManager dm;
067:
068: public void init() throws ServletException {
069: super .init();
070: asm = (ApplicationServerManager) Jetspeed.getComponentManager()
071: .getComponent(ApplicationServerManager.class);
072: registry = (PortletRegistry) Jetspeed.getComponentManager()
073: .getComponent(PortletRegistry.class);
074: portletFactory = (PortletFactory) Jetspeed
075: .getComponentManager().getComponent("portletFactory");
076: dm = (DeploymentManager) Jetspeed.getComponentManager()
077: .getComponent("deploymentManager");
078: }
079:
080: public void destroy() {
081: super .destroy();
082: }
083:
084: protected void doGet(HttpServletRequest request,
085: HttpServletResponse response) throws ServletException,
086: IOException {
087: process(request, response, false);
088: }
089:
090: protected void doPost(HttpServletRequest request,
091: HttpServletResponse response) throws ServletException,
092: IOException {
093: process(request, response, true);
094: }
095:
096: protected void process(HttpServletRequest request,
097: HttpServletResponse response, boolean posted)
098: throws ServletException, IOException {
099: // Prepare our output writer to generate the response message
100: response.setContentType("text/plain; charset=utf-8");
101: CharArrayWriter buffer = new CharArrayWriter();
102: PrintWriter writer = new PrintWriter(buffer);
103:
104: // Identify the request parameters that we need
105: String command = request.getPathInfo();
106: int result = OK;
107:
108: if (command == null) {
109: result = OK;
110: } else if (command.equals("/list")) {
111: result = list(writer);
112: } else if (command.equals("/start")) {
113: result = start(writer, request.getParameter("pa"));
114: } else if (command.equals("/stop")) {
115: result = stop(writer, request.getParameter("pa"));
116: } else if (command.equals("/undeploy")) {
117: result = undeploy(writer, request.getParameter("pa"));
118: } else if (command.equals("/unregister")) {
119: result = unregister(writer, request.getParameter("pa"));
120: } else if (command.equals("/deploy")) {
121: if (posted) {
122: result = deploy(writer, request);
123: } else {
124: writer
125: .println("Error: /deploy is only available through POST");
126: result = ERROR_INVALID;
127: }
128: } else {
129: writer.println("Error: Unknown command " + command);
130: result = ERROR_UNKNOWN_COMMAND;
131: }
132: writer.flush();
133: writer.close();
134: writer = response.getWriter();
135: if (result == OK) {
136: writer.println("OK");
137: } else {
138: writer.println("FAIL - CODE: " + result);
139: }
140: writer.print(buffer.toString());
141: writer.flush();
142: writer.close();
143: }
144:
145: protected int list(PrintWriter writer) {
146: writer.println("Listed Portlet Applications");
147: Iterator iter = registry.getPortletApplications().iterator();
148: PortletApplication pa;
149: while (iter.hasNext()) {
150: pa = (PortletApplication) iter.next();
151: writer
152: .println(pa.getId()
153: + ":"
154: + pa.getName()
155: + ":"
156: + pa.getWebApplicationDefinition()
157: .getContextRoot()
158: + ":"
159: + (portletFactory
160: .isPortletApplicationRegistered(pa) ? "ACTIVE"
161: : "INACTIVE"));
162: }
163: return OK;
164: }
165:
166: protected int start(PrintWriter writer, String paName) {
167: PortletApplication pa = null;
168: if (paName != null) {
169: pa = registry.getPortletApplication(paName);
170: }
171: if (pa == null) {
172: writer.println("Error: Unknown Portlet Application "
173: + paName);
174: return ERROR_UNKNOWN_PA;
175: }
176: if (portletFactory.isPortletApplicationRegistered(pa)) {
177: writer.println("Warning: Portlet Application " + paName
178: + " already started");
179: return OK;
180: } else if (pa.getApplicationType() == MutablePortletApplication.LOCAL) {
181: writer.println("Error: Starting LOCAL Portlet Application "
182: + paName + " not supported");
183: return ERROR_UNSUPPORTED;
184: } else if (!asm.isConnected()) {
185: writer.println("Error: Not connected to the server");
186: return ERROR_UNAVAILABLE;
187: } else {
188: try {
189: ApplicationServerManagerResult result = asm
190: .start(pa.getWebApplicationDefinition()
191: .getContextRoot());
192: if (result.isOk()) {
193: writer.println("Portlet Application " + paName
194: + " started");
195: writer.println(result.getResponse());
196: return OK;
197: } else {
198: writer.println("Error: Portlet Application "
199: + paName + " could not be started");
200: writer.println(result.getResponse());
201: return ERROR_SERVER;
202: }
203: } catch (Exception e) {
204: writer
205: .println("Error: Failed to start Portlet Application "
206: + paName + ": " + e.getMessage());
207: e.printStackTrace(writer);
208: return ERROR_UNEXPECTED;
209: }
210: }
211: }
212:
213: protected int stop(PrintWriter writer, String paName) {
214: PortletApplication pa = null;
215: if (paName != null) {
216: pa = registry.getPortletApplication(paName);
217: }
218: if (pa == null) {
219: writer.println("Error: Unknown Portlet Application "
220: + paName);
221: return ERROR_UNKNOWN_PA;
222: }
223: if (!portletFactory.isPortletApplicationRegistered(pa)) {
224: writer.println("Portlet Application " + paName
225: + " already stopped");
226: return OK;
227: } else if (pa.getApplicationType() == MutablePortletApplication.LOCAL) {
228: writer.println("Error: Stopping LOCAL Portlet Application "
229: + paName + " not supported");
230: return ERROR_UNSUPPORTED;
231: } else if (!asm.isConnected()) {
232: writer.println("Error: Not connected to the server");
233: return ERROR_UNAVAILABLE;
234: } else {
235: try {
236: ApplicationServerManagerResult result = asm
237: .stop(pa.getWebApplicationDefinition()
238: .getContextRoot());
239: if (result.isOk()) {
240: writer.println("Portlet Application " + paName
241: + " stopped");
242: writer.println(result.getResponse());
243: return OK;
244: } else {
245: writer.println("Error: Portlet Application "
246: + paName + " could not be stopped");
247: writer.println(result.getResponse());
248: return ERROR_SERVER;
249: }
250: } catch (Exception e) {
251: writer
252: .println("Error: Failed to stop Portlet Application "
253: + paName + ": " + e.getMessage());
254: e.printStackTrace(writer);
255: return ERROR_UNEXPECTED;
256: }
257: }
258: }
259:
260: protected int undeploy(PrintWriter writer, String paName) {
261: int stopResult = stop(writer, paName);
262: if (stopResult != OK) {
263: return stopResult;
264: } else if (!asm.isConnected()) {
265: writer.println("Error: Not connected to the server");
266: return ERROR_UNAVAILABLE;
267: }
268:
269: PortletApplication pa = registry.getPortletApplication(paName);
270: try {
271: ApplicationServerManagerResult result = asm.undeploy(pa
272: .getWebApplicationDefinition().getContextRoot());
273: if (result.isOk()) {
274: writer.println("Portlet Application " + paName
275: + " undeployed");
276: writer.println(result.getResponse());
277: return OK;
278: } else {
279: writer.println("Error: Portlet Application " + paName
280: + " could not be undeployed");
281: writer.println(result.getResponse());
282: return ERROR_SERVER;
283: }
284: } catch (Exception e) {
285: writer
286: .println("Error: Failed to undeploy Portlet Application "
287: + paName + ": " + e.getMessage());
288: e.printStackTrace(writer);
289: return ERROR_UNEXPECTED;
290: }
291: }
292:
293: protected int unregister(PrintWriter writer, String paName) {
294: int result = stop(writer, paName);
295:
296: if (result != OK) {
297: return result;
298: }
299:
300: PortletApplication pa = registry.getPortletApplication(paName);
301: try {
302: registry.removeApplication(pa);
303: writer.println("Portlet Application " + paName
304: + " unregistered");
305: return OK;
306: } catch (RegistryException e) {
307: writer
308: .println("Error: Failed to unregister Portlet Application "
309: + paName + ": " + e.getMessage());
310: e.printStackTrace(writer);
311: return ERROR_UNEXPECTED;
312: }
313: }
314:
315: protected int deploy(PrintWriter writer, HttpServletRequest request) {
316: if (!FileUpload.isMultipartContent(request)) {
317: writer.println("Error: No file multipart content provided");
318: return ERROR_NO_DATA;
319: }
320: File tempDir = null;
321: File tempFile = null;
322:
323: try {
324: DiskFileUpload upload = new DiskFileUpload();
325: tempDir = File.createTempFile("upload", null);
326: tempDir.deleteOnExit();
327: tempDir.delete();
328: tempDir.mkdirs();
329: tempDir.deleteOnExit();
330: List items = upload.parseRequest(request, 0, -1L, tempDir
331: .getAbsolutePath());
332: Iterator iter = items.iterator();
333: while (iter.hasNext()) {
334: FileItem item = (FileItem) iter.next();
335: if (!item.isFormField()) {
336: String fileName = item.getName();
337: tempFile = new File(tempDir, fileName);
338: tempFile.deleteOnExit();
339: item.write(tempFile);
340:
341: try {
342: DeploymentStatus status = dm.deploy(tempFile);
343: if (status.getStatus() == DeploymentStatus.STATUS_OKAY) {
344: writer.println("Deployed " + fileName);
345: return OK;
346: } else if (status.getStatus() == DeploymentStatus.STATUS_EVAL) {
347: writer.println("Error: Unrecognized file "
348: + fileName);
349: return ERROR_IGNORED;
350: } else {
351: writer
352: .println("Error: Failed to deploy file "
353: + fileName);
354: return ERROR_IGNORED;
355: }
356: } catch (Throwable e) {
357: writer.println("Error: Failed to deploy file "
358: + fileName + ": " + e.getMessage());
359: e.printStackTrace(writer);
360: return ERROR_UNEXPECTED;
361: }
362: }
363: }
364:
365: } catch (Throwable e) {
366: writer.println("Error: Failed to process uploaded data: "
367: + e.getMessage());
368: e.printStackTrace(writer);
369: return ERROR_UNEXPECTED;
370: } finally {
371: if (tempFile != null) {
372: tempFile.delete();
373: }
374: if (tempDir != null) {
375: tempDir.delete();
376: }
377: }
378: return OK;
379: }
380: }
|