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.StringReader;
38 import java.util.List;
39 import java.util.Vector;
40
41 import net.sf.tm.ContextManagerException;
42 import net.sf.tm.IContextManager;
43 import net.sf.tm.WebContext;
44 import net.sf.tm.WebContextStatus;
45
46 import org.apache.commons.lang.StringUtils;
47
48 /***
49 * Implementation of a {@link IContextManager} using the Tomcat server manager
50 * application.
51 *
52 * @author Andreas Pataki
53 * @version $Id: TomcatContextManager.java,v 1.6 2004/03/28 16:23:36 apataki Exp $
54 */
55 public class TomcatContextManager implements IContextManager {
56 private TomcatServer server;
57
58 /***
59 * Default constructor
60 */
61 public TomcatContextManager(TomcatServer server) {
62 this.server = server;
63 }
64
65 /***
66 * @see net.sf.tm.IContextManager#start(java.lang.String)
67 */
68 public void start(String context) throws ContextManagerException {
69 String result;
70 try {
71 result = server.executeManager("/start?path=" + context);
72 testResultForErrors(result);
73
74 } catch (IOException e) {
75 throw new ContextManagerException(
76 "error executing start context " + context,
77 e);
78 }
79 }
80
81 /***
82 * @see net.sf.tm.IContextManager#stop(java.lang.String)
83 */
84 public void stop(String context) throws ContextManagerException {
85 String result;
86 try {
87 result = server.executeManager("/stop?path=" + context);
88 testResultForErrors(result);
89 } catch (IOException e) {
90 throw new ContextManagerException(
91 "error executing stop context " + context,
92 e);
93 }
94 }
95
96 /***
97 * @see net.sf.tm.IContextManager#reload(String)
98 */
99 public void reload(String context) throws ContextManagerException {
100 String result;
101 try {
102 result = server.executeManager("/reload?path=" + context);
103 testResultForErrors(result);
104 } catch (IOException e) {
105 throw new ContextManagerException(
106 "error executing restart context " + context,
107 e);
108 }
109 }
110
111 /***
112 * @see net.sf.tm.IContextManager#getAll()
113 */
114 public List getAll() throws ContextManagerException {
115 String result;
116 try {
117 result = server.executeManager("/list");
118 testResultForErrors(result);
119
120 return createContextListFromResult(result);
121
122 } catch (IOException e) {
123 throw new ContextManagerException("error getting all contexts", e);
124 }
125
126 }
127
128 /***
129 * Parses the result of the /list command.
130 *
131 * @param result
132 * @return A list of {@link WebContext} objects. If no context exist an
133 * empty list is returned.
134 */
135 private List createContextListFromResult(String result)
136 throws IOException {
137 Vector list = new Vector();
138
139 BufferedReader reader = new BufferedReader(new StringReader(result));
140
141 String line = reader.readLine();
142
143 while ((line = reader.readLine()) != null) {
144 WebContext context = new WebContext();
145 String[] splitLine = StringUtils.split(line, ":");
146
147 context.setPath(splitLine[0]);
148 context.setActiveSessions(Integer.parseInt(splitLine[2]));
149 if (splitLine.length > 3) {
150
151 if (splitLine.length > 4 && splitLine[3].length() == 1) {
152
153 context.setDocPath(splitLine[3] + ":" + splitLine[4]);
154 } else {
155 context.setDocPath(splitLine[3]);
156 }
157 }
158
159 if ("running".equals(splitLine[1])) {
160 context.setStatus(WebContextStatus.RUNNING);
161 } else {
162 context.setStatus(WebContextStatus.STOPPED);
163 }
164
165 list.add(context);
166 }
167
168 return list;
169 }
170
171 /***
172 * Checks if the result String is valid. A valid result String starts
173 * with OK in the first line.
174 *
175 * @param result
176 * @return
177 * @throws IOException
178 * @throws ContextManagerException
179 */
180 private String testResultForErrors(String result)
181 throws IOException, ContextManagerException {
182 if (result.startsWith("OK")) {
183 return null;
184 }
185
186
187
188 BufferedReader reader = new BufferedReader(new StringReader(result));
189 String errorMsg = reader.readLine();
190
191 throw new ContextManagerException(errorMsg);
192 }
193
194 }