Numerous formatting changes, and methods to facilitate unit testing.

This commit is contained in:
Ben Alex 2004-03-28 11:54:10 +00:00
parent dc6357d504
commit cf043ad35f

View File

@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package net.sf.acegisecurity; package net.sf.acegisecurity;
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;
@ -37,29 +36,26 @@ import java.util.Map;
* @version $Id$ * @version $Id$
*/ */
public class MethodDefinitionMap implements MethodDefinitionSource { public class MethodDefinitionMap implements MethodDefinitionSource {
//~ Static fields/initializers =============================================
private static final Log logger = LogFactory.getLog(MethodDefinitionMap.class); private static final Log logger = LogFactory.getLog(MethodDefinitionMap.class);
//~ Instance fields ========================================================
/** Map from Method to ApplicationDefinition */ /** Map from Method to ApplicationDefinition */
protected Map methodMap = new HashMap(); protected Map methodMap = new HashMap();
/** Map from Method to name pattern used for registration */ /** Map from Method to name pattern used for registration */
private Map nameMap = new HashMap(); private Map nameMap = new HashMap();
//~ Methods ================================================================
public ConfigAttributeDefinition getAttributes(MethodInvocation invocation) { public ConfigAttributeDefinition getAttributes(MethodInvocation invocation) {
return (ConfigAttributeDefinition) this.methodMap.get(invocation return (ConfigAttributeDefinition) this.methodMap.get(invocation.getMethod());
.getMethod());
} }
public Iterator getConfigAttributeDefinitions() { public Iterator getConfigAttributeDefinitions() {
return methodMap.values().iterator(); return methodMap.values().iterator();
} }
public int getMethodMapSize() {
return this.methodMap.size();
}
/** /**
* Add required authorities for a secure method. Method names can end or * Add required authorities for a secure method. Method names can end or
* start with "" for matching multiple methods. * start with "" for matching multiple methods.
@ -68,8 +64,8 @@ public class MethodDefinitionMap implements MethodDefinitionSource {
* @param attr required authorities associated with the method * @param attr required authorities associated with the method
*/ */
public void addSecureMethod(Method method, ConfigAttributeDefinition attr) { public void addSecureMethod(Method method, ConfigAttributeDefinition attr) {
logger.info("Adding secure method [" + method + "] with attributes [" logger.info("Adding secure method [" + method + "] with attributes [" +
+ attr + "]"); attr + "]");
this.methodMap.put(method, attr); this.methodMap.put(method, attr);
} }
@ -86,8 +82,8 @@ public class MethodDefinitionMap implements MethodDefinitionSource {
int lastDotIndex = name.lastIndexOf("."); int lastDotIndex = name.lastIndexOf(".");
if (lastDotIndex == -1) { if (lastDotIndex == -1) {
throw new IllegalArgumentException("'" + name throw new IllegalArgumentException("'" + name +
+ "' is not a valid method name: format is FQN.methodName"); "' is not a valid method name: format is FQN.methodName");
} }
String className = name.substring(0, lastDotIndex); String className = name.substring(0, lastDotIndex);
@ -98,8 +94,8 @@ public class MethodDefinitionMap implements MethodDefinitionSource {
Thread.currentThread().getContextClassLoader()); Thread.currentThread().getContextClassLoader());
addSecureMethod(clazz, methodName, attr); addSecureMethod(clazz, methodName, attr);
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
throw new IllegalArgumentException("Class '" + className throw new IllegalArgumentException("Class '" + className +
+ "' not found"); "' not found");
} }
} }
@ -117,24 +113,22 @@ public class MethodDefinitionMap implements MethodDefinitionSource {
ConfigAttributeDefinition attr) { ConfigAttributeDefinition attr) {
String name = clazz.getName() + '.' + mappedName; String name = clazz.getName() + '.' + mappedName;
if (logger.isDebugEnabled()) { logger.debug("Adding secure method [" + name + "] with attributes [" +
logger.debug("Adding secure method [" + name attr + "]");
+ "] with attributes [" + attr + "]");
}
Method[] methods = clazz.getDeclaredMethods(); Method[] methods = clazz.getDeclaredMethods();
List matchingMethods = new ArrayList(); List matchingMethods = new ArrayList();
for (int i = 0; i < methods.length; i++) { for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals(mappedName) if (methods[i].getName().equals(mappedName) ||
|| isMatch(methods[i].getName(), mappedName)) { isMatch(methods[i].getName(), mappedName)) {
matchingMethods.add(methods[i]); matchingMethods.add(methods[i]);
} }
} }
if (matchingMethods.isEmpty()) { if (matchingMethods.isEmpty()) {
throw new IllegalArgumentException("Couldn't find method '" throw new IllegalArgumentException("Couldn't find method '" +
+ mappedName + "' on " + clazz); mappedName + "' on " + clazz);
} }
// register all matching methods // register all matching methods
@ -142,25 +136,23 @@ public class MethodDefinitionMap implements MethodDefinitionSource {
Method method = (Method) it.next(); Method method = (Method) it.next();
String regMethodName = (String) this.nameMap.get(method); String regMethodName = (String) this.nameMap.get(method);
if ((regMethodName == null) if ((regMethodName == null) ||
|| (!regMethodName.equals(name) (!regMethodName.equals(name) &&
&& (regMethodName.length() <= name.length()))) { (regMethodName.length() <= name.length()))) {
// no already registered method name, or more specific // no already registered method name, or more specific
// method name specification now -> (re-)register method // method name specification now -> (re-)register method
if (logger.isDebugEnabled() && (regMethodName != null)) { if (regMethodName != null) {
logger.debug("Replacing attributes for secure method [" logger.debug("Replacing attributes for secure method [" +
+ method + "]: current name [" + name method + "]: current name [" + name +
+ "] is more specific than [" + regMethodName + "]"); "] is more specific than [" + regMethodName + "]");
} }
this.nameMap.put(method, name); this.nameMap.put(method, name);
addSecureMethod(method, attr); addSecureMethod(method, attr);
} else { } else {
if (logger.isDebugEnabled() && (regMethodName != null)) { logger.debug("Keeping attributes for secure method [" + method +
logger.debug("Keeping attributes for secure method [" "]: current name [" + name +
+ method + "]: current name [" + name "] is not more specific than [" + regMethodName + "]");
+ "] is not more specific than [" + regMethodName + "]");
}
} }
} }
} }
@ -175,10 +167,9 @@ public class MethodDefinitionMap implements MethodDefinitionSource {
* @return if the names match * @return if the names match
*/ */
private boolean isMatch(String methodName, String mappedName) { private boolean isMatch(String methodName, String mappedName) {
return (mappedName.endsWith("*") return (mappedName.endsWith("*") &&
&& methodName.startsWith(mappedName.substring(0, mappedName.length() methodName.startsWith(mappedName.substring(0, mappedName.length() - 1))) ||
- 1))) (mappedName.startsWith("*") &&
|| (mappedName.startsWith("*") methodName.endsWith(mappedName.substring(1, mappedName.length())));
&& methodName.endsWith(mappedName.substring(1, mappedName.length())));
} }
} }