KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > container > AbstractServerRun


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.integration.ant.container;
21
22 import java.io.IOException JavaDoc;
23 import java.net.ServerSocket JavaDoc;
24 import java.net.Socket JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 /**
28  * Abstract class for starting/stopping an application server. When this
29  * application is first called to start the server, a listener socket is
30  * set up. Then, we it is later called to stop the server, we connect to the
31  * listener socket and tell the server to stop.
32  *
33  * @version $Id: AbstractServerRun.java,v 1.7 2004/04/18 12:21:50 vmassol Exp $
34  */

35 public abstract class AbstractServerRun extends Thread JavaDoc
36 {
37     /**
38      * Internal socket port that we use to stop the server.
39      */

40     private int port = 7777;
41
42     /**
43      * Host name. We assume the server is started and stoppped in the same
44      * local machine
45      */

46     private String JavaDoc host = "127.0.0.1";
47
48     /**
49      * The command line arguments
50      */

51     private String JavaDoc[] args;
52
53     /**
54      * Flag that specifies if the server is already started to prevent
55      * starting it if it is.
56      */

57     private boolean isStarted = false;
58
59     /**
60      * Thread in which the server is running
61      */

62     private Thread JavaDoc runningServerThread;
63     
64     /**
65      * @param theArgs the command line arguments
66      */

67     public AbstractServerRun(String JavaDoc[] theArgs)
68     {
69         this.args = theArgs;
70     }
71
72     /**
73      * Starts the server (in a blocking mode) and set up a socket listener.
74      *
75      * @return the thread in which the server has been started
76      * @param theArgs the command line arguments
77      * @exception Exception if any error happens when starting the server
78      */

79     protected abstract Thread JavaDoc doStartServer(String JavaDoc[] theArgs) throws Exception JavaDoc;
80
81     /**
82      * Stops the server by connecting to the socket set up when the server
83      * was started.
84      *
85      * @param theArgs the command line arguments
86      * @param theRunningServerThread the thread in which the server is running.
87      * This is useful for example if there is no simple way to stop the
88      * server and thus you need to simply try to stop the running thread.
89      * @exception Exception if any error happens when stopping the server
90      */

91     protected abstract void doStopServer(String JavaDoc[] theArgs,
92         Thread JavaDoc theRunningServerThread) throws Exception JavaDoc;
93
94     /**
95      * Parse and process the command line to start/stop the server.
96      */

97     protected final void doRun()
98     {
99         // Look for a -start or -stop flag
100
boolean isStart = true;
101         Vector JavaDoc newArgs = new Vector JavaDoc();
102
103         for (int i = 0; i < this.args.length; i++)
104         {
105             if (this.args[i].equalsIgnoreCase("-start"))
106             {
107                 isStart = true;
108             }
109             else if (this.args[i].equalsIgnoreCase("-stop"))
110             {
111                 isStart = false;
112             }
113             else if (this.args[i].equalsIgnoreCase("-port"))
114             {
115                 this.port = Integer.parseInt(this.args[i + 1]);
116                 i++;
117             }
118             else
119             {
120                 newArgs.add(this.args[i]);
121             }
122         }
123
124         // Remove the command line arguments that should not be part of the
125
// server command line (i.e. our own arguments).
126
String JavaDoc[] strArgs = new String JavaDoc[0];
127
128         this.args = (String JavaDoc[]) newArgs.toArray(strArgs);
129
130         if (isStart)
131         {
132             startServer();
133         }
134         else
135         {
136             stopServer();
137         }
138     }
139
140     /**
141      * Starts the server.
142      */

143     private void startServer()
144     {
145         // If the server is already started, do nothing
146
if (this.isStarted)
147         {
148             return;
149         }
150
151         try
152         {
153             this.runningServerThread = doStartServer(this.args);
154         }
155         catch (Exception JavaDoc e)
156         {
157             e.printStackTrace();
158             throw new RuntimeException JavaDoc("Error starting server");
159         }
160
161         // Server is now started
162
this.isStarted = true;
163
164         // Start a socket listener that will listen for stop commands.
165
start();
166     }
167
168     /**
169      * Stops the running server.
170      */

171     private void stopServer()
172     {
173         // Open socket connection
174
Socket JavaDoc clientSocket = null;
175
176         try
177         {
178             clientSocket = new Socket JavaDoc(this.host, this.port);
179         }
180         catch (Exception JavaDoc e)
181         {
182             e.printStackTrace();
183             throw new RuntimeException JavaDoc("Error opening socket to " + this.host
184                 + ":" + this.port + "]");
185         }
186         finally
187         {
188             try
189             {
190                 if (clientSocket != null)
191                 {
192                     clientSocket.close();
193                 }
194             }
195             catch (IOException JavaDoc e)
196             {
197                 throw new RuntimeException JavaDoc("Cannot close client socket");
198             }
199         }
200     }
201
202     /**
203      * Sets up a listener socket and wait until we receive a request on it to
204      * stop the running server.
205      */

206     public void run()
207     {
208         ServerSocket JavaDoc serverSocket = setUpListenerSocket();
209
210         // Accept a client socket connection
211
try
212         {
213             serverSocket.accept();
214         }
215         catch (IOException JavaDoc e)
216         {
217             throw new RuntimeException JavaDoc("Error accepting connection for "
218                 + "server socket [" + serverSocket + "]");
219         }
220         finally
221         {
222             // Stop server socket
223
try
224             {
225                 serverSocket.close();
226             }
227             catch (IOException JavaDoc e)
228             {
229                 throw new RuntimeException JavaDoc("Cannot close server socket ["
230                     + serverSocket + "]");
231             }
232         }
233
234         // Stop server
235
try
236         {
237             this.doStopServer(this.args, this.runningServerThread);
238         }
239         catch (Exception JavaDoc e)
240         {
241             e.printStackTrace();
242             throw new RuntimeException JavaDoc("Cannot stop server");
243         }
244
245         // Stop server socket
246
try
247         {
248             serverSocket.close();
249         }
250         catch (IOException JavaDoc e)
251         {
252             throw new RuntimeException JavaDoc("Cannot close server socket ["
253                 + serverSocket + "]");
254         }
255     }
256
257     /**
258      * Sets up the listener socket.
259      *
260      * @return the listener socket that has been set up
261      */

262     private ServerSocket JavaDoc setUpListenerSocket()
263     {
264         ServerSocket JavaDoc serverSocket = null;
265
266         try
267         {
268             serverSocket = new ServerSocket JavaDoc(this.port);
269         }
270         catch (IOException JavaDoc e)
271         {
272             e.printStackTrace();
273             throw new RuntimeException JavaDoc("Error setting up the server "
274                 + "listener socket");
275         }
276
277         return serverSocket;
278     }
279 }
280
Popular Tags