5

我使用 osgi 和蓝图,我搜索如何读取我的包中的文件?如:mybundle

  • 文件.json
  • OSGI-INF/蓝图/蓝图.xml
  • 网络信息
  • *

我想在 myservice 中读取 file.json。

4

1 回答 1

10

为此,简单的方法是在您的 bean 中注入 bundlecontext

蓝图.xml

<bean id="plugin" class="com.timactive.MyBean" init-method="start">
    <property name="bcontext" ref="blueprintBundleContext"></property>
 </bean>

可能的参考:

blueprintBundle 提供 bundle 的 Bundle 对象。

blueprintBundleContext 提供bundle 的BundleContext 对象。

blueprintContainer 为包提供 BlueprintContainer 对象。

blueprintConverter 为提供对 Blueprint Container 类型转换工具的访问的包提供 Converter 对象。类型转换有更多信息。来源:http ://www.ibm.com/developerworks/opensource/library/os-osgiblueprint/

在你的课堂上:

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;
}
于 2013-05-06T15:08:16.030 回答