~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/security/CustomSSLSocketFactory.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * JBoss, Home of Professional Open Source
 
3
 */
 
4
package org.jboss.remoting.security;
 
5
 
 
6
import javax.net.ssl.SSLSocket;
 
7
import javax.net.ssl.SSLSocketFactory;
 
8
import java.io.IOException;
 
9
import java.net.InetAddress;
 
10
import java.net.Socket;
 
11
import java.net.UnknownHostException;
 
12
 
 
13
/**
 
14
 * SSL socket factory whose configuration is customized.
 
15
 *
 
16
 * @author  <a href="mailto:mazz@jboss.com">John Mazzitelli</a>
 
17
 * @version $Revision: 1099 $
 
18
 */
 
19
public class CustomSSLSocketFactory
 
20
   extends SSLSocketFactory
 
21
{
 
22
   private SSLSocketFactory theDelegate;
 
23
   private SSLSocketBuilderMBean theBuilder;
 
24
 
 
25
   /**
 
26
    * Constructor for {@link CustomSSLSocketFactory}. The factory can be <code>null</code> - call
 
27
    * {@link #setFactory(SSLSocketFactory)} to set it later.
 
28
    *
 
29
    * @param factory the true factory this class delegates to
 
30
    * @param builder the class that built this custom factory - contains all the configuration for this factory
 
31
    */
 
32
   public CustomSSLSocketFactory( SSLSocketFactory factory,
 
33
                                  SSLSocketBuilder builder )
 
34
   {
 
35
      super();
 
36
      theBuilder  = builder;
 
37
      theDelegate = factory;
 
38
   }
 
39
 
 
40
   public CustomSSLSocketFactory()
 
41
   {
 
42
 
 
43
   }
 
44
 
 
45
   /**
 
46
    * Sets the builder that created the factory.  
 
47
    * @param sslSocketBuilder
 
48
    */
 
49
   public void setSSLSocketBuilder(SSLSocketBuilderMBean sslSocketBuilder)
 
50
   {
 
51
      this.theBuilder = sslSocketBuilder;
 
52
   }
 
53
 
 
54
 
 
55
   /**
 
56
    * Returns the builder that created this factory.  You can obtain the configuration of this factory
 
57
    * by examining the returned object's configuration.
 
58
    *
 
59
    * @return the builder
 
60
    */
 
61
   public SSLSocketBuilderMBean getSSLSocketBuilder()
 
62
   {
 
63
      return theBuilder;
 
64
   }
 
65
   
 
66
   /**
 
67
    * Sets a new factory in this object - this is the factory that this object will use to create new sockets.
 
68
    *
 
69
    * @param  factory the new factory
 
70
    *
 
71
    * @throws IllegalArgumentException if factory is <code>null</code>
 
72
    */
 
73
   public void setFactory( SSLSocketFactory factory )
 
74
   {
 
75
      if ( factory == null )
 
76
      {
 
77
         throw new IllegalArgumentException( "Factory cannot be null" );
 
78
      }
 
79
 
 
80
      theDelegate = factory;
 
81
   }
 
82
 
 
83
   /**
 
84
    * @see javax.net.ssl.SSLSocketFactory#createSocket(java.net.Socket, java.lang.String, int, boolean)
 
85
    */
 
86
   public Socket createSocket( Socket  s,
 
87
                               String  host,
 
88
                               int     port,
 
89
                               boolean autoClose )
 
90
   throws IOException
 
91
   {
 
92
      SSLSocket sock = (SSLSocket) theDelegate.createSocket( s, host, port, autoClose );
 
93
      setSocketModes( sock );
 
94
      return sock;
 
95
   }
 
96
 
 
97
   /**
 
98
    * @see javax.net.SocketFactory#createSocket()
 
99
    */
 
100
   public Socket createSocket()
 
101
   throws IOException
 
102
   {
 
103
      SSLSocket sock = (SSLSocket) theDelegate.createSocket();
 
104
      setSocketModes( sock );
 
105
      return sock;
 
106
   }
 
107
 
 
108
   /**
 
109
    * @see javax.net.SocketFactory#createSocket(java.net.InetAddress, int, java.net.InetAddress, int)
 
110
    */
 
111
   public Socket createSocket( InetAddress address,
 
112
                               int         port,
 
113
                               InetAddress localAddress,
 
114
                               int         localPort )
 
115
   throws IOException
 
116
   {
 
117
      SSLSocket sock = (SSLSocket) theDelegate.createSocket( address, port, localAddress, localPort );
 
118
      setSocketModes( sock );
 
119
      return sock;
 
120
   }
 
121
 
 
122
   /**
 
123
    * @see javax.net.SocketFactory#createSocket(java.net.InetAddress, int)
 
124
    */
 
125
   public Socket createSocket( InetAddress host,
 
126
                               int         port )
 
127
   throws IOException
 
128
   {
 
129
      SSLSocket sock = (SSLSocket) theDelegate.createSocket( host, port );
 
130
      setSocketModes( sock );
 
131
      return sock;
 
132
   }
 
133
 
 
134
   /**
 
135
    * @see javax.net.SocketFactory#createSocket(java.lang.String, int, java.net.InetAddress, int)
 
136
    */
 
137
   public Socket createSocket( String      host,
 
138
                               int         port,
 
139
                               InetAddress localHost,
 
140
                               int         localPort )
 
141
   throws IOException,
 
142
          UnknownHostException
 
143
   {
 
144
      SSLSocket sock = (SSLSocket) theDelegate.createSocket( host, port, localHost, localPort );
 
145
      setSocketModes( sock );
 
146
      return sock;
 
147
   }
 
148
 
 
149
   /**
 
150
    * @see javax.net.SocketFactory#createSocket(java.lang.String, int)
 
151
    */
 
152
   public Socket createSocket( String host,
 
153
                               int    port )
 
154
   throws IOException,
 
155
          UnknownHostException
 
156
   {
 
157
      SSLSocket sock = (SSLSocket) theDelegate.createSocket( host, port );
 
158
      setSocketModes( sock );
 
159
      return sock;
 
160
   }
 
161
 
 
162
   /**
 
163
    * @see javax.net.ssl.SSLSocketFactory#getDefaultCipherSuites()
 
164
    */
 
165
   public String[] getDefaultCipherSuites()
 
166
   {
 
167
      return theDelegate.getDefaultCipherSuites();
 
168
   }
 
169
 
 
170
   /**
 
171
    * @see javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites()
 
172
    */
 
173
   public String[] getSupportedCipherSuites()
 
174
   {
 
175
      return theDelegate.getSupportedCipherSuites();
 
176
   }
 
177
 
 
178
   /**
 
179
    * @see java.lang.Object#equals(java.lang.Object)
 
180
    */
 
181
   public boolean equals( Object obj )
 
182
   {
 
183
      return theDelegate.equals( obj );
 
184
   }
 
185
 
 
186
   /**
 
187
    * @see java.lang.Object#hashCode()
 
188
    */
 
189
   public int hashCode()
 
190
   {
 
191
      return theDelegate.hashCode();
 
192
   }
 
193
 
 
194
   /**
 
195
    * @see java.lang.Object#toString()
 
196
    */
 
197
   public String toString()
 
198
   {
 
199
      return theDelegate.toString();
 
200
   }
 
201
 
 
202
   /**
 
203
    * Sets the socket modes according to the custom configuration.
 
204
    *
 
205
    * @param sock the socket whose modes are to be set
 
206
    */
 
207
   private void setSocketModes( SSLSocket sock )
 
208
   {
 
209
      if ( theBuilder != null )
 
210
      {
 
211
         sock.setUseClientMode( theBuilder.isSocketUseClientMode() );
 
212
 
 
213
         if ( theBuilder.isClientAuthModeWant() )
 
214
         {
 
215
            sock.setNeedClientAuth( false );
 
216
            sock.setWantClientAuth( true );
 
217
         }
 
218
         else if ( theBuilder.isClientAuthModeNeed() )
 
219
         {
 
220
            sock.setWantClientAuth( false );
 
221
            sock.setNeedClientAuth( true );
 
222
         }
 
223
         else
 
224
         {
 
225
            sock.setWantClientAuth( false );
 
226
            sock.setNeedClientAuth( false );
 
227
         }
 
228
      }
 
229
 
 
230
      return;
 
231
   }
 
232
}
 
 
b'\\ No newline at end of file'