java osgi 启动原理_打造一个基于OSGi的Web Application——在WebApplication中启动OSGi

1 //初始化Framework环境2 privatestaticvoidinitFramework(Framework framework, ServletContextEvent event)throwsIOException {3 BundleContext bundleContext=framework.getBundleContext();4 ServletContext servletContext=event.getServletContext();5 6 //将ServletContext注册为服务7 registerContext(bundleContext, servletContext);8 9 File file=bundleContext.getDataFile(".init");10 if(!file.isFile()) {//第一次初始化11 if(logger.isInfoEnabled()) logger.info("Init Framework

9b8a8a44dd1c74ae49c20a7cd451974e.png");12 13 String pluginLocation=servletContext.getInitParameter(CONTEXT_PARAM_OSGI_PLUGINS_LOCATION);14 if(pluginLocation==null) pluginLocation=DEFAULT_OSGI_PLUGINS_LOCATION;15 elseif(!pluginLocation.startsWith("/")) pluginLocation="/".concat(pluginLocation);16 17 //安装bundle18 File bundleRoot=newFile(servletContext.getRealPath(pluginLocation));19 if(bundleRoot.isDirectory()) {20 if(logger.isInfoEnabled()) logger.info("Load Framework bundles from:"+pluginLocation);21 22 File bundleFiles[]=bundleRoot.listFiles(newFilenameFilter() {23 publicbooleanaccept(File dir, String name) {24 returnname.endsWith(".jar");25 }26 });27 28 if(bundleFiles!=null&&bundleFiles.length>0) {29 for(File bundleFile : bundleFiles) {30 try{31 bundleContext.installBundle(bundleFile.toURL().toExternalForm());32 if(logger.isInfoEnabled()) logger.info("Install bundle success:"+bundleFile.getName());33 }catch(Throwable e) {34 if(logger.isWarnEnabled()) logger.warn("Install bundle error:"+bundleFile, e);35 }36 }37 }38 39 for(Bundle bundle : bundleContext.getBundles()) {40 if(bundle.getState()==Bundle.INSTALLED||bundle.getState()==Bundle.RESOLVED) {41 if(bundle.getHeaders().get(Constants.BUNDLE_ACTIVATOR)!=null) {42 try{43 bundle.start(Bundle.START_ACTIVATION_POLICY);44 if(logger.isInfoEnabled()) logger.info("Start bundle:"+bundle);45 }catch(Throwable e) {46 if(logger.isWarnEnabled()) logger.warn("Start bundle error:"+bundle, e);47 }48 }49 }50 }51 }52 53 newFileWriter(file).close();54 if(logger.isInfoEnabled()) logger.info("Framework inited.");55 }56 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值