KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > embedded > JaasSecurityManagerService


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.ejb3.embedded;
23
24
25 import org.jboss.ejb3.naming.BrainlessContext;
26 import org.jboss.logging.Logger;
27 import org.jboss.security.auth.callback.SecurityAssociationHandler;
28 import org.jboss.security.auth.login.XMLLoginConfigImpl;
29 import org.jboss.security.plugins.JBossAuthorizationManager;
30 import org.jboss.security.plugins.JaasSecurityManager;
31 import org.jboss.security.plugins.SecurityDomainContext;
32
33 import javax.naming.*;
34 import javax.naming.spi.ObjectFactory JavaDoc;
35 import javax.security.auth.login.Configuration JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Hashtable JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * @author carlo
42  */

43 public class JaasSecurityManagerService
44 {
45    @SuppressWarnings JavaDoc("unused")
46    private static final Logger log = Logger.getLogger(JaasSecurityManager.class);
47
48    private static final String JavaDoc SECURITY_MGR_PATH = "java:/jaas";
49
50    private static Map JavaDoc<String JavaDoc, JaasSecurityManager> cache = new HashMap JavaDoc<String JavaDoc, JaasSecurityManager>();
51
52    private Hashtable JavaDoc initialContextProperties;
53
54    public JaasSecurityManagerService()
55    {
56
57    }
58
59    public void setInitialContextProperties(Hashtable JavaDoc initialContextProperties)
60    {
61       this.initialContextProperties = initialContextProperties;
62    }
63
64    private InitialContext getInitialContext() throws NamingException
65    {
66       if (initialContextProperties != null) return new InitialContext(initialContextProperties);
67       else return new InitialContext();
68    }
69
70    public void start() throws Exception JavaDoc
71    {
72       XMLLoginConfigImpl configuration = new XMLLoginConfigImpl();
73       configuration.setConfigResource("login-config.xml");
74       configuration.loadConfig();
75
76       Configuration.setConfiguration(configuration);
77
78       Context ctx = getInitialContext();
79
80       String JavaDoc factoryName = SecurityDomainObjectFactory.class.getName();
81       Reference ref = new Reference("nl.wolfc.embedded.security.plugins.JaasSecurityManager", factoryName, null);
82       ctx.rebind(SECURITY_MGR_PATH, ref);
83    }
84
85    private static JaasSecurityManager getSecurityManager(String JavaDoc name)
86    {
87       JaasSecurityManager manager = cache.get(name);
88       if (manager != null)
89       {
90          //log.info("cache hit");
91
return manager;
92       }
93       synchronized (cache)
94       {
95          if (manager != null)
96             return manager;
97
98          manager = new JaasSecurityManager(name, new SecurityAssociationHandler());
99          cache.put(name, manager);
100       }
101       return manager;
102    }
103
104    public static class SecurityDomainObjectFactory implements ObjectFactory JavaDoc
105    {
106       @SuppressWarnings JavaDoc("unused")
107       private static final Logger log = Logger.getLogger(SecurityDomainObjectFactory.class);
108       
109       public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name name, Context nameCtx, Hashtable JavaDoc<?, ?> environment) throws Exception JavaDoc
110       {
111          /**
112           * The lookup will be of the form java:/jaas/other
113           * So check for name.get(1)
114           */

115          String JavaDoc securityDomainName = name.get(1);
116          final SecurityDomainContext ctx = new SecurityDomainContext(getSecurityManager(securityDomainName), null);
117          ctx.setAuthorizationManager(new JBossAuthorizationManager(securityDomainName, new SecurityAssociationHandler()));
118          return new BrainlessContext()
119          {
120             public Object JavaDoc lookup(Name name) throws NamingException
121             {
122                log.debug("lookup " + name);
123                if(name.size() < 2)
124                   return lookup(name.get(0));
125                else
126                   return ctx.lookup(name.get(1));
127             }
128
129             public Object JavaDoc lookup(String JavaDoc name) throws NamingException
130             {
131                log.debug("lookup " + name);
132                return getSecurityManager(name);
133             }
134          };
135       }
136    }
137 }
138
Popular Tags