Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 248   Methods: 7
NCLOC: 144   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
VAJRemoteUtil.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   
 import java.io.BufferedReader;
 58   
 import java.io.File;
 59   
 import java.io.IOException;
 60   
 import java.io.InputStream;
 61   
 import java.io.InputStreamReader;
 62   
 import java.net.HttpURLConnection;
 63   
 import java.net.URL;
 64   
 import java.util.Enumeration;
 65   
 import java.util.Vector;
 66   
 import org.apache.tools.ant.BuildException;
 67   
 import org.apache.tools.ant.Task;
 68   
 
 69   
 /**
 70   
  * Helper class for VAJ tasks. Holds Workspace singleton and
 71   
  * wraps IvjExceptions into BuildExceptions
 72   
  *
 73   
  * @author Wolf Siberski, TUI Infotec GmbH
 74   
  */
 75   
 class VAJRemoteUtil implements VAJUtil{
 76   
     // calling task
 77   
     Task caller;
 78   
 
 79   
     // VAJ remote tool server
 80   
     String remoteServer;
 81   
 
 82  0
     public VAJRemoteUtil(Task caller, String remote) {
 83  0
         this.caller = caller;
 84  0
         this.remoteServer = remote;
 85   
     }
 86   
 
 87   
     /**
 88   
      * export the array of Packages
 89   
      */
 90  0
     public void exportPackages(File destDir,
 91   
                                String[] includePatterns, String[] excludePatterns,
 92   
                                boolean exportClasses, boolean exportDebugInfo, boolean exportResources,
 93   
                                boolean exportSources, boolean useDefaultExcludes, boolean overwrite) {
 94  0
         try {
 95  0
             String request = "http://" + remoteServer + "/servlet/vajexport?"
 96   
                 + VAJExportServlet.WITH_DEBUG_INFO + "=" + exportDebugInfo + "&"
 97   
                 + VAJExportServlet.OVERWRITE_PARAM + "=" + overwrite + "&"
 98   
                 + assembleImportExportParams(destDir,
 99   
                                               includePatterns, excludePatterns,
 100   
                                               exportClasses, exportResources,
 101   
                                               exportSources, useDefaultExcludes);
 102  0
             sendRequest(request);
 103   
         } catch (Exception ex) {
 104  0
             throw new BuildException(ex);
 105   
         }
 106   
     }
 107   
 
 108   
     /**
 109   
      * Do the import.
 110   
      */
 111  0
     public void importFiles(
 112   
                             String importProject, File srcDir,
 113   
                             String[] includePatterns, String[] excludePatterns,
 114   
                             boolean importClasses, boolean importResources,
 115   
                             boolean importSources, boolean useDefaultExcludes) {
 116  0
         try {
 117  0
             String request = "http://" + remoteServer + "/servlet/vajimport?"
 118   
                 + VAJImportServlet.PROJECT_NAME_PARAM + "="
 119   
                 + importProject + "&"
 120   
                 + assembleImportExportParams(srcDir,
 121   
                                               includePatterns, excludePatterns,
 122   
                                               importClasses, importResources,
 123   
                                               importSources, useDefaultExcludes);
 124  0
             sendRequest(request);
 125   
         } catch (Exception ex) {
 126  0
             throw new BuildException(ex);
 127   
         }
 128   
 
 129   
     }
 130   
 
 131   
     /**
 132   
      * Assemble string for parameters common for import and export
 133   
      * Helper method to remove double code.
 134   
      */
 135  0
     private String assembleImportExportParams(
 136   
                                               File dir,
 137   
                                               String[] includePatterns, String[] excludePatterns,
 138   
                                               boolean includeClasses, boolean includeResources,
 139   
                                               boolean includeSources, boolean useDefaultExcludes) {
 140  0
         String result =
 141   
             VAJToolsServlet.DIR_PARAM + "="
 142   
             + dir.getAbsolutePath().replace('\\', '/') + "&"
 143   
             + VAJToolsServlet.CLASSES_PARAM + "=" + includeClasses + "&"
 144   
             + VAJToolsServlet.RESOURCES_PARAM + "=" + includeResources + "&"
 145   
             + VAJToolsServlet.SOURCES_PARAM + "=" + includeSources + "&"
 146   
             + VAJToolsServlet.DEFAULT_EXCLUDES_PARAM + "=" + useDefaultExcludes;
 147   
 
 148  0
         if (includePatterns != null) {
 149  0
             for (int i = 0; i < includePatterns.length; i++){
 150  0
                 result = result + "&" + VAJExportServlet.INCLUDE_PARAM + "="
 151   
                     + includePatterns[i].replace(' ', '+').replace('\\', '/');
 152   
             }
 153   
         }
 154  0
         if (excludePatterns != null) {
 155  0
             for (int i = 0; i < excludePatterns.length; i++){
 156  0
                 result = result + "&" + VAJExportServlet.EXCLUDE_PARAM + "="
 157   
                     + excludePatterns[i].replace(' ', '+').replace('\\', '/');
 158   
             }
 159   
         }
 160   
 
 161  0
         return result;
 162   
     }
 163   
 
 164   
     /**
 165   
      * Load specified projects.
 166   
      */
 167  0
     public void loadProjects(Vector projectDescriptions) {
 168  0
         try {
 169  0
             String request = "http://" + remoteServer + "/servlet/vajload?";
 170  0
             String delimiter = "";
 171  0
             for (Enumeration e = projectDescriptions.elements(); e.hasMoreElements();) {
 172  0
                 VAJProjectDescription pd = (VAJProjectDescription) e.nextElement();
 173  0
                 request = request
 174   
                     + delimiter + VAJLoadServlet.PROJECT_NAME_PARAM
 175   
                     + "=" + pd.getName().replace(' ', '+')
 176   
                     + "&" + VAJLoadServlet.VERSION_PARAM
 177   
                     + "=" + pd.getVersion().replace(' ', '+');
 178   
                 //the first param needs no delimiter, but all other
 179  0
                 delimiter = "&";
 180   
             }
 181  0
             sendRequest(request);
 182   
         } catch (Exception ex) {
 183  0
             throw new BuildException(ex);
 184   
         }
 185   
     }
 186   
 
 187   
     /**
 188   
      * logs a message.
 189   
      */
 190  0
     public void log(String msg, int level) {
 191  0
         caller.log(msg, level);
 192   
     }
 193   
 
 194   
     /**
 195   
      * Sends a servlet request.
 196   
      */
 197  0
     private void sendRequest(String request) {
 198  0
         boolean requestFailed = false;
 199  0
         try {
 200  0
             log("Request: " + request, MSG_DEBUG);
 201   
 
 202   
             //must be HTTP connection
 203  0
             URL requestUrl = new URL(request);
 204  0
             HttpURLConnection connection =
 205   
                 (HttpURLConnection) requestUrl.openConnection();
 206   
 
 207  0
             InputStream is = null;
 208   
             // retry three times
 209  0
             for (int i = 0; i < 3; i++) {
 210  0
                 try {
 211  0
                     is = connection.getInputStream();
 212  0
                     break;
 213   
                 } catch (IOException ex) {
 214   
                 }
 215   
             }
 216  0
             if (is == null) {
 217  0
                 log("Can't get " + request, MSG_ERR);
 218  0
                 throw new BuildException("Couldn't execute " + request);
 219   
             }
 220   
 
 221   
             // log the response
 222  0
             BufferedReader br = new BufferedReader(new InputStreamReader(is));
 223  0
             String line = br.readLine();
 224  0
             while (line != null) {
 225  0
                 int level = MSG_ERR;
 226  0
                 try {
 227   
                     // the first char of each line contains the log level
 228  0
                     level = Integer.parseInt(line.substring(0, 1));
 229  0
                     if (level == MSG_ERR) {
 230  0
                         requestFailed = true;
 231   
                     }
 232   
                 } catch (Exception e) {
 233  0
                     log("Response line doesn't contain log level!", MSG_ERR);
 234   
                 }
 235  0
                 log(line.substring(2), level);
 236  0
                 line = br.readLine();
 237   
             }
 238   
 
 239   
         } catch (IOException ex) {
 240  0
             log("Error sending tool request to VAJ" + ex, MSG_ERR);
 241  0
             throw new BuildException("Couldn't execute " + request);
 242   
         }
 243  0
         if (requestFailed) {
 244  0
             throw new BuildException("VAJ tool request failed");
 245   
         }
 246   
     }
 247   
 }
 248