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