1 /* 2 * Copyright 2004-2009 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springmodules.validation.bean.context.aop; 18 19 import org.aopalliance.intercept.MethodInterceptor; 20 import org.aopalliance.intercept.MethodInvocation; 21 import org.springmodules.validation.bean.context.ValidationContext; 22 import org.springmodules.validation.bean.context.ValidationContextHolder; 23 import org.springmodules.validation.bean.context.ValidationContextUtils; 24 25 /** 26 * A parent class to any around validation context advice that intercepts method invocations. 27 * <p/> 28 * This advice is based on {@link org.springmodules.validation.bean.context.DefaultValidationContext} and relies on 29 * the {@link #getValidationContextTokens(org.aopalliance.intercept.MethodInvocation)} implementation to supply the 30 * appropiate context. 31 * 32 * @author Uri Boness 33 */ 34 public abstract class AbstractValidationContextInterceptor implements MethodInterceptor { 35 36 private boolean extendExistingContext = false; 37 38 public Object invoke(MethodInvocation methodInvocation) throws Throwable { 39 40 String[] contextTokens = getValidationContextTokens(methodInvocation); 41 42 ValidationContext originalContext = ValidationContextHolder.getValidationContext(); 43 if (originalContext != null && extendExistingContext) { 44 ValidationContextUtils.extendContext(contextTokens); 45 } else { 46 ValidationContextUtils.setContext(contextTokens); 47 } 48 49 Object result = methodInvocation.proceed(); 50 51 // setting the validation context back to the original one (the one that was set before this advice was called) 52 if (originalContext != null) { 53 ValidationContextHolder.setValidationContext(originalContext); 54 } else { 55 ValidationContextUtils.clearContext(); 56 } 57 58 return result; 59 60 } 61 62 /** 63 * Will be implemented by all subclasses to determine what validation context tokens will be supported by the 64 * validation context associated with the given method invocation. 65 * 66 * @param methodInvocation The given method invocation. 67 * @return The tokens supported by the validation context associated with the given method invocation. 68 */ 69 protected abstract String[] getValidationContextTokens(MethodInvocation methodInvocation); 70 71 72 //============================================== Setter/Getter ===================================================== 73 74 /** 75 * Determines whether this interceptor should extend the already exsiting context (if one already exists). If so, 76 * the tokens returned by {@link #getValidationContextTokens(org.aopalliance.intercept.MethodInvocation)} will be 77 * supported along with all tokens supported by the already existing validation context. 78 * <p/> 79 * By default this advice does <b>not</b> extend the already existing context, instead it creates and sets a new one. 80 * 81 * @param extendExistingContext Determines whether this interceptor should extend the already exsiting context 82 * (if one already exists). 83 */ 84 public void setExtendExistingContext(boolean extendExistingContext) { 85 this.extendExistingContext = extendExistingContext; 86 } 87 }