View Javadoc

1   /*
2    * Copyright (c) 2003, Andreas Pataki 
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    *
9    * Redistributions of source code must retain the above copyright notice,
10   * this list of conditions and the following disclaimer.
11   * 
12   * Redistributions in binary form must reproduce the above copyright notice,
13   * this list of conditions and the following disclaimer in the documentation
14   * and/or other materials provided with the distribution.
15   * 
16   * Neither the name of the author nor the names of its contributors
17   * may be used to endorse or promote products derived from this software
18   * without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS
21   * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
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         // first line is the OK message, must be skiped
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                 // this value is only available since Tomcat 4.1 
151                 if (splitLine.length > 4 && splitLine[3].length() == 1) {
152                     // on windows the filename may also be split so join it
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         // result contains an error message
187 
188         BufferedReader reader = new BufferedReader(new StringReader(result));
189         String errorMsg = reader.readLine();
190 
191         throw new ContextManagerException(errorMsg);
192     }
193 
194 }