| 1 | /* |
| 2 | * Copyright 2006-2007 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 | package org.springframework.batch.core.step.item; |
| 17 | |
| 18 | import org.apache.commons.logging.Log; |
| 19 | import org.apache.commons.logging.LogFactory; |
| 20 | import org.springframework.aop.Advisor; |
| 21 | import org.springframework.aop.framework.Advised; |
| 22 | import org.springframework.batch.item.validator.ValidationException; |
| 23 | import org.springframework.batch.item.validator.Validator; |
| 24 | import org.springframework.transaction.interceptor.TransactionInterceptor; |
| 25 | import org.springframework.util.Assert; |
| 26 | |
| 27 | /** |
| 28 | * Simple validator for internal use only (package private to make it testable). |
| 29 | * Asserts that its argument has no more than the specified number of |
| 30 | * transaction interceptors in its advice chain. |
| 31 | * |
| 32 | * @author Dave Syer |
| 33 | * |
| 34 | */ |
| 35 | class TransactionInterceptorValidator implements Validator { |
| 36 | |
| 37 | protected Log logger = LogFactory.getLog(getClass()); |
| 38 | |
| 39 | private final int maxCount; |
| 40 | |
| 41 | /** |
| 42 | * @param maxCount |
| 43 | */ |
| 44 | public TransactionInterceptorValidator(int maxCount) { |
| 45 | super(); |
| 46 | this.maxCount = maxCount; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Assert that the object passed in has no more than the maximum number of |
| 51 | * transaction interceptors in its advice chain. |
| 52 | * |
| 53 | * @see org.springframework.batch.item.validator.Validator#validate(java.lang.Object) |
| 54 | */ |
| 55 | public void validate(Object value) throws ValidationException { |
| 56 | Assert.notNull(value, "JobRepository must be provided"); |
| 57 | Assert.state(countTransactionInterceptors(value) <= maxCount, |
| 58 | "JobRepository has more than one transaction interceptor. " |
| 59 | + "Do not declare a separate transaction advice if using the JobRepositoryFactoryBean."); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param object an Object, possibly advised |
| 64 | * @return the number of transaction interceptors in the advice chain |
| 65 | */ |
| 66 | private int countTransactionInterceptors(Object object) { |
| 67 | int count = 0; |
| 68 | Object target = object; |
| 69 | while (target instanceof Advised) { |
| 70 | Advised advised = (Advised) target; |
| 71 | Advisor[] interceptors = advised.getAdvisors(); |
| 72 | for (int i = 0; i < interceptors.length; i++) { |
| 73 | if (interceptors[i].getAdvice() instanceof TransactionInterceptor) { |
| 74 | count++; |
| 75 | } |
| 76 | } |
| 77 | try { |
| 78 | target = advised.getTargetSource().getTarget(); |
| 79 | } |
| 80 | catch (Exception e) { |
| 81 | logger.warn("Target could not be obtained from advised instance.", e); |
| 82 | } |
| 83 | } |
| 84 | return count; |
| 85 | } |
| 86 | |
| 87 | } |