KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > propertyeditor > SecurityDomainEditor


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.security.propertyeditor;
8
9 import java.beans.PropertyEditorSupport JavaDoc;
10 import java.security.KeyStore JavaDoc;
11 import java.security.Principal JavaDoc;
12 import java.util.Set JavaDoc;
13
14 import javax.naming.InitialContext JavaDoc;
15 import javax.net.ssl.KeyManagerFactory;
16 import javax.net.ssl.TrustManagerFactory;
17 import javax.security.auth.Subject JavaDoc;
18
19 import org.jboss.security.SecurityDomain;
20 import org.jboss.logging.Logger;
21
22 /** A property editor for org.jboss.security.SecurityDomain types. This editor
23  * transforms a jndi name string to a SecurityDomain by looking up the binding.
24  * The only unusual aspect of this editor is that the jndi name is usually of
25  * the form java:/jaas/xxx and the java:/jaas context is a dynamic ObjectFactory
26  * that will create a binding for any xxx. If there is an attempt to lookup a
27  * binding before it has been created by the underlying service that provides
28  * the SecurityDomain, the lookup will return the default security service
29  * which typically does not implement SecurityDomain. In this case, the editor
30  * will create a proxy that delays the lookup of the SecurityDomain until the
31  * first method invocation against the proxy.
32  *
33  * @author Scott.Stark@jboss.org
34  * @version $Revision: 1.1.2.1 $
35  */

36 public class SecurityDomainEditor
37    extends PropertyEditorSupport JavaDoc
38 {
39    private static Logger log = Logger.getLogger(SecurityDomainEditor.class);
40    private String JavaDoc domainName;
41
42    /** Get the SecurityDomain from the text which is the jndi name of the
43     * SecurityDomain binding. This may have to create a proxy if the current
44     * value of the binding is not a SecurityDomain.
45     * @param text - the name of the Principal
46     */

47    public void setAsText(final String JavaDoc text)
48    {
49       this.domainName = text;
50       try
51       {
52          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
53          Object JavaDoc ref = ctx.lookup(text);
54          SecurityDomain domain = null;
55          if( ref instanceof SecurityDomain )
56          {
57             domain = (SecurityDomain) ref;
58          }
59          else
60          {
61             // Create a proxy to delay the lookup until needed
62
domain = new SecurityDomainProxy(domainName);
63          }
64          setValue(domain);
65       }
66       catch(Exception JavaDoc e)
67       {
68          log.error("Failed to lookup SecurityDomain, "+domainName, e);
69       }
70    }
71
72    /** Return the original security domain jndi name since we cannot get
73     * this back from the SecurityDomain itself.
74     * @return
75     */

76    public String JavaDoc getAsText()
77    {
78       return domainName;
79    }
80
81    /** A proxy that delays the lookup of the SecurityDomain until there
82     * is a SecurityDomain method invocation. This gets around the problem
83     * of a service not exposing its SecurityDomain binding until its started.
84     */

85    static class SecurityDomainProxy implements SecurityDomain
86    {
87       SecurityDomain delegate;
88       private String JavaDoc jndiName;
89
90       SecurityDomainProxy(String JavaDoc jndiName)
91       {
92          this.jndiName = jndiName;
93       }
94
95       private synchronized void initDelegate()
96       {
97          if( delegate == null )
98          {
99             try
100             {
101             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
102             delegate = (SecurityDomain) ctx.lookup(jndiName);
103             }
104             catch(Exception JavaDoc e)
105             {
106                SecurityException JavaDoc se = new SecurityException JavaDoc("Failed to lookup SecurityDomain, "+jndiName);
107                se.initCause(e);
108                throw se;
109             }
110          }
111       }
112
113       public KeyStore JavaDoc getKeyStore() throws SecurityException JavaDoc
114       {
115          initDelegate();
116          return delegate.getKeyStore();
117       }
118
119       public KeyManagerFactory getKeyManagerFactory() throws SecurityException JavaDoc
120       {
121          initDelegate();
122          return delegate.getKeyManagerFactory();
123       }
124
125       public KeyStore JavaDoc getTrustStore() throws SecurityException JavaDoc
126       {
127          initDelegate();
128          return delegate.getTrustStore();
129       }
130
131       public TrustManagerFactory getTrustManagerFactory() throws SecurityException JavaDoc
132       {
133          initDelegate();
134          return delegate.getTrustManagerFactory();
135       }
136
137       public String JavaDoc getSecurityDomain()
138       {
139          initDelegate();
140          return delegate.getSecurityDomain();
141       }
142
143       public boolean isValid(Principal JavaDoc principal, Object JavaDoc credential)
144       {
145          return this.isValid(principal, credential, null);
146       }
147
148       public boolean isValid(Principal JavaDoc principal, Object JavaDoc credential,
149          Subject JavaDoc activeSubject)
150       {
151          initDelegate();
152          return delegate.isValid(principal, credential, activeSubject);
153       }
154
155       public Subject JavaDoc getActiveSubject()
156       {
157          initDelegate();
158          return delegate.getActiveSubject();
159       }
160
161       public Principal JavaDoc getPrincipal(Principal JavaDoc principal)
162       {
163          initDelegate();
164          return delegate.getPrincipal(principal);
165       }
166
167       public boolean doesUserHaveRole(Principal JavaDoc principal, Set JavaDoc roles)
168       {
169          initDelegate();
170          return delegate.doesUserHaveRole(principal, roles);
171       }
172
173       public Set JavaDoc getUserRoles(Principal JavaDoc principal)
174       {
175          initDelegate();
176          return delegate.getUserRoles(principal);
177       }
178    }
179 }
180
Popular Tags