001: /*
002: * $Id: TlsPropertiesSocketFactory.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.api.security.tls;
012:
013: import java.io.IOException;
014: import java.net.InetAddress;
015: import java.net.Socket;
016:
017: import javax.net.ssl.SSLSocketFactory;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: /**
023: * A socket factory that is configured via Properties, using a {@link TlsConfiguration}
024: * that has been stored via {@link TlsPropertiesMapper}.
025: */
026: public class TlsPropertiesSocketFactory extends SSLSocketFactory {
027:
028: private Log logger = LogFactory.getLog(getClass());
029: private boolean anon;
030: private String namespace;
031: private SSLSocketFactory factory;
032:
033: public TlsPropertiesSocketFactory(boolean anon, String namespace) {
034: logger.debug("creating: " + anon + "; " + namespace);
035: this .anon = anon;
036: this .namespace = namespace;
037: }
038:
039: private synchronized SSLSocketFactory getFactory()
040: throws IOException {
041: if (null == factory) {
042: logger.debug("creating factory");
043: TlsPropertiesMapper propertiesMapper = new TlsPropertiesMapper(
044: namespace);
045: TlsConfiguration configuration = new TlsConfiguration(
046: TlsConfiguration.DEFAULT_KEYSTORE);
047: propertiesMapper.readFromProperties(configuration, System
048: .getProperties());
049: try {
050: configuration.initialise(anon, namespace);
051: factory = configuration.getSocketFactory();
052: } catch (Exception e) {
053: throw (IOException) new IOException(e.getMessage())
054: .initCause(e);
055: }
056: }
057: return factory;
058: }
059:
060: public Socket createSocket(Socket s, String host, int port,
061: boolean autoClose) throws IOException {
062: return getFactory().createSocket(s, host, port, autoClose);
063: }
064:
065: public String[] getDefaultCipherSuites() {
066: try {
067: return getFactory().getDefaultCipherSuites();
068: } catch (Exception e) {
069: return new String[0];
070: }
071: }
072:
073: public String[] getSupportedCipherSuites() {
074: try {
075: return getFactory().getSupportedCipherSuites();
076: } catch (Exception e) {
077: return new String[0];
078: }
079: }
080:
081: public Socket createSocket(String arg0, int arg1)
082: throws IOException {
083: return getFactory().createSocket(arg0, arg1);
084: }
085:
086: public Socket createSocket(InetAddress arg0, int arg1)
087: throws IOException {
088: return getFactory().createSocket(arg0, arg1);
089: }
090:
091: public Socket createSocket(String arg0, int arg1, InetAddress arg2,
092: int arg3) throws IOException {
093: return getFactory().createSocket(arg0, arg1);
094: }
095:
096: public Socket createSocket(InetAddress arg0, int arg1,
097: InetAddress arg2, int arg3) throws IOException {
098: return getFactory().createSocket(arg0, arg1, arg2, arg3);
099: }
100:
101: // see http://forum.java.sun.com/thread.jspa?threadID=701799&messageID=4280973
102: public Socket createSocket() throws IOException {
103: return getFactory().createSocket();
104: }
105:
106: }
|