OSGI读取配置文件的方法
来源:互联网 发布:微商和淘宝哪个好 编辑:程序博客网 时间:2024/05/19 20:44
import com.liming.tp.framework.util.PropertiesConfiguration;import net.sf.ehcache.Cache;import net.sf.ehcache.CacheManager;import net.sf.ehcache.config.CacheConfiguration;import org.apache.log4j.Logger;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import java.io.InputStream;import java.util.Properties;/** * <b> * Title:</b> 主题 * <br><b> * Description:</b> 类功能描述 * <br><b> * Copyright:</b> Copyright (c) 2013 * <br><b> * * @author tp-group * @version 1.0 */public class InitActivator implements BundleActivator { private static Logger logger = Logger.getLogger(InitActivator.class); private static BundleContext context; /** * 获取Bundle上下文 * @return Bundle上下文 */ public static BundleContext getContext() { return context; } /* * (non-Javadoc) * @see org.osgi.tp.BundleActivator#start(org.osgi.tp.BundleContext) */ public void start(BundleContext bundleContext) throws Exception { InitActivator.context = bundleContext; Properties props = new Properties(); InputStream is = InitActivator.class.getResourceAsStream("/LMStart.configuration"); props.load(is); PropertiesConfiguration.parseConfiguration(props); CacheManager manager = new CacheManager(InitActivator.class.getResource("/mycache.xml")); Cache cache = manager.getCache("mycache"); CacheConfiguration config = cache.getCacheConfiguration(); config.setTimeToIdleSeconds(60); config.setTimeToLiveSeconds(120); config.setMaxEntriesLocalHeap(10000); config.setMaxEntriesLocalDisk(1000000); } /* * (non-Javadoc) * @see org.osgi.tp.BundleActivator#stop(org.osgi.tp.BundleContext) */ public void stop(BundleContext bundleContext) throws Exception { InitActivator.context = null; }}
在osgi环境中,可以在activator中获得配置文件,方法如上。
但是在业务方法中,想获得文件,可不是那么容易的事情了,例如以下代码:
BusinessClass.class.getResourceAsStream("/LMStart.configuration");此时会返回null,这是OSGI的classloader在作怪,此时唯一的好办法,就是这样:
ExampleActivator.bundleContext.getBundle().getEntry(fileName)通过Activator类的静态方法,获得bundleContext,再通过bundle的getEntry方法,就可以获得正确的URL。
- OSGI读取配置文件的方法
- OSGI使用logback,读取配置文件
- 读取配置文件的方法
- 读取配置文件的方法案例
- 读取xml配置文件的方法
- 读取配置文件的方法getcfg()
- play读取配置文件的方法
- spring 读取配置文件的方法
- spring 读取配置文件的方法
- 读取配置文件properties的方法
- play读取配置文件的方法
- Spring读取配置文件的方法
- java读取配置文件的方法
- 读取配置文件properties的方法
- 配置文件读取的n种方法
- java读取配置文件的几种方法
- java应用程序读取配置文件的方法
- java读取配置文件的几种方法
- unix网络编程笔记
- Ubuntu下OpenVPN客户端配置教程
- 九度oj 1504:把数组排成最小的数 (贪心)
- ubuntu13.04更新源
- HDU 1878 欧拉回路 Tarjan+欧拉判定
- OSGI读取配置文件的方法
- 8、对产品类别的业务管理分析
- Java学习笔记
- ok6410学习笔记(13.总线设备驱动)
- 数字图像处理标准图像Lena的故事
- java中volatile关键字的含义
- Weakref应用
- 移动考勤正在撬开考勤市场的大门
- Best Practice in Writing a COM-Visible Assembly (C#)