1 7 package org.jboss.security.propertyeditor; 8 9 import java.beans.PropertyEditorSupport ; 10 import java.security.KeyStore ; 11 import java.security.Principal ; 12 import java.util.Set ; 13 14 import javax.naming.InitialContext ; 15 import javax.net.ssl.KeyManagerFactory; 16 import javax.net.ssl.TrustManagerFactory; 17 import javax.security.auth.Subject ; 18 19 import org.jboss.security.SecurityDomain; 20 import org.jboss.logging.Logger; 21 22 36 public class SecurityDomainEditor 37 extends PropertyEditorSupport 38 { 39 private static Logger log = Logger.getLogger(SecurityDomainEditor.class); 40 private String domainName; 41 42 47 public void setAsText(final String text) 48 { 49 this.domainName = text; 50 try 51 { 52 InitialContext ctx = new InitialContext (); 53 Object ref = ctx.lookup(text); 54 SecurityDomain domain = null; 55 if( ref instanceof SecurityDomain ) 56 { 57 domain = (SecurityDomain) ref; 58 } 59 else 60 { 61 domain = new SecurityDomainProxy(domainName); 63 } 64 setValue(domain); 65 } 66 catch(Exception e) 67 { 68 log.error("Failed to lookup SecurityDomain, "+domainName, e); 69 } 70 } 71 72 76 public String getAsText() 77 { 78 return domainName; 79 } 80 81 85 static class SecurityDomainProxy implements SecurityDomain 86 { 87 SecurityDomain delegate; 88 private String jndiName; 89 90 SecurityDomainProxy(String jndiName) 91 { 92 this.jndiName = jndiName; 93 } 94 95 private synchronized void initDelegate() 96 { 97 if( delegate == null ) 98 { 99 try 100 { 101 InitialContext ctx = new InitialContext (); 102 delegate = (SecurityDomain) ctx.lookup(jndiName); 103 } 104 catch(Exception e) 105 { 106 SecurityException se = new SecurityException ("Failed to lookup SecurityDomain, "+jndiName); 107 se.initCause(e); 108 throw se; 109 } 110 } 111 } 112 113 public KeyStore getKeyStore() throws SecurityException 114 { 115 initDelegate(); 116 return delegate.getKeyStore(); 117 } 118 119 public KeyManagerFactory getKeyManagerFactory() throws SecurityException 120 { 121 initDelegate(); 122 return delegate.getKeyManagerFactory(); 123 } 124 125 public KeyStore getTrustStore() throws SecurityException 126 { 127 initDelegate(); 128 return delegate.getTrustStore(); 129 } 130 131 public TrustManagerFactory getTrustManagerFactory() throws SecurityException 132 { 133 initDelegate(); 134 return delegate.getTrustManagerFactory(); 135 } 136 137 public String getSecurityDomain() 138 { 139 initDelegate(); 140 return delegate.getSecurityDomain(); 141 } 142 143 public boolean isValid(Principal principal, Object credential) 144 { 145 return this.isValid(principal, credential, null); 146 } 147 148 public boolean isValid(Principal principal, Object credential, 149 Subject activeSubject) 150 { 151 initDelegate(); 152 return delegate.isValid(principal, credential, activeSubject); 153 } 154 155 public Subject getActiveSubject() 156 { 157 initDelegate(); 158 return delegate.getActiveSubject(); 159 } 160 161 public Principal getPrincipal(Principal principal) 162 { 163 initDelegate(); 164 return delegate.getPrincipal(principal); 165 } 166 167 public boolean doesUserHaveRole(Principal principal, Set roles) 168 { 169 initDelegate(); 170 return delegate.doesUserHaveRole(principal, roles); 171 } 172 173 public Set getUserRoles(Principal principal) 174 { 175 initDelegate(); 176 return delegate.getUserRoles(principal); 177 } 178 } 179 } 180 | Popular Tags |