KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > security > config > JBossContextConfig


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.web.tomcat.security.config;
23
24 //$Id: JBossContextConfig.java 58201 2006-11-08 18:21:15Z anil.saldhana@jboss.com $
25

26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.Set JavaDoc;
31  
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34
35 import org.apache.catalina.Authenticator;
36 import org.apache.catalina.startup.ContextConfig;
37 import org.jboss.logging.Logger;
38
39 /**
40  * Extension of Catalina ContextConfig that will allow
41  * plugging custom authenticators at the host level
42  * in the least intrusive way to the tomcat layer.
43  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
44  * @since Mar 6, 2006
45  * @version $Revision: 58201 $
46  */

47 public class JBossContextConfig extends ContextConfig
48 {
49    private static Logger log = Logger.getLogger(JBossContextConfig.class);
50    
51    private boolean isTrace = log.isTraceEnabled();
52    
53    /**
54     * Create a new JBossContextConfig.
55     */

56    public JBossContextConfig()
57    {
58       super();
59       try
60       {
61          Map JavaDoc authMap = this.getAuthenticators();
62          if(authMap.size() > 0)
63             customAuthenticators = authMap;
64       }catch(Exception JavaDoc e)
65       {
66          // TODO
67
log.debug("Failed to customize authenticators::",e);
68       }
69    }
70    
71    /**
72     * Map of Authenticators
73     * @return
74     * @throws Exception
75     */

76    private Map JavaDoc getAuthenticators() throws Exception JavaDoc
77    {
78       Map JavaDoc cmap = new HashMap JavaDoc();
79       ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
80       
81       Properties JavaDoc authProps = this.getAuthenticatorsFromJndi();
82       if(authProps != null)
83       {
84          Set JavaDoc keys = authProps.keySet();
85          Iterator JavaDoc iter = keys != null ? keys.iterator() : null;
86          while(iter != null && iter.hasNext())
87          {
88             String JavaDoc key = (String JavaDoc)iter.next();
89             String JavaDoc authenticatorStr = (String JavaDoc)authProps.get(key);
90             Class JavaDoc authClass = tcl.loadClass(authenticatorStr);
91             cmap.put(key, (Authenticator)authClass.newInstance());
92          }
93       }
94       if(isTrace)
95          log.trace("Authenticators plugged in::"+cmap);
96       return cmap;
97    }
98    
99    /**
100     * Get the key-pair of authenticators from the
101     * JNDI (set up by the Tomcat Deployer)
102     * TODO: the authenticators need to be injected
103     *
104     * @return
105     * @throws NamingException
106     */

107    private Properties JavaDoc getAuthenticatorsFromJndi() throws NamingException JavaDoc
108    {
109       return (Properties JavaDoc)(new InitialContext JavaDoc()).lookup("TomcatAuthenticators");
110    }
111 }
112
Popular Tags