KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > ssl > Context


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security.ssl;
8
9 import java.io.IOException JavaDoc;
10 import java.security.KeyManagementException JavaDoc;
11 import java.security.NoSuchAlgorithmException JavaDoc;
12 import javax.net.ssl.KeyManagerFactory;
13 import javax.net.ssl.SSLContext;
14 import javax.net.ssl.TrustManager;
15 import javax.net.ssl.TrustManagerFactory;
16
17 import org.jboss.logging.Logger;
18 import org.jboss.security.SecurityDomain;
19
20 /**
21  * Utility class with a static method that returns an initialized JSSE
22  * SSLContext for a given JBossSX SecurityDomain.
23  *
24  * @see javax.net.ssl.KeyManagerFactory
25  * @see javax.net.ssl.SSLContext
26  * @see javax.net.ssl.TrustManager
27  * @see javax.net.ssl.TrustManagerFactory
28  * @see org.jboss.security.SecurityDomain
29  *
30  * @author Scott.Stark@jboss.org
31  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
32  *
33  * @version $Revision: 1.2.6.1 $
34  */

35 class Context
36 {
37    private static Logger log = Logger.getLogger(Context.class);
38
39    /*
40     * Returns an initialized JSSE SSLContext that uses the KeyManagerFactory
41     * and TrustManagerFactory objects encapsulated by a given JBossSX
42     * SecurityDomain.
43     */

44    static SSLContext forDomain(SecurityDomain securityDomain)
45       throws IOException JavaDoc
46    {
47       SSLContext sslCtx = null;
48       try
49       {
50          sslCtx = SSLContext.getInstance("TLS");
51          KeyManagerFactory keyMgr = securityDomain.getKeyManagerFactory();
52          if( keyMgr == null )
53             throw new IOException JavaDoc("KeyManagerFactory is null for security domain: "+securityDomain.getSecurityDomain());
54          TrustManagerFactory trustMgr = securityDomain.getTrustManagerFactory();
55          TrustManager[] trustMgrs = null;
56          if( trustMgr != null )
57             trustMgrs = trustMgr.getTrustManagers();
58          sslCtx.init(keyMgr.getKeyManagers(), trustMgrs, null);
59          return sslCtx;
60       }
61       catch(NoSuchAlgorithmException JavaDoc e)
62       {
63          log.error("Failed to get SSLContext for TLS algorithm", e);
64          throw new IOException JavaDoc("Failed to get SSLContext for TLS algorithm");
65       }
66       catch(KeyManagementException JavaDoc e)
67       {
68          log.error("Failed to init SSLContext", e);
69          throw new IOException JavaDoc("Failed to init SSLContext");
70       }
71       catch(SecurityException JavaDoc e)
72       {
73          log.error("Failed to init SSLContext", e);
74          throw new IOException JavaDoc("Failed to init SSLContext");
75       }
76    }
77 }
78
Popular Tags