我的项目目录结构(在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
我想在CSS文件中使用style.CSS,该文件包含在类StyleImpl.java
中的另一个OSGi捆绑包MyProjectContainingCSS
中的OSGi捆绑束MyProjectContainingCSS
中,
类似于:
public class StyleImpl {
public static void main(String[] args) {
css = this.getClass().getResource("/com/me/myapp/style.css").toExternalForm();
scene.getStylesheets().add(css);
}
}
如何在一个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
要从自己的捆绑包中获取文件,可以执行以下操作:
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();
}