OSGI web的Application 联系客服

发布时间 : 星期一 文章OSGI web的Application更新完毕开始阅读42f9fd0ef78a6529647d5302

34

35 @Override

36 public void init() throws ServletException {

37 // Ensure that our ContainerServlet properties have been set

38 if ((wrapper == null ) || (context == null )) throw new UnavailableException( \39

40 // Ensure that Framework have been set 41

Framework

framework

=

FrameworkConfigListener.getFramework();

42 if (framework == null ) throw new UnavailableException( \43

44 // 将context注册为服务

45 registration = registerContext(framework.getBundleContext(), context); 46 } 47

48 private static ServiceRegistration registerContext(BundleContext bundleContext, Context context) {

49 Properties properties = new Properties();

50 properties.setProperty( \51 properties.setProperty( \

52 return bundleContext.registerService(Context. class .getName(), context, properties); 53 } 54

55 @Override

56 public void destroy() {

57 if (registration == null ) return ;

58 59

Framework

framework

=

FrameworkConfigListener.getFramework();

60 if (framework == null ) return ; 61

62 if (framework.getState() == Framework.ACTIVE) registration.unregister();

63 registration = null ; 64 }

65 } 通过ContainerServlet接口提供的setWrapper方法,我们获得了一个Wrapper实例,这个实例对应于 TomcatContextServlet部署到Tomcat中的封装类,通过其getParent方法我们就可以获得Servlet所在的 Context了。

接下来在init方法中,我们将获得的Context实例,通过Framework注册到OSGi容器中去。在destroy方法中,注销Context 的注册,这样形成了一个完整的生命周期。

然后,将这个TomcatContextServlet部署到web.xml中去:

1 2 < servlet >

3 < servlet-name > TomcatContextServlet 4

<

servlet-class

>

org.dbstar.osgi.web.launcher.tomcat.TomcatContextServlet 5 < load-on-startup > 1

6 设置使这个Servlet在WebContainer初始化时加载,否则它将没有加载的机会,因为我们在应用中不会直接使用到这个Servlet。

最后还有一件事情不要忘记了,我们需要将org.apache.catalina及其相关的package export到OSGi容器中去,这样才能在OSGi容器中供给bundle来import。参照《打造一个基于OSGi的Web Application——为OSGi容器提供Web Application环境 》一文中提到的方式,我们将catalina.jar作为extension Fragment的方式,引入到OSGi容器中去。 Catalina的MANIFEST.MF:

1 Manifest-Version: 1.0

2 Bundle-ManifestVersion: 2

3 Bundle-Name: Catalina Extension Fragment

4 Bundle-SymbolicName: org.apache.catalina_extension ; singleton:=true 5 Bundle-Version: 5.5.28 6 Bundle-Vendor: dbstar

7 Fragment-Host: system.bundle ; extension:=framework 8 Bundle-RequiredExecutionEnvironment: J2SE- 1.5

9 Export-Package: org.apache.catalina , org.apache.catalina.authenticator , 10 org.apache.catalina.connector , org.apache.catalina.core , org.apache.cat 11 alina.deploy , org.apache.catalina.loader , org.apache.catalina.mbeans , or 12 g.apache.catalina.realm , org.apache.catalina.security , org.apache.catal 13 ina.session , org.apache.catalina.startup , org.apache.catalina.users , org 14 .apache.catalina.util , org.apache.catalina.valves