Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用嵌入式OSGi容器_Java_Osgi_Apache Felix - Fatal编程技术网

Java 使用嵌入式OSGi容器

Java 使用嵌入式OSGi容器,java,osgi,apache-felix,Java,Osgi,Apache Felix,我正在构建一些模块,我想将它们公开为OSGi捆绑包,而不需要任何对OSGi库的实际依赖关系。使用声明性服务选项似乎可以做到这一点 然而,因为我对OSGi比较陌生(至少在创建捆绑包方面),所以我想测试它是否能够正常工作,为此,我想建立一个小型嵌入式OSGi环境 目前我有一个单独的包,它导出一个API,还提供一个接口的存根实现 我按照以下教程设置了环境: 嵌入式felix实现似乎工作正常,但存在两个问题: Bundle bundle = felix.getBundleContext().in

我正在构建一些模块,我想将它们公开为OSGi捆绑包,而不需要任何对OSGi库的实际依赖关系。使用声明性服务选项似乎可以做到这一点

然而,因为我对OSGi比较陌生(至少在创建捆绑包方面),所以我想测试它是否能够正常工作,为此,我想建立一个小型嵌入式OSGi环境

目前我有一个单独的包,它导出一个API,还提供一个接口的存根实现

我按照以下教程设置了环境:

嵌入式felix实现似乎工作正常,但存在两个问题:

Bundle bundle = felix.getBundleContext().installBundle("/path/to/bundle.jar")
bundle.start();
System.out.println(bundle.getRegisteredServices());
这会打印出
null
,因此,虽然捆绑包似乎启动正常,但它似乎不会公开任何服务

其次,我想知道是否需要做一些特殊的事情来启动和运行声明性服务。我的maven依赖项是:

<dependencies>
    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.framework</artifactId>
        <version>4.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.scr</artifactId>
        <version>1.6.2</version>
    </dependency>
</dependencies>

然而,乍一看,这似乎有点乐观。如何为嵌入式felix引擎启用声明性服务?

这两个问题的解决方案都是在加载我自己的捆绑包之前将“scr”jar(用于解析声明性服务)作为捆绑包加载

因为jar位于我的maven存储库中,它应该可以跨系统工作,所以下面的代码从它所在的任何位置加载scr jar:

    URL url = getClass().getClassLoader().getResource("org/apache/felix/scr/ScrService.class");
    String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", "");
    framework.getBundleContext().installBundle(jarPath).start();
在这一点之后,我加载了我自己的包,包中的服务被正确地检测到

另一方面,您可以通过向初始映射添加一些属性来启用日志记录:

    map.put("ds.showtrace", "true");
    map.put("ds.showerrors", "true");
有关更多属性,请访问

作为将来的参考,下面是我用来启动和运行它的所有代码

private void initialize() throws BundleException, URISyntaxException {
    Map<String, String> map = new HashMap<String, String>();

    // make sure the cache is cleaned
    map.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);

    // more properties available at: http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html
    map.put("ds.showtrace", "true");
    map.put("ds.showerrors", "true");

    System.out.println("Building OSGi Framework");
    FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
    Framework framework = frameworkFactory.newFramework(map);

    System.out.println("Starting OSGi Framework");
    framework.start();

    // declarative services dependency is necessary, otherwise they won't be picked up!
    loadScrBundle(framework);

    framework.getBundleContext().installBundle("file:/path/to/myBundle.jar").start();

    ServiceReference reference = framework.getBundleContext().getServiceReference("my.Interface");
    System.out.println(framework.getBundleContext().getService(reference));

    for (Bundle bundle : framework.getBundleContext().getBundles()) {
        System.out.println("Bundle: " + bundle.getSymbolicName());
        if (bundle.getRegisteredServices() != null) {
            for (ServiceReference serviceReference : bundle.getRegisteredServices())
                System.out.println("\tRegistered service: " + serviceReference);
        }
    }
}

