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 yapbaop.core;
008:
009: import org.codehaus.aspectwerkz.definition.SystemDefinitionContainer;
010: import org.codehaus.aspectwerkz.definition.SystemDefinition;
011: import org.codehaus.aspectwerkz.definition.AspectDefinition;
012: import org.codehaus.aspectwerkz.definition.AdviceDefinition;
013: import org.codehaus.aspectwerkz.reflect.ClassInfo;
014: import org.codehaus.aspectwerkz.reflect.ReflectHelper;
015: import org.codehaus.aspectwerkz.reflect.impl.asm.AsmClassInfo;
016: import org.codehaus.aspectwerkz.reflect.impl.java.JavaClassInfo;
017: import org.codehaus.aspectwerkz.aspect.AdviceType;
018: import org.codehaus.aspectwerkz.transform.inlining.AsmHelper;
019: import org.codehaus.aspectwerkz.transform.inlining.AspectModelManager;
020: import org.codehaus.aspectwerkz.transform.aopalliance.AopAllianceAspectModel;
021: import org.codehaus.aspectwerkz.org.objectweb.asm.Type;
022: import org.aopalliance.intercept.MethodInterceptor;
023: import org.aopalliance.intercept.MethodInvocation;
024:
025: import java.lang.reflect.Method;
026:
027: /**
028: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
029: */
030: public class Yapbaop extends AopAllianceAspectModel {//extend not required but wants to acess constants
031:
032: // register the AOP Alliance model (do not require the -D option of AW ext container)
033: static {
034: System.setProperty("aspectwerkz.extension.aspectmodels",
035: AopAllianceAspectModel.class.getName()
036: .replace('/', '.'));
037: AspectModelManager.getModels();
038: }
039:
040: // the AOP Alliance aspect for method
041: private final static Method AOP_ALLIANCE_METHOD_AROUND;
042: static {
043: try {
044: AOP_ALLIANCE_METHOD_AROUND = MethodInterceptor.class
045: .getDeclaredMethod("invoke",
046: new Class[] { MethodInvocation.class });
047: } catch (Throwable t) {
048: throw new Error(t.toString());
049: }
050: }
051:
052: public static Handle bindAspect(Class aopAllianceAspect,
053: String pointcut) {
054: ClassLoader cl = Thread.currentThread().getContextClassLoader();
055: ClassInfo aspectInfo = JavaClassInfo
056: .getClassInfo(aopAllianceAspect);
057:
058: // check some
059: if (aspectInfo.getMethod(ReflectHelper
060: .calculateHash(AOP_ALLIANCE_METHOD_AROUND)) == null) {
061: throw new RuntimeException(
062: "Aspect is not AOP Alliance compatible - "
063: + aopAllianceAspect.getName());
064: }
065:
066: SystemDefinition def = SystemDefinitionContainer
067: .getVirtualDefinitionAt(cl);
068: AspectDefinition aspectDef = new AspectDefinition(aspectInfo
069: .getName(), aspectInfo, def);
070: AdviceDefinition adviceDef = AdviceDefinition.newInstance(
071: "invoke",// as per AOPAlliance
072: AdviceType.AROUND, "execution(" + pointcut + ")", null,
073: aspectInfo.getName(), aspectInfo.getName(),
074: aspectInfo.getMethod(ReflectHelper
075: .calculateHash(AOP_ALLIANCE_METHOD_AROUND)),
076: aspectDef);
077:
078: // make it an AOP Alliance aspect for the compiler
079: aspectDef.setAspectModel(ASPECT_MODEL_TYPE);
080: aspectDef.setContainerClassName(ASPECT_CONTAINER_CLASS_NAME);
081:
082: // add the advice
083: aspectDef.addAroundAdviceDefinition(adviceDef);
084:
085: // add the aspect
086: def.addAspect(aspectDef);
087:
088: return new Handle(adviceDef);
089: }
090:
091: public static class Handle {
092: private AdviceDefinition m_definition;
093:
094: private Handle(AdviceDefinition def) {
095: m_definition = def;
096: }
097:
098: public void unbind() {
099: m_definition.setExpressionInfo(null);
100: }
101: }
102: }
|