View Javadoc

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  package org.apache.jetspeed.manager;
18  
19  import java.io.CharArrayWriter;
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.commons.fileupload.DiskFileUpload;
32  import org.apache.commons.fileupload.FileItem;
33  import org.apache.commons.fileupload.FileUpload;
34  import org.apache.jetspeed.Jetspeed;
35  import org.apache.jetspeed.components.portletregistry.PortletRegistry;
36  import org.apache.jetspeed.components.portletregistry.RegistryException;
37  import org.apache.jetspeed.deployment.DeploymentManager;
38  import org.apache.jetspeed.deployment.DeploymentStatus;
39  import org.apache.jetspeed.factory.PortletFactory;
40  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
41  import org.apache.jetspeed.om.common.portlet.PortletApplication;
42  import org.apache.jetspeed.tools.pamanager.servletcontainer.ApplicationServerManager;
43  import org.apache.jetspeed.tools.pamanager.servletcontainer.ApplicationServerManagerResult;
44  
45  /***
46   * ManagerServlet ala Tomcat ManagerServlet 
47   *
48   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
49   * @version $Id: ManagerServlet.java 517719 2007-03-13 15:05:48Z ate $
50   */
51  public class ManagerServlet extends HttpServlet
52  {
53      private static int               OK                    = 0;
54      private static int               ERROR_NO_DATA         = 1;
55      private static int               ERROR_UNKNOWN_COMMAND = 2;
56      private static int               ERROR_UNKNOWN_PA      = 3;
57      private static int               ERROR_INVALID         = 4;
58      private static int               ERROR_UNSUPPORTED     = 5;
59      private static int               ERROR_UNAVAILABLE     = 6;
60      private static int               ERROR_SERVER          = 7;
61      private static int               ERROR_UNEXPECTED      = 8;
62      private static int               ERROR_IGNORED         = 9;
63  
64      private ApplicationServerManager asm;
65      private PortletRegistry          registry;
66      private PortletFactory           portletFactory;
67      private DeploymentManager        dm;
68  
69      public void init() throws ServletException
70      {
71          super.init();
72          asm = (ApplicationServerManager) Jetspeed.getComponentManager().getComponent(ApplicationServerManager.class);
73          registry = (PortletRegistry) Jetspeed.getComponentManager().getComponent(PortletRegistry.class);
74          portletFactory = (PortletFactory) Jetspeed.getComponentManager().getComponent("portletFactory");
75          dm = (DeploymentManager) Jetspeed.getComponentManager().getComponent("deploymentManager");
76      }
77  
78      public void destroy()
79      {
80          super.destroy();
81      }
82  
83      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
84      {
85          process(request, response, false);
86      }
87  
88      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
89      {
90          process(request, response, true);
91      }
92  
93      protected void process(HttpServletRequest request, HttpServletResponse response, boolean posted)
94                      throws ServletException, IOException
95      {
96          // Prepare our output writer to generate the response message
97          response.setContentType("text/plain; charset=utf-8");
98          CharArrayWriter buffer = new CharArrayWriter();
99          PrintWriter writer = new PrintWriter(buffer);
100 
101         // Identify the request parameters that we need
102         String command = request.getPathInfo();
103         int result = OK;
104 
105         if (command == null)
106         {
107             result = OK;
108         }
109         else if (command.equals("/list"))
110         {
111             result = list(writer);
112         }
113         else if (command.equals("/start"))
114         {
115             result = start(writer, request.getParameter("pa"));
116         }
117         else if (command.equals("/stop"))
118         {
119             result = stop(writer, request.getParameter("pa"));
120         }
121         else if (command.equals("/undeploy"))
122         {
123             result = undeploy(writer, request.getParameter("pa"));
124         }
125         else if (command.equals("/unregister"))
126         {
127             result = unregister(writer, request.getParameter("pa"));
128         }
129         else if (command.equals("/deploy"))
130         {
131             if (posted)
132             {
133                 result = deploy(writer, request);
134             }
135             else
136             {
137                 writer.println("Error: /deploy is only available through POST");
138                 result = ERROR_INVALID;
139             }
140         }
141         else
142         {
143             writer.println("Error: Unknown command " + command);
144             result = ERROR_UNKNOWN_COMMAND;
145         }
146         writer.flush();
147         writer.close();
148         writer = response.getWriter();
149         if (result == OK)
150         {
151             writer.println("OK");
152         }
153         else
154         {
155             writer.println("FAIL - CODE: " + result);
156         }
157         writer.print(buffer.toString());
158         writer.flush();
159         writer.close();
160     }
161 
162     protected int list(PrintWriter writer)
163     {
164         writer.println("Listed Portlet Applications");
165         Iterator iter = registry.getPortletApplications().iterator();
166         PortletApplication pa;
167         while (iter.hasNext())
168         {
169             pa = (PortletApplication) iter.next();
170             writer.println(pa.getId() + ":" + pa.getName() + ":" + pa.getWebApplicationDefinition().getContextRoot()
171                            + ":" + (portletFactory.isPortletApplicationRegistered(pa) ? "ACTIVE" : "INACTIVE"));
172         }
173         return OK;
174     }
175 
176     protected int start(PrintWriter writer, String paName)
177     {
178         PortletApplication pa = null;
179         if (paName != null)
180         {
181             pa = registry.getPortletApplication(paName);
182         }
183         if (pa == null)
184         {
185             writer.println("Error: Unknown Portlet Application " + paName);
186             return ERROR_UNKNOWN_PA;
187         }
188         if (portletFactory.isPortletApplicationRegistered(pa))
189         {
190             writer.println("Warning: Portlet Application " + paName + " already started");
191             return OK;
192         }
193         else if (pa.getApplicationType() == MutablePortletApplication.LOCAL)
194         {
195             writer.println("Error: Starting LOCAL Portlet Application " + paName + " not supported");
196             return ERROR_UNSUPPORTED;
197         }
198         else if (!asm.isConnected())
199         {
200             writer.println("Error: Not connected to the server");
201             return ERROR_UNAVAILABLE;
202         }
203         else
204         {
205             try
206             {
207                 ApplicationServerManagerResult result = asm.start(pa.getWebApplicationDefinition().getContextRoot());
208                 if (result.isOk())
209                 {
210                     writer.println("Portlet Application " + paName + " started");
211                     writer.println(result.getResponse());
212                     return OK;
213                 }
214                 else
215                 {
216                     writer.println("Error: Portlet Application " + paName + " could not be started");
217                     writer.println(result.getResponse());
218                     return ERROR_SERVER;
219                 }
220             }
221             catch (Exception e)
222             {
223                 writer.println("Error: Failed to start Portlet Application " + paName + ": " + e.getMessage());
224                 e.printStackTrace(writer);
225                 return ERROR_UNEXPECTED;
226             }
227         }
228     }
229 
230     protected int stop(PrintWriter writer, String paName)
231     {
232         PortletApplication pa = null;
233         if (paName != null)
234         {
235             pa = registry.getPortletApplication(paName);
236         }
237         if (pa == null)
238         {
239             writer.println("Error: Unknown Portlet Application " + paName);
240             return ERROR_UNKNOWN_PA;
241         }
242         if (!portletFactory.isPortletApplicationRegistered(pa))
243         {
244             writer.println("Portlet Application " + paName + " already stopped");
245             return OK;
246         }
247         else if (pa.getApplicationType() == MutablePortletApplication.LOCAL)
248         {
249             writer.println("Error: Stopping LOCAL Portlet Application " + paName + " not supported");
250             return ERROR_UNSUPPORTED;
251         }
252         else if (!asm.isConnected())
253         {
254             writer.println("Error: Not connected to the server");
255             return ERROR_UNAVAILABLE;
256         }
257         else
258         {
259             try
260             {
261                 ApplicationServerManagerResult result = asm.stop(pa.getWebApplicationDefinition().getContextRoot());
262                 if (result.isOk())
263                 {
264                     writer.println("Portlet Application " + paName + " stopped");
265                     writer.println(result.getResponse());
266                     return OK;
267                 }
268                 else
269                 {
270                     writer.println("Error: Portlet Application " + paName + " could not be stopped");
271                     writer.println(result.getResponse());
272                     return ERROR_SERVER;
273                 }
274             }
275             catch (Exception e)
276             {
277                 writer.println("Error: Failed to stop Portlet Application " + paName + ": " + e.getMessage());
278                 e.printStackTrace(writer);
279                 return ERROR_UNEXPECTED;
280             }
281         }
282     }
283 
284     protected int undeploy(PrintWriter writer, String paName)
285     {
286         int stopResult = stop(writer, paName);
287         if (stopResult != OK)
288         {
289             return stopResult;
290         }
291         else if (!asm.isConnected())
292         {
293             writer.println("Error: Not connected to the server");
294             return ERROR_UNAVAILABLE;
295         }
296 
297         PortletApplication pa = registry.getPortletApplication(paName);
298         try
299         {
300             ApplicationServerManagerResult result = asm.undeploy(pa.getWebApplicationDefinition().getContextRoot());
301             if (result.isOk())
302             {
303                 writer.println("Portlet Application " + paName + " undeployed");
304                 writer.println(result.getResponse());
305                 return OK;
306             }
307             else
308             {
309                 writer.println("Error: Portlet Application " + paName + " could not be undeployed");
310                 writer.println(result.getResponse());
311                 return ERROR_SERVER;
312             }
313         }
314         catch (Exception e)
315         {
316             writer.println("Error: Failed to undeploy Portlet Application " + paName + ": " + e.getMessage());
317             e.printStackTrace(writer);
318             return ERROR_UNEXPECTED;
319         }
320     }
321 
322     protected int unregister(PrintWriter writer, String paName)
323     {
324         int result = stop(writer, paName);
325 
326         if (result != OK)
327         {
328             return result;
329         }
330 
331         PortletApplication pa = registry.getPortletApplication(paName);
332         try
333         {
334             registry.removeApplication(pa);
335             writer.println("Portlet Application " + paName + " unregistered");
336             return OK;
337         }
338         catch (RegistryException e)
339         {
340             writer.println("Error: Failed to unregister Portlet Application " + paName + ": " + e.getMessage());
341             e.printStackTrace(writer);
342             return ERROR_UNEXPECTED;
343         }
344     }
345 
346     protected int deploy(PrintWriter writer, HttpServletRequest request)
347     {
348         if (  !FileUpload.isMultipartContent(request) )
349         {
350             writer.println("Error: No file multipart content provided");
351             return ERROR_NO_DATA;
352         }
353         File tempDir = null;
354         File tempFile = null;
355 
356         try
357         {
358             DiskFileUpload upload = new DiskFileUpload();
359             tempDir = File.createTempFile("upload", null);
360             tempDir.deleteOnExit();
361             tempDir.delete();
362             tempDir.mkdirs();
363             tempDir.deleteOnExit();
364             List items = upload.parseRequest(request,0,-1L,tempDir.getAbsolutePath());
365             Iterator iter = items.iterator();
366             while ( iter.hasNext() )
367             {
368                 FileItem item = (FileItem)iter.next();
369                 if (!item.isFormField())
370                 {
371                     String fileName = item.getName();
372                     tempFile = new File(tempDir, fileName );
373                     tempFile.deleteOnExit();
374                     item.write(tempFile);
375 
376                     try
377                     {
378                         DeploymentStatus status = dm.deploy(tempFile);
379                         if ( status.getStatus() == DeploymentStatus.STATUS_OKAY )
380                         {
381                             writer.println("Deployed " + fileName);
382                             return OK;
383                         }
384                         else if ( status.getStatus() == DeploymentStatus.STATUS_EVAL )
385                         {
386                             writer.println("Error: Unrecognized file "+ fileName);
387                             return ERROR_IGNORED;
388                         }
389                         else
390                         {
391                             writer.println("Error: Failed to deploy file "+ fileName);
392                             return ERROR_IGNORED;
393                         }                    
394                     }
395                     catch (Throwable e)
396                     {
397                         writer.println("Error: Failed to deploy file " + fileName + ": " + e.getMessage());
398                         e.printStackTrace(writer);
399                         return ERROR_UNEXPECTED;
400                     }
401                 }
402             }
403 
404         }
405         catch (Throwable e)
406         {
407             writer.println("Error: Failed to process uploaded data: "+e.getMessage());
408             e.printStackTrace(writer);
409             return ERROR_UNEXPECTED;
410         }
411         finally
412         {
413             if (tempFile != null)
414             {
415                 tempFile.delete();
416             }
417             if (tempDir != null)
418             {
419                 tempDir.delete();
420             }
421         }
422         return OK;
423     }
424 }