View Javadoc

1   /*
2    * $Id: TlsPropertiesSocketFactory.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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      @Override
64      public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException
65      {
66          return getFactory().createSocket(s, host, port, autoClose);
67      }
68  
69      @Override
70      public String[] getDefaultCipherSuites()
71      {
72          try 
73          {
74              return getFactory().getDefaultCipherSuites();
75          }
76          catch (Exception e)
77          {
78              return new String[0];
79          }
80      }
81  
82      @Override
83      public String[] getSupportedCipherSuites()
84      {
85          try 
86          {
87              return getFactory().getSupportedCipherSuites();
88          }
89          catch (Exception e)
90          {
91              return new String[0];
92          }
93      }
94  
95      @Override
96      public Socket createSocket(String host, int port) throws IOException
97      {
98          return getFactory().createSocket(host, port);
99      }
100 
101     @Override
102     public Socket createSocket(InetAddress host, int port) throws IOException
103     {
104         return getFactory().createSocket(host, port);
105     }
106 
107     @Override
108     public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException
109     {
110         return getFactory().createSocket(host, port);
111     }
112 
113     @Override
114     public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException
115     {
116         return getFactory().createSocket(address, port, localAddress, localPort);
117     }
118 
119     // see http://forum.java.sun.com/thread.jspa?threadID=701799&messageID=4280973
120     @Override
121     public Socket createSocket() throws IOException
122     {
123         return getFactory().createSocket();
124     } 
125     
126 }
127 
128