001: /**************************************************************************************
002: * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package org.codehaus.aspectwerkz.extension.hotswap;
008:
009: import org.codehaus.aspectwerkz.definition.*;
010: import org.codehaus.aspectwerkz.hook.impl.ClassPreProcessorHelper;
011: import org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor;
012: import org.codehaus.aspectwerkz.transform.ClassCacheTuple;
013: import org.codehaus.aspectwerkz.transform.inlining.deployer.Deployer;
014: import org.codehaus.aspectwerkz.transform.inlining.deployer.DeploymentHandle;
015:
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.Map;
019: import java.util.HashMap;
020: import java.util.Set;
021:
022: /**
023: * eworld/wlw/aop
024: *
025: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
026: */
027: public class EWorldUtil {
028:
029: private static final Map s_weaveStatus = new HashMap();
030:
031: public static boolean isWeaved(final String uuid,
032: final String aspectName) {
033: Map aspects = (Map) s_weaveStatus.get(uuid);
034: if (aspects == null || aspects.keySet().size() == 0) {
035: return false;
036: } else {
037: Boolean status = (Boolean) aspects.get(aspectName);
038: if (status == null) {
039: return false;
040: } else {
041: return status.booleanValue();
042: }
043: }
044: }
045:
046: public static void activate(final String uuid,
047: final String aspectName, final String adviceName,
048: final String expression, final String pointcutName) {
049: ClassLoader loader = EWorldUtil.class.getClassLoader();
050: DeploymentScope scope = SystemDefinitionContainer
051: .getDefinitionFor(loader, uuid).getDeploymentScope(
052: "demo");
053:
054: try {
055: Class aspect = Class.forName(aspectName, false, loader);
056: Deployer.deploy(aspect, "<aspect class=\"" + aspectName
057: + "\">" + "<advice name=\"" + adviceName
058: + "\" type=\"around\" bind-to=\"" + expression
059: + "\"/>" + "</aspect>", scope);
060: setStatus(uuid, aspectName, Boolean.TRUE);
061: } catch (Throwable t) {
062: t.printStackTrace();
063: }
064:
065: // System.out.println(
066: // "activate = " + uuid + "," + aspectName + "." + adviceName + " @ " + expression + "," +
067: // pointcutName
068: // );
069: // SystemDefinition sysDef = SystemDefinitionContainer.getSystemDefinition(
070: // ClassLoader.getSystemClassLoader(), uuid
071: // );
072: // if (sysDef == null) {
073: // return;
074: // }
075: // AspectDefinition aspectDef = sysDef.getAspectDefinition(aspectName);
076: //
077: // Expression pcExpression =
078: // ExpressionNamespace.getExpressionNamespace(aspectDef).createExpression(
079: // expression,
080: // "",
081: // pointcutName
082: // );
083: //
084: // AdviceDefinition newDef = null;
085: // boolean found = false;
086: // for (Iterator arounds = aspectDef.getAroundAdviceDefinitions().iterator(); arounds.hasNext();) {
087: // AdviceDefinition around = (AdviceDefinition)arounds.next();
088: // if (around.getName().equals(aspectName + "." + adviceName)) {
089: // // copy the logMethod advice
090: // // note: we could add a totally new advice as well
091: // newDef = around.copyAt(pcExpression);
092: //
093: // // take care of the runtime Pointcut mirror if any
094: // CflowStack as = SystemLoader.getCflowStack(ClassLoader.getSystemClassLoader());
095: // AspectManager am = as.getAspectManager(uuid);
096: // Pointcut pc =
097: // am.getPointcutManager(aspectDef.getName()).getPointcut(newDef.getExpression().getExpression());
098: // if (pc!=null) {
099: // pc.addAroundAdvice(aspectDef.getName() + "/" + around.getName());
100: // }
101: //
102: // System.out.println("<adding> " + around.getName() + " at " + pointcutName);
103: // found = true;
104: // break;
105: // }
106: // }
107: // if (!found) {
108: // System.err.println(" advice not found");
109: // }
110: // else {
111: // aspectDef.addAroundAdvice(newDef);
112: // StartupManager.reinitializeSystem(ClassLoader.getSystemClassLoader(), sysDef);
113: // }
114: // setStatus(uuid, aspectName, Boolean.TRUE);
115: }
116:
117: public static void deactivate(final String uuid,
118: final String aspectName, final String adviceName,
119: final String pointcutName) {
120: ClassLoader loader = EWorldUtil.class.getClassLoader();
121: DeploymentScope scope = SystemDefinitionContainer
122: .getDefinitionFor(loader, uuid).getDeploymentScope(
123: "demo");
124:
125: try {
126: Class aspect = Class.forName(aspectName, false, loader);
127: Deployer.undeploy(aspect);
128: setStatus(uuid, aspectName, Boolean.FALSE);
129: } catch (Throwable t) {
130: t.printStackTrace();
131: }
132: //
133: // System.out.println("deactivate = " + uuid + "," + aspectName + "." + adviceName + " @ " +
134: // pointcutName);
135: // SystemDefinition sysDef = SystemDefinitionContainer.getSystemDefinition(
136: // ClassLoader.getSystemClassLoader(), uuid
137: // );
138: // if (sysDef == null) {
139: // return;
140: // }
141: // AspectDefinition aspectDef = sysDef.getAspectDefinition(aspectName);
142: //
143: // List removedAdviceDefs = new ArrayList();
144: // boolean found = false;
145: // for (Iterator arounds = aspectDef.getAroundAdviceDefinitions().iterator(); arounds.hasNext();) {
146: // AdviceDefinition around = (AdviceDefinition)arounds.next();
147: // if (around.getName().equals(aspectName + "." + adviceName)) {
148: // found = true;
149: // if (pointcutName.equals(around.getExpression().getName()) ||
150: // pointcutName.equals(around.getExpression().getExpression())) {
151: //
152: // // take care of the runtime Pointcut mirror if any
153: // CflowStack as = SystemLoader.getCflowStack(ClassLoader.getSystemClassLoader());
154: // AspectManager am = as.getAspectManager(uuid);
155: // Pointcut pc =
156: // am.getPointcutManager(aspectDef.getName()).getPointcut(around.getExpression().getExpression());
157: // pc.removeAroundAdvice(aspectDef.getName() + "/" + around.getName());
158: //
159: // System.out.println("<removing> " + around.getName() + " at " + pointcutName);
160: // removedAdviceDefs.add(around);
161: // }
162: // }
163: // }
164: // if (!found) {
165: // System.err.println(" advice not found");
166: // }
167: // for (Iterator arounds = removedAdviceDefs.iterator(); arounds.hasNext();) {
168: // aspectDef.removeAroundAdvice((AdviceDefinition)arounds.next());
169: // }
170: // StartupManager.reinitializeSystem(ClassLoader.getSystemClassLoader(), sysDef);
171: //
172: // setStatus(uuid, aspectName, Boolean.FALSE);
173: }
174:
175: public static void activateCache(String expression,
176: String pointcutName) {
177: activate("eworld/wlw/aop", "examples.caching.CachingAspect",
178: "cache", expression, pointcutName);
179: }
180:
181: public static void deactivateCache(String pointcutName) {
182: deactivate("eworld/wlw/aop", "examples.caching.CachingAspect",
183: "cache", pointcutName);
184: }
185:
186: public static void activateTrace(String expression,
187: String pointcutName) {
188: activate("eworld/wlw/aop", "examples.logging.LoggingAspect",
189: "logMethod", expression, pointcutName);
190: }
191:
192: public static void deactivateTrace(String pointcutName) {
193: deactivate("eworld/wlw/aop", "examples.logging.LoggingAspect",
194: "logMethod", pointcutName);
195: }
196:
197: public static void hotswap(String classPattern) {
198: ClassLoader loader = EWorldUtil.class.getClassLoader();
199: DeploymentScope scope = SystemDefinitionContainer
200: .getDefinitionFor(loader, "eworld/wlw/aop")
201: .getDeploymentScope("demo");
202: //throw new UnsupportedOperationException("not supported in AW 2.0");
203: }
204:
205: public static void dumpSystemDefinitions(ClassLoader loader) {
206: java.io.PrintStream out = System.out;
207: out.println("dumpSystemDefinitions [ " + loader + " ]");
208: Set defs = SystemDefinitionContainer.getDefinitionsFor(loader);
209: for (Iterator sysDefs = defs.iterator(); sysDefs.hasNext();) {
210: SystemDefinition sysDef = (SystemDefinition) sysDefs.next();
211: out.print(sysDef.getUuid());
212: out.println("");
213: for (Iterator prepares = sysDef.getPreparePackages()
214: .iterator(); prepares.hasNext();) {
215: out.print("[Prepare] " + prepares.next());
216: out.println("");
217: }
218: for (Iterator aspectDefs = sysDef.getAspectDefinitions()
219: .iterator(); aspectDefs.hasNext();) {
220: AspectDefinition aspectDef = (AspectDefinition) aspectDefs
221: .next();
222: out.print("[Aspect] " + aspectDef.getName());
223: out.println("");
224: for (Iterator arounds = aspectDef
225: .getAroundAdviceDefinitions().iterator(); arounds
226: .hasNext();) {
227: AdviceDefinition around = (AdviceDefinition) arounds
228: .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 uuid,
241: final String aspectName, final Boolean status) {
242: Map aspects = (Map) s_weaveStatus.get(uuid);
243: if (aspects == null) {
244: aspects = new HashMap();
245: s_weaveStatus.put(uuid, aspects);
246: }
247: aspects.put(aspectName, status);
248: }
249: }
|