View Javadoc

1   /*
2    * $Id: TlsPropertiesSocketFactory.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.umo.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          logger.debug("creating: " + anon + "; " + namespace);
37          this.anon = anon;
38          this.namespace = namespace;
39      }
40  
41      private synchronized SSLSocketFactory getFactory() throws IOException
42      {
43          if (null == factory)
44          {
45              logger.debug("creating factory");
46              TlsPropertiesMapper propertiesMapper = new TlsPropertiesMapper(namespace);
47              TlsConfiguration configuration = new TlsConfiguration(TlsConfiguration.DEFAULT_KEYSTORE); 
48              propertiesMapper.readFromProperties(configuration, System.getProperties());
49              try 
50              {
51                  configuration.initialise(anon, namespace);
52                  factory = configuration.getSocketFactory();
53              } 
54              catch (Exception e)
55              {
56                  throw (IOException) new IOException(e.getMessage()).initCause(e);
57              }
58          }
59          return factory;
60      }
61  
62      public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException
63      {
64          return getFactory().createSocket(s, host, port, autoClose);
65      }
66  
67      public String[] getDefaultCipherSuites()
68      {
69          try 
70          {
71              return getFactory().getDefaultCipherSuites();
72          }
73          catch (Exception e)
74          {
75              return new String[0];
76          }
77      }
78  
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      public Socket createSocket(String arg0, int arg1) throws IOException
92      {
93          return getFactory().createSocket(arg0, arg1);
94      }
95  
96      public Socket createSocket(InetAddress arg0, int arg1) throws IOException
97      {
98          return getFactory().createSocket(arg0, arg1);
99      }
100 
101     public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException
102     {
103         return getFactory().createSocket(arg0, arg1);
104     }
105 
106     public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
107     {
108         return getFactory().createSocket(arg0, arg1, arg2, arg3);
109     }
110 
111     // see http://forum.java.sun.com/thread.jspa?threadID=701799&messageID=4280973
112     public Socket createSocket() throws IOException
113     {
114         return getFactory().createSocket();
115     } 
116     
117 }
118 
119