tim*_*ive 5 osgi apache-karaf blueprint-osgi osgi-bundle
我使用osgi和blueprint,我搜索如何读取我的包中的文件?如:mybundle
我想在myservice中读取file.json.
要做到这一点,简单的方法是在bean中注入bundlecontext
blueprint.xml
<bean id="plugin" class="com.timactive.MyBean" init-method="start">
<property name="bcontext" ref="blueprintBundleContext"></property>
</bean>
Run Code Online (Sandbox Code Playgroud)
可能的参考:
blueprintBundle 提供bundle的Bundle对象.
blueprintBundleContext 提供bundle的BundleContext对象.
blueprintContainer 为包提供BlueprintContainer对象.
blueprintConverter 为bundle提供Converter对象,该对象提供对Blueprint Container类型转换工具的访问.类型转换有更多信息.来源: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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6114 次 |
| 最近记录: |