1 4 package com.tc.aspectwerkz.aspect; 5 6 7 import com.tc.aspectwerkz.AspectContext; 8 import com.tc.aspectwerkz.definition.AspectDefinition; 9 import com.tc.aspectwerkz.definition.SystemDefinition; 10 import com.tc.aspectwerkz.definition.SystemDefinitionContainer; 11 12 import java.lang.ref.Reference ; 13 import java.lang.ref.WeakReference ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 18 29 public abstract class AbstractAspectContainer implements AspectContainer { 30 31 protected Class m_aspectClass; 32 protected Reference m_classLoader; 33 protected String m_uuid; 34 protected String m_qualifiedName; 35 private AspectDefinition m_aspectDefinitionLazy; 36 37 public static final int ASPECT_CONSTRUCTION_TYPE_UNKNOWN = 0; 38 public static final int ASPECT_CONSTRUCTION_TYPE_DEFAULT = 1; 39 public static final int ASPECT_CONSTRUCTION_TYPE_ASPECT_CONTEXT = 2; 40 public static final Object [] EMPTY_OBJECT_ARRAY = new Object []{}; 41 42 45 protected int m_constructionType = ASPECT_CONSTRUCTION_TYPE_UNKNOWN; 46 47 56 public AbstractAspectContainer(Class aspectClass, ClassLoader aopSystemClassLoader, String uuid, String qualifiedName, Map parameters) { 57 m_aspectClass = aspectClass; m_classLoader = new WeakReference (aopSystemClassLoader); 59 m_uuid = uuid; 60 m_qualifiedName = qualifiedName; 61 } 62 63 68 public AspectDefinition getAspectDefinition() { 69 if (m_aspectDefinitionLazy == null) { 70 SystemDefinition def = SystemDefinitionContainer.getDefinitionFor(getDefiningSystemClassLoader(), m_uuid); 71 if (def == null) { 72 throw new RuntimeException ( 73 "Definition " + m_uuid + " not found from " + getDefiningSystemClassLoader() 74 ); 75 } 76 for (Iterator iterator = def.getAspectDefinitions().iterator(); iterator.hasNext();) { 77 AspectDefinition aspectDefinition = (AspectDefinition) iterator.next(); 78 if (m_qualifiedName.equals(aspectDefinition.getQualifiedName())) { 79 m_aspectDefinitionLazy = aspectDefinition; 80 } 81 } 82 if (m_aspectDefinitionLazy == null) { 83 throw new RuntimeException ( 84 "Aspect definition not found " + m_qualifiedName + " from " + getDefiningSystemClassLoader() 85 ); 86 } 87 } 88 return m_aspectDefinitionLazy; 89 } 90 91 94 public ClassLoader getDefiningSystemClassLoader() { 95 return ((ClassLoader ) m_classLoader.get()); 96 } 97 98 public Object aspectOf() { 99 return createAspect(getContext(null)); 100 } 101 102 public Object aspectOf(Class klass) { 103 return createAspect(getContext(klass)); 104 } 105 106 public Object aspectOf(Object instance) { 107 return createAspect(getContext(instance)); 108 } 109 110 public Object aspectOf(Thread thread) { 111 return createAspect(getContext(thread)); 112 } 113 114 121 protected abstract Object createAspect(AspectContext aspectContext); 122 123 129 protected AspectContext getContext(Object associated) { 130 AspectDefinition aspectDefinition = getAspectDefinition(); 131 return new AspectContext( 132 m_uuid, 133 m_aspectClass, 134 aspectDefinition.getName(), 135 aspectDefinition.getDeploymentModel(), 136 aspectDefinition, 137 aspectDefinition.getParameters(), 138 associated 139 ); 140 } 141 142 147 public Class getAspectClass() { 148 return m_aspectClass; 149 } 150 } | Popular Tags |