1 | /*
|
---|
2 | * Copyright 2001-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
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 | /**
|
---|
34 | * Helper class for VAJ tasks. Holds Workspace singleton and
|
---|
35 | * wraps IvjExceptions into BuildExceptions
|
---|
36 | *
|
---|
37 | */
|
---|
38 | class VAJRemoteUtil implements VAJUtil {
|
---|
39 | // calling task
|
---|
40 | Task caller;
|
---|
41 |
|
---|
42 | // VAJ remote tool server
|
---|
43 | String remoteServer;
|
---|
44 |
|
---|
45 | public VAJRemoteUtil(Task caller, String remote) {
|
---|
46 | this.caller = caller;
|
---|
47 | this.remoteServer = remote;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * export the array of Packages
|
---|
52 | */
|
---|
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 | /**
|
---|
74 | * Do the import.
|
---|
75 | */
|
---|
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 | /**
|
---|
98 | * Add parameters common for import and export to vector
|
---|
99 | * Helper method to remove double code.
|
---|
100 | */
|
---|
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 | /**
|
---|
127 | * Load specified projects.
|
---|
128 | */
|
---|
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 | /**
|
---|
147 | * logs a message.
|
---|
148 | */
|
---|
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 | /**
|
---|
172 | * Sends a servlet request.
|
---|
173 | *
|
---|
174 | * The passed URL and parameter list are combined into a
|
---|
175 | * valid URL (with proper URL encoding for the parameters)
|
---|
176 | * and the URL is requested.
|
---|
177 | *
|
---|
178 | * @param request Request URL without trailing characters (no ?)
|
---|
179 | * @param parameters Vector of URLParam objects to append as parameters.
|
---|
180 | */
|
---|
181 | private void sendRequest(String request, Vector parameters) {
|
---|
182 | boolean requestFailed = false;
|
---|
183 |
|
---|
184 | // Build request & URL encode parameters
|
---|
185 | 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 | //must be HTTP connection
|
---|
197 | URL requestUrl = new URL(url);
|
---|
198 | HttpURLConnection connection =
|
---|
199 | (HttpURLConnection) requestUrl.openConnection();
|
---|
200 |
|
---|
201 | InputStream is = null;
|
---|
202 | // retry three times
|
---|
203 | for (int i = 0; i < 3; i++) {
|
---|
204 | try {
|
---|
205 | is = connection.getInputStream();
|
---|
206 | break;
|
---|
207 | } catch (IOException ex) {
|
---|
208 | // ignore
|
---|
209 | }
|
---|
210 | }
|
---|
211 | if (is == null) {
|
---|
212 | log("Can't get " + url, MSG_ERR);
|
---|
213 | throw new BuildException("Couldn't execute " + url);
|
---|
214 | }
|
---|
215 |
|
---|
216 | // log the response
|
---|
217 | BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
---|
218 | String line = br.readLine();
|
---|
219 | while (line != null) {
|
---|
220 | int level = MSG_ERR;
|
---|
221 | try {
|
---|
222 | // the first char of each line contains the log level
|
---|
223 | 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 | }
|
---|