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