KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > ide > VAJRemoteUtil


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 JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.net.HttpURLConnection JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLEncoder JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Vector JavaDoc;
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 JavaDoc remoteServer;
44
45     public VAJRemoteUtil(Task caller, String JavaDoc remote) {
46         this.caller = caller;
47         this.remoteServer = remote;
48     }
49
50     /**
51      * export the array of Packages
52      */

53     public void exportPackages(File JavaDoc destDir,
54                                String JavaDoc[] includePatterns, String JavaDoc[] excludePatterns,
55                                boolean exportClasses, boolean exportDebugInfo,
56                                boolean exportResources, boolean exportSources,
57                                boolean useDefaultExcludes, boolean overwrite) {
58         try {
59             String JavaDoc request = "http://" + remoteServer + "/servlet/vajexport";
60             Vector JavaDoc parameters = new Vector JavaDoc();
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 JavaDoc ex) {
69             throw new BuildException(ex);
70         }
71     }
72
73     /**
74      * Do the import.
75      */

76     public void importFiles(
77                             String JavaDoc importProject, File JavaDoc srcDir,
78                             String JavaDoc[] includePatterns, String JavaDoc[] excludePatterns,
79                             boolean importClasses, boolean importResources,
80                             boolean importSources, boolean useDefaultExcludes) {
81         try {
82             String JavaDoc request = "http://" + remoteServer + "/servlet/vajimport";
83             Vector JavaDoc parameters = new Vector JavaDoc();
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 JavaDoc 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 JavaDoc parameters,
102                                             File JavaDoc dir,
103                                             String JavaDoc[] includePatterns, String JavaDoc[] 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 JavaDoc projectDescriptions) {
130         try {
131             String JavaDoc request = "http://" + remoteServer + "/servlet/vajload";
132             Vector JavaDoc parameters = new Vector JavaDoc();
133             for (Enumeration JavaDoc 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 JavaDoc ex) {
142             throw new BuildException(ex);
143         }
144     }
145
146     /**
147      * logs a message.
148      */

149     public void log(String JavaDoc msg, int level) {
150         caller.log(msg, level);
151     }
152
153     
154     private class URLParam {
155         private String JavaDoc name;
156         private String JavaDoc value;
157         public URLParam(String JavaDoc name, String JavaDoc value) {
158             this.name = name;
159             this.value = value;
160         }
161         public URLParam(String JavaDoc name, boolean value) {
162             this.name = name;
163             this.value = (new Boolean JavaDoc(value)).toString();
164         }
165         public void setValue(String JavaDoc value) { this.value = value; }
166         public void setName(String JavaDoc name) { this.name = name; }
167         public String JavaDoc getName() { return name; }
168         public String JavaDoc 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 JavaDoc request, Vector JavaDoc parameters) {
182         boolean requestFailed = false;
183         
184         // Build request & URL encode parameters
185
String JavaDoc 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 JavaDoc requestUrl = new URL JavaDoc(url);
198             HttpURLConnection JavaDoc connection =
199                 (HttpURLConnection JavaDoc) requestUrl.openConnection();
200
201             InputStream JavaDoc 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 JavaDoc 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 JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
218             String JavaDoc 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 JavaDoc 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 JavaDoc 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