KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > aspect > container > AspectFactoryManager


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.container;
5
6
7 import com.tc.aspectwerkz.DeploymentModel;
8 import com.tc.aspectwerkz.transform.inlining.AsmHelper;
9 import com.tc.aspectwerkz.util.ContextClassLoader;
10
11 /**
12  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
13  */

14 public class AspectFactoryManager {
15
16   public static String JavaDoc getAspectFactoryClassName(String JavaDoc aspectClassName, String JavaDoc aspectQualifiedName) {
17     return aspectClassName.replace('.', '/') + "$" + aspectQualifiedName.replace('.', '/').replace('/', '_') + "_AWFactory";
18   }
19
20   // TODO refactor for offline stuff -genjp
21

22   /**
23    * Ensure that the aspect factory is loaded.
24    *
25    * @param aspectFactoryClassName
26    * @param aspectClassName
27    * @param aspectQualifiedName
28    * @param containerClassName
29    * @param rawParameters
30    * @param loader
31    * @param deploymentModelAsString
32    */

33   public static void loadAspectFactory(
34           String JavaDoc aspectFactoryClassName,
35           String JavaDoc uuid,
36           String JavaDoc aspectClassName,
37           String JavaDoc aspectQualifiedName,
38           String JavaDoc containerClassName,
39           String JavaDoc rawParameters,
40           ClassLoader JavaDoc loader,
41           String JavaDoc deploymentModelAsString) {
42
43     try {
44       ContextClassLoader.forName(loader, aspectFactoryClassName.replace('/', '.'));
45     } catch (ClassNotFoundException JavaDoc e) {
46       // compile it
47
DeploymentModel deploymentModel = DeploymentModel.getDeploymentModelFor(deploymentModelAsString);
48       AbstractAspectFactoryCompiler compiler;
49
50       if (DeploymentModel.PER_JVM.equals(deploymentModel)) {
51         compiler = new PerJVMAspectFactoryCompiler(
52                 uuid,
53                 aspectClassName,
54                 aspectQualifiedName,
55                 containerClassName,
56                 rawParameters,
57                 loader
58         );
59       } else if (DeploymentModel.PER_CLASS.equals(deploymentModel)) {
60         compiler = new LazyPerXFactoryCompiler.PerClassAspectFactoryCompiler(
61                 uuid,
62                 aspectClassName,
63                 aspectQualifiedName,
64                 containerClassName,
65                 rawParameters,
66                 loader
67         );
68       } else if (DeploymentModel.PER_INSTANCE.equals(deploymentModel)) {
69         compiler = new PerObjectFactoryCompiler.PerInstanceFactoryCompiler(
70                 uuid,
71                 aspectClassName,
72                 aspectQualifiedName,
73                 containerClassName,
74                 rawParameters,
75                 loader
76         );
77       } else if (DeploymentModel.PER_TARGET.equals(deploymentModel)
78               || DeploymentModel.PER_THIS.equals(deploymentModel)) {
79         compiler = new PerObjectFactoryCompiler(
80                 uuid,
81                 aspectClassName,
82                 aspectQualifiedName,
83                 containerClassName,
84                 rawParameters,
85                 loader
86         );
87       } else if (DeploymentModel.PER_CFLOW.equals(deploymentModel)
88               || DeploymentModel.PER_CFLOWBELOW.equals(deploymentModel)) {
89         compiler = new PerCflowXAspectFactoryCompiler(
90                 uuid,
91                 aspectClassName,
92                 aspectQualifiedName,
93                 containerClassName,
94                 rawParameters,
95                 loader
96         );
97       } else {
98         //FIXME perThread
99
throw new Error JavaDoc("aspect factory not implemented for deployment model: " + deploymentModel);
100       }
101       Artifact artifact = compiler.compile();
102       Class JavaDoc factory = AsmHelper.defineClass(loader, artifact.bytecode, artifact.className);
103
104 // System.out.println("factory.getClassLoader() = " + factory.getClassLoader());
105
// try {
106
// Method m = factory.getMethod("aspectOf", new Class[]{});
107
// Object aspect = m.invoke(null, new Object[]{});
108
// System.out.println("aspect = " + aspect);
109
// } catch (NoSuchMethodException e1) {
110
// e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
111
// } catch (IllegalAccessException e1) {
112
// e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
113
// } catch (InvocationTargetException e1) {
114
// e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
115
// }
116
}
117   }
118 }
119
Popular Tags