1 22 package org.jboss.resource.security; 23 24 import java.util.Map ; 25 import javax.management.MBeanServer ; 26 import javax.management.MalformedObjectNameException ; 27 import javax.management.ObjectName ; 28 import javax.resource.spi.ManagedConnectionFactory ; 29 import javax.security.auth.Subject ; 30 import javax.security.auth.callback.CallbackHandler ; 31 import javax.security.auth.login.LoginException ; 32 33 import org.jboss.logging.Logger; 34 import org.jboss.mx.util.MBeanServerLocator; 35 import org.jboss.security.auth.spi.AbstractServerLoginModule; 36 37 38 47 public abstract class AbstractPasswordCredentialLoginModule 48 extends AbstractServerLoginModule 49 { 50 private static final Logger log = Logger.getLogger(AbstractPasswordCredentialLoginModule.class); 51 private MBeanServer server; 52 private ObjectName managedConnectionFactoryName; 53 private ManagedConnectionFactory mcf; 54 55 private Boolean ignoreMissigingMCF; 56 57 public AbstractPasswordCredentialLoginModule() 58 { 59 60 } 61 62 public void initialize(Subject subject, CallbackHandler handler, Map sharedState, Map options) 63 { 64 super.initialize(subject, handler, sharedState, options); 65 String name = (String ) options.get("managedConnectionFactoryName"); 66 try 67 { 68 managedConnectionFactoryName = new ObjectName (name); 69 } 70 catch (MalformedObjectNameException mone) 71 { 72 throw new IllegalArgumentException ("Malformed ObjectName: " + name); 73 } 74 75 if (managedConnectionFactoryName == null) 76 { 77 throw new IllegalArgumentException ("Must supply a managedConnectionFactoryName!"); 78 } 79 Object flag = options.get("ignoreMissigingMCF"); 80 if( flag instanceof Boolean ) 81 ignoreMissigingMCF = (Boolean ) flag; 82 else if( flag != null ) 83 ignoreMissigingMCF = Boolean.valueOf(flag.toString()); 84 server = MBeanServerLocator.locateJBoss(); 85 getMcf(); 86 } 87 88 94 public boolean login() throws LoginException 95 { 96 if (mcf == null) 97 { 98 return false; 99 } 100 return super.login(); 101 } 102 103 public boolean logout() throws LoginException 104 { 105 removeCredentials(); 106 return super.logout(); 107 } 108 109 protected ManagedConnectionFactory getMcf() 110 { 111 if (mcf == null) 112 { 113 try 114 { 115 mcf = (ManagedConnectionFactory ) server.getAttribute( 116 managedConnectionFactoryName, 117 "ManagedConnectionFactory"); 118 } 119 catch (Exception e) 120 { 121 log.error("The ConnectionManager mbean: " + managedConnectionFactoryName 122 + " specified in a ConfiguredIdentityLoginModule could not be found." 123 + " ConnectionFactory will be unusable!"); 124 if( Boolean.TRUE != ignoreMissigingMCF ) 125 { 126 throw new IllegalArgumentException ("Managed Connection Factory not found: " 127 + managedConnectionFactoryName); 128 } 129 } if (log.isTraceEnabled()) 131 { 132 log.trace("mcfname: " + managedConnectionFactoryName); 133 } 134 } 136 return mcf; 137 } 138 139 protected MBeanServer getServer() 140 { 141 return server; 142 } 143 144 148 protected void removeCredentials() 149 { 150 sharedState.remove("javax.security.auth.login.name"); 151 sharedState.remove("javax.security.auth.login.password"); 152 SubjectActions.removeCredentials(subject, mcf); 153 } 154 155 } 156 157 | Popular Tags |