Java ApacheFelixOSGi框架&x27;s WeavingHook.weave()方法不是';t调用

Java ApacheFelixOSGi框架&x27;s WeavingHook.weave()方法不是';t调用,java,osgi,apache-felix,load-time-weaving,Java,Osgi,Apache Felix,Load Time Weaving,我使用的工具包括: .../system/org/apache/felix/org.apache.felix.framework/4.0.3.redhat-610379/org.apache.felix.framework-4.0.3.redhat-610379.jar 包含: ** New Feature * [FELIX-2959] - [Framework] Implement OSGi R4.3 class loader byte-code weaving hook 我添

我使用的工具包括:

.../system/org/apache/felix/org.apache.felix.framework/4.0.3.redhat-610379/org.apache.felix.framework-4.0.3.redhat-610379.jar
包含:

 ** New Feature
     * [FELIX-2959] - [Framework] Implement OSGi R4.3 class loader byte-code weaving hook
我添加到
../etc/startup.properties

igb/igb.osgi.weaving/1.0.0/igb.osgi.weaving-1.0.0.jar=1
我的包的激活器:

package igb.osgi.weaving;

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.hooks.weaving.WeavingHook;

public class Activator implements BundleActivator   {
    public void start( BundleContext bc ) throws Exception {
        System.out.println( "Starting " + Activator.class.getName() );
        // this doesn't work
        //bc.registerService( Weaver.class, new Weaver(), new Hashtable<String, Object>() );
        // this is the proper way, see BJ Hargrave's accepted answer below
        bc.registerService( WeavingHook.class, new Weaver(), new Hashtable<String, Object>() );
        }
    public void stop( BundleContext bc ) throws Exception {
        System.out.println( "Stopping " + Activator.class.getName() );
        }
    } // Activator
../system/igb/igb.osgi.weaving/1.0.0/igb.osgi.weaving-1.0.0.jar/META-INF/MANIFEST.MF

Manifest-Version: 1.0
Bnd-LastModified: 1406380868073
Build-Jdk: 1.7.0_51
Built-By: Geri
Bundle-Activator: igb.osgi.weaving.Activator
Bundle-ManifestVersion: 2
Bundle-Name: IGB OSGi Weaving
Bundle-SymbolicName: igb.osgi.weaving
Bundle-Version: 1.0.0
Created-By: Apache Maven Bundle Plugin
Export-Package: igb.osgi.weaving;uses:="org.osgi.framework,org.osgi.fram
 ework.hooks.weaving";version="1.0.0"
Import-Package: org.osgi.framework;version="[1.7,2)",org.osgi.framework.
 hooks.weaving;version="[1.0,2)"
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"
Tool: Bnd-2.3.0.201405100607
我的包在Fabric8开始时启动:

Please wait while Fabric8 is loading...
Starting igb.osgi.weaving.Activator
100% [========================================================================]

