【问题标题】:How can I go about using a **CSS** resource file in one OSGi bundle from another OSGi bundle如何在另一个 OSGi 包的一个 OSGi 包中使用 **CSS** 资源文件
【发布时间】:2016-07-22 12:14:28
【问题描述】:

我的项目目录结构(在 Eclipse 中):

MyProjectContainingCSS/
    src/        --> "source directory" on Eclipse's classpath/buildpath
        com.me.myapp
            style.css


MyProjectInheritingCSS/
    src/        --> "source directory" on Eclipse's classpath/buildpath
        com.me.myapp
            StyleImpl.java

我想在类StyleImpl.java 中的另一个OSGi 包MyProjectContainingCSS 中的OSGi 包MyProjectContainingCSS 中使用CSS 文件style.css

类似:

public class StyleImpl {
    public static void main(String[] args) {
        css = this.getClass().getResource("/com/me/myapp/style.css").toExternalForm();
        scene.getStylesheets().add(css);
    }
}

如何在另一个 OSGi 包的一个 OSGi 包中使用 CSS 资源文件?

提前谢谢大家。

更新

bnd.bnd 文件

Bundle-Version: 0.0.0.${tstamp}
-buildpath: \
    ../cnf/plugins/org.apache.felix.dependencymanager.annotation-3.2.0.jar;version=file,\
    org.apache.felix.dependencymanager,\
    osgi.core,\
    launcher;version=latest,\
    libs/commons-io-2.4.jar;version=file
Private-Package: ui.impl
Export-Package: ui
Import-Package: *

运行描述符

-runfw: org.apache.felix.framework;version='[4,5)'
-runee: JavaSE-1.8
-runsystemcapabilities: ${native_capability}

-resolve.effective: active;skip:="osgi.service"
-runbundles: \
    org.apache.felix.dependencymanager,\
    org.apache.felix.dependencymanager.runtime,\
    org.apache.felix.dependencymanager.shell,\
    org.apache.felix.metatype,\
    org.apache.felix.eventadmin,\
    org.apache.felix.configadmin,\
    org.apache.felix.log,\
    org.apache.felix.gogo.command,\
    org.apache.felix.gogo.runtime,\
    org.apache.felix.gogo.shell,\
    launcher;version=latest,\
    ui;version=latest,\
    mainscreen;version=latest
-runsystempackages: javafx.application,javafx.scene,javafx.stage,javafx.scene.layout,javafx.event,javafx.collections,javafx.scene.control,javafx.scene.paint,javafx.scene.shape

【问题讨论】:

  • 在 CSS 包中创建一个类,将 InputStream 暴露给 CSS 文件,并在另一个包中导入这个类。
  • 一个包如何知道具有特定名称的特定资源将在另一个包中找到?这种假设意味着您违反了模块化封装。尝试找到一个不对其他模块做出假设的解决方案。为什么不描述你试图解决的真正的问题?

标签: java css resources osgi bndtools


【解决方案1】:

要从您自己的包中获取文件,您可以:

Bundle bundle = FrameworkUtil.getBundle(this.getClass());
Enumeration<URL> resources = bundle.getResources("/com/me/myapp/style.css");   
if (resources != null) {
    URL myCSS = resources.nextElement();
}            

如果您可以找到其他 OSGi 捆绑对象,您也可以这样做。我会尝试这样的事情:

    Bundle bundle = FrameworkUtil.getBundle(this.getClass());
    Bundle[] bArray = bundle.getBundleContext().getBundles();
    Bundle cssBundle = null;
    for (Bundle b : bArray){
        if(b.getSymbolicName().equals("com.me.myapp")) {
            cssBundle = b;
            break; 
        }
    }
    Enumeration<URL> resources = cssBundle.getResources("/com/me/myapp/style.css");   
    if (resources != null) {
        URL myCSS = resources.nextElement();
    }

【讨论】:

  • 非常感谢@Marcos Zolnowski 我已将您的代码放在我的一个名为MainScreen 的类中,但我无法使用FrameworkUtil.getBundle 方法。它会通过警告The method getBundle(Class&lt;capture#1-of ? extends MainScreen&gt;) is undefined for the type FrameworkUtil 突出显示错误。可能是什么问题。
  • FrameworkUtil 是 org.osgi.framework 包中的一个类。同包的Bundle。您使用什么版本的 OSGi?
  • 我对 OSGi 完全陌生。我不确定如何检查 OSGi 版本。我在 eclipse Mars.2 Release (4.5.2) 中使用 BND 工具
  • 没问题,因为系统不会告诉你。你能告诉我org.osgi.framework包的版本吗?
  • 某些版本的 bndtools 存在泛型问题。但我现在不确定。你可以尝试投:FrameworkUtil.getBundle((Class) myClass)
猜你喜欢
  • 2011-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 2011-10-08
  • 2016-06-06
  • 2018-06-12
  • 2011-10-31
  • 2010-11-27
相关资源
最近更新 更多