|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractServerRun.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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;
|
|
| 23 |
import java.net.ServerSocket;
|
|
| 24 |
import java.net.Socket;
|
|
| 25 |
import java.util.Vector;
|
|
| 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 238918 2004-04-18 12:21:50Z vmassol $
|
|
| 34 |
*/
|
|
| 35 |
public abstract class AbstractServerRun extends Thread |
|
| 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 host = "127.0.0.1"; |
|
| 47 |
|
|
| 48 |
/**
|
|
| 49 |
* The command line arguments
|
|
| 50 |
*/
|
|
| 51 |
private String[] 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 runningServerThread;
|
|
| 63 |
|
|
| 64 |
/**
|
|
| 65 |
* @param theArgs the command line arguments
|
|
| 66 |
*/
|
|
| 67 | 0 |
public AbstractServerRun(String[] theArgs)
|
| 68 |
{
|
|
| 69 | 0 |
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 doStartServer(String[] theArgs) throws Exception; |
|
| 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[] theArgs, |
|
| 92 |
Thread theRunningServerThread) throws Exception;
|
|
| 93 |
|
|
| 94 |
/**
|
|
| 95 |
* Parse and process the command line to start/stop the server.
|
|
| 96 |
*/
|
|
| 97 | 0 |
protected final void doRun() |
| 98 |
{
|
|
| 99 |
// Look for a -start or -stop flag
|
|
| 100 | 0 |
boolean isStart = true; |
| 101 | 0 |
Vector newArgs = new Vector();
|
| 102 |
|
|
| 103 | 0 |
for (int i = 0; i < this.args.length; i++) |
| 104 |
{
|
|
| 105 | 0 |
if (this.args[i].equalsIgnoreCase("-start")) |
| 106 |
{
|
|
| 107 | 0 |
isStart = true;
|
| 108 |
} |
|
| 109 | 0 |
else if (this.args[i].equalsIgnoreCase("-stop")) |
| 110 |
{
|
|
| 111 | 0 |
isStart = false;
|
| 112 |
} |
|
| 113 | 0 |
else if (this.args[i].equalsIgnoreCase("-port")) |
| 114 |
{
|
|
| 115 | 0 |
this.port = Integer.parseInt(this.args[i + 1]); |
| 116 | 0 |
i++; |
| 117 |
} |
|
| 118 |
else
|
|
| 119 |
{
|
|
| 120 | 0 |
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 | 0 |
String[] strArgs = new String[0];
|
| 127 |
|
|
| 128 | 0 |
this.args = (String[]) newArgs.toArray(strArgs);
|
| 129 |
|
|
| 130 | 0 |
if (isStart)
|
| 131 |
{
|
|
| 132 | 0 |
startServer(); |
| 133 |
} |
|
| 134 |
else
|
|
| 135 |
{
|
|
| 136 | 0 |
stopServer(); |
| 137 |
} |
|
| 138 |
} |
|
| 139 |
|
|
| 140 |
/**
|
|
| 141 |
* Starts the server.
|
|
| 142 |
*/
|
|
| 143 | 0 |
private void startServer() |
| 144 |
{
|
|
| 145 |
// If the server is already started, do nothing
|
|
| 146 | 0 |
if (this.isStarted) |
| 147 |
{
|
|
| 148 | 0 |
return;
|
| 149 |
} |
|
| 150 |
|
|
| 151 | 0 |
try
|
| 152 |
{
|
|
| 153 | 0 |
this.runningServerThread = doStartServer(this.args); |
| 154 |
} |
|
| 155 |
catch (Exception e)
|
|
| 156 |
{
|
|
| 157 | 0 |
e.printStackTrace(); |
| 158 | 0 |
throw new RuntimeException("Error starting server"); |
| 159 |
} |
|
| 160 |
|
|
| 161 |
// Server is now started
|
|
| 162 | 0 |
this.isStarted = true; |
| 163 |
|
|
| 164 |
// Start a socket listener that will listen for stop commands.
|
|
| 165 | 0 |
start(); |
| 166 |
} |
|
| 167 |
|
|
| 168 |
/**
|
|
| 169 |
* Stops the running server.
|
|
| 170 |
*/
|
|
| 171 | 0 |
private void stopServer() |
| 172 |
{
|
|
| 173 |
// Open socket connection
|
|
| 174 | 0 |
Socket clientSocket = null;
|
| 175 |
|
|
| 176 | 0 |
try
|
| 177 |
{
|
|
| 178 | 0 |
clientSocket = new Socket(this.host, this.port); |
| 179 |
} |
|
| 180 |
catch (Exception e)
|
|
| 181 |
{
|
|
| 182 | 0 |
e.printStackTrace(); |
| 183 | 0 |
throw new RuntimeException("Error opening socket to " + this.host |
| 184 |
+ ":" + this.port + "]"); |
|
| 185 |
} |
|
| 186 |
finally
|
|
| 187 |
{
|
|
| 188 | 0 |
try
|
| 189 |
{
|
|
| 190 | 0 |
if (clientSocket != null) |
| 191 |
{
|
|
| 192 | 0 |
clientSocket.close(); |
| 193 |
} |
|
| 194 |
} |
|
| 195 |
catch (IOException e)
|
|
| 196 |
{
|
|
| 197 | 0 |
throw new RuntimeException("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 | 0 |
public void run() |
| 207 |
{
|
|
| 208 | 0 |
ServerSocket serverSocket = setUpListenerSocket(); |
| 209 |
|
|
| 210 |
// Accept a client socket connection
|
|
| 211 | 0 |
try
|
| 212 |
{
|
|
| 213 | 0 |
serverSocket.accept(); |
| 214 |
} |
|
| 215 |
catch (IOException e)
|
|
| 216 |
{
|
|
| 217 | 0 |
throw new RuntimeException("Error accepting connection for " |
| 218 |
+ "server socket [" + serverSocket + "]"); |
|
| 219 |
} |
|
| 220 |
finally
|
|
| 221 |
{
|
|
| 222 |
// Stop server socket
|
|
| 223 | 0 |
try
|
| 224 |
{
|
|
| 225 | 0 |
serverSocket.close(); |
| 226 |
} |
|
| 227 |
catch (IOException e)
|
|
| 228 |
{
|
|
| 229 | 0 |
throw new RuntimeException("Cannot close server socket [" |
| 230 |
+ serverSocket + "]");
|
|
| 231 |
} |
|
| 232 |
} |
|
| 233 |
|
|
| 234 |
// Stop server
|
|
| 235 | 0 |
try
|
| 236 |
{
|
|
| 237 | 0 |
this.doStopServer(this.args, this.runningServerThread); |
| 238 |
} |
|
| 239 |
catch (Exception e)
|
|
| 240 |
{
|
|
| 241 | 0 |
e.printStackTrace(); |
| 242 | 0 |
throw new RuntimeException("Cannot stop server"); |
| 243 |
} |
|
| 244 |
|
|
| 245 |
// Stop server socket
|
|
| 246 | 0 |
try
|
| 247 |
{
|
|
| 248 | 0 |
serverSocket.close(); |
| 249 |
} |
|
| 250 |
catch (IOException e)
|
|
| 251 |
{
|
|
| 252 | 0 |
throw new RuntimeException("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 | 0 |
private ServerSocket setUpListenerSocket()
|
| 263 |
{
|
|
| 264 | 0 |
ServerSocket serverSocket = null;
|
| 265 |
|
|
| 266 | 0 |
try
|
| 267 |
{
|
|
| 268 | 0 |
serverSocket = new ServerSocket(this.port); |
| 269 |
} |
|
| 270 |
catch (IOException e)
|
|
| 271 |
{
|
|
| 272 | 0 |
e.printStackTrace(); |
| 273 | 0 |
throw new RuntimeException("Error setting up the server " |
| 274 |
+ "listener socket");
|
|
| 275 |
} |
|
| 276 |
|
|
| 277 | 0 |
return serverSocket;
|
| 278 |
} |
|
| 279 |
} |
|
| 280 |
|
|
||||||||||