______    _          _      _____
|  ___|  | |        (_)    |  _  |
| |_ __ _| |__  _ __ _  ___ \ V /
|  _/ _` | '_ \| '__| |/ __|/ _ \
| || (_| | |_) | |  | | (__| |_| |
\_| \__,_|_.__/|_|  |_|\___\_____/
  Fabric8 Container (1.1.0.CR5)
  http://fabric8.io/

Type 'help' to get started
and 'help [cmd]' for help on a specific command.
Hit '<ctrl-d>' or 'osgi:shutdown' to shutdown this container.

Open a browser to http://localhost:8181 to access the management console

Create a new Fabric via 'fabric:create'
or join an existing Fabric via 'fabric:join [someUrls]'

Fabric8:karaf@root>
我的Weaver服务得到认可:

Fabric8:karaf@root> ls 1
You are about to access system bundle 1.  Do you wish to continue (yes/no): yes

IGB OSGi Weaving (1) provides:
------------------------------
objectClass = igb.osgi.weaving.Weaver
service.id = 3
但它似乎没有被使用:

Fabric8:karaf@root> ls -u|grep -i igb
Fabric8:karaf@root>
包含igb的
../data/log/karaf.log
行:

Line 27: 2014-07-26 14:38:28,426 | DEBUG | -610379-thread-1 | BlueprintExtender                | 10 - org.apache.aries.blueprint.core - 1.0.1.redhat-610379 | Scanning bundle igb.osgi.weaving for blueprint application
Line 28: 2014-07-26 14:38:28,426 | DEBUG | -610379-thread-1 | BlueprintExtender                | 10 - org.apache.aries.blueprint.core - 1.0.1.redhat-610379 | No blueprint application found in bundle igb.osgi.weaving
Line 6824: 2014-07-26 14:38:33,028 | DEBUG | y-1.1.0-thread-1 | geronimo-osgi-registry           | 113 - org.apache.geronimo.specs.geronimo-osgi-registry - 1.1.0 | Bundle Considered for class providers: igb.osgi.weaving
Line 6825: 2014-07-26 14:38:33,028 | DEBUG | y-1.1.0-thread-1 | geronimo-osgi-registry           | 113 - org.apache.geronimo.specs.geronimo-osgi-registry - 1.1.0 | adding bundle igb.osgi.weaving [1]
Line 12143: 2014-07-26 14:38:39,783 | INFO  | .Beta37-thread-1 | runtime                          | 59 - gravia-runtime-api - 1.1.0.Beta37 | Installed: Module[igb.osgi.weaving:1.0.0]
Line 12343: 2014-07-26 14:38:40,017 | DEBUG | .Beta37-thread-1 | resource                         | 58 - gravia-resource - 1.1.0.Beta37 | Add to RuntimeEnvironment: Resource[igb.osgi.weaving:1.0.0]
Line 12344: 2014-07-26 14:38:40,017 | DEBUG | .Beta37-thread-1 | resource                         | 58 - gravia-resource - 1.1.0.Beta37 |    IdentityCapability[atts={gravia.identity=igb.osgi.weaving, version=1.0.0},[igb.osgi.weaving:1.0.0]]
Line 12344: 2014-07-26 14:38:40,017 | DEBUG | .Beta37-thread-1 | resource                         | 58 - gravia-resource - 1.1.0.Beta37 |    IdentityCapability[atts={gravia.identity=igb.osgi.weaving, version=1.0.0},[igb.osgi.weaving:1.0.0]]
Line 13469: 2014-07-26 14:38:43,168 | DEBUG | -2.13.2-thread-1 | Activator                        | 155 - org.apache.camel.camel-core - 2.13.2 | Bundle started: igb.osgi.weaving
草案内容如下:

框架提取编织钩子服务,并为每个必须加载的类调用它们的编织方法

我把这理解为无事可做。我错在哪里


PS:
Activator
的代码经过调整以反映接受的答案。

您需要在OSGi编织钩子服务名称下注册服务,以便框架可以找到它

bc.registerService( org.osgi.framework.hooks.weaving.WeavingHook.class, new Weaver(), new Hashtable<String, Object>() );
bc.registerService(org.osgi.framework.hooks.weaving.WeavingHook.class,new Weaver(),new Hashtable());

框架不知道你的igb.osgi.weave.Weaver服务是什么。

就是这样!非常感谢。虽然框架从Weaver开始就知道了,但类是从org.osgi.framework.hooks.weaving.WeavingHook派生的,因此一个简单的
instancof
可以揭示它。此外,(BundleContext的javadoc)()误导了我,因为它调用了参数
clazz
(grrr“注释只能编辑5分钟”)
clazz—可以在其名称下找到服务的类。
WeavingHook是一个接口。规范是否不精确,或者实现是否不完全符合规范,而是符合一般意义?经过思考,我得出结论,在注册每个服务(笛卡尔积)时,所有服务的
ìnstanceof
会不必要地膨胀框架的代码,并且可能会引起性能问题。因此,这个框架一如既往地做得很好。尽管有一些例子可以说明这一点。但现在至少有一个在上面…:-)
Line 27: 2014-07-26 14:38:28,426 | DEBUG | -610379-thread-1 | BlueprintExtender                | 10 - org.apache.aries.blueprint.core - 1.0.1.redhat-610379 | Scanning bundle igb.osgi.weaving for blueprint application
Line 28: 2014-07-26 14:38:28,426 | DEBUG | -610379-thread-1 | BlueprintExtender                | 10 - org.apache.aries.blueprint.core - 1.0.1.redhat-610379 | No blueprint application found in bundle igb.osgi.weaving
Line 6824: 2014-07-26 14:38:33,028 | DEBUG | y-1.1.0-thread-1 | geronimo-osgi-registry           | 113 - org.apache.geronimo.specs.geronimo-osgi-registry - 1.1.0 | Bundle Considered for class providers: igb.osgi.weaving
Line 6825: 2014-07-26 14:38:33,028 | DEBUG | y-1.1.0-thread-1 | geronimo-osgi-registry           | 113 - org.apache.geronimo.specs.geronimo-osgi-registry - 1.1.0 | adding bundle igb.osgi.weaving [1]
Line 12143: 2014-07-26 14:38:39,783 | INFO  | .Beta37-thread-1 | runtime                          | 59 - gravia-runtime-api - 1.1.0.Beta37 | Installed: Module[igb.osgi.weaving:1.0.0]
Line 12343: 2014-07-26 14:38:40,017 | DEBUG | .Beta37-thread-1 | resource                         | 58 - gravia-resource - 1.1.0.Beta37 | Add to RuntimeEnvironment: Resource[igb.osgi.weaving:1.0.0]
Line 12344: 2014-07-26 14:38:40,017 | DEBUG | .Beta37-thread-1 | resource                         | 58 - gravia-resource - 1.1.0.Beta37 |    IdentityCapability[atts={gravia.identity=igb.osgi.weaving, version=1.0.0},[igb.osgi.weaving:1.0.0]]
Line 12344: 2014-07-26 14:38:40,017 | DEBUG | .Beta37-thread-1 | resource                         | 58 - gravia-resource - 1.1.0.Beta37 |    IdentityCapability[atts={gravia.identity=igb.osgi.weaving, version=1.0.0},[igb.osgi.weaving:1.0.0]]
Line 13469: 2014-07-26 14:38:43,168 | DEBUG | -2.13.2-thread-1 | Activator                        | 155 - org.apache.camel.camel-core - 2.13.2 | Bundle started: igb.osgi.weaving
bc.registerService( org.osgi.framework.hooks.weaving.WeavingHook.class, new Weaver(), new Hashtable<String, Object>() );