KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > il > uil2 > UILServerILService


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq.il.uil2;
23
24 import java.io.IOException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.net.ServerSocket JavaDoc;
28 import java.net.Socket JavaDoc;
29 import java.net.UnknownHostException JavaDoc;
30 import java.util.Properties JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.net.ServerSocketFactory;
33
34 import org.jboss.mq.il.Invoker;
35 import org.jboss.mq.il.ServerIL;
36 import org.jboss.mq.il.ServerILJMXService;
37 import org.jboss.mq.il.uil2.msgs.MsgTypes;
38 import org.jboss.mq.il.uil2.msgs.BaseMsg;
39 import org.jboss.security.SecurityDomain;
40 import org.jboss.system.server.ServerConfigUtil;
41
42 /** This is the server side MBean for the UIL2 transport layer.
43  *
44  * @author Scott.Stark@jboss.org
45  * @version $Revision: 45305 $
46  *
47  * @jmx:mbean extends="org.jboss.mq.il.ServerILJMXServiceMBean"
48  */

49 public class UILServerILService extends ServerILJMXService
50       implements MsgTypes, Runnable JavaDoc, UILServerILServiceMBean
51 {
52    final static int SO_TIMEOUT = 5000;
53
54    /** The security domain name to use with SSL aware socket factories.
55     */

56    private String JavaDoc securityDomain;
57    /* The javax.net.SocketFactory implementation class to use on the client.
58     */

59    private String JavaDoc clientSocketFactoryName;
60    /** The socket factory used to obtain the server socket.
61     */

62    private ServerSocketFactory serverSocketFactory;
63    /** The UIL2 server socket clients connect to
64     */

65    private ServerSocket JavaDoc serverSocket;
66    private UILServerIL serverIL;
67    private boolean running;
68    /** The server jms listening port */
69    private int serverBindPort = 0;
70    /** The server jms address the listening socket binds to */
71    private InetAddress JavaDoc bindAddress = null;
72    /** The thread that manages the client connection attempts */
73    private Thread JavaDoc acceptThread;
74    /** The address passed to the client il layer as the address that should
75     * be used to connect to the server.
76     */

77    private InetAddress JavaDoc clientAddress;
78    /** The address passed to the client il layer as the address that should
79     * be used to connect to the server.
80     */

81    private String JavaDoc connectAddress;
82    /** The port passed to the client il layer as the address that should
83     * be used to connect to the server.
84     */

85    private int connectPort;
86    /**
87     * If the TcpNoDelay option should be used on the socket.
88     */

89    private boolean enableTcpNoDelay = false;
90
91    /**
92     * The socket read timeout.
93     */

94    private int readTimeout = 0;
95
96    /**
97     * The client socket read timeout.
98     */

99    private int clientReadTimeout = 0;
100
101    /**
102     * The buffer size.
103     */

104    private int bufferSize = 1;
105
106    /**
107     * The chunk size.
108     */

109    private int chunkSize = 0x40000000;
110
111    /**
112     * The connection properties passed to the client to connect to this IL
113     */

114    private Properties JavaDoc connectionProperties;
115
116    /**
117     * Used to construct the GenericConnectionFactory (bindJNDIReferences()
118     * builds it) Sets up the connection properties need by a client to use this
119     * IL
120     *
121     * @return The ClientConnectionProperties value
122     */

123    public Properties JavaDoc getClientConnectionProperties()
124    {
125       return connectionProperties;
126    }
127
128    /**
129     * Used to construct the GenericConnectionFactory (bindJNDIReferences()
130     * builds it)
131     *
132     * @return The ServerIL value
133     * @return ServerIL the instance of this IL
134     */

135    public ServerIL getServerIL()
136    {
137       return serverIL;
138    }
139
140    /** Client socket accept thread.
141     */

142    public void run()
143    {
144       boolean trace = log.isTraceEnabled();
145       while (running)
146       {
147          Socket JavaDoc socket = null;
148          SocketManager socketMgr = null;
149          try
150          {
151             socket = serverSocket.accept();
152             if( trace )
153                log.trace("Accepted connection: "+socket);
154             socket.setSoTimeout(readTimeout);
155             socket.setTcpNoDelay(enableTcpNoDelay);
156             socketMgr = new SocketManager(socket);
157             ServerSocketManagerHandler handler = new ServerSocketManagerHandler(getJMSServer(), socketMgr);
158             socketMgr.setHandler(handler);
159             socketMgr.setBufferSize(bufferSize);
160             socketMgr.setChunkSize(chunkSize);
161             Invoker s = getJMSServer();
162             socketMgr.start(s.getThreadGroup());
163          }
164          catch (IOException JavaDoc e)
165          {
166             if (running)
167                log.warn("Failed to setup client connection", e);
168          }
169          catch(Throwable JavaDoc e)
170          {
171             if (running || trace)
172                log.warn("Unexpected error in setup of client connection", e);
173          }
174       }
175    }
176
177    /**
178     * Starts this IL, and binds it to JNDI
179     *
180     * @exception Exception Description of Exception
181     */

182    public void startService() throws Exception JavaDoc
183    {
184       super.startService();
185       running = true;
186
187       // Use the default javax.net.ServerSocketFactory if none was set
188
if (serverSocketFactory == null)
189          serverSocketFactory = ServerSocketFactory.getDefault();
190
191       /* See if the server socket supports setSecurityDomain(SecurityDomain)
192       if an securityDomain was specified
193       */

194       if (securityDomain != null)
195       {
196          try
197          {
198             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
199             Class JavaDoc ssfClass = serverSocketFactory.getClass();
200             SecurityDomain domain = (SecurityDomain) ctx.lookup(securityDomain);
201             Class JavaDoc[] parameterTypes = {SecurityDomain.class};
202             Method JavaDoc m = ssfClass.getMethod("setSecurityDomain", parameterTypes);
203             Object JavaDoc[] args = {domain};
204             m.invoke(serverSocketFactory, args);
205          }
206          catch (NoSuchMethodException JavaDoc e)
207          {
208             log.error("Socket factory does not support setSecurityDomain(SecurityDomain)");
209          }
210          catch (Exception JavaDoc e)
211          {
212             log.error("Failed to setSecurityDomain=" + securityDomain + " on socket factory");
213          }
214       }
215
216       // Create the server socket using the socket factory
217
serverSocket = serverSocketFactory.createServerSocket(serverBindPort, 50, bindAddress);
218
219       InetAddress JavaDoc socketAddress = serverSocket.getInetAddress();
220       log.info("JBossMQ UIL service available at : " + socketAddress + ":" + serverSocket.getLocalPort());
221       acceptThread = new Thread JavaDoc(getJMSServer().getThreadGroup(), this, "UILServerILService Accept Thread");
222       acceptThread.start();
223
224       /* We need to check the socketAddress against "0.0.0.0/0.0.0.0"
225          because this is not a valid address on Win32 while it is for
226          *NIX. See BugParade bug #4343286.
227       */

228       socketAddress = ServerConfigUtil.fixRemoteAddress(socketAddress);
229       // If an explicit client bind address has been specified use it
230
if( clientAddress != null )
231          socketAddress = clientAddress;
232       serverIL = new UILServerIL(socketAddress, serverSocket.getLocalPort(),
233             clientSocketFactoryName, enableTcpNoDelay, bufferSize, chunkSize, clientReadTimeout, connectAddress, connectPort);
234
235       // Initialize the connection poperties using the base class.
236
connectionProperties = super.getClientConnectionProperties();
237       connectionProperties.setProperty(UILServerILFactory.CLIENT_IL_SERVICE_KEY, UILClientILService.class.getName());
238       connectionProperties.setProperty(UILServerILFactory.UIL_PORT_KEY, "" + serverSocket.getLocalPort());
239       connectionProperties.setProperty(UILServerILFactory.UIL_ADDRESS_KEY, "" + socketAddress.getHostAddress());
240       connectionProperties.setProperty(UILServerILFactory.UIL_TCPNODELAY_KEY, enableTcpNoDelay ? "yes" : "no");
241       connectionProperties.setProperty(UILServerILFactory.UIL_BUFFERSIZE_KEY, "" + bufferSize);
242       connectionProperties.setProperty(UILServerILFactory.UIL_CHUNKSIZE_KEY, "" + chunkSize);
243       connectionProperties.setProperty(UILServerILFactory.UIL_RECEIVE_REPLIES_KEY, "No");
244       connectionProperties.setProperty(UILServerILFactory.UIL_SOTIMEOUT_KEY, "" + clientReadTimeout);
245       connectionProperties.setProperty(UILServerILFactory.UIL_CONNECTADDRESS_KEY, "" + connectAddress);
246       connectionProperties.setProperty(UILServerILFactory.UIL_CONNECTPORT_KEY, "" + connectPort);
247
248       bindJNDIReferences();
249       BaseMsg.setUseJMSServerMsgIDs(true);
250    }
251
252    /**
253     * Stops this IL, and unbinds it from JNDI
254     */

255    public void stopService()
256    {
257       try
258       {
259          running = false;
260          unbindJNDIReferences();
261
262          // unbind Server Socket if needed
263
if (serverSocket != null)
264          {
265             serverSocket.close();
266          }
267       }
268       catch (Exception JavaDoc e)
269       {
270          log.error("Exception occured when trying to stop UIL Service: ", e);
271       }
272    }
273
274    /**
275     * Get the UIL server listening port
276     *
277     * @return Value of property serverBindPort.
278     *
279     * @jmx:managed-attribute
280     */

281    public int getServerBindPort()
282    {
283       return serverBindPort;
284    }
285
286    /**
287     * Set the UIL server listening port
288     *
289     * @param serverBindPort New value of property serverBindPort.
290     *
291     * @jmx:managed-attribute
292     */

293    public void setServerBindPort(int serverBindPort)
294    {
295       this.serverBindPort = serverBindPort;
296    }
297
298    /**
299     * Get the interface address the UIL2 server bind its listening port on
300     *
301     * @jmx:managed-attribute
302     */

303    public String JavaDoc getBindAddress()
304    {
305       String JavaDoc addr = "0.0.0.0";
306       if (bindAddress != null)
307          addr = bindAddress.getHostName();
308       return addr;
309    }
310
311    /**
312     * Set the interface address the UIL2 server bind its listening port on
313     *
314     * @jmx:managed-attribute
315     */

316    public void setBindAddress(String JavaDoc host) throws UnknownHostException JavaDoc
317    {
318       // If host is null or empty use any address
319
if (host == null || host.length() == 0)
320          bindAddress = null;
321       else
322          bindAddress = InetAddress.getByName(host);
323    }
324    
325    public InetAddress JavaDoc getClientAddress()
326    {
327       return clientAddress;
328    }
329
330    public void setClientAddress(InetAddress JavaDoc addr)
331    {
332       log.warn("ClientAddress has been deprecated, use ConnectAddress");
333       clientAddress = addr;
334    }
335    
336    public String JavaDoc getConnectAddress()
337    {
338       return connectAddress;
339    }
340
341    public void setConnectAddress(String JavaDoc addr)
342    {
343       connectAddress = addr;
344    }
345    
346    public int getConnectPort()
347    {
348       return connectPort;
349    }
350
351    public void setConnectPort(int port)
352    {
353       connectPort = port;
354    }
355
356    /**
357     * Gets the enableTcpNoDelay.
358     * @return Returns a boolean
359     *
360     * @jmx:managed-attribute
361     */

362    public boolean getEnableTcpNoDelay()
363    {
364       return enableTcpNoDelay;
365    }
366
367    /**
368     * Sets the enableTcpNoDelay.
369     * @param enableTcpNoDelay The enableTcpNoDelay to set
370     *
371     * @jmx:managed-attribute
372     */

373    public void setEnableTcpNoDelay(boolean enableTcpNoDelay)
374    {
375       this.enableTcpNoDelay = enableTcpNoDelay;
376    }
377
378    /**
379     * Gets the buffer size.
380     * @return Returns an int
381     *
382     * @jmx:managed-attribute
383     */

384    public int getBufferSize()
385    {
386       return bufferSize;
387    }
388
389    /**
390     * Sets the buffer size.
391     * @param size the buffer size
392     *
393     * @jmx:managed-attribute
394     */

395    public void setBufferSize(int size)
396    {
397       this.bufferSize = size;
398    }
399
400    /**
401     * Gets the chunk size.
402     * @return Returns an int
403     *
404     * @jmx:managed-attribute
405     */

406    public int getChunkSize()
407    {
408       return chunkSize;
409    }
410
411    /**
412     * Sets the chunk size.
413     * @param size the chunk size
414     *
415     * @jmx:managed-attribute
416     */

417    public void setChunkSize(int size)
418    {
419       this.chunkSize = size;
420    }
421
422    public int getReadTimeout()
423    {
424       return readTimeout;
425    }
426
427    public void setReadTimeout(int timeout)
428    {
429       this.readTimeout = timeout;
430    }
431
432    public int getClientReadTimeout()
433    {
434       return clientReadTimeout;
435    }
436
437    public void setClientReadTimeout(int timeout)
438    {
439       this.clientReadTimeout = timeout;
440    }
441
442    /** Get the javax.net.SocketFactory implementation class to use on the
443     *client.
444     * @jmx:managed-attribute
445     */

446    public String JavaDoc getClientSocketFactory()
447    {
448       return clientSocketFactoryName;
449    }
450
451    /** Set the javax.net.SocketFactory implementation class to use on the
452     *client.
453     * @jmx:managed-attribute
454     */

455    public void setClientSocketFactory(String JavaDoc name)
456    {
457       this.clientSocketFactoryName = name;
458    }
459
460    /** Set the javax.net.ServerSocketFactory implementation class to use to
461     *create the service SocketFactory.
462     *@jmx:managed-attribute
463     */

464    public void setServerSocketFactory(String JavaDoc name) throws Exception JavaDoc
465    {
466       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
467       Class JavaDoc ssfClass = loader.loadClass(name);
468       serverSocketFactory = (ServerSocketFactory) ssfClass.newInstance();
469    }
470
471    /** Get the javax.net.ServerSocketFactory implementation class to use to
472     *create the service SocketFactory.
473     *@jmx:managed-attribute
474     */

475    public String JavaDoc getServerSocketFactory()
476    {
477       String JavaDoc name = null;
478       if (serverSocketFactory != null)
479          name = serverSocketFactory.getClass().getName();
480       return name;
481    }
482
483    /** Set the security domain name to use with SSL aware socket factories
484     *@jmx:managed-attribute
485     */

486    public void setSecurityDomain(String JavaDoc domainName)
487    {
488       this.securityDomain = domainName;
489    }
490
491    /** Get the security domain name to use with SSL aware socket factories
492     *@jmx:managed-attribute
493     */

494    public String JavaDoc getSecurityDomain()
495    {
496       return this.securityDomain;
497    }
498 }
499
Popular Tags