1 21 package com.presumo.jms.plugin.implementation.transport.tcp; 22 23 import java.util.Vector ; 24 25 import java.net.InetAddress ; 26 import java.net.Socket ; 27 import java.net.ServerSocket ; 28 29 import java.io.InterruptedIOException ; 30 import java.io.IOException ; 31 import javax.jms.JMSException ; 32 33 import com.presumo.jms.plugin.transport.ServerTransport; 34 import com.presumo.jms.resources.Resources; 35 import com.presumo.jms.router.ConnectionListener; 36 import com.presumo.jms.router.Router; 37 import com.presumo.jms.router.RemoteSession; 38 39 import com.presumo.util.config.Preferences; 40 import com.presumo.util.log.Logger; 41 import com.presumo.util.log.LoggerFactory; 42 43 49 public class ServerTransportImpl 50 implements ServerTransport, ConnectionListener, Runnable 51 { 52 public static final int DEFAULT_PORT = 2323; 53 public static final String DEFAULT_HOST = "localhost"; 54 public static final int DEFAULT_SO_TIMEOUT = 5000; 55 public static final int DEFAULT_ACCEPT_BACKLOG = 10; 56 public static final String ACCEPT_BACKLOG_KEY = "AcceptBacklog"; 57 public static final String CLASSNAME_KEY = "ServerTransportClass"; 58 59 private String host; 60 private int port; 61 private int acceptBacklog; 62 63 private Router router; 64 65 private ServerSocket serverSocket; 66 67 private Thread acceptThread; 68 69 private volatile boolean closed = false; 70 71 private Vector connections; 72 73 74 78 82 public ServerTransportImpl() 83 { 84 } 85 86 87 91 94 public void setURL(String url) 95 { 96 logger.entry("setURL: " + url); 97 98 host = "localhost"; 99 port = DEFAULT_PORT; 100 101 try { 102 host = getHost(url); 103 port = getPort(url); 104 } catch (JMSException jmsex) { 105 logger.exception(jmsex); 106 } 107 108 logger.exit("setURL"); 109 } 110 111 114 public void setPreferences(Preferences prefs) 115 { 116 logger.entry("setPreferences"); 117 118 String temp = prefs.get(ACCEPT_BACKLOG_KEY); 119 if (temp != null) { 120 try { 121 acceptBacklog = Integer.parseInt(temp); 122 } catch (java.lang.NumberFormatException nfe) { 123 logger.exception(nfe); 124 acceptBacklog = DEFAULT_ACCEPT_BACKLOG; 125 } 126 } else { 127 acceptBacklog = DEFAULT_ACCEPT_BACKLOG; 128 } 129 logger.exit("setPreferences"); 130 } 131 132 public void setRouter(Router r) 133 { 134 logger.entry("setRouter", r); 135 136 router = r; 137 138 logger.exit("setRouter"); 139 } 140 141 144 public synchronized void start() throws JMSException 145 { 146 logger.entry("start"); 147 148 if (closed) 149 throw new JMSException ("start() called on a closed server transport."); 150 151 try { 152 serverSocket = new ServerSocket (port, acceptBacklog, InetAddress.getByName(host)); 153 serverSocket.setSoTimeout(DEFAULT_SO_TIMEOUT); 154 } catch (IOException ioe) { 155 JMSException jmse = new JMSException (Resources.getResourceString("PJMSE0001")); 156 jmse.setLinkedException(ioe); 157 throw jmse; 158 } 159 160 acceptThread = new Thread (this, "JMS_TCP/IP_ServerSocket_Listener"); 161 acceptThread.start(); 162 163 logger.exit("start"); 164 } 165 166 167 170 public synchronized void close() 171 { 172 logger.entry("close"); 173 174 if (! closed) { 175 176 closed = true; 177 try { 178 serverSocket.close(); 179 180 } catch(IOException ioe) { 181 logger.exception(ioe); 182 } 183 184 try { 185 acceptThread.join(); 186 } catch (InterruptedException ie) {} 187 if (connections != null) { 188 for (int i=0; i < connections.size(); i++) { 189 RemoteSession session = (RemoteSession) connections.get(i); 190 session.close(); 191 } 192 } 193 194 } 195 196 logger.exit("close"); 197 } 198 199 204 public void run() 205 { 206 logger.entry("run"); 207 208 logger.info("PJMSI5001", host + ":" + port); 209 while(! closed) { 210 211 try { 212 Socket connection = serverSocket.accept(); 213 214 if (closed) continue; try { 217 logger.debug("Connection accepted from: " + connection.toString()); 218 219 TransportImpl client = new TransportImpl(connection); 220 RemoteSession session = new RemoteSession(router, client, this); 221 222 if (connections == null) 223 connections = new Vector (); 224 connections.add(session); 225 226 } catch (Exception jmse) { 227 logger.exception(jmse); 228 connection.close(); 229 } 230 } catch (InterruptedIOException iioe) { 231 } catch (IOException ioe) { 232 233 if (! closed) { 237 logger.exception(ioe); 238 } 239 } 240 } 241 242 logger.exit("run"); 243 } 244 245 248 public void connectionLost(RemoteSession session) 249 { 250 session.close(); 251 connections.remove(session); 252 } 253 254 255 256 260 private String getHost(String url) throws JMSException 261 { 262 logger.entry("getHost", url); 263 264 int loc = url.lastIndexOf('/'); 265 int loc2 = url.lastIndexOf(':'); 266 267 if (loc == -1 || loc2 == -1) { 268 throw new JMSException ("Malformed URL: " + url); 269 } 270 271 String retval = url.substring(loc+1, loc2); 272 logger.exit("getHost", retval); 273 return retval; 274 } 275 276 private int getPort(String url) throws JMSException 277 { 278 logger.entry("getPort", url); 279 280 int loc = url.lastIndexOf(':'); 281 if (loc == -1) { 282 throw new JMSException ("Malformed URL: " + url); 283 } 284 285 int retval = 0; 286 try { 287 retval = Integer.parseInt(url.substring(loc+1)); 288 } catch (NumberFormatException nfe) { 289 JMSException jmsex = new JMSException ("Malformed URL: " + url); 290 jmsex.setLinkedException(nfe); 291 throw jmsex; 292 } 293 294 logger.exit("getPort", new Integer (retval)); 295 return retval; 296 } 297 298 299 private static Logger logger = 301 LoggerFactory.getLogger(ServerTransportImpl.class, Resources.getBundle()); 302 304 } 305 | Popular Tags |