KickJava   Java API By Example, From Geeks To Geeks.

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


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.exception.WrappedRuntimeException;
9
10 import java.lang.reflect.Constructor JavaDoc;
11 import java.lang.reflect.InvocationTargetException JavaDoc;
12 import java.util.Map JavaDoc;
13
14 /**
15  * Implements a sample aspect container strategy.
16  * </p>
17  * Use container="com.tc.aspectwerkz.aspect.DefaultAspectContainerStrategy" in the aop.xml
18  * The aspect must then have a no-arg constructor or a single arg constructor with param "AspectContext".
19  *
20  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
21  */

22 public class DefaultAspectContainerStrategy extends AbstractAspectContainer {
23
24   //FIXME - needs to have data structures for the aspects to handle cycle - current impl is old and BOGUS
25

26   /**
27    * The constructor for the aspect.
28    */

29   protected Constructor JavaDoc m_aspectConstructor = null;
30
31   /**
32    * Creates a new aspect container strategy.
33    */

34   public DefaultAspectContainerStrategy(Class JavaDoc aspectClass, ClassLoader JavaDoc aopSystemClassLoader, String JavaDoc uuid, String JavaDoc qualifiedName, Map JavaDoc parameters) {
35     super(aspectClass, aopSystemClassLoader, uuid, qualifiedName, parameters);
36   }
37
38   /**
39    * Creates a new aspect instance.
40    *
41    * @return the new aspect instance
42    */

43   protected Object JavaDoc createAspect(AspectContext aspectContext) {
44     if (m_aspectConstructor == null) {
45       m_aspectConstructor = findConstructor();
46     }
47     try {
48       switch (m_constructionType) {
49         case ASPECT_CONSTRUCTION_TYPE_DEFAULT:
50           return m_aspectConstructor.newInstance(EMPTY_OBJECT_ARRAY);
51         case ASPECT_CONSTRUCTION_TYPE_ASPECT_CONTEXT:
52           return m_aspectConstructor.newInstance(new Object JavaDoc[]{aspectContext});
53         default:
54           throw new Error JavaDoc("should not happen");
55       }
56     } catch (InvocationTargetException JavaDoc e) {
57       e.printStackTrace();
58       throw new WrappedRuntimeException(e.getTargetException());
59     } catch (Exception JavaDoc e) {
60       throw new WrappedRuntimeException(e);
61     }
62   }
63
64   /**
65    * Grabs the correct constructor for the aspect.
66    *
67    * @return the constructor for the aspect
68    */

69   protected Constructor JavaDoc findConstructor() {
70     Constructor JavaDoc aspectConstructor = null;
71     Class JavaDoc aspectClass = getAspectClass();
72     Constructor JavaDoc[] constructors = aspectClass.getDeclaredConstructors();
73     for (int i = 0; i < constructors.length; i++) {
74       Constructor JavaDoc constructor = constructors[i];
75       Class JavaDoc[] parameterTypes = constructor.getParameterTypes();
76       if (parameterTypes.length == 0) {
77         m_constructionType = ASPECT_CONSTRUCTION_TYPE_DEFAULT;
78         aspectConstructor = constructor;
79       } else if ((parameterTypes.length == 1) && parameterTypes[0].equals(AspectContext.class)) {
80         m_constructionType = ASPECT_CONSTRUCTION_TYPE_ASPECT_CONTEXT;
81         aspectConstructor = constructor;
82         break;
83       }
84     }
85     if (m_constructionType == ASPECT_CONSTRUCTION_TYPE_UNKNOWN) {
86       throw new RuntimeException JavaDoc(
87               "aspect ["
88                       + aspectClass.getName()
89                       +
90                       "] does not have a valid constructor (either default no-arg or one that takes a AspectContext type as its only parameter)"
91                       + " to be used with container=\"com.tc.aspectwerkz.aspect.DefaultAspectContainerStrategy\""
92       );
93     }
94     return aspectConstructor;
95   }
96 }
Popular Tags