|
|||||||||||||||||||
| 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 | |||||||||||||||
| Listener.java | 66,7% | 89,4% | 88,9% | 85,7% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Replica is published under the terms
|
|
| 3 |
* of the Apache Software License.
|
|
| 4 |
*/
|
|
| 5 |
package replica.server;
|
|
| 6 |
|
|
| 7 |
import java.io.IOException;
|
|
| 8 |
import java.net.*;
|
|
| 9 |
import java.util.List;
|
|
| 10 |
import java.util.Vector;
|
|
| 11 |
import java.util.logging.Level;
|
|
| 12 |
import java.util.logging.Logger;
|
|
| 13 |
|
|
| 14 |
import org.springframework.context.ApplicationContext;
|
|
| 15 |
|
|
| 16 |
/**
|
|
| 17 |
* @author Pedro Costa
|
|
| 18 |
* @author Helder Silva
|
|
| 19 |
* @since 15/Jan/2004
|
|
| 20 |
*/
|
|
| 21 |
public class Listener implements Runnable { |
|
| 22 |
|
|
| 23 |
static Logger logger = Logger.getLogger(Listener.class.getName()); |
|
| 24 |
|
|
| 25 |
private int port = 7654; |
|
| 26 |
private boolean alive = true; |
|
| 27 |
private List serverProcesses = new Vector(); |
|
| 28 |
|
|
| 29 |
private ApplicationContext context;
|
|
| 30 |
private String serverProcessBeanId = "serverProcess"; |
|
| 31 |
|
|
| 32 |
ServerSocket server; |
|
| 33 |
|
|
| 34 |
/**
|
|
| 35 |
* Default Constructor.
|
|
| 36 |
*/
|
|
| 37 | 6 |
public Listener() {
|
| 38 | 6 |
super();
|
| 39 |
} |
|
| 40 |
|
|
| 41 | 0 |
public Listener(ApplicationContext context){
|
| 42 | 0 |
this();
|
| 43 | 0 |
setContext(context); |
| 44 |
} |
|
| 45 |
|
|
| 46 |
/* (non-Javadoc)
|
|
| 47 |
* @see java.lang.Runnable#run()
|
|
| 48 |
*/
|
|
| 49 | 3 |
public void run() { |
| 50 | 3 |
while(isAlive()){
|
| 51 | 3 |
try{
|
| 52 | 3 |
logger.log( Level.INFO, "Starting listener on port " + getPort() + "."); |
| 53 |
|
|
| 54 | 3 |
server = new ServerSocket(getPort());
|
| 55 |
|
|
| 56 | 3 |
while( isAlive() ){
|
| 57 | 4 |
createNewServerProcess( server.accept() ); |
| 58 |
} |
|
| 59 |
} |
|
| 60 |
catch(IOException e){
|
|
| 61 | 0 |
e.printStackTrace(); |
| 62 |
} |
|
| 63 |
finally{
|
|
| 64 | 3 |
logger.log( Level.INFO, "Stoping listener on port " + getPort() + "."); |
| 65 |
|
|
| 66 | 3 |
if( server != null ){ |
| 67 | 3 |
try{ server.close(); }catch(Throwable t){ } |
| 68 |
} |
|
| 69 |
} |
|
| 70 |
} |
|
| 71 |
} |
|
| 72 |
|
|
| 73 |
/**
|
|
| 74 |
* Create a new socket process for a new connection.
|
|
| 75 |
*
|
|
| 76 |
* @param socket new connection.
|
|
| 77 |
*/
|
|
| 78 | 4 |
protected void createNewServerProcess( Socket socket ){ |
| 79 | 4 |
logger.log( Level.INFO, "Received connection on port " + getPort() + " from [" + socket.getInetAddress() + ":"+ socket.getPort() + "]."); |
| 80 |
|
|
| 81 | 4 |
Thread t = new Thread( getNewServerProcess(socket) );
|
| 82 | 2 |
t.start(); |
| 83 | 2 |
getServerProcesses().add( t ); |
| 84 | 2 |
new Thread( new Monitor( t ) ).start(); |
| 85 |
} |
|
| 86 |
|
|
| 87 | 5 |
protected ServerProcess getNewServerProcess( Socket socket ){
|
| 88 |
|
|
| 89 | 5 |
ServerProcess serverProcess = (ServerProcess)getContext().getBean(getServerProcessBeanId()); |
| 90 | 3 |
serverProcess.setSocket( socket ); |
| 91 | 3 |
return serverProcess;
|
| 92 |
} |
|
| 93 |
|
|
| 94 |
/**
|
|
| 95 |
* @return
|
|
| 96 |
*/
|
|
| 97 | 18 |
public int getPort() { |
| 98 | 18 |
return port;
|
| 99 |
} |
|
| 100 |
|
|
| 101 |
/**
|
|
| 102 |
* @param i
|
|
| 103 |
*/
|
|
| 104 | 3 |
public void setPort(int i) { |
| 105 | 3 |
port = i; |
| 106 |
} |
|
| 107 |
|
|
| 108 |
/**
|
|
| 109 |
* @return
|
|
| 110 |
*/
|
|
| 111 | 12 |
public synchronized boolean isAlive() { |
| 112 | 12 |
return alive;
|
| 113 |
} |
|
| 114 |
|
|
| 115 |
/**
|
|
| 116 |
* @param b
|
|
| 117 |
*/
|
|
| 118 | 3 |
public synchronized void setAlive(boolean b) { |
| 119 | 3 |
alive = b; |
| 120 |
} |
|
| 121 |
|
|
| 122 |
/**
|
|
| 123 |
* Stops the listener;
|
|
| 124 |
*
|
|
| 125 |
*/
|
|
| 126 | 3 |
public synchronized void stop(){ |
| 127 | 3 |
if( isAlive() ){
|
| 128 | 3 |
setAlive( false );
|
| 129 | 3 |
Socket s = null;
|
| 130 | 3 |
try{
|
| 131 | 3 |
s = new Socket("localhost", getPort()); |
| 132 |
} |
|
| 133 |
catch(IOException e){
|
|
| 134 | 0 |
logger.log( Level.WARNING, e.getMessage(), e ); |
| 135 |
} |
|
| 136 |
finally{
|
|
| 137 | 3 |
if ( s != null ){ try{ s.close(); }catch(IOException e){} } |
| 138 |
} |
|
| 139 |
} |
|
| 140 |
} |
|
| 141 |
|
|
| 142 |
/**
|
|
| 143 |
* @return
|
|
| 144 |
*/
|
|
| 145 | 11 |
public List getServerProcesses() {
|
| 146 | 11 |
return serverProcesses;
|
| 147 |
} |
|
| 148 |
|
|
| 149 |
/**
|
|
| 150 |
* @param list
|
|
| 151 |
*/
|
|
| 152 | 0 |
private void setServerProcesses(List list) { |
| 153 | 0 |
serverProcesses = list; |
| 154 |
} |
|
| 155 |
|
|
| 156 |
/**
|
|
| 157 |
* Simple Monitor for when a Server Process ends,
|
|
| 158 |
* and remove it from the list of running processes.
|
|
| 159 |
*
|
|
| 160 |
* @author Pedro Costa
|
|
| 161 |
* @version 16/Jan/2004 - 23:58:07
|
|
| 162 |
*/
|
|
| 163 |
private class Monitor implements Runnable{ |
|
| 164 |
// the thread to be monitored
|
|
| 165 |
Thread master; |
|
| 166 | 2 |
Monitor( Thread t ){
|
| 167 | 2 |
master = t; |
| 168 |
} |
|
| 169 |
|
|
| 170 | 2 |
public void run(){ |
| 171 | 2 |
if( master != null ){ |
| 172 | 2 |
try{
|
| 173 | 2 |
master.join(); |
| 174 |
} |
|
| 175 |
catch(InterruptedException e){
|
|
| 176 |
} |
|
| 177 |
|
|
| 178 | 2 |
getServerProcesses().remove( master ); |
| 179 |
|
|
| 180 | 2 |
logger.log( Level.FINE, "Removed server process.");
|
| 181 |
} |
|
| 182 |
} |
|
| 183 |
} |
|
| 184 |
|
|
| 185 |
/**
|
|
| 186 |
* @return
|
|
| 187 |
*/
|
|
| 188 | 5 |
public ApplicationContext getContext() {
|
| 189 | 5 |
return context;
|
| 190 |
} |
|
| 191 |
|
|
| 192 |
/**
|
|
| 193 |
* @param context
|
|
| 194 |
*/
|
|
| 195 | 3 |
public void setContext(ApplicationContext context) { |
| 196 | 3 |
this.context = context;
|
| 197 |
} |
|
| 198 |
|
|
| 199 |
/**
|
|
| 200 |
* @return
|
|
| 201 |
*/
|
|
| 202 | 5 |
public String getServerProcessBeanId() {
|
| 203 | 5 |
return serverProcessBeanId;
|
| 204 |
} |
|
| 205 |
|
|
| 206 |
/**
|
|
| 207 |
* @param string
|
|
| 208 |
*/
|
|
| 209 | 3 |
public void setServerProcessBeanId(String string) { |
| 210 | 3 |
serverProcessBeanId = string; |
| 211 |
} |
|
| 212 |
|
|
| 213 |
} |
|
| 214 |
|
|
||||||||||