private void loadScrBundle(Framework framework) throws URISyntaxException, BundleException {
    URL url = getClass().getClassLoader().getResource("org/apache/felix/scr/ScrService.class");
    if (url == null)
        throw new RuntimeException("Could not find the class org.apache.felix.scr.ScrService");
    String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", "");
    System.out.println("Found declarative services implementation: " + jarPath);
    framework.getBundleContext().installBundle(jarPath).start();
}
private void initialize()抛出BundleException、URISyntaxException{
Map Map=newhashmap();
//确保缓存已清理
map.put(Constants.FRAMEWORK\u STORAGE\u CLEAN,Constants.FRAMEWORK\u STORAGE\u CLEAN\u ONFIRSTINIT);
//更多属性可在以下网址获得:http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html
map.put(“ds.showtrace”,“true”);
地图放置(“ds.RORS”、“真”);
System.out.println(“构建OSGi框架”);
FrameworkFactory=ServiceLoader.load(FrameworkFactory.class).iterator().next();
frameworkFactory.newFramework(map);
System.out.println(“启动OSGi框架”);
framework.start();
//声明性服务依赖关系是必需的,否则将无法获取它们!
载荷约束(框架);
framework.getBundleContext().installBundle(“文件:/path/to/myBundle.jar”).start();
ServiceReference=framework.getBundleContext().getServiceReference(“my.Interface”);
System.out.println(framework.getBundleContext().getService(参考));
for(Bundle:framework.getBundleContext().getBundles()){
System.out.println(“Bundle:+Bundle.getSymbolicName());
if(bundle.getRegisteredServices()!=null){
对于(ServiceReference ServiceReference:bundle.getRegisteredServices())
System.out.println(“\t注册服务:“+serviceReference”);
}
}
}
私有void loadScrBundle(框架框架)抛出URISyntaxException、bundlexception{
URL URL=getClass().getClassLoader().getResource(“org/apache/felix/scr/ScrService.class”);
如果(url==null)
抛出新的RuntimeException(“找不到类org.apache.felix.scr.ScrService”);
字符串jarPath=url.toURI().getSchemeSpecificPart().replaceAll(!*,);
System.out.println(“发现声明性服务实现:“+jarPath”);
framework.getBundleContext().installBundle(jarPath.start();
}

我建议看一看。它允许在OSGi容器中测试捆绑包。
很好的一点是它与junit集成,因此您的测试看起来非常类似于普通测试。我有一个类似的问题,希望你能看看,看看你能不能帮上忙。我真的被困住了,非常感谢你能提供的任何帮助。先谢谢你。
private void initialize() throws BundleException, URISyntaxException {
    Map<String, String> map = new HashMap<String, String>();

    // make sure the cache is cleaned
    map.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);

    // more properties available at: http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html
    map.put("ds.showtrace", "true");
    map.put("ds.showerrors", "true");

    System.out.println("Building OSGi Framework");
    FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
    Framework framework = frameworkFactory.newFramework(map);

    System.out.println("Starting OSGi Framework");
    framework.start();

    // declarative services dependency is necessary, otherwise they won't be picked up!
    loadScrBundle(framework);

    framework.getBundleContext().installBundle("file:/path/to/myBundle.jar").start();

    ServiceReference reference = framework.getBundleContext().getServiceReference("my.Interface");
    System.out.println(framework.getBundleContext().getService(reference));

    for (Bundle bundle : framework.getBundleContext().getBundles()) {
        System.out.println("Bundle: " + bundle.getSymbolicName());
        if (bundle.getRegisteredServices() != null) {
            for (ServiceReference serviceReference : bundle.getRegisteredServices())
                System.out.println("\tRegistered service: " + serviceReference);
        }
    }
}

private void loadScrBundle(Framework framework) throws URISyntaxException, BundleException {
    URL url = getClass().getClassLoader().getResource("org/apache/felix/scr/ScrService.class");
    if (url == null)
        throw new RuntimeException("Could not find the class org.apache.felix.scr.ScrService");
    String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", "");
    System.out.println("Found declarative services implementation: " + jarPath);
    framework.getBundleContext().installBundle(jarPath).start();
}