KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > transport > http > ssl > HTTPSServerInvoker


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.http.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 javax.management.MBeanServer JavaDoc;
14 import javax.management.MBeanServerInvocationHandler JavaDoc;
15 import javax.management.MalformedObjectNameException JavaDoc;
16 import javax.management.ObjectName JavaDoc;
17 import javax.net.ServerSocketFactory;
18 import javax.net.ssl.SSLServerSocketFactory;
19 import org.jboss.remoting.InvokerLocator;
20 import org.jboss.remoting.security.ServerSocketFactoryMBean;
21 import org.jboss.remoting.transport.http.HTTPServerInvoker;
22
23 /**
24  * Extension of the HTTPserverInvoker which uses a custom server socket
25  * which is created using custom server socket factory that can support SSL.
26  *
27  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
28  */

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

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

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

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