|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AbstractServerRun.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus.integration.ant.container;
|
|
58 |
|
|
59 |
import java.io.IOException;
|
|
60 |
import java.net.ServerSocket;
|
|
61 |
import java.net.Socket;
|
|
62 |
import java.util.Vector;
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Abstract class for starting/stopping an application server. When this
|
|
66 |
* application is first called to start the server, a listener socket is
|
|
67 |
* set up. Then, we it is later called to stop the server, we connect to the
|
|
68 |
* listener socket and tell the server to stop.
|
|
69 |
*
|
|
70 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
71 |
* @author <a href="mailto:digital@ix.net.au">Robert Leftwich</a>
|
|
72 |
*
|
|
73 |
* @version $Id: AbstractServerRun.java,v 1.3 2003/05/26 09:43:00 cmlenz Exp $
|
|
74 |
*/
|
|
75 |
public abstract class AbstractServerRun extends Thread |
|
76 |
{ |
|
77 |
/**
|
|
78 |
* Internal socket port that we use to stop the server.
|
|
79 |
*/
|
|
80 |
private int port = 7777; |
|
81 |
|
|
82 |
/**
|
|
83 |
* Host name. We assume the server is started and stoppped in the same
|
|
84 |
* local machine
|
|
85 |
*/
|
|
86 |
private String host = "127.0.0.1"; |
|
87 |
|
|
88 |
/**
|
|
89 |
* The command line arguments
|
|
90 |
*/
|
|
91 |
private String[] args;
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Flag that specifies if the server is already started to prevent
|
|
95 |
* starting it if it is.
|
|
96 |
*/
|
|
97 |
private boolean isStarted = false; |
|
98 |
|
|
99 |
/**
|
|
100 |
* @param theArgs the command line arguments
|
|
101 |
*/
|
|
102 | 0 |
public AbstractServerRun(String[] theArgs)
|
103 |
{ |
|
104 | 0 |
this.args = theArgs;
|
105 |
} |
|
106 |
|
|
107 |
/**
|
|
108 |
* Starts the server (in a blocking mode) and set up a socket listener.
|
|
109 |
*
|
|
110 |
* @param theArgs the command line arguments
|
|
111 |
* @exception Exception if any error happens when starting the server
|
|
112 |
*/
|
|
113 |
protected abstract void doStartServer(String[] theArgs) throws Exception; |
|
114 |
|
|
115 |
/**
|
|
116 |
* Stops the server by connecting to the socket set up when the server
|
|
117 |
* was started.
|
|
118 |
*
|
|
119 |
* @param theArgs the command line arguments
|
|
120 |
* @exception Exception if any error happens when stopping the server
|
|
121 |
*/
|
|
122 |
protected abstract void doStopServer(String[] theArgs) throws Exception; |
|
123 |
|
|
124 |
/**
|
|
125 |
* Parse and process the command line to start/stop the server.
|
|
126 |
*/
|
|
127 | 0 |
protected final void doRun() |
128 |
{ |
|
129 |
// Look for a -start or -stop flag
|
|
130 | 0 |
boolean isStart = true; |
131 | 0 |
Vector newArgs = new Vector();
|
132 |
|
|
133 | 0 |
for (int i = 0; i < this.args.length; i++) |
134 |
{ |
|
135 | 0 |
if (this.args[i].equalsIgnoreCase("-start")) |
136 |
{ |
|
137 | 0 |
isStart = true;
|
138 |
} |
|
139 | 0 |
else if (this.args[i].equalsIgnoreCase("-stop")) |
140 |
{ |
|
141 | 0 |
isStart = false;
|
142 |
} |
|
143 | 0 |
else if (this.args[i].equalsIgnoreCase("-port")) |
144 |
{ |
|
145 | 0 |
this.port = Integer.parseInt(this.args[i + 1]); |
146 | 0 |
i++; |
147 |
} |
|
148 |
else
|
|
149 |
{ |
|
150 | 0 |
newArgs.add(this.args[i]);
|
151 |
} |
|
152 |
} |
|
153 |
|
|
154 |
// Remove the command line arguments that should not be part of the
|
|
155 |
// server command line (i.e. our own arguments).
|
|
156 | 0 |
String[] strArgs = new String[0];
|
157 |
|
|
158 | 0 |
this.args = (String[]) newArgs.toArray(strArgs);
|
159 |
|
|
160 | 0 |
if (isStart)
|
161 |
{ |
|
162 | 0 |
startServer(); |
163 |
} |
|
164 |
else
|
|
165 |
{ |
|
166 | 0 |
stopServer(); |
167 |
} |
|
168 |
} |
|
169 |
|
|
170 |
/**
|
|
171 |
* Starts the server.
|
|
172 |
*/
|
|
173 | 0 |
private void startServer() |
174 |
{ |
|
175 |
// If the server is already started, do nothing
|
|
176 | 0 |
if (this.isStarted) |
177 |
{ |
|
178 | 0 |
return;
|
179 |
} |
|
180 |
|
|
181 | 0 |
try
|
182 |
{ |
|
183 | 0 |
doStartServer(this.args);
|
184 |
} |
|
185 |
catch (Exception e)
|
|
186 |
{ |
|
187 | 0 |
e.printStackTrace(); |
188 | 0 |
throw new RuntimeException("Error starting server"); |
189 |
} |
|
190 |
|
|
191 |
|
|
192 |
// Server is now started
|
|
193 | 0 |
this.isStarted = true; |
194 |
|
|
195 | 0 |
new Thread(this).start(); |
196 |
} |
|
197 |
|
|
198 |
/**
|
|
199 |
* Stops the running server.
|
|
200 |
*/
|
|
201 | 0 |
private void stopServer() |
202 |
{ |
|
203 |
// Open socket connection
|
|
204 | 0 |
Socket clientSocket = null;
|
205 |
|
|
206 | 0 |
try
|
207 |
{ |
|
208 | 0 |
clientSocket = new Socket(this.host, this.port); |
209 |
} |
|
210 |
catch (Exception e)
|
|
211 |
{ |
|
212 | 0 |
e.printStackTrace(); |
213 | 0 |
throw new RuntimeException("Error opening socket to " + this.host |
214 |
+ ":" + this.port + "]"); |
|
215 |
} |
|
216 |
finally
|
|
217 |
{ |
|
218 | 0 |
try
|
219 |
{ |
|
220 | 0 |
if (clientSocket != null) |
221 |
{ |
|
222 | 0 |
clientSocket.close(); |
223 |
} |
|
224 |
} |
|
225 |
catch (IOException e)
|
|
226 |
{ |
|
227 | 0 |
throw new RuntimeException("Cannot close client socket"); |
228 |
} |
|
229 |
} |
|
230 |
} |
|
231 |
|
|
232 |
/**
|
|
233 |
* Sets up a listener socket and wait until we receive a request on it to
|
|
234 |
* stop the running server.
|
|
235 |
*/
|
|
236 | 0 |
public void run() |
237 |
{ |
|
238 | 0 |
ServerSocket serverSocket = setUpListenerSocket(); |
239 |
|
|
240 |
// Accept a client socket connection
|
|
241 | 0 |
try
|
242 |
{ |
|
243 | 0 |
serverSocket.accept(); |
244 |
} |
|
245 |
catch (IOException e)
|
|
246 |
{ |
|
247 | 0 |
throw new RuntimeException("Error accepting connection for " |
248 |
+ "server socket [" + serverSocket + "]"); |
|
249 |
} |
|
250 |
finally
|
|
251 |
{ |
|
252 |
// Stop server socket
|
|
253 | 0 |
try
|
254 |
{ |
|
255 | 0 |
serverSocket.close(); |
256 |
} |
|
257 |
catch (IOException e)
|
|
258 |
{ |
|
259 | 0 |
throw new RuntimeException("Cannot close server socket [" |
260 |
+ serverSocket + "]");
|
|
261 |
} |
|
262 |
} |
|
263 |
|
|
264 |
// Stop server
|
|
265 | 0 |
try
|
266 |
{ |
|
267 | 0 |
this.doStopServer(this.args); |
268 |
} |
|
269 |
catch (Exception e)
|
|
270 |
{ |
|
271 | 0 |
e.printStackTrace(); |
272 | 0 |
throw new RuntimeException("Cannot stop server"); |
273 |
} |
|
274 |
|
|
275 |
// Stop server socket
|
|
276 | 0 |
try
|
277 |
{ |
|
278 | 0 |
serverSocket.close(); |
279 |
} |
|
280 |
catch (IOException e)
|
|
281 |
{ |
|
282 | 0 |
throw new RuntimeException("Cannot close server socket [" |
283 |
+ serverSocket + "]");
|
|
284 |
} |
|
285 |
} |
|
286 |
|
|
287 |
/**
|
|
288 |
* Sets up the listener socket.
|
|
289 |
*
|
|
290 |
* @return the listener socket that has been set up
|
|
291 |
*/
|
|
292 | 0 |
private ServerSocket setUpListenerSocket()
|
293 |
{ |
|
294 | 0 |
ServerSocket serverSocket = null;
|
295 |
|
|
296 | 0 |
try
|
297 |
{ |
|
298 | 0 |
serverSocket = new ServerSocket(this.port); |
299 |
} |
|
300 |
catch (IOException e)
|
|
301 |
{ |
|
302 | 0 |
e.printStackTrace(); |
303 | 0 |
throw new RuntimeException("Error setting up the server " |
304 |
+ "listener socket");
|
|
305 |
} |
|
306 |
|
|
307 | 0 |
return serverSocket;
|
308 |
} |
|
309 |
} |
|
310 |
|
|