KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > autoproxy > OrderedTxCheckAdvisor


1 /*
2  * Copyright 2002-2005 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.springframework.aop.framework.autoproxy;
18
19 import java.lang.reflect.Method JavaDoc;
20
21 import org.springframework.aop.framework.CountingBeforeAdvice;
22 import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
23 import org.springframework.beans.factory.InitializingBean;
24 import org.springframework.transaction.NoTransactionException;
25 import org.springframework.transaction.interceptor.TransactionInterceptor;
26
27 /**
28  * Before advisor that allow us to manipulate ordering to check
29  * that superclass sorting works correctly.
30  * It doesn't actually <i>do</i> anything except count
31  * method invocations and check for presence of transaction context.
32  * <br>Matches setters.
33  *
34  * @author Rod Johnson
35  */

36 public class OrderedTxCheckAdvisor extends StaticMethodMatcherPointcutAdvisor implements InitializingBean {
37
38     /**
39      * Should we insist on the presence of a transaction attribute
40      * or refuse to accept one?
41      */

42     private boolean requireTransactionContext = false;
43
44     public boolean isRequireTransactionContext() {
45         return requireTransactionContext;
46     }
47
48     public void setRequireTransactionContext(boolean b) {
49         requireTransactionContext = b;
50     }
51
52     public CountingBeforeAdvice getCountingBeforeAdvice() {
53         return (CountingBeforeAdvice) getAdvice();
54     }
55
56     public void afterPropertiesSet() throws Exception JavaDoc {
57         setAdvice(new TxCountingBeforeAdvice());
58     }
59
60     public boolean matches(Method JavaDoc m, Class JavaDoc targetClass) {
61         return m.getName().startsWith("set");
62     }
63
64
65     private class TxCountingBeforeAdvice extends CountingBeforeAdvice {
66
67         public void before(Method JavaDoc m, Object JavaDoc[] args, Object JavaDoc target) throws Throwable JavaDoc {
68             // do transaction checks
69
if (requireTransactionContext) {
70                 TransactionInterceptor.currentTransactionStatus();
71             }
72             else {
73                 try {
74                     TransactionInterceptor.currentTransactionStatus();
75                     throw new RuntimeException JavaDoc("Shouldn't have a transaction");
76                 }
77                 catch (NoTransactionException ex) {
78                     // this is Ok
79
}
80             }
81             super.before(m, args, target);
82         }
83     }
84
85 }
86
Popular Tags