KickJava   Java API By Example, From Geeks To Geeks.

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


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.Serializable JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.aopalliance.aop.Advice;
25
26 import org.springframework.aop.ClassFilter;
27 import org.springframework.aop.DynamicIntroductionAdvice;
28 import org.springframework.aop.IntroductionAdvisor;
29 import org.springframework.aop.IntroductionInfo;
30 import org.springframework.core.Ordered;
31 import org.springframework.util.Assert;
32 import org.springframework.util.ClassUtils;
33
34 /**
35  * Simple {@link org.springframework.aop.IntroductionAdvisor} implementation
36  * that by default applies to any class.
37  *
38  * @author Rod Johnson
39  * @author Juergen Hoeller
40  * @since 11.11.2003
41  */

42 public class DefaultIntroductionAdvisor implements IntroductionAdvisor, ClassFilter, Ordered, Serializable JavaDoc {
43
44     private final Advice advice;
45     
46     private final Set JavaDoc interfaces = new HashSet JavaDoc();
47
48     private int order = Integer.MAX_VALUE;
49
50
51     /**
52      * Create a DefaultIntroductionAdvisor for the given advice.
53      * @param advice the Advice to apply (may implement the
54      * {@link org.springframework.aop.IntroductionInfo} interface)
55      * @see #addInterface
56      */

57     public DefaultIntroductionAdvisor(Advice advice) {
58         this(advice, (advice instanceof IntroductionInfo ? (IntroductionInfo) advice : null));
59     }
60
61     /**
62      * Create a DefaultIntroductionAdvisor for the given advice.
63      * @param advice the Advice to apply
64      * @param introductionInfo the IntroductionInfo that describes
65      * the interface to introduce (may be <code>null</code>)
66      */

67     public DefaultIntroductionAdvisor(Advice advice, IntroductionInfo introductionInfo) {
68         Assert.notNull(advice, "Advice must not be null");
69         this.advice = advice;
70         if (introductionInfo != null) {
71             Class JavaDoc[] introducedInterfaces = introductionInfo.getInterfaces();
72             if (introducedInterfaces.length == 0) {
73                 throw new IllegalArgumentException JavaDoc("IntroductionAdviceSupport implements no interfaces");
74             }
75             for (int i = 0; i < introducedInterfaces.length; i++) {
76                 addInterface(introducedInterfaces[i]);
77             }
78         }
79     }
80
81     /**
82      * Create a DefaultIntroductionAdvisor for the given advice.
83      * @param advice the Advice to apply
84      * @param intf the interface to introduce
85      */

86     public DefaultIntroductionAdvisor(DynamicIntroductionAdvice advice, Class JavaDoc intf) {
87         Assert.notNull(advice, "Advice must not be null");
88         this.advice = advice;
89         addInterface(intf);
90     }
91
92
93     /**
94      * Add the specified interface to the list of interfaces to introduce.
95      * @param intf the interface to introduce
96      */

97     public void addInterface(Class JavaDoc intf) {
98         Assert.notNull(intf, "Interface must not be null");
99         if (!intf.isInterface()) {
100             throw new IllegalArgumentException JavaDoc("Specified class [" + intf.getName() + "] must be an interface");
101         }
102         this.interfaces.add(intf);
103     }
104
105     public Class JavaDoc[] getInterfaces() {
106         return (Class JavaDoc[]) this.interfaces.toArray(new Class JavaDoc[this.interfaces.size()]);
107     }
108
109     public void validateInterfaces() throws IllegalArgumentException JavaDoc {
110         for (Iterator JavaDoc it = this.interfaces.iterator(); it.hasNext();) {
111             Class JavaDoc ifc = (Class JavaDoc) it.next();
112             if (this.advice instanceof DynamicIntroductionAdvice &&
113                     !((DynamicIntroductionAdvice) this.advice).implementsInterface(ifc)) {
114              throw new IllegalArgumentException JavaDoc("DynamicIntroductionAdvice [" + this.advice + "] " +
115                      "does not implement interface [" + ifc.getName() + "] specified for introduction");
116             }
117         }
118     }
119
120
121     public void setOrder(int order) {
122         this.order = order;
123     }
124
125     public int getOrder() {
126         return this.order;
127     }
128
129
130     public Advice getAdvice() {
131         return this.advice;
132     }
133
134     public boolean isPerInstance() {
135         return true;
136     }
137
138     public ClassFilter getClassFilter() {
139         return this;
140     }
141
142     public boolean matches(Class JavaDoc clazz) {
143         return true;
144     }
145
146
147     public boolean equals(Object JavaDoc other) {
148         if (this == other) {
149             return true;
150         }
151         if (!(other instanceof DefaultIntroductionAdvisor)) {
152             return false;
153         }
154         DefaultIntroductionAdvisor otherAdvisor = (DefaultIntroductionAdvisor) other;
155         return (this.advice.equals(otherAdvisor.advice) && this.interfaces.equals(otherAdvisor.interfaces));
156     }
157
158     public int hashCode() {
159         return this.advice.hashCode() * 13 + this.interfaces.hashCode();
160     }
161
162     public String JavaDoc toString() {
163         return ClassUtils.getShortName(getClass()) + ": advice [" + this.advice + "]; interfaces " +
164                 ClassUtils.classNamesToString(this.interfaces);
165     }
166
167 }
168
Popular Tags