Java培训课程:OSGI如何读取插件中的资源文件
来源:学生作业帮助网 编辑:作业帮 时间:2024/05/15 11:51:53 JAVA
Java培训课程:OSGI如何读取插件中的资源文件JAVA
【网络综合-Java培训课程:OSGI如何读取插件中的资源文件】:
思路就是通过bundleContext来取得资源。
首先,要在对应的插件中先建立一个Activator需要实现BundleActivator接口,
代码:
然后再需要查找资源的地方,取得bundleContext,通过bundleContext的getResource方法取得URL类型的resource,代码:
JAVA
思路就是通过bundleContext来取得资源。
首先,要在对应的插件中先建立一个Activator需要实现BundleActivator接口,
代码:
- import org.osgi.framework.BundleActivator;
- import org.osgi.framework.BundleContext;
- public class Activator implements BundleActivator {
- private static BundleContext bundleContext;
- public static BundleContext getBundleContext() {
- return bundleContext;
- }
- public void start(BundleContext context) throws Exception {
- Activator.bundleContext = context;
- }
- public void stop(BundleContext context) throws Exception {
- }
- }
然后再需要查找资源的地方,取得bundleContext,通过bundleContext的getResource方法取得URL类型的resource,代码:
- public static InputStream getResourceByContext(String path) {
- try {
- BundleContext bundleContext = Activator.getBundleContext();
- URL resource = bundleContext.getBundle().getResource("/web" + path);
- InputStream in = resource.openStream();
- if (in == null) {
- String msg = "\nNot found \"" + path + "\";";
- log.error(msg);
- }
- return in;
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- }
- return null;
- }