java - FrameworkUtil.getBundle 总是返回 NULL

标签 java maven osgi osgi-bundle

我正在尝试使用 OSGi 框架安装 bundle 。下面是我的代码,尝试获取 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();     

        }

当我调试代码时,我发现getBundle method中FrameworkUtil类的-

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;
    }

cl is not an instance of BundleReference这就是为什么它总是返回 NULL。当我检查cl时,我发现了这样的东西-

sun.misc.Launcher$AppClassLoader@69956995我相信这不是 BundleReference 的实例?我需要这个org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader

下面是我的 pom.xml 文件 -

<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>

我这里有什么遗漏的吗?我想我面临的问题是 OSGi 类加载。因为我认为 - 每个 Bundle 都有自己的类加载器,即 DefaultClassLoader。但就我而言,类加载器是不同的,我不知道如何解决这个问题?

最佳答案

您的类未由 OSGi bundle 类加载器加载。事实上,它是由 AppClassLoader 加载的,它是 JVM 中的默认类加载器,用于加载主应用程序类路径上的类,即使用 -classpath 列出的 jar 和目录。命令行变量。

您实际上创建了 OSGi 包吗?您是否启动了 OSGi 框架并将 bundle 安装到其中?因为看起来您只是在这里运行纯 Java。

关于java - FrameworkUtil.getBundle 总是返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18240452/

相关文章:

java - HTTP 状态 405 - Rest API 的方法不允许错误

java - DataSnapshot for 循环 (Android Studio) 未附加 Firebase 数据库路径的子项

java - Apache Spark Maven POM 错误

java - 为什么找不到我包含的 JSP?

svn - Grails 插件 Maven 存储库

java - 解决冲突的依赖关系 (Maven)

eclipse - OSGI:WAITING声明性服务组件启动的最佳方法是什么?

ant - 使用 BND 嵌入第三方 JAR

java - Pax 运行者出错

java - 以 LIFO 顺序将条目存储在 Java 映射中