KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > ant > AbstractCatalinaTask


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.ant;
20
21
22 import java.io.BufferedOutputStream 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.URLConnection JavaDoc;
28 import org.apache.catalina.util.Base64;
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.Project;
31
32
33 /**
34  * Abstract base class for Ant tasks that interact with the
35  * <em>Manager</em> web application for dynamically deploying and
36  * undeploying applications. These tasks require Ant 1.4 or later.
37  *
38  * @author Craig R. McClanahan
39  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
40  * @since 4.1
41  */

42
43 public abstract class AbstractCatalinaTask extends BaseRedirectorHelperTask {
44
45
46     // ----------------------------------------------------- Instance Variables
47

48
49     /**
50      * manager webapp's encoding.
51      */

52     private static String JavaDoc CHARSET = "utf-8";
53
54
55     // ------------------------------------------------------------- Properties
56

57
58     /**
59      * The charset used during URL encoding.
60      */

61     protected String JavaDoc charset = "ISO-8859-1";
62
63     public String JavaDoc getCharset() {
64         return (this.charset);
65     }
66
67     public void setCharset(String JavaDoc charset) {
68         this.charset = charset;
69     }
70
71
72     /**
73      * The login password for the <code>Manager</code> application.
74      */

75     protected String JavaDoc password = null;
76
77     public String JavaDoc getPassword() {
78         return (this.password);
79     }
80
81     public void setPassword(String JavaDoc password) {
82         this.password = password;
83     }
84
85
86     /**
87      * The URL of the <code>Manager</code> application to be used.
88      */

89     protected String JavaDoc url = "http://localhost:8080/manager";
90
91     public String JavaDoc getUrl() {
92         return (this.url);
93     }
94
95     public void setUrl(String JavaDoc url) {
96         this.url = url;
97     }
98
99
100     /**
101      * The login username for the <code>Manager</code> application.
102      */

103     protected String JavaDoc username = null;
104
105     public String JavaDoc getUsername() {
106         return (this.username);
107     }
108
109     public void setUsername(String JavaDoc username) {
110         this.username = username;
111     }
112
113
114     // --------------------------------------------------------- Public Methods
115

116
117     /**
118      * Execute the specified command. This logic only performs the common
119      * attribute validation required by all subclasses; it does not perform
120      * any functional logic directly.
121      *
122      * @exception BuildException if a validation error occurs
123      */

124     public void execute() throws BuildException {
125
126         if ((username == null) || (password == null) || (url == null)) {
127             throw new BuildException
128                 ("Must specify all of 'username', 'password', and 'url'");
129         }
130
131     }
132
133
134     // ------------------------------------------------------ Protected Methods
135

136
137     /**
138      * Execute the specified command, based on the configured properties.
139      *
140      * @param command Command to be executed
141      *
142      * @exception BuildException if an error occurs
143      */

144     public void execute(String JavaDoc command) throws BuildException {
145
146         execute(command, null, null, -1);
147
148     }
149
150
151     /**
152      * Execute the specified command, based on the configured properties.
153      * The input stream will be closed upon completion of this task, whether
154      * it was executed successfully or not.
155      *
156      * @param command Command to be executed
157      * @param istream InputStream to include in an HTTP PUT, if any
158      * @param contentType Content type to specify for the input, if any
159      * @param contentLength Content length to specify for the input, if any
160      *
161      * @exception BuildException if an error occurs
162      */

163     public void execute(String JavaDoc command, InputStream JavaDoc istream,
164                         String JavaDoc contentType, int contentLength)
165         throws BuildException {
166
167         URLConnection JavaDoc conn = null;
168         InputStreamReader JavaDoc reader = null;
169         try {
170
171             // Create a connection for this command
172
conn = (new URL JavaDoc(url + command)).openConnection();
173             HttpURLConnection JavaDoc hconn = (HttpURLConnection JavaDoc) conn;
174
175             // Set up standard connection characteristics
176
hconn.setAllowUserInteraction(false);
177             hconn.setDoInput(true);
178             hconn.setUseCaches(false);
179             if (istream != null) {
180                 hconn.setDoOutput(true);
181                 hconn.setRequestMethod("PUT");
182                 if (contentType != null) {
183                     hconn.setRequestProperty("Content-Type", contentType);
184                 }
185                 if (contentLength >= 0) {
186                     hconn.setRequestProperty("Content-Length",
187                                              "" + contentLength);
188                 }
189             } else {
190                 hconn.setDoOutput(false);
191                 hconn.setRequestMethod("GET");
192             }
193             hconn.setRequestProperty("User-Agent",
194                                      "Catalina-Ant-Task/1.0");
195
196             // Set up an authorization header with our credentials
197
String JavaDoc input = username + ":" + password;
198             String JavaDoc output = new String JavaDoc(Base64.encode(input.getBytes()));
199             hconn.setRequestProperty("Authorization",
200                                      "Basic " + output);
201
202             // Establish the connection with the server
203
hconn.connect();
204
205             // Send the request data (if any)
206
if (istream != null) {
207                 BufferedOutputStream JavaDoc ostream =
208                     new BufferedOutputStream JavaDoc(hconn.getOutputStream(), 1024);
209                 byte buffer[] = new byte[1024];
210                 while (true) {
211                     int n = istream.read(buffer);
212                     if (n < 0) {
213                         break;
214                     }
215                     ostream.write(buffer, 0, n);
216                 }
217                 ostream.flush();
218                 ostream.close();
219                 istream.close();
220             }
221
222             // Process the response message
223
reader = new InputStreamReader JavaDoc(hconn.getInputStream(), CHARSET);
224             StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
225             String JavaDoc error = null;
226             int msgPriority = Project.MSG_INFO;
227             boolean first = true;
228             while (true) {
229                 int ch = reader.read();
230                 if (ch < 0) {
231                     break;
232                 } else if ((ch == '\r') || (ch == '\n')) {
233                     // in Win \r\n would cause handleOutput() to be called
234
// twice, the second time with an empty string,
235
// producing blank lines
236
if (buff.length() > 0) {
237                         String JavaDoc line = buff.toString();
238                         buff.setLength(0);
239                         if (first) {
240                             if (!line.startsWith("OK -")) {
241                                 error = line;
242                                 msgPriority = Project.MSG_ERR;
243                             }
244                             first = false;
245                         }
246                         handleOutput(line, msgPriority);
247                     }
248                 } else {
249                     buff.append((char) ch);
250                 }
251             }
252             if (buff.length() > 0) {
253                 handleOutput(buff.toString(), msgPriority);
254             }
255             if (error != null && isFailOnError()) {
256                 // exception should be thrown only if failOnError == true
257
// or error line will be logged twice
258
throw new BuildException(error);
259             }
260         } catch (Throwable JavaDoc t) {
261             if (isFailOnError()) {
262                 throw new BuildException(t);
263             } else {
264                 handleErrorOutput(t.getMessage());
265             }
266         } finally {
267             closeRedirector();
268             if (reader != null) {
269                 try {
270                     reader.close();
271                 } catch (Throwable JavaDoc u) {
272                     ;
273                 }
274                 reader = null;
275             }
276             if (istream != null) {
277                 try {
278                     istream.close();
279                 } catch (Throwable JavaDoc u) {
280                     ;
281                 }
282                 istream = null;
283             }
284         }
285
286     }
287
288
289 }
290
Popular Tags