KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > interceptor > NameMatchTransactionAttributeSource


1 /*
2  * Copyright 2002-2006 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.transaction.interceptor;
18
19 import java.io.Serializable JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.springframework.util.ObjectUtils;
30 import org.springframework.util.PatternMatchUtils;
31
32 /**
33  * Simple {@link TransactionAttributeSource} implementation that
34  * allows attributes to be matched by registered name.
35  *
36  * @author Juergen Hoeller
37  * @since 21.08.2003
38  * @see #isMatch
39  * @see MethodMapTransactionAttributeSource
40  */

41 public class NameMatchTransactionAttributeSource implements TransactionAttributeSource, Serializable JavaDoc {
42
43     /**
44      * Logger available to subclasses.
45      * <p>Static for optimal serialization.
46      */

47     protected static final Log logger = LogFactory.getLog(NameMatchTransactionAttributeSource.class);
48
49     /** Keys are method names; values are TransactionAttributes */
50     private Map JavaDoc nameMap = new HashMap JavaDoc();
51
52
53     /**
54      * Set a name/attribute map, consisting of method names
55      * (e.g. "myMethod") and TransactionAttribute instances
56      * (or Strings to be converted to TransactionAttribute instances).
57      * @see TransactionAttribute
58      * @see TransactionAttributeEditor
59      */

60     public void setNameMap(Map JavaDoc nameMap) {
61         Iterator JavaDoc it = nameMap.entrySet().iterator();
62         while (it.hasNext()) {
63             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
64             String JavaDoc name = (String JavaDoc) entry.getKey();
65
66             // Check whether we need to convert from String to TransactionAttribute.
67
TransactionAttribute attr = null;
68             if (entry.getValue() instanceof TransactionAttribute) {
69                 attr = (TransactionAttribute) entry.getValue();
70             }
71             else {
72                 TransactionAttributeEditor editor = new TransactionAttributeEditor();
73                 editor.setAsText(entry.getValue().toString());
74                 attr = (TransactionAttribute) editor.getValue();
75             }
76
77             addTransactionalMethod(name, attr);
78         }
79     }
80
81     /**
82      * Parses the given properties into a name/attribute map.
83      * Expects method names as keys and String attributes definitions as values,
84      * parsable into TransactionAttribute instances via TransactionAttributeEditor.
85      * @see #setNameMap
86      * @see TransactionAttributeEditor
87      */

88     public void setProperties(Properties JavaDoc transactionAttributes) {
89         TransactionAttributeEditor tae = new TransactionAttributeEditor();
90         for (Iterator JavaDoc it = transactionAttributes.keySet().iterator(); it.hasNext(); ) {
91             String JavaDoc methodName = (String JavaDoc) it.next();
92             String JavaDoc value = transactionAttributes.getProperty(methodName);
93             tae.setAsText(value);
94             TransactionAttribute attr = (TransactionAttribute) tae.getValue();
95             addTransactionalMethod(methodName, attr);
96         }
97     }
98
99     /**
100      * Add an attribute for a transactional method.
101      * <p>Method names can be exact matches, or of the pattern "xxx*",
102      * "*xxx" or "*xxx*" for matching multiple methods.
103      * @param methodName the name of the method
104      * @param attr attribute associated with the method
105      */

106     public void addTransactionalMethod(String JavaDoc methodName, TransactionAttribute attr) {
107         if (logger.isDebugEnabled()) {
108             logger.debug("Adding transactional method [" + methodName + "] with attribute [" + attr + "]");
109         }
110         this.nameMap.put(methodName, attr);
111     }
112
113
114     public TransactionAttribute getTransactionAttribute(Method JavaDoc method, Class JavaDoc targetClass) {
115         // look for direct name match
116
String JavaDoc methodName = method.getName();
117         TransactionAttribute attr = (TransactionAttribute) this.nameMap.get(methodName);
118
119         if (attr == null) {
120             // Look for most specific name match.
121
String JavaDoc bestNameMatch = null;
122             for (Iterator JavaDoc it = this.nameMap.keySet().iterator(); it.hasNext();) {
123                 String JavaDoc mappedName = (String JavaDoc) it.next();
124                 if (isMatch(methodName, mappedName) &&
125                         (bestNameMatch == null || bestNameMatch.length() <= mappedName.length())) {
126                     attr = (TransactionAttribute) this.nameMap.get(mappedName);
127                     bestNameMatch = mappedName;
128                 }
129             }
130         }
131
132         return attr;
133     }
134
135     /**
136      * Return if the given method name matches the mapped name.
137      * <p>The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches,
138      * as well as direct equality. Can be overridden in subclasses.
139      * @param methodName the method name of the class
140      * @param mappedName the name in the descriptor
141      * @return if the names match
142      * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
143      */

144     protected boolean isMatch(String JavaDoc methodName, String JavaDoc mappedName) {
145         return PatternMatchUtils.simpleMatch(mappedName, methodName);
146     }
147
148
149     public boolean equals(Object JavaDoc other) {
150         if (this == other) {
151             return true;
152         }
153         if (!(other instanceof NameMatchTransactionAttributeSource)) {
154             return false;
155         }
156         NameMatchTransactionAttributeSource otherTas = (NameMatchTransactionAttributeSource) other;
157         return ObjectUtils.nullSafeEquals(this.nameMap, otherTas.nameMap);
158     }
159
160     public int hashCode() {
161         return NameMatchTransactionAttributeSource.class.hashCode();
162     }
163
164     public String JavaDoc toString() {
165         return getClass().getName() + ": " + this.nameMap;
166     }
167
168 }
169
Popular Tags