1 22 package javax.security.auth.message.config; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 import java.security.PrivilegedExceptionAction ; 27 import java.security.SecurityPermission ; 28 import java.util.Map ; 29 30 import javax.security.auth.message.AuthException; 31 32 33 35 83 public abstract class AuthConfigFactory 84 { 85 private static AuthConfigFactory _factory = null; 86 private static final String FACTORY_PROP = "authconfigprovider.factory"; 87 88 89 private static final String DEFAULT_FACTORY_NAME = 90 "org.jboss.security.auth.message.config.JBossAuthConfigFactory"; 91 92 public AuthConfigFactory() 93 { 94 } 95 96 105 public abstract String [] detachListener(RegistrationListener listener, String layer, 106 String appContext); 107 108 141 public abstract AuthConfigProvider getConfigProvider( String layer, 142 String appContext, RegistrationListener listener); 143 144 160 public static AuthConfigFactory getFactory() 161 { 162 SecurityManager sm = System.getSecurityManager(); 164 if (sm != null) 165 sm.checkPermission(new SecurityPermission ("getFactory")); 166 167 if (_factory == null) 168 { 169 String factoryName = null; 170 Class clazz = null; 171 try 172 { 173 LoadAction action = new LoadAction(); 174 try 175 { 176 clazz = (Class ) AccessController.doPrivileged(action); 177 factoryName = action.getName(); 178 } 179 catch (PrivilegedActionException ex) 180 { 181 factoryName = action.getName(); 182 Exception e = ex.getException(); 183 if (e instanceof ClassNotFoundException ) 184 throw (ClassNotFoundException ) e; 185 else 186 throw new IllegalStateException ("Failure during load of class: " + action.getName() + e); 187 } 188 _factory = (AuthConfigFactory) clazz.newInstance(); 189 } 190 catch (ClassNotFoundException e) 191 { 192 String msg = "Failed to find AuthConfigFactory : " + factoryName; 193 IllegalStateException ise = new IllegalStateException (msg); 194 ise.initCause(e); 195 throw ise; 196 } 197 catch (IllegalAccessException e) 198 { 199 String msg = "Unable to access class : " + factoryName; 200 IllegalStateException ise = new IllegalStateException (msg); 201 ise.initCause(e); 202 throw ise; 203 } 204 catch (InstantiationException e) 205 { 206 String msg = "Failed to create instance of: " + factoryName; 207 IllegalStateException ise = new IllegalStateException (msg); 208 ise.initCause(e); 209 throw ise; 210 } 211 catch (ClassCastException e) 212 { 213 StringBuffer msg = new StringBuffer (factoryName + " Is not a AuthConfigFactory, "); 214 msg.append("ACF.class.CL: "+ AuthConfigFactory.class.getClassLoader()); 215 msg.append("\nACF.class.CS: " + AuthConfigFactory.class.getProtectionDomain().getCodeSource()); 216 msg.append("\nACF.class.hash: "+System.identityHashCode(AuthConfigFactory.class)); 217 msg.append("\nclazz.CL: "+clazz.getClassLoader()); 218 msg.append("\nclazz.CS: "+clazz.getProtectionDomain().getCodeSource()); 219 msg.append("\nclazz.super.CL: "+clazz.getSuperclass().getClassLoader()); 220 msg.append("\nclazz.super.CS: "+clazz.getSuperclass().getProtectionDomain().getCodeSource()); 221 msg.append("\nclazz.super.hash: "+System.identityHashCode(clazz.getSuperclass())); 222 ClassCastException cce = new ClassCastException (msg.toString()); 223 cce.initCause(e); 224 throw cce; 225 } 226 } 227 return _factory; 228 } 229 230 public abstract RegistrationContext getRegistrationContext(String registrationID); 231 232 public abstract String [] getRegistrationIDs(AuthConfigProvider provider); 233 234 public abstract void refresh() throws AuthException, SecurityException ; 235 236 public abstract String registerConfigProvider( String className, Map properties,String layer, 237 String appContext, String description) throws AuthException, SecurityException ; 238 239 public abstract boolean removeRegistration( String registrationID); 240 241 public static void setFactory(AuthConfigFactory factory) 242 { 243 _factory = factory; 244 } 245 246 250 public static interface RegistrationContext 251 { 252 258 String getAppContext(); 259 260 266 String getDescription(); 267 268 273 String getMessageLayer(); 274 } 275 276 281 public static interface RegistrationListener 282 { 283 298 public void notify( String layer, String appContext); 299 } 300 301 305 private static class LoadAction implements PrivilegedExceptionAction 306 { 307 private String name; 308 public String getName() 309 { 310 return name; 311 } 312 public Object run() 313 throws Exception 314 { 315 name = System.getProperty(FACTORY_PROP); 316 if( name == null ) 317 { 318 name = DEFAULT_FACTORY_NAME; 320 } 321 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 322 Class factoryClass = loader.loadClass(name); 323 return factoryClass; 324 } 325 } 326 } 327 | Popular Tags |