Equinox Server-side(一)
Equinox作为一个OSGI的示范工程.那是相当的牛b啊.Eclipse就是建立在Equinox上的.所以说Equinox是个好东西啊.
既然Equinox是个好东西,那怎么将Equinox应用到Http Server上,让那些web应用作为bundle在Equinox如插件般插来插去,那是多么惬意的事情.
现在就来看看怎么实现这个目标吧.
现在Equinox实现与Http Server整合有两种方式
1.将Equinox建立在servlet容器上,由Equinox的servletbridge来转发request
2.将http服务作为一个bundle,让其他bundle来访问其http服务.
先来说说 1.
a)先装上tomcat
b)下载bridge.war (http://www.eclipse.org/equinox/server/downloads/bridge.war) 它主要的作用是将HTTP请求转发给OSGI HTTP Services
c)将bridge.war拷到webapps下,启动tomcat!OK,OSGI也会随着一起起来.
这个时候就可以在osgi下install ,start ,stop 各种bundle了
现在有两种方式将web应用发布成bunlde
i).用由org.eclipse.eqinox.servlet.bridge.http注册的OSGi HttpService来发布这个bundle
ii).作为org.eclipse.equinox.http.registry的扩展点来发布.
现在看i.)
先写一个servlet:
public class HelloWorldServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
resp.getWriter().write("<html><body>Hello World – sample servlet</body></html>");
}
}
这个时候需要一个Activator来获得BundleContext 并注册上面的Servlet为Http Service
public class Activator implements BundleActivator {
private ServiceTracker httpServiceTracker;
public void start(BundleContext context) throws Exception {
httpServiceTracker = new HttpServiceTracker(context);
httpServiceTracker.open();
}
public void stop(BundleContext context) throws Exception {
httpServiceTracker.close();
httpServiceTracker = null;
}
private class HttpServiceTracker extends ServiceTracker {
public HttpServiceTracker(BundleContext context) {
super(context, HttpService.class.getName(), null);
}
public Object addingService(ServiceReference reference) {
HttpService httpService = (HttpService) context.getService(reference);
try {
httpService.registerResources("/helloworld.html", "/helloworld.html", null); //$NON-NLS-1$ //$NON-NLS-2$
httpService.registerServlet("/helloworld", new HelloWorldServlet(), null, null); //$NON-NLS-1$
} catch (Exception e) {
e.printStackTrace();
}
return httpService;
}
public void removedService(ServiceReference reference, Object service) {
HttpService httpService = (HttpService) service;
httpService.unregister("/helloworld.html"); //$NON-NLS-1$
httpService.unregister("/helloworld"); //$NON-NLS-1$
super.removedService(reference, service);
}
}
}
首先从BundleContext中得到HttpService的reference。然后从BundleContext中拿到HttpService的实例。向httpService注册你的Servlet。最后导出为一个Bundle。
现在导出成一个bundle,在刚才的OSGI中install 并start .然后在浏览器输入http://localhost/bridge/helloworld,就可以看到效果了.
在看ii)将web应用作为扩展点,这样Activator就可以省去了.用plugin.xml来代替Activator描述注册信息,如下
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin>
<extension-point id="servlets" name="HttpService servlets" schema="schema/servlets.exsd"/>
<extension-point id="resources" name="HttpService resources" schema="schema/resources.exsd"/>
<extension-point id="httpcontexts" name="HttpService httpcontexts" schema="schema/httpcontexts.exsd"/>
<extension
id="helloServlet"
point="org.eclipse.equinox.http.registry.servlets">
<servlet
alias="/ext/helloworld"
class="sample.http.registry.HelloWorldServlet">
<init-param
name="servlet-name"
value="Test Servlet">
</init-param>
<init-param
name="testParam"
value="test param value">
</init-param>
</servlet>
</extension>
<extension
id="helloResource"
point="org.eclipse.equinox.http.registry.resources">
<resource
alias="/ext/helloworld.html"
base-name="/helloworld.html"
/>
</extension>
</plugin>
OK,现在导出成一个bundle,在刚才的OSGI中install 并start .然后在浏览器输入http://localhost/bridge/helloworld,看看是不是和刚才的效果一样啊
好了下次在讲讲2.是怎么实现的