osgi蓝图如何读取捆绑包中的资源文件

osgi蓝图如何读取捆绑包中的资源文件,osgi,apache-karaf,osgi-bundle,blueprint-osgi,Osgi,Apache Karaf,Osgi Bundle,Blueprint Osgi,我使用osgi和blueprint,我搜索如何读取捆绑包中的文件? 例如: mybundle file.json OSGI-INF/blueprint/blueprint.xml WEB-INF * 我想在myservice中读取file.json。要做到这一点,最简单的方法是在bean中注入bundlecontext blueprint.xml <bean id="plugin" class="com.timactive.MyBean" init-method="start">

我使用osgi和blueprint,我搜索如何读取捆绑包中的文件? 例如: mybundle

  • file.json
  • OSGI-INF/blueprint/blueprint.xml
  • WEB-INF
  • *

我想在myservice中读取file.json。

要做到这一点,最简单的方法是在bean中注入bundlecontext

blueprint.xml

<bean id="plugin" class="com.timactive.MyBean" init-method="start">
    <property name="bcontext" ref="blueprintBundleContext"></property>
 </bean>
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext
public class MyBean  {

    public BundleContext bcontext;
    public boolean start(){
    try {
    Bundle bundle = bcontext.getBundle();
    InputStream is = bundle.getEntry("/file.json").openStream();
    String jsondb =  readFile(is);

    } catch (IOException e) {
                LOG.error("The file treefield.json not found", e);
                return(false);
            }

        }

        return(true);
    }

    private String readFile(InputStream is ) throws IOException {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
   }
   public void setBcontext(BundleContext bcontext) {
    this.bcontext = bcontext;
}