View Javadoc

1   /*
2    * $Id: TlsPropertiesSocketFactory.java 12269 2008-07-10 04:19:03Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.api.security.tls;
12  
13  import java.io.IOException;
14  import java.net.InetAddress;
15  import java.net.Socket;
16  
17  import javax.net.ssl.SSLSocketFactory;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  /**
23   * A socket factory that is configured via Properties, using a {@link TlsConfiguration}
24   * that has been stored via {@link TlsPropertiesMapper}.
25   */
26  public class TlsPropertiesSocketFactory extends SSLSocketFactory
27  {
28  
29      private Log logger = LogFactory.getLog(getClass());
30      private boolean anon;
31      private String namespace;
32      private SSLSocketFactory factory;
33  
34      public TlsPropertiesSocketFactory(boolean anon, String namespace)
35      {
36          super();
37          logger.debug("creating: " + anon + "; " + namespace);
38          this.anon = anon;
39          this.namespace = namespace;
40      }
41  
42      private synchronized SSLSocketFactory getFactory() throws IOException
43      {
44          if (null == factory)
45          {
46              logger.debug("creating factory");
47              TlsPropertiesMapper propertiesMapper = new TlsPropertiesMapper(namespace);
48              TlsConfiguration configuration = new TlsConfiguration(TlsConfiguration.DEFAULT_KEYSTORE); 
49              propertiesMapper.readFromProperties(configuration, System.getProperties());
50              try 
51              {
52                  configuration.initialise(anon, namespace);
53                  factory = configuration.getSocketFactory();
54              } 
55              catch (Exception e)
56              {
57                  throw (IOException) new IOException(e.getMessage()).initCause(e);
58              }
59          }
60          return factory;
61      }
62  
63      public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException
64      {
65          return getFactory().createSocket(s, host, port, autoClose);
66      }
67  
68      public String[] getDefaultCipherSuites()
69      {
70          try 
71          {
72              return getFactory().getDefaultCipherSuites();
73          }
74          catch (Exception e)
75          {
76              return new String[0];
77          }
78      }
79  
80      public String[] getSupportedCipherSuites()
81      {
82          try 
83          {
84              return getFactory().getSupportedCipherSuites();
85          }
86          catch (Exception e)
87          {
88              return new String[0];
89          }
90      }
91  
92      public Socket createSocket(String arg0, int arg1) throws IOException
93      {
94          return getFactory().createSocket(arg0, arg1);
95      }
96  
97      public Socket createSocket(InetAddress arg0, int arg1) throws IOException
98      {
99          return getFactory().createSocket(arg0, arg1);
100     }
101 
102     public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException
103     {
104         return getFactory().createSocket(arg0, arg1);
105     }
106 
107     public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
108     {
109         return getFactory().createSocket(arg0, arg1, arg2, arg3);
110     }
111 
112     // see http://forum.java.sun.com/thread.jspa?threadID=701799&messageID=4280973
113     public Socket createSocket() throws IOException
114     {
115         return getFactory().createSocket();
116     } 
117     
118 }
119 
120