1 8 package org.codehaus.aspectwerkz.aspect; 9 10 import java.util.HashMap ; 11 import java.util.Map ; 12 import java.util.WeakHashMap ; 13 14 import org.codehaus.aspectwerkz.AspectContext; 15 16 17 24 public abstract class AbstractAspectContainer implements AspectContainer { 25 26 public static final int ASPECT_CONSTRUCTION_TYPE_UNKNOWN = 0; 27 public static final int ASPECT_CONSTRUCTION_TYPE_DEFAULT = 1; 28 public static final int ASPECT_CONSTRUCTION_TYPE_ASPECT_CONTEXT = 2; 29 public static final Object [] EMPTY_OBJECT_ARRAY = new Object []{}; 30 31 34 protected final Object [] ARRAY_WITH_SINGLE_ASPECT_CONTEXT = new Object [1]; 35 36 39 protected int m_constructionType = ASPECT_CONSTRUCTION_TYPE_UNKNOWN; 40 41 44 protected final AspectContext m_aspectContext; 45 46 49 protected Object m_perJvm; 50 51 54 protected final Map m_perClass = new WeakHashMap (); 55 56 59 protected final Map m_perInstance = new WeakHashMap (); 60 61 64 protected final Map m_perThread = new WeakHashMap (); 65 66 69 protected final Map m_adviceInfos = new HashMap (); 70 71 76 public AbstractAspectContainer(final AspectContext aspectContext) { 77 if (aspectContext == null) { 78 throw new IllegalArgumentException ("cross-cutting info can not be null"); 79 } 80 81 m_aspectContext = aspectContext; 82 ARRAY_WITH_SINGLE_ASPECT_CONTEXT[0] = m_aspectContext; 83 } 84 85 90 public AspectContext getContext() { 91 return m_aspectContext; 92 } 93 94 100 public Object aspectOf() { 101 if (m_perJvm == null) { 102 m_perJvm = createAspect(); 103 } 104 return m_perJvm; 105 } 106 107 113 public Object aspectOf(final Class klass) { 114 synchronized (m_perClass) { 115 if (!m_perClass.containsKey(klass)) { 116 m_perClass.put(klass, createAspect()); 117 } 118 } 119 return m_perClass.get(klass); 120 } 121 122 128 public Object aspectOf(final Object instance) { 129 synchronized (m_perInstance) { 130 if (!m_perInstance.containsKey(instance)) { 131 m_perInstance.put(instance, createAspect()); 132 } 133 } 134 return m_perInstance.get(instance); 135 } 136 137 143 public Object aspectOf(final Thread thread) { 144 synchronized (m_perThread) { 145 if (!m_perThread.containsKey(thread)) { 146 m_perThread.put(thread, createAspect()); 147 } 148 } 149 return m_perThread.get(thread); 150 } 151 152 157 protected abstract Object createAspect(); 158 } | Popular Tags |