KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > ssl > JBossSocketFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.net.ssl;
23
24 import java.io.IOException JavaDoc;
25 import java.security.KeyStore JavaDoc;
26
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.net.ssl.TrustManager;
30 import javax.net.ssl.KeyManager;
31 import javax.net.ssl.TrustManagerFactory;
32 import javax.net.ssl.KeyManagerFactory;
33
34 import org.jboss.security.SecurityDomain;
35 import org.apache.tomcat.util.net.jsse.JSSESocketFactory;
36
37 /**
38  * Extends the tomcat JSSE14SocketFactory to obtain the server key and trust
39  * stores from the SecurityDomain defined by the securityDomain attribute
40  * of the connector.
41  *
42  */

43 public class JBossSocketFactory
44    extends JSSESocketFactory
45 {
46    private SecurityDomain securityDomain;
47
48    public JBossSocketFactory()
49    {
50    }
51
52    public void setAttribute(String JavaDoc name, Object JavaDoc value)
53    {
54       if (name.equalsIgnoreCase("securityDomain"))
55       {
56          try
57          {
58             setSecurityDomainName((String JavaDoc) value);
59          }
60          catch (Exception JavaDoc e)
61          {
62             IllegalArgumentException JavaDoc ex =
63                new IllegalArgumentException JavaDoc("Failed to set security domain");
64             ex.initCause(e);
65             throw ex;
66          }
67       }
68       super.setAttribute(name, value);
69    }
70
71    /**
72     * Set the SecurityDomain to use for the key/trust stores
73     *
74     * @param jndiName - the jndi name of the SecurityDomain binding
75     * @throws NamingException
76     * @throws IOException
77     */

78    public void setSecurityDomainName(String JavaDoc jndiName)
79       throws NamingException JavaDoc, IOException JavaDoc
80    {
81       InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
82       securityDomain = (SecurityDomain) iniCtx.lookup(jndiName);
83    }
84
85    /**
86     * Gets the SSL server's keystore from the SecurityDomain.
87     *
88     * @param type - ignored, this comes from the security domain config
89     * @param pass - ignore, this comes from the security domain config
90     * @return the KeyStore for the server cert
91     * @throws IOException
92     */

93    protected KeyStore JavaDoc getKeystore(String JavaDoc type, String JavaDoc pass)
94       throws IOException JavaDoc
95    {
96       verifySecurityDomain();
97       return securityDomain.getKeyStore();
98    }
99
100    /*
101     * Gets the SSL server's truststore from the SecurityDomain.
102     
103     * @param type - ignored, this comes from the security domain config
104     * @return the KeyStore for the trusted signers store
105     */

106    protected KeyStore JavaDoc getTrustStore(String JavaDoc type) throws IOException JavaDoc
107    {
108       verifySecurityDomain();
109       return securityDomain.getTrustStore();
110    }
111
112    /**
113     * Override to obtain the TrustManagers from the security domain.
114     *
115     * @param keystoreType - ignored, this comes from the security domain
116     * @param algorithm - ignored, this comes from the security domain
117     * @return the array of TrustManagers from the security domain
118     * @throws Exception
119     */

120    protected TrustManager[] getTrustManagers(String JavaDoc keystoreType, String JavaDoc algorithm)
121       throws Exception JavaDoc
122    {
123       verifySecurityDomain();
124       TrustManagerFactory tmf = securityDomain.getTrustManagerFactory();
125       TrustManager[] trustMgrs = null;
126
127       if( tmf != null )
128       {
129           trustMgrs = tmf.getTrustManagers();
130       }
131       return trustMgrs;
132    }
133
134    /**
135     * Override to obtain the KeyManagers from the security domain.
136     *
137     * @param keystoreType - ignored, this comes from the security domain
138     * @param algorithm - ignored, this comes from the security domain
139     * @param keyAlias - ignored
140     * @return the array of KeyManagers from the security domain
141     * @throws Exception
142     */

143    protected KeyManager[] getKeyManagers(String JavaDoc keystoreType, String JavaDoc algorithm,
144       String JavaDoc keyAlias)
145       throws Exception JavaDoc
146    {
147       verifySecurityDomain();
148       KeyManagerFactory kmf = securityDomain.getKeyManagerFactory();
149       KeyManager[] keyMgrs = null;
150       if( kmf != null )
151       {
152          keyMgrs = kmf.getKeyManagers();
153       }
154       return keyMgrs;
155    }
156    
157    private void verifySecurityDomain()
158    {
159       String JavaDoc str = "securityDomain is null." +
160             "Set it as an attribute in the connector setting";
161       
162       if(this.securityDomain == null)
163          throw new IllegalStateException JavaDoc(str);
164    }
165 }
166
Popular Tags