001: package org.mockejb.interceptor;
002:
003: import java.lang.reflect.Method;
004: import java.util.*;
005:
006: /**
007: * Provides the implementation of the AspectSystem by adding aspects to the
008: * list and providing access to this list.
009: * Note that all "add" method first check if the same aspect
010: * is already in the list. If it is, the aspect is removed from the list and then added again.
011: * This is to prevent having the same aspect in the list multiple times.
012: * Aspect's "equals" method is used to compare aspects/interceptors, so it is
013: * the aspect providers must have this method implemented.
014: *
015: * @author Alexander Ananiev
016: */
017: public class AspectSystemImpl implements AspectSystem {
018:
019: /**
020: * Note the synchronization
021: */
022: private List aspectList = Collections
023: .synchronizedList(new LinkedList());
024:
025: /**
026: *
027: * @see org.mockejb.interceptor.AspectSystem#add(org.mockejb.interceptor.Aspect)
028: */
029: public void add(Aspect aspect) {
030:
031: removeIfExists(aspect);
032:
033: aspectList.add(aspect);
034:
035: }
036:
037: /**
038: *
039: * @see org.mockejb.interceptor.AspectSystem#addFirst(org.mockejb.interceptor.Aspect)
040: */
041: public void addFirst(Aspect aspect) {
042:
043: removeIfExists(aspect);
044:
045: aspectList.add(0, aspect);
046:
047: }
048:
049: /**
050: * @see org.mockejb.interceptor.AspectSystem#add(org.mockejb.interceptor.Pointcut, org.mockejb.interceptor.Interceptor)
051: */
052: public void add(Pointcut pointcut, Interceptor interceptor) {
053:
054: Aspect aspect = new InterceptorContainerAspect(pointcut,
055: interceptor);
056:
057: removeIfExists(aspect);
058:
059: aspectList.add(aspect);
060:
061: }
062:
063: /**
064: * @see org.mockejb.interceptor.AspectSystem#addFirst(org.mockejb.interceptor.Pointcut, org.mockejb.interceptor.Interceptor)
065: */
066: public void addFirst(Pointcut pointcut, Interceptor interceptor) {
067:
068: Aspect aspect = new InterceptorContainerAspect(pointcut,
069: interceptor);
070:
071: removeIfExists(aspect);
072:
073: aspectList.add(0, aspect);
074:
075: }
076:
077: protected void removeIfExists(Aspect aspect) {
078:
079: aspectList.remove(aspect);
080: }
081:
082: /**
083: * @see org.mockejb.interceptor.AspectSystem#getAspectList()
084: */
085: public List getAspectList() {
086:
087: return aspectList;
088: }
089:
090: /**
091: * Clears the list of aspects for this AspectSystem
092: *
093: */
094: public void clear() {
095:
096: aspectList.clear();
097:
098: }
099:
100: /**
101: * @see org.mockejb.interceptor.AspectSystem#findInterceptors(java.lang.reflect.Method, java.lang.reflect.Method)
102: */
103: public List findInterceptors(Method proxyMethod, Method targetMethod) {
104:
105: //System.out.println("Intercepted method: "+interceptedMethod+" Target method: "+targetMethod);
106:
107: List resultList = new ArrayList();
108: for (Iterator i = aspectList.iterator(); i.hasNext();) {
109: Aspect aspect = (Aspect) i.next();
110:
111: if (proxyMethod != null
112: && aspect.getPointcut().matchesJointpoint(
113: proxyMethod)
114: || targetMethod != null
115: && aspect.getPointcut().matchesJointpoint(
116: targetMethod)) {
117:
118: resultList.add(aspect);
119: }
120: }
121:
122: return resultList;
123:
124: }
125:
126: }
|