1 7 package org.jboss.security.plugins; 8 9 import java.lang.reflect.Constructor ; 10 import java.lang.reflect.Method ; 11 import java.net.MalformedURLException ; 12 import java.net.URL ; 13 import javax.management.Attribute ; 14 import javax.management.AttributeList ; 15 import javax.management.AttributeNotFoundException ; 16 import javax.management.DynamicMBean ; 17 import javax.management.InvalidAttributeValueException ; 18 import javax.management.MBeanAttributeInfo ; 19 import javax.management.MBeanConstructorInfo ; 20 import javax.management.MBeanException ; 21 import javax.management.MBeanInfo ; 22 import javax.management.MBeanOperationInfo ; 23 import javax.management.ReflectionException ; 24 import javax.security.auth.login.Configuration ; 25 import javax.security.auth.login.AppConfigurationEntry ; 26 27 import org.jboss.logging.Logger; 28 29 35 public class DefaultLoginConfig implements DynamicMBean 36 { 37 private static Logger log = Logger.getLogger(DefaultLoginConfig.class); 38 private String authConfig = "auth.conf"; 39 private Configuration theConfig; 40 41 42 public DefaultLoginConfig() 43 { 44 } 45 46 48 public String getAuthConfig() 49 { 50 return authConfig; 51 } 52 53 56 public void setAuthConfig(String authConfURL) throws MalformedURLException 57 { 58 this.authConfig = authConfURL; 59 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 61 URL loginConfig = loader.getResource(authConfig); 62 if( loginConfig != null ) 63 { 64 System.setProperty("java.security.auth.login.config", loginConfig.toExternalForm()); 65 if (log.isInfoEnabled()) 66 log.info("Using JAAS LoginConfig: "+loginConfig.toExternalForm()); 67 } 68 else 69 { 70 log.warn("Resource: "+authConfig+" not found"); 71 } 72 } 73 74 80 public Configuration getConfiguration(Configuration currentConfig) 81 { 82 if( theConfig == null ) 83 { 84 theConfig = Configuration.getConfiguration(); 85 log.debug("theConfig set to: "+theConfig); 86 } 87 return theConfig; 88 } 89 90 public Object getAttribute(String name) 92 throws AttributeNotFoundException , MBeanException , ReflectionException 93 { 94 if( name.equals("AuthConfig") ) 95 return getAuthConfig(); 96 throw new AttributeNotFoundException (name+": is not an attribute"); 97 } 98 99 public AttributeList getAttributes(String [] names) 100 { 101 AttributeList list = new AttributeList (); 102 for(int n = 0; n < names.length; n ++) 103 { 104 String name = names[n]; 105 try 106 { 107 Object value = getAttribute(name); 108 Attribute attr = new Attribute (name, value); 109 list.add(attr); 110 } 111 catch(Exception e) 112 { 113 } 114 } 115 return list; 116 } 117 118 public MBeanInfo getMBeanInfo() 119 { 120 Class c = getClass(); 121 MBeanAttributeInfo [] attrInfo = { 122 new MBeanAttributeInfo ("AuthConfig", "java.lang.String", 123 "", true, true, false) 124 }; 125 Constructor ctor = null; 126 try 127 { 128 Class [] sig = {}; 129 ctor = c.getDeclaredConstructor(sig); 130 } 131 catch(Exception e) 132 { 133 } 134 MBeanConstructorInfo [] ctorInfo = { 135 new MBeanConstructorInfo ("Default ctor", ctor) 136 }; 137 Method getConfiguration = null; 138 try 139 { 140 Class [] sig = {Configuration .class}; 141 getConfiguration = c.getDeclaredMethod("getConfiguration", sig); 142 } 143 catch(Exception e) 144 { 145 } 146 MBeanOperationInfo [] opInfo = { 147 new MBeanOperationInfo ("Access the LoginConfiguration", getConfiguration) 148 }; 149 MBeanInfo info = new MBeanInfo (c.getName(), "Default JAAS LoginConfig", 150 attrInfo, ctorInfo, opInfo, null); 151 return info; 152 } 153 154 public Object invoke(String method, Object [] args, String [] signature) 155 throws MBeanException , ReflectionException 156 { 157 Object value = null; 158 if( method.equals("getConfiguration") ) 159 { 160 Configuration currentConfig = (Configuration ) args[0]; 161 value = this.getConfiguration(currentConfig); 162 } 163 return value; 164 } 165 166 public void setAttribute(Attribute attribute) 167 throws AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException 168 { 169 String name = attribute.getName(); 170 String value = (String ) attribute.getValue(); 171 if( name.equals("AuthConfig") ) 172 { 173 try 174 { 175 setAuthConfig(value); 176 } 177 catch(Exception e) 178 { 179 throw new MBeanException (e); 180 } 181 } 182 else 183 throw new AttributeNotFoundException (name+": is not an attribute"); 184 } 185 186 public AttributeList setAttributes(AttributeList attributeList) 187 { 188 AttributeList list = new AttributeList (); 189 for(int n = 0; n < attributeList.size(); n ++) 190 { 191 Attribute attr = (Attribute ) attributeList.get(n); 192 try 193 { 194 setAttribute(attr); 195 list.add(attr); 196 } 197 catch(Exception e) 198 { 199 } 200 } 201 return list; 202 } 203 205 } 206 | Popular Tags |