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