Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 223   Methods: 9
NCLOC: 103   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
VAJToolsServlet.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.optional.ide;
 56   
 
 57   
 
 58   
 
 59   
 import java.io.IOException;
 60   
 import javax.servlet.ServletException;
 61   
 import javax.servlet.http.HttpServlet;
 62   
 import javax.servlet.http.HttpServletRequest;
 63   
 import javax.servlet.http.HttpServletResponse;
 64   
 import org.apache.tools.ant.BuildException;
 65   
 import org.apache.tools.ant.util.StringUtils;
 66   
 
 67   
 /**
 68   
  * Abstract base class to provide common services for the
 69   
  * VAJ tool API servlets
 70   
  *
 71   
  * @author Wolf Siberski, based on servlets written by Glenn McAllister
 72   
  */
 73   
 public abstract class VAJToolsServlet extends HttpServlet {
 74   
     /**
 75   
      * Adaptation of VAJUtil for servlet context.
 76   
      */
 77   
     class VAJLocalServletUtil extends VAJLocalUtil {
 78  0
         public void log(String msg, int level) {
 79  0
             try {
 80  0
                 if (msg != null) {
 81  0
                     msg = msg.replace('\r', ' ');
 82  0
                     int i = 0;
 83  0
                     while (i < msg.length()) {
 84  0
                         int nlPos = msg.indexOf('\n', i);
 85  0
                         if (nlPos == -1) {
 86  0
                             nlPos = msg.length();
 87   
                         }
 88  0
                         response.getWriter().println(Integer.toString(level)
 89   
                                                      + " " + msg.substring(i, nlPos));
 90  0
                         i = nlPos + 1;
 91   
                     }
 92   
                 }
 93   
             } catch (IOException e) {
 94  0
                 throw new BuildException("logging failed. msg was: "
 95   
                                          + e.getMessage());
 96   
             }
 97   
         }
 98   
     }
 99   
 
 100   
     // constants for servlet param names
 101   
     public static final String DIR_PARAM = "dir";
 102   
     public static final String INCLUDE_PARAM = "include";
 103   
     public static final String EXCLUDE_PARAM = "exclude";
 104   
     public static final String CLASSES_PARAM = "cls";
 105   
     public static final String SOURCES_PARAM = "src";
 106   
     public static final String RESOURCES_PARAM = "res";
 107   
     public static final String DEFAULT_EXCLUDES_PARAM = "dex";
 108   
     public static final String PROJECT_NAME_PARAM = "project";
 109   
 
 110   
 
 111   
     // current request
 112   
     HttpServletRequest  request;
 113   
 
 114   
     // response to current request
 115   
     HttpServletResponse response;
 116   
 
 117   
     // implementation of VAJUtil used by the servlet
 118   
     VAJUtil util;
 119   
 
 120   
 
 121   
     /**
 122   
      * Execute the request by calling the appropriate
 123   
      * VAJ tool API methods. This method must be implemented
 124   
      * by the concrete servlets
 125   
      */
 126   
     protected abstract void executeRequest();
 127   
 
 128   
     /**
 129   
      * Respond to a HTTP request. This method initializes
 130   
      * the servlet and handles errors.
 131   
      * The real work is done in the abstract method executeRequest()
 132   
      */
 133  0
     public void doGet(HttpServletRequest req, HttpServletResponse res)
 134   
         throws ServletException, IOException {
 135  0
         try {
 136  0
             response = res;
 137  0
             request = req;
 138  0
             initRequest();
 139  0
             executeRequest();
 140   
         } catch (BuildException e) {
 141  0
             util.log("Error occured: " + e.getMessage(), VAJUtil.MSG_ERR);
 142   
         } catch (Exception e) {
 143  0
             try {
 144  0
                 if (!(e instanceof BuildException)) {
 145  0
                     String trace = StringUtils.getStackTrace(e);
 146  0
                     util.log("Program error in " + this.getClass().getName()
 147   
                              + ":\n" + trace, VAJUtil.MSG_ERR);
 148   
                 }
 149   
             } catch (Throwable t) {
 150  0
                 t.printStackTrace();
 151   
             } finally {
 152  0
                 if (!(e instanceof BuildException)) {
 153  0
                     throw new ServletException(e.getMessage());
 154   
                 }
 155   
             }
 156   
         }
 157   
     }
 158   
 
 159   
     /**
 160   
      * initialize the servlet.
 161   
      */
 162  0
     protected void initRequest() throws IOException {
 163  0
         response.setContentType("text/ascii");
 164  0
         if (util == null) {
 165  0
             util = new VAJLocalServletUtil();
 166   
         }
 167   
     }
 168   
 
 169   
     /**
 170   
      * Get the VAJUtil implementation
 171   
      */
 172  0
     VAJUtil getUtil() {
 173  0
         return util;
 174   
     }
 175   
 
 176   
     /**
 177   
      * Get the boolean value of a parameter.
 178   
      */
 179  0
     protected boolean getBooleanParam(String param) {
 180  0
         return getBooleanParam(param, false);
 181   
     }
 182   
 
 183   
     /**
 184   
      * Get the boolean value of a parameter, with a default value if
 185   
      * the parameter hasn't been passed to the servlet.
 186   
      */
 187  0
     protected boolean getBooleanParam(String param, boolean defaultValue) {
 188  0
         String value = getFirstParamValueString(param);
 189  0
         if (value != null) {
 190  0
             return toBoolean(value);
 191   
         } else {
 192  0
             return defaultValue;
 193   
         }
 194   
     }
 195   
 
 196   
     /**
 197   
      * Returns the first encountered value for a parameter.
 198   
      */
 199  0
     protected String getFirstParamValueString(String param) {
 200  0
         String[] paramValuesArray = request.getParameterValues(param);
 201  0
         if (paramValuesArray == null) {
 202  0
             return null;
 203   
         }
 204  0
         return paramValuesArray[0];
 205   
     }
 206   
 
 207   
     /**
 208   
      * Returns all values for a parameter.
 209   
      */
 210  0
     protected String[] getParamValues(String param) {
 211  0
         return request.getParameterValues(param);
 212   
     }
 213   
 
 214   
     /**
 215   
      * A utility method to translate the strings "yes", "true", and "ok"
 216   
      * to boolean true, and everything else to false.
 217   
      */
 218  0
     protected boolean toBoolean(String string) {
 219  0
         String lower = string.toLowerCase();
 220  0
         return (lower.equals("yes") || lower.equals("true") || lower.equals("ok"));
 221   
     }
 222   
 }
 223