View Javadoc

1   /*
2    * $Id: AutoDiscoverySecurityProviderFactory.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.provider;
12  
13  import org.mule.MuleRuntimeException;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.util.ClassUtils;
16  import org.mule.util.SystemUtils;
17  
18  import java.security.Provider;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * Automatically discovers the JDK we are running on and returns a corresponding
25   * {@link SecurityProviderInfo}. <p/> Implementations of this class are thread-safe.
26   * 
27   * @author <a href="mailto:aperepel@gmail.com">Andrew Perepelytsya</a>
28   */
29  public class AutoDiscoverySecurityProviderFactory implements SecurityProviderFactory
30  {
31  
32      /**
33       * Default is Sun's JSSE.
34       */
35      public static final SecurityProviderInfo DEFAULT_SECURITY_PROVIDER = new SunSecurityProviderInfo();
36  
37      /**
38       * Logger used by this class.
39       */
40      protected transient Log logger = LogFactory.getLog(getClass());
41  
42      /**
43       * Security provider properties for IBM JDK.
44       */
45      private static final SecurityProviderInfo IBM_SECURITY_PROVIDER = new IBMSecurityProviderInfo();
46  
47      /**
48       * Security provider properties for IBM JDK 1.4.2 and higher.
49       */
50      // private static final SecurityProviderInfo IBM_SECURITY_PROVIDER_2 = new
51      // IBMSecurityProvider2Info();
52  
53      public SecurityProviderInfo getSecurityProviderInfo()
54      {
55          SecurityProviderInfo info;
56  
57          if (SystemUtils.isIbmJDK())
58          {
59              // TODO test IBM JDK 1.4.2 more thoroughly and decide if
60              // it's worth including this newer provider support.
61              // switch to IBM's security provider
62              // if (SystemUtils.isJavaVersionAtLeast(142)) {
63              // IBM JSSE2
64              // info = IBM_SECURITY_PROVIDER_2;
65              // } else {
66              // older IBM JSSE
67              info = IBM_SECURITY_PROVIDER;
68              // }
69          }
70          else
71          {
72              info = DEFAULT_SECURITY_PROVIDER;
73  
74          }
75  
76          // BEA's JRockit uses Sun's JSSE, so defaults are fine
77  
78          return info;
79      }
80  
81      public Provider getProvider()
82      {
83          SecurityProviderInfo info = getSecurityProviderInfo();
84  
85          if (logger.isInfoEnabled())
86          {
87              logger.info("Using " + info.getClass().getName());
88          }
89  
90          try
91          {
92              return (Provider) ClassUtils.instanciateClass(info.getProviderClass(), null);
93          }
94          catch (Exception ex)
95          {
96              throw new MuleRuntimeException(
97                  CoreMessages.failedToInitSecurityProvider(info.getProviderClass()), ex);
98          }
99      }
100 }