1 17 18 package org.apache.tools.ant.taskdefs.optional.ide; 19 20 import java.io.BufferedReader ; 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.net.HttpURLConnection ; 26 import java.net.URL ; 27 import java.net.URLEncoder ; 28 import java.util.Enumeration ; 29 import java.util.Vector ; 30 import org.apache.tools.ant.BuildException; 31 import org.apache.tools.ant.Task; 32 33 38 class VAJRemoteUtil implements VAJUtil { 39 Task caller; 41 42 String remoteServer; 44 45 public VAJRemoteUtil(Task caller, String remote) { 46 this.caller = caller; 47 this.remoteServer = remote; 48 } 49 50 53 public void exportPackages(File destDir, 54 String [] includePatterns, String [] excludePatterns, 55 boolean exportClasses, boolean exportDebugInfo, 56 boolean exportResources, boolean exportSources, 57 boolean useDefaultExcludes, boolean overwrite) { 58 try { 59 String request = "http://" + remoteServer + "/servlet/vajexport"; 60 Vector parameters = new Vector (); 61 parameters.addElement(new URLParam(VAJExportServlet.WITH_DEBUG_INFO, exportDebugInfo)); 62 parameters.addElement(new URLParam(VAJExportServlet.OVERWRITE_PARAM, overwrite)); 63 assembleImportExportParams(parameters, destDir, 64 includePatterns, excludePatterns, 65 exportClasses, exportResources, 66 exportSources, useDefaultExcludes); 67 sendRequest(request, parameters); 68 } catch (Exception ex) { 69 throw new BuildException(ex); 70 } 71 } 72 73 76 public void importFiles( 77 String importProject, File srcDir, 78 String [] includePatterns, String [] excludePatterns, 79 boolean importClasses, boolean importResources, 80 boolean importSources, boolean useDefaultExcludes) { 81 try { 82 String request = "http://" + remoteServer + "/servlet/vajimport"; 83 Vector parameters = new Vector (); 84 parameters.addElement(new 85 URLParam(VAJImportServlet.PROJECT_NAME_PARAM, importProject)); 86 assembleImportExportParams(parameters, srcDir, 87 includePatterns, excludePatterns, 88 importClasses, importResources, 89 importSources, useDefaultExcludes); 90 sendRequest(request, parameters); 91 } catch (Exception ex) { 92 throw new BuildException(ex); 93 } 94 95 } 96 97 101 private void assembleImportExportParams(Vector parameters, 102 File dir, 103 String [] includePatterns, String [] excludePatterns, 104 boolean includeClasses, boolean includeResources, 105 boolean includeSources, boolean useDefaultExcludes) { 106 parameters.addElement(new URLParam(VAJToolsServlet.DIR_PARAM, dir.getPath())); 107 parameters.addElement(new URLParam(VAJToolsServlet.CLASSES_PARAM, includeClasses)); 108 parameters.addElement(new URLParam(VAJToolsServlet.RESOURCES_PARAM, includeResources)); 109 parameters.addElement(new URLParam(VAJToolsServlet.SOURCES_PARAM, includeSources)); 110 parameters.addElement(new URLParam(VAJToolsServlet.DEFAULT_EXCLUDES_PARAM, useDefaultExcludes)); 111 112 if (includePatterns != null) { 113 for (int i = 0; i < includePatterns.length; i++) { 114 parameters.addElement(new 115 URLParam(VAJExportServlet.INCLUDE_PARAM, includePatterns[i])); 116 } 117 } 118 if (excludePatterns != null) { 119 for (int i = 0; i < excludePatterns.length; i++) { 120 parameters.addElement(new 121 URLParam(VAJExportServlet.EXCLUDE_PARAM, excludePatterns[i])); 122 } 123 } 124 } 125 126 129 public void loadProjects(Vector projectDescriptions) { 130 try { 131 String request = "http://" + remoteServer + "/servlet/vajload"; 132 Vector parameters = new Vector (); 133 for (Enumeration e = projectDescriptions.elements(); e.hasMoreElements();) { 134 VAJProjectDescription pd = (VAJProjectDescription) e.nextElement(); 135 parameters.addElement(new 136 URLParam(VAJLoadServlet.PROJECT_NAME_PARAM, pd.getName())); 137 parameters.addElement(new 138 URLParam(VAJLoadServlet.VERSION_PARAM, pd.getVersion())); 139 } 140 sendRequest(request, parameters); 141 } catch (Exception ex) { 142 throw new BuildException(ex); 143 } 144 } 145 146 149 public void log(String msg, int level) { 150 caller.log(msg, level); 151 } 152 153 154 private class URLParam { 155 private String name; 156 private String value; 157 public URLParam(String name, String value) { 158 this.name = name; 159 this.value = value; 160 } 161 public URLParam(String name, boolean value) { 162 this.name = name; 163 this.value = (new Boolean (value)).toString(); 164 } 165 public void setValue(String value) { this.value = value; } 166 public void setName(String name) { this.name = name; } 167 public String getName() { return name; } 168 public String getValue() { return value; } 169 } 170 171 181 private void sendRequest(String request, Vector parameters) { 182 boolean requestFailed = false; 183 184 String url = request + "?"; 186 for (int i=0; i<parameters.size(); i++) { 187 URLParam p = (URLParam)parameters.elementAt(i); 188 url += p.getName() + "=" + URLEncoder.encode(p.getValue()); 189 url += (i==parameters.size()-1)?"":"&"; 190 } 191 192 193 try { 194 log("Request: " + url, MSG_DEBUG); 195 196 URL requestUrl = new URL (url); 198 HttpURLConnection connection = 199 (HttpURLConnection ) requestUrl.openConnection(); 200 201 InputStream is = null; 202 for (int i = 0; i < 3; i++) { 204 try { 205 is = connection.getInputStream(); 206 break; 207 } catch (IOException ex) { 208 } 210 } 211 if (is == null) { 212 log("Can't get " + url, MSG_ERR); 213 throw new BuildException("Couldn't execute " + url); 214 } 215 216 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 218 String line = br.readLine(); 219 while (line != null) { 220 int level = MSG_ERR; 221 try { 222 level = Integer.parseInt(line.substring(0, 1)); 224 if (level == MSG_ERR) { 225 requestFailed = true; 226 } 227 } catch (Exception e) { 228 log("Response line doesn't contain log level!", MSG_ERR); 229 } 230 log(line.substring(2), level); 231 line = br.readLine(); 232 } 233 234 } catch (IOException ex) { 235 log("Error sending tool request to VAJ" + ex, MSG_ERR); 236 throw new BuildException("Couldn't execute " + url); 237 } 238 if (requestFailed) { 239 throw new BuildException("VAJ tool request failed"); 240 } 241 } 242 } 243 | Popular Tags |