KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > aspect > AbstractAspectContainer


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
13 import java.lang.ref.WeakReference JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17
18 /**
19  * Abstract base class for an aspect container implementations that is passing an AspectContext when
20  * creating the aspect instance if there is such a single arg constructor in the aspect.
21  * <p/>
22  * Provides support for getting the AspectDefinition.
23  * <p/>
24  * Can be used as a base class for user defined container.
25  *
26  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
27  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
28  */

29 public abstract class AbstractAspectContainer implements AspectContainer {
30
31   protected Class JavaDoc m_aspectClass;
32   protected Reference JavaDoc m_classLoader;
33   protected String JavaDoc m_uuid;
34   protected String JavaDoc 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 JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[]{};
41
42   /**
43    * The aspect construction type. Defaults to unknown
44    */

45   protected int m_constructionType = ASPECT_CONSTRUCTION_TYPE_UNKNOWN;
46
47   /**
48    * Create a new container
49    *
50    * @param aspectClass
51    * @param aopSystemClassLoader the classloader of the defining system (not necessary the one of the aspect class)
52    * @param uuid
53    * @param qualifiedName
54    * @param parameters
55    */

56   public AbstractAspectContainer(Class JavaDoc aspectClass, ClassLoader JavaDoc aopSystemClassLoader, String JavaDoc uuid, String JavaDoc qualifiedName, Map JavaDoc parameters) {
57     m_aspectClass = aspectClass;// we hold a strong ref this the container is hold by the aspect factory only
58
m_classLoader = new WeakReference JavaDoc(aopSystemClassLoader);
59     m_uuid = uuid;
60     m_qualifiedName = qualifiedName;
61   }
62
63   /**
64    * Lazy getter for the aspect definition
65    *
66    * @return the aspect definition
67    */

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 JavaDoc(
73                 "Definition " + m_uuid + " not found from " + getDefiningSystemClassLoader()
74         );
75       }
76       for (Iterator JavaDoc 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 JavaDoc(
84                 "Aspect definition not found " + m_qualifiedName + " from " + getDefiningSystemClassLoader()
85         );
86       }
87     }
88     return m_aspectDefinitionLazy;
89   }
90
91   /**
92    * @return the classloader of the system defining the aspect handled by that container instance
93    */

94   public ClassLoader JavaDoc getDefiningSystemClassLoader() {
95     return ((ClassLoader JavaDoc) m_classLoader.get());
96   }
97
98   public Object JavaDoc aspectOf() {
99     return createAspect(getContext(null));
100   }
101
102   public Object JavaDoc aspectOf(Class JavaDoc klass) {
103     return createAspect(getContext(klass));
104   }
105
106   public Object JavaDoc aspectOf(Object JavaDoc instance) {
107     return createAspect(getContext(instance));
108   }
109
110   public Object JavaDoc aspectOf(Thread JavaDoc thread) {
111     return createAspect(getContext(thread));
112   }
113
114   /**
115    * To be implemented by the concrete aspect containers.
116    * <p/>
117    * Should return a new aspect instance.
118    *
119    * @return a new aspect instance
120    */

121   protected abstract Object JavaDoc createAspect(AspectContext aspectContext);
122
123   /**
124    * Create a new AspectContext associated with the given instance (class/instance/thread/null)
125    *
126    * @param associated
127    * @return the context
128    */

129   protected AspectContext getContext(Object JavaDoc 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   /**
143    * Returns the aspect class
144    *
145    * @return the aspect class
146    */

147   public Class JavaDoc getAspectClass() {
148     return m_aspectClass;
149   }
150 }
Popular Tags