1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.ssl;
21  
22  
23  import java.io.IOException;
24  import java.net.InetAddress;
25  import java.net.Socket;
26  import java.net.UnknownHostException;
27  import java.security.GeneralSecurityException;
28  
29  import javax.net.SocketFactory;
30  
31  
32  /**
33   * Simple Socket factory to create sockets with or without SSL enabled.
34   * If SSL enabled a "bougus" SSL Context is used (suitable for test purposes)
35   * 
36   * @version $Rev: 683827 $, $Date: 2008-08-08 05:34:27 +0200 (Fr, 08 Aug 2008) $
37   */
38  public class SSLSocketFactory extends SocketFactory
39  {
40      private static boolean sslEnabled = true;
41  
42      private static javax.net.ssl.SSLSocketFactory sslFactory = null;
43  
44      private static javax.net.SocketFactory factory = null;
45  
46  
47      public static SocketFactory getDefault()
48      {
49          return new SSLSocketFactory();
50      }
51  
52  
53      public SSLSocketFactory()
54      {
55          super();
56      }
57  
58  
59      public Socket createSocket( String arg1, int arg2 ) throws IOException, UnknownHostException
60      {
61          if ( isSslEnabled() )
62          {
63              return getSSLFactory().createSocket( arg1, arg2 );
64          }
65          else
66          {
67              return new Socket( arg1, arg2 );
68          }
69      }
70  
71  
72      public Socket createSocket( String arg1, int arg2, InetAddress arg3, int arg4 ) throws IOException,
73          UnknownHostException
74      {
75          if ( isSslEnabled() )
76          {
77              return getSSLFactory().createSocket( arg1, arg2, arg3, arg4 );
78          }
79          else
80          {
81              return new Socket( arg1, arg2, arg3, arg4 );
82          }
83      }
84  
85  
86      public Socket createSocket( InetAddress arg1, int arg2 ) throws IOException
87      {
88          if ( isSslEnabled() )
89          {
90              return getSSLFactory().createSocket( arg1, arg2 );
91          }
92          else
93          {
94              return new Socket( arg1, arg2 );
95          }
96      }
97  
98  
99      public Socket createSocket( InetAddress arg1, int arg2, InetAddress arg3, int arg4 ) throws IOException
100     {
101         if ( isSslEnabled() )
102         {
103             return getSSLFactory().createSocket( arg1, arg2, arg3, arg4 );
104         }
105         else
106         {
107             return new Socket( arg1, arg2, arg3, arg4 );
108         }
109     }
110 
111 
112     public static javax.net.SocketFactory getSocketFactory()
113     {
114         if ( factory == null )
115         {
116             factory = new SSLSocketFactory();
117         }
118         return factory;
119     }
120 
121 
122     private javax.net.ssl.SSLSocketFactory getSSLFactory()
123     {
124         if ( sslFactory == null )
125         {
126             try
127             {
128                 sslFactory = BogusSSLContextFactory.getInstance( false ).getSocketFactory();
129             }
130             catch ( GeneralSecurityException e )
131             {
132                 throw new RuntimeException( "could not create SSL socket", e );
133             }
134         }
135         return sslFactory;
136     }
137 
138 
139     public static boolean isSslEnabled()
140     {
141         return sslEnabled;
142     }
143 
144 
145     public static void setSslEnabled( boolean newSslEnabled )
146     {
147         sslEnabled = newSslEnabled;
148     }
149 
150 }