Coverage report

  %line %branch
org.apache.jetspeed.manager.ManagerServlet
0% 
0% 

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

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.