KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > definition > AspectDefinition


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.definition;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import com.tc.aspectwerkz.DeploymentModel;
13 import com.tc.aspectwerkz.reflect.ClassInfo;
14 import com.tc.aspectwerkz.reflect.ConstructorInfo;
15 import com.tc.aspectwerkz.aspect.DefaultAspectContainerStrategy;
16 import com.tc.aspectwerkz.transform.inlining.model.AspectWerkzAspectModel;
17
18 /**
19  * Holds the meta-data for the aspect.
20  *
21  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
22  * @author <a HREF="mailto:alex@gnilux.com">Alexandre Vasseur </a>
23  */

24 public class AspectDefinition {
25
26   private final static String JavaDoc DEFAULT_ASPECTCONTAINER_CLASSNAME = DefaultAspectContainerStrategy.class.getName();
27
28   /**
29    * The name of the aspect (nickname).
30    */

31   private String JavaDoc m_name;
32
33   /**
34    * The nickname of the aspect prefixed by the <system uuid>/
35    */

36   private String JavaDoc m_qualifiedName;
37
38   /**
39    * The aspect class info.
40    */

41   private final ClassInfo m_classInfo;
42
43   /**
44    * The deployment model for the aspect.
45    */

46   private DeploymentModel m_deploymentModel = DeploymentModel.PER_JVM;
47
48   /**
49    * The around advices.
50    */

51   private final List JavaDoc m_aroundAdviceDefinitions = new ArrayList JavaDoc();
52
53   /**
54    * The before advices.
55    */

56   private final List JavaDoc m_beforeAdviceDefinitions = new ArrayList JavaDoc();
57
58   /**
59    * The after advices.
60    */

61   private final List JavaDoc m_afterAdviceDefinitions = new ArrayList JavaDoc();
62
63   /**
64    * The interface introductions (pure interfaces)
65    */

66   private final List JavaDoc m_interfaceIntroductionDefinitions = new ArrayList JavaDoc();
67
68   /**
69    * The pointcuts.
70    */

71   private final List JavaDoc m_pointcutDefinitions = new ArrayList JavaDoc();
72
73   /**
74    * The parameters passed to the aspect at definition time.
75    */

76   private Map JavaDoc m_parameters = new HashMap JavaDoc();
77
78   /**
79    * The container implementation class name or null if no container (inlined instantiation in the factory)
80    */

81   private String JavaDoc m_containerClassName;
82
83   /**
84    * The system definition.
85    */

86   private SystemDefinition m_systemDefinition;
87
88   /**
89    * The aspect model. Defaults to AspectWerkz
90    */

91   private String JavaDoc m_aspectModelType = AspectWerkzAspectModel.TYPE;
92
93   /**
94    * Creates a new aspect meta-data instance.
95    *
96    * @param name the name of the aspect
97    * @param classInfo the class info for the aspect
98    * @param systemDefinition
99    */

100   public AspectDefinition(final String JavaDoc name, final ClassInfo classInfo, final SystemDefinition systemDefinition) {
101     if (name == null) {
102       throw new IllegalArgumentException JavaDoc("aspect name can not be null");
103     }
104     if (classInfo == null) {
105       throw new IllegalArgumentException JavaDoc("aspect class info can not be null");
106     }
107     m_name = name;
108     m_classInfo = classInfo;
109     m_systemDefinition = systemDefinition;
110     m_qualifiedName = systemDefinition.getUuid() + '/' + name;
111
112     // if no-arg ctor not found, set the default aspect container just in case it is an old 2.0 aspect
113
// and the user forgot to add the container="...DefaultAspectContainerStrategy"
114
// while still using a ctor(AspectContext) style in the aspect
115
boolean hasNoArg = false;
116     for (int i = 0; i < m_classInfo.getConstructors().length; i++) {
117       ConstructorInfo constructorInfo = m_classInfo.getConstructors()[i];
118       if ("()V".equals(constructorInfo.getSignature())) {
119         hasNoArg = true;
120         break;
121       }
122     }
123     if (!hasNoArg) {
124       setContainerClassName(DEFAULT_ASPECTCONTAINER_CLASSNAME);
125     }
126   }
127
128   /**
129    * Returns the name for the advice
130    *
131    * @return the name
132    */

133   public String JavaDoc getName() {
134     return m_name;
135   }
136
137   /**
138    * Sets the name for the aspect.
139    *
140    * @param name the name
141    */

142   public void setName(final String JavaDoc name) {
143     m_name = name.trim();
144   }
145
146   /**
147    * Returns the fully qualified name for the advice
148    *
149    * @return the fully qualified name
150    */

151   public String JavaDoc getQualifiedName() {
152     return m_qualifiedName;
153   }
154
155   /**
156    * Returns the system definition.
157    *
158    * @return
159    */

160   public SystemDefinition getSystemDefinition() {
161     return m_systemDefinition;
162   }
163
164   /**
165    * Returns the class name.
166    *
167    * @return the class name
168    */

169   public String JavaDoc getClassName() {
170     return m_classInfo.getName();
171   }
172
173   /**
174    * Returns the class info.
175    *
176    * @return the class info
177    */

178   public ClassInfo getClassInfo() {
179     return m_classInfo;
180   }
181
182   /**
183    * Returns the aspect model.
184    *
185    * @return the aspect model
186    */

187   public String JavaDoc getAspectModel() {
188     return m_aspectModelType;
189   }
190
191   /**
192    * Checks if the aspect defined is an AspectWerkz aspect.
193    *
194    * @return
195    */

196   public boolean isAspectWerkzAspect() {
197     return m_aspectModelType.equals(AspectWerkzAspectModel.TYPE);
198   }
199
200   /**
201    * Sets the aspect model.
202    *
203    * @param aspectModelType the aspect model
204    */

205   public void setAspectModel(final String JavaDoc aspectModelType) {
206     m_aspectModelType = aspectModelType;
207   }
208
209   /**
210    * Sets the deployment model.
211    *
212    * @param deploymentModel the deployment model
213    */

214   public void setDeploymentModel(final DeploymentModel deploymentModel) {
215     m_deploymentModel = deploymentModel;
216   }
217
218   /**
219    * Returns the deployment model.
220    *
221    * @return the deployment model
222    */

223   public DeploymentModel getDeploymentModel() {
224     return m_deploymentModel;
225   }
226
227   /**
228    * Adds a new around advice.
229    *
230    * @param adviceDef the around advice
231    */

232   public void addAroundAdviceDefinition(final AdviceDefinition adviceDef) {
233     if (!m_aroundAdviceDefinitions.contains(adviceDef)) {
234       m_aroundAdviceDefinitions.add(adviceDef);
235     }
236   }
237
238   /**
239    * Returns the around advices.
240    *
241    * @return the around advices
242    */

243   public List JavaDoc getAroundAdviceDefinitions() {
244     return m_aroundAdviceDefinitions;
245   }
246
247   /**
248    * Adds a new before advice.
249    *
250    * @param adviceDef the before advice
251    */

252   public void addBeforeAdviceDefinition(final AdviceDefinition adviceDef) {
253     if (!m_beforeAdviceDefinitions.contains(adviceDef)) {
254       m_beforeAdviceDefinitions.add(adviceDef);
255     }
256   }
257
258   /**
259    * Returns the before advices.
260    *
261    * @return the before advices
262    */

263   public List JavaDoc getBeforeAdviceDefinitions() {
264     return m_beforeAdviceDefinitions;
265   }
266
267   /**
268    * Adds a new after advice.
269    *
270    * @param adviceDef the after advice
271    */

272   public void addAfterAdviceDefinition(final AdviceDefinition adviceDef) {
273     if (!m_afterAdviceDefinitions.contains(adviceDef)) {
274       m_afterAdviceDefinitions.add(adviceDef);
275     }
276   }
277
278   /**
279    * Returns the after advices.
280    *
281    * @return the after advices
282    */

283   public List JavaDoc getAfterAdviceDefinitions() {
284     return m_afterAdviceDefinitions;
285   }
286
287   /**
288    * Adds a new pure interface introduction.
289    *
290    * @param interfaceIntroDef the introduction
291    */

292   public void addInterfaceIntroductionDefinition(final InterfaceIntroductionDefinition interfaceIntroDef) {
293     m_interfaceIntroductionDefinitions.add(interfaceIntroDef);
294   }
295
296   /**
297    * Returns the interface introductions.
298    *
299    * @return the introductions
300    */

301   public List JavaDoc getInterfaceIntroductionDefinitions() {
302     return m_interfaceIntroductionDefinitions;
303   }
304
305   /**
306    * Adds a new pointcut definition.
307    *
308    * @param pointcutDef the pointcut definition
309    */

310   public void addPointcutDefinition(final PointcutDefinition pointcutDef) {
311     m_pointcutDefinitions.add(pointcutDef);
312   }
313
314   /**
315    * Returns the pointcuts.
316    *
317    * @return the pointcuts
318    */

319   public Collection JavaDoc getPointcutDefinitions() {
320     return m_pointcutDefinitions;
321   }
322
323   /**
324    * Adds a new parameter to the advice.
325    *
326    * @param name the name of the parameter
327    * @param value the value for the parameter
328    */

329   public void addParameter(final String JavaDoc name, final Object JavaDoc value) {
330     m_parameters.put(name, value);
331   }
332
333   /**
334    * Returns the parameters as a Map.
335    *
336    * @return the parameters
337    */

338   public Map JavaDoc getParameters() {
339     return m_parameters;
340   }
341
342   /**
343    * Sets the name of the container implementation class.
344    *
345    * @param containerClassName the container class name
346    */

347   public void setContainerClassName(final String JavaDoc containerClassName) {
348     if (containerClassName != null) {
349       m_containerClassName = containerClassName.replace('/', '.');
350     } else {
351       m_containerClassName = null;
352     }
353   }
354
355   /**
356    * Returns the name of the container implementation class.
357    *
358    * @return the container class name or null if no container is set
359    */

360   public String JavaDoc getContainerClassName() {
361     return m_containerClassName;
362   }
363
364   /**
365    * Returns all the advices for this aspect.
366    *
367    * @return all the advices
368    */

369   public List JavaDoc getAdviceDefinitions() {
370     final List JavaDoc allAdvices = new ArrayList JavaDoc();
371     allAdvices.addAll(m_aroundAdviceDefinitions);
372     allAdvices.addAll(m_beforeAdviceDefinitions);
373     allAdvices.addAll(m_afterAdviceDefinitions);
374     return allAdvices;
375   }
376 }
Popular Tags