简体   繁体   English

OSGi WeavingHook示例

[英]OSGi WeavingHook Examples

Does anybody have any examples of using the OSGi 4.3+ Weaving Hook Service? 有没有人有使用OSGi 4.3+ Weaving Hook服务的例子? What about with AspectJ, ASM, JavaAssist? 使用AspectJ,ASM,JavaAssist怎么样? Is anybody actually using OSGi WeavingHooks? 有人在使用OSGi WeavingHooks吗?

The example in OSGi Core 5.0.0 section 56.2 simply leaves out the actual weaving and says "the final weaving is left as an exercise to the reader". OSGi Core 5.0.0第56.2节中的示例简单地省略了实际的编织,并说“最后的编织留给了读者”。

My goal is to: 我的目标是:

  1. create an annotation (@MyAnnotation) that I can place on fields (primitives or objects). 创建一个可以放在字段(基元或对象)上的注释(@MyAnnotation)。
  2. create an org.osgi.framework.hooks.weaving.WeavingHook to weave classes with that annotation 创建一个org.osgi.framework.hooks.weaving.WeavingHook来编织带有该注释的类
  3. use load-time weaving to pointcut at any modification of fields with that annotation 使用加载时编织在具有该注释的任何字段修改处切入
  4. fire EventAdmin events that the field was modified. 触发已修改字段的EventAdmin事件。
  5. dynamically update the bundle wiring from the WeavingHook to wire to the EventAdmin bundle. 从WeavingHook动态更新捆绑线路,以连接到EventAdmin捆绑包。

My problem is mostly with #3. 我的问题主要是#3。

I'm currently trying to use the AspectJ WeavingAdaptor to do the weaving, but I'm having problems getting my aspect library in to it, since its expecting the java.net.URL[] aspectURLs in the constructor to be either jars or directories it can find on the filesystem, and not bundles. 我目前正在尝试使用AspectJ WeavingAdaptor进行编织,但是我遇到了问题,因为我希望构造函数中的java.net.URL [] aspectURL可以是jar或者目录它可以在文件系统上找到,而不是捆绑。 Also, i'm not sure how to handle any new classes generated by the weaver through callbacks to the acceptClass(String name, bytes[]) method of GeneratedClassHandler . 另外,我不知道如何通过回调到acceptClass处理由韦弗产生的任何新类(字符串名称,字节[])的方法GeneratedClassHandler

Maybe WeavingAdaptor is not the right place to start my weaving? 也许WeavingAdaptor不适合开始编织? Or maybe I shouldn't use AspectJ? 或者也许我不应该使用AspectJ?

MyAnnotation.java MyAnnotation.java

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}

MyWeavingHook.java MyWeavingHook.java

public class MyWeavingHook implements WeavingHook {

    public class MyWeavingClassloader implements WeavingClassLoader {

        private Bundle b;

        public MyWeavingClassLoader(Bundle b) {
            this.b = b;
        }

        void acceptClass(java.lang.String name, byte[] bytes) {
            //no way to get this back into the woven classes bundle classloader?
        } 

        URL[] getAspectURLs() {
            //how do I get a handle to my aspect library that AspectJ can understand?
        }
    }

    public void weave(WovenClass myclass) {
        Bundle b = Framework.getBundle(MyWeavingHook.class);
        WeavingClassLoader wc = new WeavingClassLoader(b);
        WeavingAdaptor w = new WeavingAdaptor(wc);
        if (shouldWeave(myclass))
          myclass.setBytes(w.weave(myClass.getBytes()));
        //should catch exceptions
    }

    private boolean shouldWeave(WovenClass myclass) {
        //not sure of the best logic to pick which classes to weave yet
    }
}

MyAspect.aj MyAspect.aj

privileged aspect MyAspect {
    after() : set(* *) && @annotation(MyAnnotation) {
      //send EventAdmin event
    }
}

MyTestClass.java MyTestClass.java

public class MyTestClass {
    @MyAnnotation
    private int myField;

    public void doSomething() {
      //do stuff with myField
    }
}

I could use Spring AOP, but I want this to work for any bundle, not just beans instantiated through Spring or Blueprint. 我可以使用Spring AOP,但我希望这适用于任何bundle,而不仅仅是通过Spring或Blueprint实例化的bean。 Also, Equinox Weaving doesn't seem to be using the OSGi weaving hook spec yet and I don't want to be tied to Equinox. 此外,Equinox Weaving似乎还没有使用OSGi编织钩子规范,我不想与Equinox绑定。 I have no problem scrapping AspectJ if something else works better. 如果其他东西效果更好的话,我也没有问题去掉AspectJ。

Reference to a similar question: Is it possible to do bytecode manipulation when using OSGi? 引用类似问题: 使用OSGi时是否可以进行字节码操作?

UPDATE: 更新:

End result is I just used Equinox Aspects and installed it into Karaf. 最终结果是我刚刚使用Equinox Aspects并将其安装到Karaf中。 Was 3 bundles, one library, and a system property. 有3个捆绑包,一个库和一个系统属性。 I'll use it until they either update it to us OSGi weaving or I write my own OSGi weaving hooks to use the AspectJ code similar to Equinox Aspects. 我将使用它,直到他们将它更新为我们的OSGi编织或我编写自己的OSGi编织钩子以使用类似于Equinox Aspects的AspectJ代码。 I don't like the weaving indicators required to get Equinox Aspects to work because it introduces a require-bundle/reexport or an import-package on the AspectJ RT in the bundle to be woven. 我不喜欢使Equinox Aspects工作所需的编织指标,因为它在要编织的包中的AspectJ RT上引入了require-bundle / reexport或import-package。 This dependency should be dynamically added and advised outside of the bundle. 应该在bundle之外动态添加和建议此依赖项。

Have a look at ProxyWeavingHook from Proxy module of Apache Aries. 看看Apache Aries的Proxy模块中的ProxyWeavingHook It uses the ASM library directly to modify the bytecode so more low level. 它直接使用ASM库来修改字节码,使其更低级别。

WeavingAdaptor expects your WeavingClassLoader to be derived from URLClassLoader so both available constructors basically do the same thing in the end. WeavingAdaptor期望您的WeavingClassLoader派生自URLClassLoader,因此两个可用的构造函数最终基本上都做同样的事情。 Checkout http://www.slideshare.net/mfrancis/bytecode-weaving to see how BundleWiring can be utilized in order to gain access to classpath URLs. 查看http://www.slideshare.net/mfrancis/bytecode-weaving以了解如何使用BundleWiring以获取对类路径URL的访问权限。 You can add AspectJ runtime packages to wovenClass.getDynamic'Imports() in order to avoid direct AspectJ references. 您可以将AspectJ运行时包添加到wovenClass.getDynamic'Imports()以避免直接的AspectJ引用。 BundleWiring is the way also to provide AspectJ urls for the WeavingAdaptor. BundleWiring也是为WeavingAdaptor提供AspectJ URL的方法。

I think that there's no way to support new classes coming from acceptClass because weaving hook states that dynamic imports can only be used while inside the weave method. 我认为没有办法支持来自acceptClass的新类,因为编织钩子声明动态导入只能在weave方法中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM