001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/ant/AbstractCatalinaTask.java,v 1.2 2002/02/12 22:14:01 craigmcc Exp $
003: * $Revision: 1.2 $
004: * $Date: 2002/02/12 22:14:01 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 2002 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: */
061:
062: package org.apache.catalina.ant;
063:
064: import java.io.BufferedOutputStream;
065: import java.io.InputStream;
066: import java.io.InputStreamReader;
067: import java.net.HttpURLConnection;
068: import java.net.URL;
069: import java.net.URLConnection;
070: import org.apache.catalina.util.Base64;
071: import org.apache.tools.ant.BuildException;
072: import org.apache.tools.ant.Project;
073: import org.apache.tools.ant.Task;
074:
075: /**
076: * Abstract base class for Ant tasks that interact with the
077: * <em>Manager</em> web application for dynamically deploying and
078: * undeploying applications. These tasks require Ant 1.4 or later.
079: *
080: * @author Craig R. McClanahan
081: * @version $Revision: 1.2 $ $Date: 2002/02/12 22:14:01 $
082: * @since 4.1
083: */
084:
085: public abstract class AbstractCatalinaTask extends Task {
086:
087: // ----------------------------------------------------- Instance Variables
088:
089: // ------------------------------------------------------------- Properties
090:
091: /**
092: * The login password for the <code>Manager</code> application.
093: */
094: protected String password = null;
095:
096: public String getPassword() {
097: return (this .password);
098: }
099:
100: public void setPassword(String password) {
101: this .password = password;
102: }
103:
104: /**
105: * The URL of the <code>Manager</code> application to be used.
106: */
107: protected String url = "http://localhost:8080/manager";
108:
109: public String getUrl() {
110: return (this .url);
111: }
112:
113: public void setUrl(String url) {
114: this .url = url;
115: }
116:
117: /**
118: * The login username for the <code>Manager</code> application.
119: */
120: protected String username = null;
121:
122: public String getUsername() {
123: return (this .username);
124: }
125:
126: public void setUsername(String username) {
127: this .username = username;
128: }
129:
130: // --------------------------------------------------------- Public Methods
131:
132: /**
133: * Execute the specified command. This logic only performs the common
134: * attribute validation required by all subclasses; it does not perform
135: * any functional logic directly.
136: *
137: * @exception BuildException if a validation error occurs
138: */
139: public void execute() throws BuildException {
140:
141: if ((username == null) || (password == null) || (url == null)) {
142: throw new BuildException(
143: "Must specify all of 'username', 'password', and 'url'");
144: }
145:
146: }
147:
148: // ------------------------------------------------------ Protected Methods
149:
150: /**
151: * Execute the specified command, based on the configured properties.
152: *
153: * @param command Command to be executed
154: *
155: * @exception BuildException if an error occurs
156: */
157: public void execute(String command) throws BuildException {
158:
159: execute(command, null, null, -1);
160:
161: }
162:
163: /**
164: * Execute the specified command, based on the configured properties.
165: * The input stream will be closed upon completion of this task, whether
166: * it was executed successfully or not.
167: *
168: * @param command Command to be executed
169: * @param istream InputStream to include in an HTTP PUT, if any
170: * @param contentType Content type to specify for the input, if any
171: * @param contentLength Content length to specify for the input, if any
172: *
173: * @exception BuildException if an error occurs
174: */
175: public void execute(String command, InputStream istream,
176: String contentType, int contentLength)
177: throws BuildException {
178:
179: URLConnection conn = null;
180: InputStreamReader reader = null;
181: try {
182:
183: // Create a connection for this command
184: conn = (new URL(url + command)).openConnection();
185: HttpURLConnection hconn = (HttpURLConnection) conn;
186:
187: // Set up standard connection characteristics
188: hconn.setAllowUserInteraction(false);
189: hconn.setDoInput(true);
190: hconn.setUseCaches(false);
191: if (istream != null) {
192: hconn.setDoOutput(true);
193: hconn.setRequestMethod("PUT");
194: if (contentType != null) {
195: hconn.setRequestProperty("Content-Type",
196: contentType);
197: }
198: if (contentLength >= 0) {
199: hconn.setRequestProperty("Content-Length", ""
200: + contentLength);
201: }
202: } else {
203: hconn.setDoOutput(false);
204: hconn.setRequestMethod("GET");
205: }
206: hconn.setRequestProperty("User-Agent",
207: "Catalina-Ant-Task/1.0");
208:
209: // Set up an authorization header with our credentials
210: String input = username + ":" + password;
211: String output = new String(Base64.encode(input.getBytes()));
212: hconn
213: .setRequestProperty("Authorization", "Basic "
214: + output);
215:
216: // Establish the connection with the server
217: hconn.connect();
218:
219: // Send the request data (if any)
220: if (istream != null) {
221: BufferedOutputStream ostream = new BufferedOutputStream(
222: hconn.getOutputStream(), 1024);
223: byte buffer[] = new byte[1024];
224: while (true) {
225: int n = istream.read(buffer);
226: if (n < 0) {
227: break;
228: }
229: ostream.write(buffer, 0, n);
230: }
231: ostream.flush();
232: ostream.close();
233: istream.close();
234: }
235:
236: // Process the response message
237: reader = new InputStreamReader(hconn.getInputStream());
238: StringBuffer buff = new StringBuffer();
239: String error = null;
240: boolean first = true;
241: while (true) {
242: int ch = reader.read();
243: if (ch < 0) {
244: break;
245: } else if ((ch == '\r') || (ch == '\n')) {
246: String line = buff.toString();
247: buff.setLength(0);
248: log(line, Project.MSG_INFO);
249: if (first) {
250: if (!line.startsWith("OK -")) {
251: error = line;
252: }
253: first = false;
254: }
255: } else {
256: buff.append((char) ch);
257: }
258: }
259: if (buff.length() > 0) {
260: log(buff.toString(), Project.MSG_INFO);
261: }
262: if (error != null) {
263: throw new BuildException(error);
264: }
265:
266: } catch (Throwable t) {
267: throw new BuildException(t);
268: } finally {
269: if (reader != null) {
270: try {
271: reader.close();
272: } catch (Throwable u) {
273: ;
274: }
275: reader = null;
276: }
277: if (istream != null) {
278: try {
279: istream.close();
280: } catch (Throwable u) {
281: ;
282: }
283: istream = null;
284: }
285: }
286:
287: }
288:
289: }
|