View Javadoc

    1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.domain.validation;
    17
    18  import org.springframework.aop.framework.AopConfigException;
    19  import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
    20
    21  import org.springframework.beans.factory.InitializingBean;
    22
    23  import org.springframework.util.Assert;
    24
    25  import java.lang.reflect.Method;
    26
    27
    28  /***
    29   * Advisor for the {@link ValidationInterceptor}.
    30   *
    31   * <p>
    32   * Intended to be used with Spring's
    33   * <code>DefaultAdvisorAutoProxyCreator</code>.
    34   * </p>
    35   *
    36   * <p>
    37   * Registers {@link ValidationInterceptor} for every <code>Method</code>
    38   * against a class that directly or through its superclasses implements {@link
    39   * #supportsClass} and has a signature match those defined by {@link
    40   * #methods}.
    41   * </p>
    42   *
    43   * @author Ben Alex
    44   * @version $Id: ValidationAdvisor.java,v 1.4 2005/11/17 00:55:50 benalex Exp $
    45   */
    46  public class ValidationAdvisor extends StaticMethodMatcherPointcutAdvisor
    47      implements InitializingBean {
    48      //~ Instance fields ========================================================
    49
    50      private Class<? extends Object> supportsClass;
    51      private String[] methods = {"create", "update", "createOrUpdate"};
    52
    53      //~ Constructors ===========================================================
    54
    55      public ValidationAdvisor(ValidationInterceptor advice) {
    56          super(advice);
    57
    58          if (advice == null) {
    59              throw new AopConfigException(
    60                  "Cannot construct a BindAndValidateAdvisor using a "
    61                  + "null BindAndValidateInterceptor");
    62          }
    63      }
    64
    65      //~ Methods ================================================================
    66
    67      public void setMethods(String[] methods) {
    68          this.methods = methods;
    69      }
    70
    71      public String[] getMethods() {
    72          return methods;
    73      }
    74
    75      public void setSupportsClass(Class<? extends Object> clazz) {
    76          this.supportsClass = clazz;
    77      }
    78
    79      public Class getSupportsClass() {
    80          return supportsClass;
    81      }
    82
    83      public void afterPropertiesSet() throws Exception {
    84          Assert.notNull(supportsClass, "A supportsClass is required");
    85          Assert.notNull(methods, "A list of valid methods is required");
    86          Assert.notEmpty(methods, "A list of valid methods is required");
    87      }
    88
    89      public boolean matches(Method m, Class targetClass) {
    90          // Check there are actual arguments
    91          if (m.getParameterTypes().length == 0) {
    92              return false;
    93          }
    94
    95          // Check the method name matches one we're interested in
    96          boolean found = false;
    97
    98          for (int i = 0; i < methods.length; i++) {
    99              if (m.getName().equals(methods[i])) {
    100                 found = true;
    101             }
    102         }
    103
    104         if (!found) {
    105             return false;
    106         }
    107
    108         // Check the target is of the type of class we wish to advise
    109         if (supportsClass.isAssignableFrom(targetClass)) {
    110             return true;
    111         }
    112
    113         return false;
    114     }
    115 }