KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > definition > deployer > StandardAspectModuleDeployer


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
5 package com.tc.aspectwerkz.definition.deployer;
6
7 import com.tc.aspectwerkz.DeploymentModel;
8 import com.tc.aspectwerkz.definition.SystemDefinition;
9 import com.tc.aspectwerkz.definition.SystemDefinitionContainer;
10 import com.tc.aspectwerkz.transform.Properties;
11 import com.tc.aspectwerkz.util.Strings;
12 import com.tc.logging.CustomerLogging;
13 import com.tc.logging.TCLogger;
14 // import com.tc.object.loaders.NamedClassLoader;
15

16 import java.util.ArrayList JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 /**
25  * TODO document class
26  *
27  * @author Jonas Bonér
28  */

29 public class StandardAspectModuleDeployer implements AspectModuleDeployer {
30
31   public static final String JavaDoc[] ASPECT_MODULES;
32   static {
33     ASPECT_MODULES = Strings.splitString(Properties.ASPECT_MODULES, ",");
34   }
35
36   private final static Map JavaDoc modules = new HashMap JavaDoc();
37   private final static TCLogger logger = CustomerLogging.getDSOInstrumentationLogger();
38
39   private final List JavaDoc m_builders = new ArrayList JavaDoc();
40   private final SystemDefinition m_systemDef;
41   private final ClassLoader JavaDoc m_loader;
42
43
44   /**
45    * Loads all registered aspect modules, builds and deploy them in the class loader specified.
46    *
47    * @param loader
48    * @param moduleNames
49    */

50   public static void deploy(final ClassLoader JavaDoc loader, String JavaDoc[] moduleNames) {
51     for (int i = 0; i < moduleNames.length; i++) {
52       deploy(loader, moduleNames[i]);
53     }
54   }
55
56   public static void deploy(final ClassLoader JavaDoc loader, String JavaDoc moduleName) {
57     synchronized(modules) {
58       // TODO loaders collection should use weak refs to class loaders
59
Set JavaDoc loaders = (Set JavaDoc) modules.get(moduleName);
60       if(loaders==null) {
61         loaders = new HashSet JavaDoc();
62         modules.put(moduleName, loaders);
63       }
64
65       if(!loaders.contains(loader)) {
66         logger.info("Loading aspect module [" + moduleName + "] in loader " + getLoaderName(loader));
67         new StandardAspectModuleDeployer(loader).doDeploy(moduleName);
68         loaders.add(loader);
69       }
70     }
71   }
72
73   private static String JavaDoc getLoaderName(final ClassLoader JavaDoc loader) {
74     // XXX: Dependency on DSO L1 code from AW. This isn't kosher at the moment
75
// if(loader instanceof NamedClassLoader) {
76
// return ((NamedClassLoader) loader).__tc_getClassLoaderName();
77
// }
78
return loader.getClass().getName()+"@"+System.identityHashCode(loader);
79   }
80
81
82   /**
83    * Creates, registers and returns an aspect definition builder.
84    * Use-case: Get an aspect builder and then use it to add advice and pointcut builders to build up a full aspect
85    * definintion programatically.
86    *
87    * @param aspectClass
88    * @param scope
89    * @param containerClassName
90    * @return a newly registered aspect builder
91    */

92   public synchronized AspectDefinitionBuilder newAspectBuilder(final String JavaDoc aspectClass,
93                                                                final DeploymentModel scope,
94                                                                final String JavaDoc containerClassName) {
95     AspectDefinitionBuilder aspectDefinitionBuilder = new AspectDefinitionBuilder(
96             aspectClass, scope, containerClassName, m_systemDef, m_loader
97     );
98     m_builders.add(aspectDefinitionBuilder);
99     return aspectDefinitionBuilder;
100   }
101
102   /**
103    * Creates and adds a new mixin builder to the deployment set.
104    *
105    * @param aspectClass
106    * @param deploymentModel
107    * @param pointcut
108    */

109   public synchronized void addMixin(final String JavaDoc aspectClass,
110                                     final DeploymentModel deploymentModel,
111                                     final String JavaDoc pointcut,
112                                     final boolean isTransient) {
113     m_builders.add(new MixinDefinitionBuilder(aspectClass, deploymentModel, pointcut, isTransient, m_systemDef, m_loader));
114   }
115
116   public ClassLoader JavaDoc getClassLoader() {
117     return m_loader;
118   }
119
120   /**
121    * Creates a new aspect module.
122    *
123    * @param loader
124    */

125   private StandardAspectModuleDeployer(final ClassLoader JavaDoc loader) {
126     m_systemDef = SystemDefinitionContainer.getVirtualDefinitionFor(loader);
127     m_loader = loader;
128   }
129
130   private synchronized void doDeploy(String JavaDoc moduleName) {
131     loadModule(moduleName);
132     buildModule();
133     SystemDefinitionContainer.printDeploymentInfoFor(m_loader);
134   }
135
136   private void loadModule(String JavaDoc moduleName) {
137     try {
138       Class JavaDoc aspectModuleClass = getClass().getClassLoader().loadClass(moduleName);
139       AspectModule aspectModule = (AspectModule) aspectModuleClass.newInstance();
140       aspectModule.deploy(this);
141     } catch (Throwable JavaDoc e) {
142       logger.error("Aspect module [" + moduleName + "] could not be deployed in "+getLoaderName(m_loader)+"; "+e.toString(), e);
143     }
144   }
145
146   private void buildModule() {
147     for (Iterator JavaDoc it = m_builders.iterator(); it.hasNext();) {
148       ((DefinitionBuilder) it.next()).build();
149     }
150   }
151
152 }
153
154
Popular Tags