001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.aop.support;
018:
019: import java.io.Serializable;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Set;
023:
024: import org.aopalliance.aop.Advice;
025:
026: import org.springframework.aop.ClassFilter;
027: import org.springframework.aop.DynamicIntroductionAdvice;
028: import org.springframework.aop.IntroductionAdvisor;
029: import org.springframework.aop.IntroductionInfo;
030: import org.springframework.core.Ordered;
031: import org.springframework.util.Assert;
032: import org.springframework.util.ClassUtils;
033:
034: /**
035: * Simple {@link org.springframework.aop.IntroductionAdvisor} implementation
036: * that by default applies to any class.
037: *
038: * @author Rod Johnson
039: * @author Juergen Hoeller
040: * @since 11.11.2003
041: */
042: public class DefaultIntroductionAdvisor implements IntroductionAdvisor,
043: ClassFilter, Ordered, Serializable {
044:
045: private final Advice advice;
046:
047: private final Set interfaces = new HashSet();
048:
049: private int order = Integer.MAX_VALUE;
050:
051: /**
052: * Create a DefaultIntroductionAdvisor for the given advice.
053: * @param advice the Advice to apply (may implement the
054: * {@link org.springframework.aop.IntroductionInfo} interface)
055: * @see #addInterface
056: */
057: public DefaultIntroductionAdvisor(Advice advice) {
058: this (
059: advice,
060: (advice instanceof IntroductionInfo ? (IntroductionInfo) advice
061: : null));
062: }
063:
064: /**
065: * Create a DefaultIntroductionAdvisor for the given advice.
066: * @param advice the Advice to apply
067: * @param introductionInfo the IntroductionInfo that describes
068: * the interface to introduce (may be <code>null</code>)
069: */
070: public DefaultIntroductionAdvisor(Advice advice,
071: IntroductionInfo introductionInfo) {
072: Assert.notNull(advice, "Advice must not be null");
073: this .advice = advice;
074: if (introductionInfo != null) {
075: Class[] introducedInterfaces = introductionInfo
076: .getInterfaces();
077: if (introducedInterfaces.length == 0) {
078: throw new IllegalArgumentException(
079: "IntroductionAdviceSupport implements no interfaces");
080: }
081: for (int i = 0; i < introducedInterfaces.length; i++) {
082: addInterface(introducedInterfaces[i]);
083: }
084: }
085: }
086:
087: /**
088: * Create a DefaultIntroductionAdvisor for the given advice.
089: * @param advice the Advice to apply
090: * @param intf the interface to introduce
091: */
092: public DefaultIntroductionAdvisor(DynamicIntroductionAdvice advice,
093: Class intf) {
094: Assert.notNull(advice, "Advice must not be null");
095: this .advice = advice;
096: addInterface(intf);
097: }
098:
099: /**
100: * Add the specified interface to the list of interfaces to introduce.
101: * @param intf the interface to introduce
102: */
103: public void addInterface(Class intf) {
104: Assert.notNull(intf, "Interface must not be null");
105: if (!intf.isInterface()) {
106: throw new IllegalArgumentException("Specified class ["
107: + intf.getName() + "] must be an interface");
108: }
109: this .interfaces.add(intf);
110: }
111:
112: public Class[] getInterfaces() {
113: return (Class[]) this .interfaces
114: .toArray(new Class[this .interfaces.size()]);
115: }
116:
117: public void validateInterfaces() throws IllegalArgumentException {
118: for (Iterator it = this .interfaces.iterator(); it.hasNext();) {
119: Class ifc = (Class) it.next();
120: if (this .advice instanceof DynamicIntroductionAdvice
121: && !((DynamicIntroductionAdvice) this .advice)
122: .implements Interface(ifc)) {
123: throw new IllegalArgumentException(
124: "DynamicIntroductionAdvice [" + this .advice
125: + "] "
126: + "does not implement interface ["
127: + ifc.getName()
128: + "] specified for introduction");
129: }
130: }
131: }
132:
133: public void setOrder(int order) {
134: this .order = order;
135: }
136:
137: public int getOrder() {
138: return this .order;
139: }
140:
141: public Advice getAdvice() {
142: return this .advice;
143: }
144:
145: public boolean isPerInstance() {
146: return true;
147: }
148:
149: public ClassFilter getClassFilter() {
150: return this ;
151: }
152:
153: public boolean matches(Class clazz) {
154: return true;
155: }
156:
157: public boolean equals(Object other) {
158: if (this == other) {
159: return true;
160: }
161: if (!(other instanceof DefaultIntroductionAdvisor)) {
162: return false;
163: }
164: DefaultIntroductionAdvisor otherAdvisor = (DefaultIntroductionAdvisor) other;
165: return (this .advice.equals(otherAdvisor.advice) && this .interfaces
166: .equals(otherAdvisor.interfaces));
167: }
168:
169: public int hashCode() {
170: return this .advice.hashCode() * 13 + this .interfaces.hashCode();
171: }
172:
173: public String toString() {
174: return ClassUtils.getShortName(getClass()) + ": advice ["
175: + this .advice + "]; interfaces "
176: + ClassUtils.classNamesToString(this.interfaces);
177: }
178:
179: }
|