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
");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 }