KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > transport > socket > ssl > SSLSocketServerInvoker


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.transport.socket.ssl;
8
9 import java.io.IOException JavaDoc;
10 import java.net.InetAddress JavaDoc;
11 import java.net.ServerSocket JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Properties JavaDoc;
14 import javax.management.MBeanServer JavaDoc;
15 import javax.management.MBeanServerInvocationHandler JavaDoc;
16 import javax.management.MalformedObjectNameException JavaDoc;
17 import javax.management.ObjectName JavaDoc;
18 import javax.net.ServerSocketFactory;
19 import javax.net.ssl.SSLServerSocketFactory;
20 import org.jboss.remoting.InvokerLocator;
21 import org.jboss.remoting.security.ServerSocketFactoryMBean;
22 import org.jboss.remoting.transport.socket.SocketServerInvoker;
23
24 /**
25  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
26  */

27 public class SSLSocketServerInvoker extends SocketServerInvoker implements SSLSocketServerInvokerMBean
28 {
29    protected ServerSocketFactory serverSocketFactory = null;
30
31
32    public SSLSocketServerInvoker(InvokerLocator locator)
33    {
34       super(locator);
35    }
36
37    public SSLSocketServerInvoker(InvokerLocator locator, Map JavaDoc configuration)
38    {
39       super(locator, configuration);
40    }
41
42    protected ServerSocket JavaDoc createServerSocket(int serverBindPort, int backlog, InetAddress JavaDoc bindAddress) throws IOException JavaDoc
43    {
44       ServerSocket JavaDoc svrSocket = null;
45       Properties JavaDoc props = getProperties();
46
47       /**
48        * At this point in time, this can ONLY be set programmatically (via setServerSocketFactory),
49        * so if it is set, then will use this. Otherwise, will try config and then just use new ServerSocket
50        * if all else failse. This is due to JMX based DI (see more below for more).
51        */

52       if(serverSocketFactory != null)
53       {
54          svrSocket = serverSocketFactory.createServerSocket(serverBindPort, backlog, bindAddress);
55       }
56       else
57       {
58
59          /**
60           * TODO: -TME This is another big hack because of dependancy on JMX within configuration.
61           * Have to wait till the mbean server is set before can actually set the server socket
62           * factory since it is an mbean (new server's DI will fix all this). Would prefer this
63           * to be in the setup() method...
64           * Also, I can't cast the mbean proxy directly to ServerSocketFactory because it is not
65           * an interface. Therefore, have to require that ServerSocketFactoryMBean is used.
66           */

67          if(props.getProperty("serverSocketFactory") != null)
68          {
69             try
70             {
71                String JavaDoc serverSocketFactoryString = props.getProperty("serverSocketFactory");
72                if(serverSocketFactoryString != null)
73                {
74                   MBeanServer JavaDoc server = getMBeanServer();
75                   ObjectName JavaDoc serverSocketFactoryObjName = new ObjectName JavaDoc(serverSocketFactoryString);
76                   if(server != null)
77                   {
78                      try
79                      {
80                         ServerSocketFactoryMBean serverSocketFactory = (ServerSocketFactoryMBean) MBeanServerInvocationHandler.newProxyInstance(server,
81                                                                                                                                                 serverSocketFactoryObjName,
82                                                                                                                                                 ServerSocketFactoryMBean.class,
83                                                                                                                                                 false);
84                         svrSocket = serverSocketFactory.createServerSocket(serverBindPort, backlog, bindAddress);
85                      }
86                      catch(Exception JavaDoc e)
87                      {
88                         log.error("Error creating mbean proxy for server socket factory for object name: " + serverSocketFactoryObjName, e);
89                         throw new IOException JavaDoc("Error createing custom server socket factory.");
90                      }
91                   }
92                   else
93                   {
94                      log.error("The 'serverSocketFactory' attribute was set with a value, but the MBeanServer reference is null.");
95                      throw new IOException JavaDoc("Error creating custom server socket factory. The invoker does not have a reference to the mbean server.");
96                   }
97                }
98             }
99             catch(MalformedObjectNameException JavaDoc e)
100             {
101                log.error("Error setting the server socket factory due to the attibute value passed not being a valid ObjectName.", e);
102                throw new IOException JavaDoc("Error creating custom server socket factory. The attributed value passed is not a valid object name.");
103             }
104             catch(NullPointerException JavaDoc e)
105             {
106                log.error("Error setting the server socket factory due to null pointer exception.", e);
107                throw new IOException JavaDoc("Error creating custom server socket factory.");
108             }
109          }
110       }
111
112       if(svrSocket == null)
113       {
114          log.debug("Creating default server socket.");
115          serverSocketFactory = SSLServerSocketFactory.getDefault();
116          svrSocket = serverSocketFactory.createServerSocket(serverBindPort, backlog, bindAddress);
117       }
118
119       log.debug("Created server socket: " + svrSocket);
120
121       return svrSocket;
122
123    }
124
125    /**
126     * Sets the server socket factory for the SocketServerInvoker to use.
127     * Can produce normal server socket or ssl server socket, depending
128     * on implementation passed.
129     *
130     * @param serverSocketFactory
131     */

132    public void setServerSocketFactory(ServerSocketFactory serverSocketFactory)
133    {
134       this.serverSocketFactory = serverSocketFactory;
135    }
136
137 }
Popular Tags