1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package net.sf.tm.tomcat;
34
35 import java.io.BufferedReader;
36 import java.io.IOException;
37 import java.io.InputStreamReader;
38 import java.net.HttpURLConnection;
39 import java.net.URL;
40
41 import org.apache.commons.codec.EncoderException;
42 import org.apache.commons.codec.binary.Base64;
43 import org.apache.commons.lang.builder.EqualsBuilder;
44 import org.apache.commons.lang.builder.HashCodeBuilder;
45 import org.apache.commons.lang.builder.ToStringBuilder;
46
47 /***
48 * Tomcat server object. Stores all connection information and provides a method
49 * for executing manager commands.
50 *
51 * @author Andreas Pataki
52 * @version $Id: TomcatServer.java,v 1.4 2003/11/17 20:53:06 apataki Exp $
53 */
54 public class TomcatServer {
55
56 /***
57 * Constant for default port
58 */
59 private static final int DEFAULT_PORT = 8080;
60 /***
61 * Constant for default manager application
62 */
63 private static final String DEFAULT_MANAGER = "/manager";
64 /***
65 * Constant for default protocol
66 */
67 private static final String DEFAULT_POTOCOL = "http";
68 /***
69 * Constant for standard port
70 */
71 private static final int STANDARD_PORT = 80;
72
73 /***
74 * The name of the server
75 */
76 private String name;
77 /***
78 * The host name of the server
79 */
80 private String host;
81 /***
82 * The protocol which is needed to access the manage application (e.g. HTTP)
83 */
84 private String protocol;
85 /***
86 * The port of the web server
87 */
88 private int port;
89 /***
90 * The user name of a user witch has the manager role
91 */
92 private String user;
93 /***
94 * The password of the user
95 */
96 private String password;
97 /***
98 * The name of the manager web context. Default is /manager
99 */
100 private String manager;
101
102 /***
103 * Default constructor. Sets default values for:
104 *
105 * <li>protocol: http
106 * <li>port: 8080
107 * <li>manager: /manager
108 */
109 public TomcatServer() {
110 setProtocol(DEFAULT_POTOCOL);
111 setPort(DEFAULT_PORT);
112 setManager(DEFAULT_MANAGER);
113 }
114
115 /***
116 * Constructor
117 *
118 * @param newHost Hostname of the tomcat server (e.g. www.someserver.com)
119 * @param newUser Username for the managing application
120 * @param newPassword Password for the managing application
121 */
122 public TomcatServer(String newHost, String newUser, String newPassword) {
123 this();
124
125 setHost(newHost);
126 setUser(newUser);
127 setPassword(newPassword);
128 }
129
130 /***
131 * @return value of the field password
132 */
133 public String getPassword() {
134 return password;
135 }
136
137 /***
138 * @return value of the field port
139 */
140 public int getPort() {
141 return port;
142 }
143
144 /***
145 * @return value of the field protocol
146 */
147 public String getProtocol() {
148 return protocol;
149 }
150
151 /***
152 * @return value of the field host
153 */
154 public String getHost() {
155 return host;
156 }
157
158 /***
159 * @return value of the field user
160 */
161 public String getUser() {
162 return user;
163 }
164
165 /***
166 * @param string value of the field password
167 */
168 public void setPassword(String string) {
169 password = string;
170 }
171
172 /***
173 * @param value new value for the field port
174 */
175 public void setPort(int value) {
176 port = value;
177 }
178
179 /***
180 * @param string new value for the field protocol
181 */
182 public void setProtocol(String string) {
183 protocol = string;
184 }
185
186 /***
187 * @param string new value for the field host
188 */
189 public void setHost(String string) {
190 host = string;
191 }
192
193 /***
194 * @param string new value for the field user
195 */
196 public void setUser(String string) {
197 user = string;
198 }
199
200 /***
201 * @return value of the field manager
202 */
203 public String getManager() {
204 return manager;
205 }
206
207 /***
208 * @param string new value for the field manager
209 */
210 public void setManager(String string) {
211 manager = string;
212 }
213
214 /***
215 * @return The name of this tomcat server
216 */
217 public String getName() {
218 return name;
219 }
220
221 /***
222 * @param string new name of the tomcat server
223 */
224 public void setName(String string) {
225 name = string;
226 }
227
228 /***
229 * Executes a manager command. Basicly this method builds the URL connection
230 * to the Tomcat manager application and returns the resulting String.
231 *
232 * <p>For a list of available commands please consult the tomcat manager
233 * documentation.
234 *
235 * @param command The manager command. Example command: /start?path=/test
236 * @return The result of the manager application as a String
237 *
238 * @throws IOException Something went wrong during URL connect
239 */
240 public String executeManager(String command) throws IOException {
241 URL url = new URL(protocol, host, port, manager + command);
242 HttpURLConnection con = (HttpURLConnection) url.openConnection();
243
244
245 con.setAllowUserInteraction(false);
246 con.setDoInput(true);
247 con.setUseCaches(false);
248 con.setDoOutput(false);
249 con.setRequestMethod("GET");
250 con.setRequestProperty("User-Agent", "Eclipse-Tomcat-Manager\1.0");
251
252
253 String auth = getUser() + ":" + getPassword();
254 String encodedAuth = null;
255 try {
256 encodedAuth = new String(new Base64().encode(auth.getBytes()));
257 con.setRequestProperty("Authorization", "Basic " + encodedAuth);
258 } catch (EncoderException e) {
259 e.printStackTrace();
260 }
261
262 con.connect();
263 BufferedReader reader =
264 new BufferedReader(new InputStreamReader(con.getInputStream()));
265
266 StringBuffer result = new StringBuffer();
267 String line;
268 while ((line = reader.readLine()) != null) {
269 result.append(line);
270 result.append("\n");
271 }
272
273 return result.toString();
274 }
275
276 /***
277 * Generates the reload URL for the given context name.
278 *
279 * <p>Example:
280 *
281 * <p>http://server:8080/manager/reload?path=/examples
282 *
283 * @param context name of the context, must containt the leading /
284 * @return reload URL as text
285 */
286 public String generateReloadURL(String context) {
287 StringBuffer url = new StringBuffer();
288 url.append(getProtocol());
289 url.append("://");
290 url.append(getHost());
291 if (getPort() != STANDARD_PORT) {
292 url.append(":" + getPort());
293 }
294 url.append(getManager());
295 url.append("/reload?path=");
296 url.append(context);
297
298 return url.toString();
299 }
300
301 /***
302 * @see java.lang.Object#toString()
303 */
304 public String toString() {
305 return ToStringBuilder.reflectionToString(this);
306 }
307
308 /***
309 * @see java.lang.Object#equals(java.lang.Object)
310 */
311 public boolean equals(Object obj) {
312 return EqualsBuilder.reflectionEquals(this, obj);
313 }
314
315 /***
316 * @see java.lang.Object#hashCode()
317 */
318 public int hashCode() {
319 return HashCodeBuilder.reflectionHashCode(this);
320 }
321
322 }