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.acegisecurity.domain.PersistableEntity;
    19  import org.acegisecurity.domain.impl.BusinessObject;
    20
    21  import org.aopalliance.intercept.MethodInterceptor;
    22  import org.aopalliance.intercept.MethodInvocation;
    23
    24  import org.apache.commons.logging.Log;
    25  import org.apache.commons.logging.LogFactory;
    26
    27  import org.springframework.beans.factory.InitializingBean;
    28
    29  import org.springframework.util.Assert;
    30
    31
    32  /***
    33   * Calls {@link ValidationManager} for method invocations.
    34   *
    35   * <p>
    36   * For each method invocation, any argument that is assignable from {@link
    37   * #argumentClasses} <b>and</b> is non-<code>null</code> will be passed to the
    38   * {@link org.acegisecurity.domain.validation.ValidationManager} for
    39   * processing.
    40   * </p>
    41   *
    42   * @author Ben Alex
    43   * @version $Id: ValidationInterceptor.java,v 1.3 2005/11/17 00:55:50 benalex Exp $
    44   */
    45  public class ValidationInterceptor implements MethodInterceptor,
    46      InitializingBean {
    47      //~ Instance fields ========================================================
    48
    49      protected final Log logger = LogFactory.getLog(getClass());
    50      private ValidationManager validationManager;
    51      private Class<?>[] argumentClasses = {BusinessObject.class, PersistableEntity.class};
    52
    53      //~ Methods ================================================================
    54
    55      public void setArgumentClasses(Class[] argumentClasses) {
    56          this.argumentClasses = argumentClasses;
    57      }
    58
    59      public Class[] getArgumentClasses() {
    60          return argumentClasses;
    61      }
    62
    63      public void setValidationManager(ValidationManager validationManager) {
    64          this.validationManager = validationManager;
    65      }
    66
    67      public ValidationManager getValidationManager() {
    68          return validationManager;
    69      }
    70
    71      public void afterPropertiesSet() throws Exception {
    72          Assert.notNull(validationManager, "A ValidationManager is required");
    73          Assert.notEmpty(argumentClasses,
    74              "A list of business object classes to validate is required");
    75      }
    76
    77      public Object invoke(MethodInvocation mi) throws Throwable {
    78          Object[] args = mi.getArguments();
    79
    80          for (int i = 0; i < args.length; i++) {
    81              if (shouldValidate(args[i])) {
    82                  if (logger.isDebugEnabled()) {
    83                      logger.debug("ValidationInterceptor calling for: '"
    84                          + args[i] + "'");
    85                  }
    86
    87                  validationManager.validate(args[i]);
    88              }
    89          }
    90
    91          return mi.proceed();
    92      }
    93
    94      private boolean shouldValidate(Object argument) {
    95          if (argument == null) {
    96              return false;
    97          }
    98
    99          for (int i = 0; i < argumentClasses.length; i++) {
    100             if (argumentClasses[i].isAssignableFrom(argument.getClass())) {
    101                 return true;
    102             }
    103         }
    104
    105         return false;
    106     }
    107 }