Coverage Report - org.mule.api.security.tls.TlsPropertiesSocketFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
TlsPropertiesSocketFactory
55%
16/29
50%
1/2
1.7
 
 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  2
     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  2
         super();
 37  2
         logger.debug("creating: " + anon + "; " + namespace);
 38  2
         this.anon = anon;
 39  2
         this.namespace = namespace;
 40  2
     }
 41  
 
 42  
     private synchronized SSLSocketFactory getFactory() throws IOException
 43  
     {
 44  2
         if (null == factory)
 45  
         {
 46  2
             logger.debug("creating factory");
 47  2
             TlsPropertiesMapper propertiesMapper = new TlsPropertiesMapper(namespace);
 48  2
             TlsConfiguration configuration = new TlsConfiguration(TlsConfiguration.DEFAULT_KEYSTORE); 
 49  2
             propertiesMapper.readFromProperties(configuration, System.getProperties());
 50  
             try 
 51  
             {
 52  2
                 configuration.initialise(anon, namespace);
 53  2
                 factory = configuration.getSocketFactory();
 54  
             } 
 55  0
             catch (Exception e)
 56  
             {
 57  0
                 throw (IOException) new IOException(e.getMessage()).initCause(e);
 58  2
             }
 59  
         }
 60  2
         return factory;
 61  
     }
 62  
 
 63  
     public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException
 64  
     {
 65  0
         return getFactory().createSocket(s, host, port, autoClose);
 66  
     }
 67  
 
 68  
     public String[] getDefaultCipherSuites()
 69  
     {
 70  
         try 
 71  
         {
 72  0
             return getFactory().getDefaultCipherSuites();
 73  
         }
 74  0
         catch (Exception e)
 75  
         {
 76  0
             return new String[0];
 77  
         }
 78  
     }
 79  
 
 80  
     public String[] getSupportedCipherSuites()
 81  
     {
 82  
         try 
 83  
         {
 84  2
             return getFactory().getSupportedCipherSuites();
 85  
         }
 86  0
         catch (Exception e)
 87  
         {
 88  0
             return new String[0];
 89  
         }
 90  
     }
 91  
 
 92  
     public Socket createSocket(String arg0, int arg1) throws IOException
 93  
     {
 94  0
         return getFactory().createSocket(arg0, arg1);
 95  
     }
 96  
 
 97  
     public Socket createSocket(InetAddress arg0, int arg1) throws IOException
 98  
     {
 99  0
         return getFactory().createSocket(arg0, arg1);
 100  
     }
 101  
 
 102  
     public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException
 103  
     {
 104  0
         return getFactory().createSocket(arg0, arg1);
 105  
     }
 106  
 
 107  
     public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
 108  
     {
 109  0
         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  0
         return getFactory().createSocket();
 116  
     } 
 117  
     
 118  
 }
 119  
 
 120