我想在OSGi平台中使用OpenStack4j。具体来说,我正在使用Apache Karaf OSGi运行时(基于Apache Felix OSGi实现)。
当OpenStack4j尝试发现连接器时出现问题。基于Getting started官方文档指南,我正在尝试使用此行实例化v2.0客户端:
OSClient os = OSFactory.builder()
.endpoint("http://host:5000/v2.0")
.credentials("user", "password")
.tenantName("tenant")
.authenticate();
我得到了这个例外:
Caused by: java.lang.ExceptionInInitializerError
at org.openstack4j.openstack.internal.OSAuthenticator.authenticateV2(OSAuthenticator.java:77)
at org.openstack4j.openstack.internal.OSAuthenticator.invoke(OSAuthenticator.java:41)
at org.openstack4j.openstack.client.OSClientBuilder$ClientV2.authenticate(OSClientBuilder.java:84)
at org.openstack4j.openstack.client.OSClientBuilder$ClientV2.authenticate(OSClientBuilder.java:65)
... 19 more
Caused by: java.lang.NullPointerException
at org.openstack4j.core.transport.internal.HttpExecutor.initService(HttpExecutor.java:32)
at org.openstack4j.core.transport.internal.HttpExecutor.<init>(HttpExecutor.java:25)
at org.openstack4j.core.transport.internal.HttpExecutor.<clinit>(HttpExecutor.java:20)
... 27 more
它似乎与连接器发现有关,但我不确定。
OpenStack4j不支持OSGi,I asked for it。我试图解决声明Apache Karaf feature的问题,基于HTTPClient 4这样:
<?xml version="1.0" encoding="UTF-8"?>
<features>
<feature name="openstack4j" version="${openstack4j-version}">
<feature version="${openstack4j-version}">openstack4j-httpclient</feature>
<bundle dependency="true">mvn:com.google.guava/guava/17.0</bundle>
<bundle dependency="true">mvn:com.fasterxml.jackson.core/jackson-databind/2.3.5</bundle>
<bundle dependency="true">mvn:com.fasterxml.jackson.core/jackson-annotations/2.3.5</bundle>
<bundle dependency="true">mvn:com.fasterxml.jackson.core/jackson-core/2.3.5</bundle>
<bundle dependency="true">mvn:com.fasterxml.jackson.dataformat/jackson-dataformat-yaml/2.3.5</bundle>
<bundle dependency="true">mvn:org.yaml/snakeyaml/1.14</bundle>
<bundle dependency="true">wrap:mvn:com.google.code.findbugs/jsr305/2.0.0</bundle>
<bundle>wrap:mvn:org.pacesys/openstack4j-core/${openstack4j-version}</bundle>
</feature>
<feature name="openstack4j-httpclient" version="${openstack4j-version}">
<bundle dependency="true">wrap:mvn:commons-logging/commons-logging/${commons.logging.version}</bundle>
<bundle dependency="true">wrap:mvn:org.apache.httpcomponents/httpclient/4.3.1</bundle>
<bundle dependency="true">wrap:mvn:org.apache.httpcomponents/httpcore/4.3</bundle>
<bundle>wrap:mvn:org.pacesys.openstack4j.connectors/openstack4j-httpclient/${openstack4j-version}</bundle>
</feature>
</features>
但它并没有解决我的问题。
如何在OSGi环境中使用OpenStack4j?
修改 我发现 NullPointerException 是由代码中的错误引起的(这里是issue)。它已被修复。
无论如何,SPI的问题仍然存在。
答案 0 :(得分:1)
我担心你的问题没有简单的解决方案。关于sources,HttpExecutor正在通过ServiceLoader寻找HttpExecutorService。这很可能不起作用,因为ServiceLoader对BundleClassloader一无所知,并且由于你从Bundle中实例化它很可能缺少必需的包导入。另一个原因可能是ServiceLoader使用父类加载器。
所以你有三种可能性 1)确保您还要在包中导入 org.openstack4j.core.transport.internal 2)将TCCL(Thread Context ClassLoader)设置为包含包的bundle的类加载器。所以ServiceLoader能够找到它 3)正如Neil评论的那样,使用Aries SPY Fly ......