KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > server > ssl > SSLProtocol


1 package com.ubermq.jms.server.ssl;
2 import com.ubermq.jms.client.unicast.*;
3 import com.ubermq.jms.common.*;
4 import com.ubermq.jms.common.datagram.impl.*;
5 import com.ubermq.jms.common.ssl.*;
6 import com.ubermq.jms.server.*;
7 import com.ubermq.kernel.*;
8 import java.io.*;
9 import java.net.*;
10 import java.nio.channels.*;
11 import javax.net.ssl.*;
12
13 /**
14  * A protocol handler for SSL.
15  *
16  * @since 2.1
17  */

18 public class SSLProtocol
19     extends DefaultProtocol
20     implements Protocol
21 {
22     private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(SSLProtocol.class);
23     
24     public static InetSocketAddress getConfiguredBindAddress()
25     {
26         return getBindAddress(Configurator.getProperty(ServerConfig.SSL_BIND_ADDRESS),
27                               Integer.valueOf(Configurator.getProperty(ServerConfig.SSL_PORT,
28                                                                        String.valueOf(MessageConstants.DEFAULT_SSL_PORT))).intValue());
29     }
30
31     private final boolean SSL_ENABLED =
32         Boolean.valueOf(Configurator.getProperty(ServerConfig.SSL_ENABLE, "false")).booleanValue();
33
34     private boolean enabled;
35     private IDatagramFactory factory;
36     private PipeEndpoint pipeEndpoint;
37
38     private SSLServerSocket s;
39     private AcceptThread acceptThread;
40
41     public SSLProtocol(IDatagramFactory f,
42                        PipeEndpoint e,
43                        InetSocketAddress bindAddress)
44     {
45         super(f, bindAddress);
46         this.factory = f;
47         this.pipeEndpoint = e;
48         this.enabled = SSL_ENABLED; // use default from the properties. can be overridden.
49
}
50
51     /**
52      * Overrides the default enabled state of the SSL protocol.
53      * The default state is taken from a properties file.
54      *
55      * @param f a boolean indicating whether
56      * the protocol is enabled.
57      */

58     public void setEnabled(boolean f)
59     {
60         this.enabled = f;
61     }
62
63     public boolean isEnabled()
64     {
65         return enabled;
66     }
67
68     public void start(IMessageProcessor dp,
69                       IConnectionInfo.ConnectionAcceptor a)
70         throws IOException
71     {
72         // load keystore
73
try
74         {
75             System.setProperty("javax.net.ssl.keyStore", Configurator.getProperty(ServerConfig.SSL_KEYSTORE));
76             System.setProperty("javax.net.ssl.keyStorePassword", Configurator.getProperty(ServerConfig.SSL_KEYSTORE_PASSWORD));
77             if (Configurator.getProperty(ServerConfig.SSL_KEYSTORE_TYPE) != null)
78                 System.setProperty("javax.net.ssl.keyStoreType", Configurator.getProperty(ServerConfig.SSL_KEYSTORE_TYPE));
79             this.s = (SSLServerSocket)SSLServerSocketFactory.getDefault().createServerSocket(getBindAddress().getPort(),
80                                                                                              10,
81                                                                                              getBindAddress().getAddress());
82             s.setEnabledCipherSuites(s.getSupportedCipherSuites());
83         }
84         catch (IOException x) {throw x;}
85         catch (Exception JavaDoc e)
86         {
87             throw new IllegalStateException JavaDoc(e.getMessage());
88         }
89
90         acceptThread = new AcceptThread(dp);
91         acceptThread.setDaemon(true);
92         acceptThread.start();
93     }
94
95     private final class AcceptThread
96         extends Thread JavaDoc
97     {
98         private IMessageProcessor dp;
99         
100         AcceptThread(IMessageProcessor dp)
101         {
102             super("SSLProtocol Acceptor");
103             this.dp = dp;
104         }
105
106         public void run()
107         {
108             try
109             {
110                 while(!isInterrupted())
111                 {
112                     final SSLSocket accepted = (SSLSocket)s.accept();
113                     accepted.setTcpNoDelay(true);
114
115                     // open pipes
116
final Pipe tosocket = Pipe.open(), fromsocket = Pipe.open();
117
118                     // connect the pipes
119
PipeConnectionInfo pci = pipeEndpoint.connectPipes(fromsocket, tosocket, factory);
120                     pci.setOriginalConnection(new PlainSocketConnectionInfo(accepted, factory, dp));
121
122                     // start I/O
123
IONormalizer.normalize(fromsocket, tosocket, accepted);
124                 }
125             }
126             catch (IOException e)
127             {
128                 log.error("", e);
129                 return;
130             }
131         }
132     }
133
134     public void stop()
135     {
136         acceptThread.interrupt();
137     }
138
139     public String JavaDoc toString()
140     {
141         return "Secure UberMQ " + getServiceURI();
142     }
143
144     /**
145      * Returns the service URI for this protocol. This may only
146      * be called after the start method has successfully completed.
147      *
148      * @return an URI describing how to connect to
149      * this protocol, or null if a URI cannot be used.
150      */

151     public java.net.URI JavaDoc getServiceURI()
152     {
153         return getServiceURI("ubermqs", MessageConstants.DEFAULT_SSL_PORT);
154     }
155
156
157 }
158
159
Popular Tags