KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > support > IntroductionInfoSupport


1 /*
2  * Copyright 2002-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
17 package org.springframework.aop.support;
18
19 import java.io.IOException JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.aopalliance.intercept.MethodInvocation;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import org.springframework.aop.IntroductionInfo;
32 import org.springframework.core.CollectionFactory;
33 import org.springframework.util.ClassUtils;
34
35 /**
36  * Support for implementations of {@link org.springframework.aop.IntroductionInfo}.
37  *
38  * <p>Allows subclasses to conveniently add all interfaces from a given object,
39  * and to suppress interfaces that should not be added. Also allows for querying
40  * all introduced interfaces.
41  *
42  * @author Rod Johnson
43  * @author Juergen Hoeller
44  */

45 public class IntroductionInfoSupport implements IntroductionInfo, Serializable JavaDoc {
46
47     protected transient Log logger = LogFactory.getLog(getClass());
48
49     /** Set of interface Classes */
50     protected Set JavaDoc publishedInterfaces = new HashSet JavaDoc();
51
52     /**
53      * Methods that we know we should implement here: key is Method, value is Boolean.
54      **/

55     private transient Map JavaDoc rememberedMethods = createRememberedMethodMap();
56
57
58     /**
59      * Suppress the specified interface, which may have been autodetected
60      * due to the delegate implementing it. Call this method to exclude
61      * internal interfaces from being visible at the proxy level.
62      * <p>Does nothing if the interface is not implemented by the delegate.
63      * @param intf the interface to suppress
64      */

65     public void suppressInterface(Class JavaDoc intf) {
66         this.publishedInterfaces.remove(intf);
67     }
68
69     public Class JavaDoc[] getInterfaces() {
70         return (Class JavaDoc[]) this.publishedInterfaces.toArray(new Class JavaDoc[this.publishedInterfaces.size()]);
71     }
72
73     /**
74      * Check whether the specified interfaces is a published introduction interface.
75      * @param intf the interface to check
76      * @return whether the interface is part of this introduction
77      */

78     public boolean implementsInterface(Class JavaDoc intf) {
79         for (Iterator JavaDoc it = this.publishedInterfaces.iterator(); it.hasNext();) {
80             Class JavaDoc pubIntf = (Class JavaDoc) it.next();
81             if (intf.isInterface() && intf.isAssignableFrom(pubIntf)) {
82                 return true;
83             }
84         }
85         return false;
86     }
87
88     /**
89      * Publish all interfaces that the given delegate implements at the proxy level.
90      * @param delegate the delegate object
91      */

92     protected void implementInterfacesOnObject(Object JavaDoc delegate) {
93         this.publishedInterfaces.addAll(ClassUtils.getAllInterfacesAsSet(delegate));
94     }
95
96     private Map JavaDoc createRememberedMethodMap() {
97         return CollectionFactory.createIdentityMapIfPossible(32);
98     }
99
100     /**
101      * Is this method on an introduced interface?
102      * @param mi the method invocation
103      * @return whether the invoked method is on an introduced interface
104      */

105     protected final boolean isMethodOnIntroducedInterface(MethodInvocation mi) {
106         Boolean JavaDoc rememberedResult = (Boolean JavaDoc) this.rememberedMethods.get(mi.getMethod());
107         if (rememberedResult != null) {
108             return rememberedResult.booleanValue();
109         }
110         else {
111             // Work it out and cache it.
112
boolean result = implementsInterface(mi.getMethod().getDeclaringClass());
113             this.rememberedMethods.put(mi.getMethod(), (result ? Boolean.TRUE : Boolean.FALSE));
114             return result;
115         }
116     }
117
118
119     //---------------------------------------------------------------------
120
// Serialization support
121
//---------------------------------------------------------------------
122

123     /**
124      * This method is implemented only to restore the logger.
125      * We don't make the logger static as that would mean that subclasses
126      * would use this class's log category.
127      */

128     private void readObject(ObjectInputStream JavaDoc ois) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
129         // Rely on default serialization; just initialize state after deserialization.
130
ois.defaultReadObject();
131
132         // Initialize transient fields.
133
this.logger = LogFactory.getLog(getClass());
134         this.rememberedMethods = createRememberedMethodMap();
135     }
136
137 }
138
Popular Tags