001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.ant;
018:
019: import java.io.BufferedInputStream;
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.net.URL;
023: import java.net.URLConnection;
024: import java.net.URLEncoder;
025:
026: import org.apache.tools.ant.BuildException;
027:
028: /**
029: * Ant task that implements the <code>/deploy</code> command, supported by
030: * the Tomcat manager application.
031: *
032: * @author Craig R. McClanahan
033: * @version $Revision: 1.6 $ $Date: 2004/02/27 14:58:40 $
034: * @since 4.1
035: */
036: public class DeployTask extends AbstractCatalinaTask {
037:
038: // ------------------------------------------------------------- Properties
039:
040: /**
041: * URL of the context configuration file for this application, if any.
042: */
043: protected String config = null;
044:
045: public String getConfig() {
046: return (this .config);
047: }
048:
049: public void setConfig(String config) {
050: this .config = config;
051: }
052:
053: /**
054: * URL of the server local web application archive (WAR) file
055: * to be deployed.
056: */
057: protected String localWar = null;
058:
059: public String getLocalWar() {
060: return (this .localWar);
061: }
062:
063: public void setLocalWar(String localWar) {
064: this .localWar = localWar;
065: }
066:
067: /**
068: * The context path of the web application we are managing.
069: */
070: protected String path = null;
071:
072: public String getPath() {
073: return (this .path);
074: }
075:
076: public void setPath(String path) {
077: this .path = path;
078: }
079:
080: /**
081: * Tag to associate with this to be deployed webapp.
082: */
083: protected String tag = null;
084:
085: public String getTag() {
086: return (this .tag);
087: }
088:
089: public void setTag(String tag) {
090: this .tag = tag;
091: }
092:
093: /**
094: * Update existing webapps.
095: */
096: protected boolean update = false;
097:
098: public boolean getUpdate() {
099: return (this .update);
100: }
101:
102: public void setUpdate(boolean update) {
103: this .update = update;
104: }
105:
106: /**
107: * URL of the web application archive (WAR) file to be deployed.
108: */
109: protected String war = null;
110:
111: public String getWar() {
112: return (this .war);
113: }
114:
115: public void setWar(String war) {
116: this .war = war;
117: }
118:
119: // --------------------------------------------------------- Public Methods
120:
121: /**
122: * Execute the requested operation.
123: *
124: * @exception BuildException if an error occurs
125: */
126: public void execute() throws BuildException {
127:
128: super .execute();
129: if (path == null) {
130: throw new BuildException("Must specify 'path' attribute");
131: }
132: if ((war == null) && (localWar == null) && (tag == null)) {
133: throw new BuildException(
134: "Must specify either 'war', 'localWar', or 'tag' attribute");
135: }
136:
137: // Building an input stream on the WAR to upload, if any
138: BufferedInputStream stream = null;
139: String contentType = null;
140: int contentLength = -1;
141: if (war != null) {
142: if (war.startsWith("file:")) {
143: try {
144: URL url = new URL(war);
145: URLConnection conn = url.openConnection();
146: contentLength = conn.getContentLength();
147: stream = new BufferedInputStream(conn
148: .getInputStream(), 1024);
149: } catch (IOException e) {
150: throw new BuildException(e);
151: }
152: } else {
153: try {
154: stream = new BufferedInputStream(
155: new FileInputStream(war), 1024);
156: } catch (IOException e) {
157: throw new BuildException(e);
158: }
159: }
160: contentType = "application/octet-stream";
161: }
162:
163: // Building URL
164: StringBuffer sb = new StringBuffer("/deploy?path=");
165: sb.append(URLEncoder.encode(this .path));
166: if ((war == null) && (config != null)) {
167: sb.append("&config=");
168: sb.append(URLEncoder.encode(config));
169: }
170: if ((war == null) && (localWar != null)) {
171: sb.append("&war=");
172: sb.append(URLEncoder.encode(localWar));
173: }
174: if (update) {
175: sb.append("&update=true");
176: }
177: if (tag != null) {
178: sb.append("&tag=");
179: sb.append(URLEncoder.encode(tag));
180: }
181:
182: execute(sb.toString(), stream, contentType, contentLength);
183:
184: }
185:
186: }
|