Java FrameworkUtil.getBundle始终返回空值
我正在尝试使用OSGi框架安装捆绑包。下面是我试图获取Java FrameworkUtil.getBundle始终返回空值,java,maven,osgi,osgi-bundle,Java,Maven,Osgi,Osgi Bundle,我正在尝试使用OSGi框架安装捆绑包。下面是我试图获取bundleContext的代码 在下面的代码中,每当这一行FrameworkUtil.getBundle返回我null 下面是我的应用程序代码- public App() { String basePath = "C:\\Tool\\LocalStorage"; final BundleContext bundleContext = FrameworkUtil.getBundle(this.get
bundleContext
的代码
在下面的代码中,每当这一行FrameworkUtil.getBundle
返回我null
下面是我的应用程序代码-
public App() {
String basePath = "C:\\Tool\\LocalStorage";
final BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
// install various bundles-
final Bundle bundle = bundleContext.installBundle(localFilenameOfBundles);
bundle.start();
}
调试代码时,我发现在FrameworkUtil类的getBundle方法中-
public static Bundle getBundle(final Class< ? > classFromBundle) {
// We use doPriv since the caller may not have permission
// to call getClassLoader.
Object cl = AccessController
.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
return classFromBundle.getClassLoader();
}
});
if (cl instanceof BundleReference) {
return ((BundleReference) cl).getBundle();
}
return null;
}
这里有我遗漏的东西吗?我想我面临的问题是OSGi类的加载。因为我认为-每个包都有自己的类加载器,一个DefaultClassLoader。但是在我的例子中,类加载器是不同的,我不确定如何修复它?您的类不是由OSGi包类加载器加载的。事实上,它是由AppClassLoader
加载的,它是JVM中加载主应用程序类路径上的类的默认类加载器,即与-classpath
命令行变量一起列出的JAR和目录
你真的创建了OSGi包吗?您是否启动了OSGi框架并将包安装到其中?因为这里看起来您只是在运行普通Java。我发现还有另一种不太明显的情况,“FrameworkUtil.getBundle”将返回null。如果您提供的类是框架系统包本身导出的额外类(包(0)),那么这些类将返回空结果。原因与尼尔·巴特利特解释的完全相同;这些类不是直接从OSGI包类加载器解析的。导入系统包提供的类的包工作得很好;只是FrameworkUtil不会让您知道这些类来自系统包
您这样做的原因是希望从类所来自的捆绑包中获取资源。我试图做的是-我试图通过上述代码以编程方式安装各种捆绑包。在我获得bundleContext之后,我可以使用它来安装和启动我想要做的各种bundle。我上面的代码是一个简单的maven项目,它是我上面提供的pom.xml文件。我如何才能在其中启动OSGi框架?我编辑了我的应用程序代码以显示如何安装和启动捆绑包。您可以使用org.OSGi.framework.launch.FrameworkFactory
API启动OSGi框架。两年前,我写了一篇综合性的博文:谢谢尼尔的链接。。当我开始使用它时,我得到了NoTouchElementException
。我已经为此开了一家新公司。任何帮助都将不胜感激。这个例外意味着您没有将OSGi框架实现(如Felix或Equinox)放在运行时类路径上。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>com.host.Stream</groupId>
<artifactId>Stream-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../Build/superpom</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.host.personallization.eye</groupId>
<artifactId>eye</artifactId>
<packaging>jar</packaging>
<name>eye</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration> <!-- Configuration of the archiver -->
<archive> <!-- Manifest specific configuration -->
<manifest> <!-- Classpath is added to the manifest of the created jar file. -->
<addClasspath>true</addClasspath> <!-- Configures the classpath prefix. This configuration option is used to
specify that all needed libraries are found under lib/ directory. -->
<classpathPrefix>lib/</classpathPrefix> <!-- Specifies the main class of the application -->
<mainClass>com.host.Stream.eye.eyeApp</mainClass>
</manifest>
</archive>
<includes>
<include>**/*.xml</include>
<include>**/*.class</include>
</includes>
<finalName>${project.artifactId}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.host.raptor</groupId>
<artifactId>ConfigWeb</artifactId>
<type>war</type>
<overWrite>true</overWrite>
<outputDirectory>buildsrc/StreamConf</outputDirectory>
<includes>**</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>target/config/StreamConf</outputDirectory>
<resources>
<resource>
<directory>buildsrc/StreamConf</directory>
<includes>
<include>**</include>
</includes>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>target/config/StreamConf/eyewiring.xml</file>
<regex>false</regex>
<replacements>
<replacement>
<token>dynamic_build_label_place_holder</token>
<value>${project.artifactId}-${project.version}-${buildNumber}</value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<id>assembly</id>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<finalName>${project.artifactId}-${project.version}-${buildNumber}</finalName>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.host.Stream</groupId>
<artifactId>Streamframework</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.host.external</groupId>
<artifactId>ucirrus-db</artifactId>
<version>0.7.3</version>
</dependency>
<dependency>
<groupId>com.host.Stream</groupId>
<artifactId>Streamcore</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-extender</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-io</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
</project>