KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedInputStream JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLConnection JavaDoc;
28 import java.net.URLEncoder JavaDoc;
29
30 import org.apache.tools.ant.BuildException;
31
32
33 /**
34  * Ant task that implements the <code>/deploy</code> command, supported by
35  * the Tomcat manager application.
36  *
37  * @author Craig R. McClanahan
38  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
39  * @since 4.1
40  */

41 public class DeployTask extends AbstractCatalinaTask {
42
43
44     // ------------------------------------------------------------- Properties
45

46
47     /**
48      * URL of the context configuration file for this application, if any.
49      */

50     protected String JavaDoc config = null;
51
52     public String JavaDoc getConfig() {
53         return (this.config);
54     }
55
56     public void setConfig(String JavaDoc config) {
57         this.config = config;
58     }
59
60
61     /**
62      * URL of the server local web application archive (WAR) file
63      * to be deployed.
64      */

65     protected String JavaDoc localWar = null;
66
67     public String JavaDoc getLocalWar() {
68         return (this.localWar);
69     }
70
71     public void setLocalWar(String JavaDoc localWar) {
72         this.localWar = localWar;
73     }
74
75
76     /**
77      * The context path of the web application we are managing.
78      */

79     protected String JavaDoc path = null;
80
81     public String JavaDoc getPath() {
82         return (this.path);
83     }
84
85     public void setPath(String JavaDoc path) {
86         this.path = path;
87     }
88
89
90     /**
91      * Tag to associate with this to be deployed webapp.
92      */

93     protected String JavaDoc tag = null;
94
95     public String JavaDoc getTag() {
96         return (this.tag);
97     }
98
99     public void setTag(String JavaDoc tag) {
100         this.tag = tag;
101     }
102
103
104     /**
105      * Update existing webapps.
106      */

107     protected boolean update = false;
108
109     public boolean getUpdate() {
110         return (this.update);
111     }
112
113     public void setUpdate(boolean update) {
114         this.update = update;
115     }
116
117
118     /**
119      * URL of the web application archive (WAR) file to be deployed.
120      */

121     protected String JavaDoc war = null;
122
123     public String JavaDoc getWar() {
124         return (this.war);
125     }
126
127     public void setWar(String JavaDoc war) {
128         this.war = war;
129     }
130
131
132     // --------------------------------------------------------- Public Methods
133

134
135     /**
136      * Execute the requested operation.
137      *
138      * @exception BuildException if an error occurs
139      */

140     public void execute() throws BuildException {
141
142         super.execute();
143         if (path == null) {
144             throw new BuildException
145                 ("Must specify 'path' attribute");
146         }
147         if ((war == null) && (localWar == null) && (config == null) && (tag == null)) {
148             throw new BuildException
149                 ("Must specify either 'war', 'localWar', 'config', or 'tag' attribute");
150         }
151
152         // Building an input stream on the WAR to upload, if any
153
BufferedInputStream JavaDoc stream = null;
154         String JavaDoc contentType = null;
155         int contentLength = -1;
156         if (war != null) {
157             if (war.startsWith("file:")) {
158                 try {
159                     URL JavaDoc url = new URL JavaDoc(war);
160                     URLConnection JavaDoc conn = url.openConnection();
161                     contentLength = conn.getContentLength();
162                     stream = new BufferedInputStream JavaDoc
163                         (conn.getInputStream(), 1024);
164                 } catch (IOException JavaDoc e) {
165                     throw new BuildException(e);
166                 }
167             } else {
168                 try {
169                     stream = new BufferedInputStream JavaDoc
170                         (new FileInputStream JavaDoc(war), 1024);
171                 } catch (IOException JavaDoc e) {
172                     throw new BuildException(e);
173                 }
174             }
175             contentType = "application/octet-stream";
176         }
177
178         // Building URL
179
StringBuffer JavaDoc sb = new StringBuffer JavaDoc("/deploy?path=");
180         try {
181             sb.append(URLEncoder.encode(this.path, getCharset()));
182             if ((war == null) && (config != null)) {
183                 sb.append("&config=");
184                 sb.append(URLEncoder.encode(config, getCharset()));
185             }
186             if ((war == null) && (localWar != null)) {
187                 sb.append("&war=");
188                 sb.append(URLEncoder.encode(localWar, getCharset()));
189             }
190             if (update) {
191                 sb.append("&update=true");
192             }
193             if (tag != null) {
194                 sb.append("&tag=");
195                 sb.append(URLEncoder.encode(tag, getCharset()));
196             }
197         } catch (UnsupportedEncodingException JavaDoc e) {
198             throw new BuildException("Invalid 'charset' attribute: " + getCharset());
199         }
200
201         execute(sb.toString(), stream, contentType, contentLength);
202
203     }
204
205
206 }
207
Popular Tags