001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.definition;
005:
006: import java.util.ArrayList;
007: import java.util.Collection;
008: import java.util.HashMap;
009: import java.util.List;
010: import java.util.Map;
011:
012: import com.tc.aspectwerkz.DeploymentModel;
013: import com.tc.aspectwerkz.reflect.ClassInfo;
014: import com.tc.aspectwerkz.reflect.ConstructorInfo;
015: import com.tc.aspectwerkz.aspect.DefaultAspectContainerStrategy;
016: import com.tc.aspectwerkz.transform.inlining.model.AspectWerkzAspectModel;
017:
018: /**
019: * Holds the meta-data for the aspect.
020: *
021: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
022: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
023: */
024: public class AspectDefinition {
025:
026: private final static String DEFAULT_ASPECTCONTAINER_CLASSNAME = DefaultAspectContainerStrategy.class
027: .getName();
028:
029: /**
030: * The name of the aspect (nickname).
031: */
032: private String m_name;
033:
034: /**
035: * The nickname of the aspect prefixed by the <system uuid>/
036: */
037: private String m_qualifiedName;
038:
039: /**
040: * The aspect class info.
041: */
042: private final ClassInfo m_classInfo;
043:
044: /**
045: * The deployment model for the aspect.
046: */
047: private DeploymentModel m_deploymentModel = DeploymentModel.PER_JVM;
048:
049: /**
050: * The around advices.
051: */
052: private final List m_aroundAdviceDefinitions = new ArrayList();
053:
054: /**
055: * The before advices.
056: */
057: private final List m_beforeAdviceDefinitions = new ArrayList();
058:
059: /**
060: * The after advices.
061: */
062: private final List m_afterAdviceDefinitions = new ArrayList();
063:
064: /**
065: * The interface introductions (pure interfaces)
066: */
067: private final List m_interfaceIntroductionDefinitions = new ArrayList();
068:
069: /**
070: * The pointcuts.
071: */
072: private final List m_pointcutDefinitions = new ArrayList();
073:
074: /**
075: * The parameters passed to the aspect at definition time.
076: */
077: private Map m_parameters = new HashMap();
078:
079: /**
080: * The container implementation class name or null if no container (inlined instantiation in the factory)
081: */
082: private String m_containerClassName;
083:
084: /**
085: * The system definition.
086: */
087: private SystemDefinition m_systemDefinition;
088:
089: /**
090: * The aspect model. Defaults to AspectWerkz
091: */
092: private String m_aspectModelType = AspectWerkzAspectModel.TYPE;
093:
094: /**
095: * Creates a new aspect meta-data instance.
096: *
097: * @param name the name of the aspect
098: * @param classInfo the class info for the aspect
099: * @param systemDefinition
100: */
101: public AspectDefinition(final String name,
102: final ClassInfo classInfo,
103: final SystemDefinition systemDefinition) {
104: if (name == null) {
105: throw new IllegalArgumentException(
106: "aspect name can not be null");
107: }
108: if (classInfo == null) {
109: throw new IllegalArgumentException(
110: "aspect class info can not be null");
111: }
112: m_name = name;
113: m_classInfo = classInfo;
114: m_systemDefinition = systemDefinition;
115: m_qualifiedName = systemDefinition.getUuid() + '/' + name;
116:
117: // if no-arg ctor not found, set the default aspect container just in case it is an old 2.0 aspect
118: // and the user forgot to add the container="...DefaultAspectContainerStrategy"
119: // while still using a ctor(AspectContext) style in the aspect
120: boolean hasNoArg = false;
121: for (int i = 0; i < m_classInfo.getConstructors().length; i++) {
122: ConstructorInfo constructorInfo = m_classInfo
123: .getConstructors()[i];
124: if ("()V".equals(constructorInfo.getSignature())) {
125: hasNoArg = true;
126: break;
127: }
128: }
129: if (!hasNoArg) {
130: setContainerClassName(DEFAULT_ASPECTCONTAINER_CLASSNAME);
131: }
132: }
133:
134: /**
135: * Returns the name for the advice
136: *
137: * @return the name
138: */
139: public String getName() {
140: return m_name;
141: }
142:
143: /**
144: * Sets the name for the aspect.
145: *
146: * @param name the name
147: */
148: public void setName(final String name) {
149: m_name = name.trim();
150: }
151:
152: /**
153: * Returns the fully qualified name for the advice
154: *
155: * @return the fully qualified name
156: */
157: public String getQualifiedName() {
158: return m_qualifiedName;
159: }
160:
161: /**
162: * Returns the system definition.
163: *
164: * @return
165: */
166: public SystemDefinition getSystemDefinition() {
167: return m_systemDefinition;
168: }
169:
170: /**
171: * Returns the class name.
172: *
173: * @return the class name
174: */
175: public String getClassName() {
176: return m_classInfo.getName();
177: }
178:
179: /**
180: * Returns the class info.
181: *
182: * @return the class info
183: */
184: public ClassInfo getClassInfo() {
185: return m_classInfo;
186: }
187:
188: /**
189: * Returns the aspect model.
190: *
191: * @return the aspect model
192: */
193: public String getAspectModel() {
194: return m_aspectModelType;
195: }
196:
197: /**
198: * Checks if the aspect defined is an AspectWerkz aspect.
199: *
200: * @return
201: */
202: public boolean isAspectWerkzAspect() {
203: return m_aspectModelType.equals(AspectWerkzAspectModel.TYPE);
204: }
205:
206: /**
207: * Sets the aspect model.
208: *
209: * @param aspectModelType the aspect model
210: */
211: public void setAspectModel(final String aspectModelType) {
212: m_aspectModelType = aspectModelType;
213: }
214:
215: /**
216: * Sets the deployment model.
217: *
218: * @param deploymentModel the deployment model
219: */
220: public void setDeploymentModel(final DeploymentModel deploymentModel) {
221: m_deploymentModel = deploymentModel;
222: }
223:
224: /**
225: * Returns the deployment model.
226: *
227: * @return the deployment model
228: */
229: public DeploymentModel getDeploymentModel() {
230: return m_deploymentModel;
231: }
232:
233: /**
234: * Adds a new around advice.
235: *
236: * @param adviceDef the around advice
237: */
238: public void addAroundAdviceDefinition(
239: final AdviceDefinition adviceDef) {
240: if (!m_aroundAdviceDefinitions.contains(adviceDef)) {
241: m_aroundAdviceDefinitions.add(adviceDef);
242: }
243: }
244:
245: /**
246: * Returns the around advices.
247: *
248: * @return the around advices
249: */
250: public List getAroundAdviceDefinitions() {
251: return m_aroundAdviceDefinitions;
252: }
253:
254: /**
255: * Adds a new before advice.
256: *
257: * @param adviceDef the before advice
258: */
259: public void addBeforeAdviceDefinition(
260: final AdviceDefinition adviceDef) {
261: if (!m_beforeAdviceDefinitions.contains(adviceDef)) {
262: m_beforeAdviceDefinitions.add(adviceDef);
263: }
264: }
265:
266: /**
267: * Returns the before advices.
268: *
269: * @return the before advices
270: */
271: public List getBeforeAdviceDefinitions() {
272: return m_beforeAdviceDefinitions;
273: }
274:
275: /**
276: * Adds a new after advice.
277: *
278: * @param adviceDef the after advice
279: */
280: public void addAfterAdviceDefinition(
281: final AdviceDefinition adviceDef) {
282: if (!m_afterAdviceDefinitions.contains(adviceDef)) {
283: m_afterAdviceDefinitions.add(adviceDef);
284: }
285: }
286:
287: /**
288: * Returns the after advices.
289: *
290: * @return the after advices
291: */
292: public List getAfterAdviceDefinitions() {
293: return m_afterAdviceDefinitions;
294: }
295:
296: /**
297: * Adds a new pure interface introduction.
298: *
299: * @param interfaceIntroDef the introduction
300: */
301: public void addInterfaceIntroductionDefinition(
302: final InterfaceIntroductionDefinition interfaceIntroDef) {
303: m_interfaceIntroductionDefinitions.add(interfaceIntroDef);
304: }
305:
306: /**
307: * Returns the interface introductions.
308: *
309: * @return the introductions
310: */
311: public List getInterfaceIntroductionDefinitions() {
312: return m_interfaceIntroductionDefinitions;
313: }
314:
315: /**
316: * Adds a new pointcut definition.
317: *
318: * @param pointcutDef the pointcut definition
319: */
320: public void addPointcutDefinition(
321: final PointcutDefinition pointcutDef) {
322: m_pointcutDefinitions.add(pointcutDef);
323: }
324:
325: /**
326: * Returns the pointcuts.
327: *
328: * @return the pointcuts
329: */
330: public Collection getPointcutDefinitions() {
331: return m_pointcutDefinitions;
332: }
333:
334: /**
335: * Adds a new parameter to the advice.
336: *
337: * @param name the name of the parameter
338: * @param value the value for the parameter
339: */
340: public void addParameter(final String name, final Object value) {
341: m_parameters.put(name, value);
342: }
343:
344: /**
345: * Returns the parameters as a Map.
346: *
347: * @return the parameters
348: */
349: public Map getParameters() {
350: return m_parameters;
351: }
352:
353: /**
354: * Sets the name of the container implementation class.
355: *
356: * @param containerClassName the container class name
357: */
358: public void setContainerClassName(final String containerClassName) {
359: if (containerClassName != null) {
360: m_containerClassName = containerClassName.replace('/', '.');
361: } else {
362: m_containerClassName = null;
363: }
364: }
365:
366: /**
367: * Returns the name of the container implementation class.
368: *
369: * @return the container class name or null if no container is set
370: */
371: public String getContainerClassName() {
372: return m_containerClassName;
373: }
374:
375: /**
376: * Returns all the advices for this aspect.
377: *
378: * @return all the advices
379: */
380: public List getAdviceDefinitions() {
381: final List allAdvices = new ArrayList();
382: allAdvices.addAll(m_aroundAdviceDefinitions);
383: allAdvices.addAll(m_beforeAdviceDefinitions);
384: allAdvices.addAll(m_afterAdviceDefinitions);
385: return allAdvices;
386: }
387: }
|