KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > extension > hotswap > EWorldUtil


1 /**************************************************************************************
2  * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package org.codehaus.aspectwerkz.extension.hotswap;
9
10 import org.codehaus.aspectwerkz.definition.*;
11 import org.codehaus.aspectwerkz.hook.impl.ClassPreProcessorHelper;
12 import org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor;
13 import org.codehaus.aspectwerkz.transform.ClassCacheTuple;
14 import org.codehaus.aspectwerkz.transform.inlining.deployer.Deployer;
15 import org.codehaus.aspectwerkz.transform.inlining.deployer.DeploymentHandle;
16
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Set JavaDoc;
22
23 /**
24  * eworld/wlw/aop
25  *
26  * @author <a HREF="mailto:alex@gnilux.com">Alexandre Vasseur </a>
27  */

28 public class EWorldUtil {
29
30     private static final Map JavaDoc s_weaveStatus = new HashMap JavaDoc();
31
32     public static boolean isWeaved(final String JavaDoc uuid, final String JavaDoc aspectName) {
33         Map JavaDoc aspects = (Map JavaDoc) s_weaveStatus.get(uuid);
34         if (aspects == null || aspects.keySet().size() == 0) {
35             return false;
36         } else {
37             Boolean JavaDoc status = (Boolean JavaDoc) aspects.get(aspectName);
38             if (status == null) {
39                 return false;
40             } else {
41                 return status.booleanValue();
42             }
43         }
44     }
45
46     public static void activate(final String JavaDoc uuid,
47                                 final String JavaDoc aspectName,
48                                 final String JavaDoc adviceName,
49                                 final String JavaDoc expression,
50                                 final String JavaDoc pointcutName) {
51         ClassLoader JavaDoc loader = EWorldUtil.class.getClassLoader();
52         DeploymentScope scope = SystemDefinitionContainer.getDefinitionFor(loader, uuid).getDeploymentScope("demo");
53
54         try {
55             Class JavaDoc aspect = Class.forName(aspectName, false, loader);
56             Deployer.deploy(aspect,
57                             "<aspect class=\""+aspectName+"\">"
58                             + "<advice name=\""+adviceName+"\" type=\"around\" bind-to=\""+expression+"\"/>"
59                             + "</aspect>",
60                             scope);
61             setStatus(uuid, aspectName, Boolean.TRUE);
62         } catch (Throwable JavaDoc t) {
63             t.printStackTrace();
64         }
65
66
67         // System.out.println(
68
// "activate = " + uuid + "," + aspectName + "." + adviceName + " @ " + expression + "," +
69
// pointcutName
70
// );
71
// SystemDefinition sysDef = SystemDefinitionContainer.getSystemDefinition(
72
// ClassLoader.getSystemClassLoader(), uuid
73
// );
74
// if (sysDef == null) {
75
// return;
76
// }
77
// AspectDefinition aspectDef = sysDef.getAspectDefinition(aspectName);
78
//
79
// Expression pcExpression =
80
// ExpressionNamespace.getExpressionNamespace(aspectDef).createExpression(
81
// expression,
82
// "",
83
// pointcutName
84
// );
85
//
86
// AdviceDefinition newDef = null;
87
// boolean found = false;
88
// for (Iterator arounds = aspectDef.getAroundAdviceDefinitions().iterator(); arounds.hasNext();) {
89
// AdviceDefinition around = (AdviceDefinition)arounds.next();
90
// if (around.getName().equals(aspectName + "." + adviceName)) {
91
// // copy the logMethod advice
92
// // note: we could add a totally new advice as well
93
// newDef = around.copyAt(pcExpression);
94
//
95
// // take care of the runtime Pointcut mirror if any
96
// CflowStack as = SystemLoader.getCflowStack(ClassLoader.getSystemClassLoader());
97
// AspectManager am = as.getAspectManager(uuid);
98
// Pointcut pc =
99
// am.getPointcutManager(aspectDef.getName()).getPointcut(newDef.getExpression().getExpression());
100
// if (pc!=null) {
101
// pc.addAroundAdvice(aspectDef.getName() + "/" + around.getName());
102
// }
103
//
104
// System.out.println("<adding> " + around.getName() + " at " + pointcutName);
105
// found = true;
106
// break;
107
// }
108
// }
109
// if (!found) {
110
// System.err.println(" advice not found");
111
// }
112
// else {
113
// aspectDef.addAroundAdvice(newDef);
114
// StartupManager.reinitializeSystem(ClassLoader.getSystemClassLoader(), sysDef);
115
// }
116
// setStatus(uuid, aspectName, Boolean.TRUE);
117
}
118
119     public static void deactivate(final String JavaDoc uuid,
120                                   final String JavaDoc aspectName,
121                                   final String JavaDoc adviceName,
122                                   final String JavaDoc pointcutName) {
123         ClassLoader JavaDoc loader = EWorldUtil.class.getClassLoader();
124         DeploymentScope scope = SystemDefinitionContainer.getDefinitionFor(loader, uuid).getDeploymentScope("demo");
125
126         try {
127             Class JavaDoc aspect = Class.forName(aspectName, false, loader);
128             Deployer.undeploy(aspect);
129             setStatus(uuid, aspectName, Boolean.FALSE);
130         } catch (Throwable JavaDoc t) {
131             t.printStackTrace();
132         }
133         //
134
// System.out.println("deactivate = " + uuid + "," + aspectName + "." + adviceName + " @ " +
135
// pointcutName);
136
// SystemDefinition sysDef = SystemDefinitionContainer.getSystemDefinition(
137
// ClassLoader.getSystemClassLoader(), uuid
138
// );
139
// if (sysDef == null) {
140
// return;
141
// }
142
// AspectDefinition aspectDef = sysDef.getAspectDefinition(aspectName);
143
//
144
// List removedAdviceDefs = new ArrayList();
145
// boolean found = false;
146
// for (Iterator arounds = aspectDef.getAroundAdviceDefinitions().iterator(); arounds.hasNext();) {
147
// AdviceDefinition around = (AdviceDefinition)arounds.next();
148
// if (around.getName().equals(aspectName + "." + adviceName)) {
149
// found = true;
150
// if (pointcutName.equals(around.getExpression().getName()) ||
151
// pointcutName.equals(around.getExpression().getExpression())) {
152
//
153
// // take care of the runtime Pointcut mirror if any
154
// CflowStack as = SystemLoader.getCflowStack(ClassLoader.getSystemClassLoader());
155
// AspectManager am = as.getAspectManager(uuid);
156
// Pointcut pc =
157
// am.getPointcutManager(aspectDef.getName()).getPointcut(around.getExpression().getExpression());
158
// pc.removeAroundAdvice(aspectDef.getName() + "/" + around.getName());
159
//
160
// System.out.println("<removing> " + around.getName() + " at " + pointcutName);
161
// removedAdviceDefs.add(around);
162
// }
163
// }
164
// }
165
// if (!found) {
166
// System.err.println(" advice not found");
167
// }
168
// for (Iterator arounds = removedAdviceDefs.iterator(); arounds.hasNext();) {
169
// aspectDef.removeAroundAdvice((AdviceDefinition)arounds.next());
170
// }
171
// StartupManager.reinitializeSystem(ClassLoader.getSystemClassLoader(), sysDef);
172
//
173
// setStatus(uuid, aspectName, Boolean.FALSE);
174
}
175
176     public static void activateCache(String JavaDoc expression, String JavaDoc pointcutName) {
177         activate(
178                 "eworld/wlw/aop",
179                 "examples.caching.CachingAspect",
180                 "cache",
181                 expression,
182                 pointcutName
183         );
184     }
185
186     public static void deactivateCache(String JavaDoc pointcutName) {
187         deactivate("eworld/wlw/aop", "examples.caching.CachingAspect", "cache", pointcutName);
188     }
189
190     public static void activateTrace(String JavaDoc expression, String JavaDoc pointcutName) {
191         activate(
192                 "eworld/wlw/aop",
193                 "examples.logging.LoggingAspect",
194                 "logMethod",
195                 expression,
196                 pointcutName
197         );
198     }
199
200     public static void deactivateTrace(String JavaDoc pointcutName) {
201         deactivate("eworld/wlw/aop", "examples.logging.LoggingAspect", "logMethod", pointcutName);
202     }
203
204     public static void hotswap(String JavaDoc classPattern) {
205         ClassLoader JavaDoc loader = EWorldUtil.class.getClassLoader();
206         DeploymentScope scope = SystemDefinitionContainer.getDefinitionFor(loader, "eworld/wlw/aop").getDeploymentScope("demo");
207         //throw new UnsupportedOperationException("not supported in AW 2.0");
208
}
209
210     public static void dumpSystemDefinitions(ClassLoader JavaDoc loader) {
211         java.io.PrintStream JavaDoc out = System.out;
212         out.println("dumpSystemDefinitions [ " + loader + " ]");
213         Set JavaDoc defs = SystemDefinitionContainer.getDefinitionsFor(loader);
214         for (Iterator JavaDoc sysDefs = defs.iterator(); sysDefs.hasNext();) {
215             SystemDefinition sysDef = (SystemDefinition) sysDefs.next();
216             out.print(sysDef.getUuid());
217             out.println("");
218             for (Iterator JavaDoc prepares = sysDef.getPreparePackages().iterator(); prepares.hasNext();) {
219                 out.print("[Prepare] " + prepares.next());
220                 out.println("");
221             }
222             for (Iterator JavaDoc aspectDefs = sysDef.getAspectDefinitions().iterator(); aspectDefs
223                     .hasNext();) {
224                 AspectDefinition aspectDef = (AspectDefinition) aspectDefs.next();
225                 out.print("[Aspect] " + aspectDef.getName());
226                 out.println("");
227                 for (Iterator JavaDoc arounds = aspectDef.getAroundAdviceDefinitions().iterator(); arounds.hasNext();) {
228                     AdviceDefinition around = (AdviceDefinition) arounds.next();
229                     out.print(" [AroundAdvice] " + around.getName());
230                     out.print(" ");
231                     out.print(around.getExpressionInfo().toString());
232                     out.println("");
233                 }
234                 out.println("\n-");
235             }
236             out.println("\n----");
237         }
238     }
239
240     private static void setStatus(final String JavaDoc uuid, final String JavaDoc aspectName, final Boolean JavaDoc status) {
241         Map JavaDoc aspects = (Map JavaDoc) s_weaveStatus.get(uuid);
242         if (aspects == null) {
243             aspects = new HashMap JavaDoc();
244             s_weaveStatus.put(uuid, aspects);
245         }
246         aspects.put(aspectName, status);
247     }
248 }
Popular Tags