设为首页 收藏本站
查看: 1053|回复: 0

[经验分享] Equinox OSGi服务器应用程序的配置步骤

[复制链接]

尚未签到

发表于 2017-2-28 10:14:30 | 显示全部楼层 |阅读模式
DSC0000.png

  本文介绍在Eclipse里如何配置一个简单的基于Eclipse Equinox OSGi实现的Web应用程序,在它的基础上可以构造更加复杂的应用,本文使用的是Eclipse 3.3.1版本,如果你的Eclipse版本在3.2.0或以上应该都可以。
  一、支持静态页面和Servlet
  1. 创建一个新的plugin项目, net.bjzhanghao.osgi.test,在向导第一步里选中“This plug-in is target,在下一步的“Plug-in Options”里选中“Generate an activator”。
DSC0001.gif

  2. 在例子项目的MANIFEST.MF里添加如下依赖项目,这些项目都是Eclipse自带的:



org.eclipse.equinox.http.jetty
org.eclipse.equinox.http.servlet
org.mortbay.jetty
org.apache.commons.logging
javax.servlet
org.eclipse.equinox.http.registry
  3. 在例子项目根目录下创建一个放置web文件的目录,如“web_files”,在这个目录下写一个简单的index.html文件。
  4. 为项目建一个plugin.xml文件,内容如下:



<plugin>
<extension point="org.eclipse.equinox.http.registry.resources">
<resource
alias="/web"
base-name="/web_files"/>
</extension>
</plugin>
  注意,这时若MANIFEST.MF里提示错误,只要在Bundle-SymbolicName这一行后面加上“;singleton:=true”即可解决。
  5. 现在可以启动这个应用程序了。在Eclipse菜单里选择“Run->Open Run Dialog...”,在左边的 “OSGi Framework”项下创建一个新的启动配置项,在右边先点“Deselect All”清空所有复选框,然后在Workspace下选中 自己的osgi项目,再点“Add Required Bundles”按钮,Eclipse会自动把所依赖的项目选中。 最后按“Debug”按钮启动,内嵌的jetty和我们的项目会一起被启动。
DSC0002.gif

  6. 打开浏览器,输入“http://localhost/web/index.html”应该可以看到index.html里的内容。
  以上只验证了静态页面,现在来配置一个servlet看看。
  7. 在项目里创建一个继承自HttpServlet的类,覆盖doGet()方法,内容是在网页上打印一些文本。
  8. 在项目的plugin.xml里添加下面的内容,这些内容指定了servlet的访问路径和实现类:



<extension point="org.eclipse.equinox.http.registry.servlets">
<servlet
alias="/exampleServlet"
class="net.bjzhanghao.osgi.example.servlet.ExampleServlet"/>
</extension>
  9. 重新启动项目,在浏览器里输入“http://localhost/exampleServlet”,应该可以看到servlet的输出。
  二、支持JSP页面
  10. 在index.html所在目录下创建一个简单的jsp文件index.jsp
  11. 打开项目的MANIFEST.MF文件,添加如下项目依赖:



org.eclipse.equinox.jsp.jasper,
org.apache.jasper,
org.eclipse.equinox.jsp.jasper.registry,
javax.servlet.jsp,
org.apache.commons.el,
org.eclipse.equinox.http.helper,
org.eclipse.osgi,
org.eclipse.osgi.services
  其中org.eclipse.equinox.http.helper需要从cvs里下载得到(目前是在/cvsroot/eclipse下的 equinox-incubator目录里,以后可能会直接放到/cvsroot/eclipse下)。
  12. 修改Activator,目的是注册一个处理扩展名为.jsp类型的servlet,感觉这一步以后应该有更简单的方法,例如通过扩展点。



public class Activator implements BundleActivator {
private ServiceTracker httpServiceTracker;
String jspContext = "/jsps";
String jspFolder = "/web_files";
public void start(BundleContext context) throws Exception {
httpServiceTracker = new HttpServiceTracker(context);
httpServiceTracker.open();
}
public void stop(BundleContext context) throws Exception {
httpServiceTracker.open();
}
private class HttpServiceTracker extends ServiceTracker {
public HttpServiceTracker(BundleContext context) {
super(context, HttpService.class.getName(), null);
}
public Object addingService(ServiceReference reference) {
final HttpService httpService = (HttpService) context
.getService(reference);
try {
HttpContext commonContext = new BundleEntryHttpContext(context
.getBundle(), jspFolder);
httpService.registerResources(jspContext, "/", commonContext);
Servlet adaptedJspServlet = new ContextPathServletAdaptor(
new JspServlet(context.getBundle(), jspFolder),
jspContext);
httpService.registerServlet(jspContext + "/*.jsp",
adaptedJspServlet, null, commonContext);
} catch (Exception e) {
e.printStackTrace();
}
return httpService;
}
public void removedService(ServiceReference reference, Object service) {
final HttpService httpService = (HttpService) service;
httpService.unregister(jspContext);
httpService.unregister(jspContext + "/*.jsp");
super.removedService(reference, service);
}
}
}
  13. 打开Debug对话框,选中workspace里的例子osgi项目和org.eclipse.equinox.http.helper项目,再按“Add Required Bundles”按钮,然后启动程序。
  14. 在浏览器里输入“http://localhost/jsps/index.jsp”,应该可以看到jsp输出。
  例子项目源代码下载(链接)。
  参考链接:


  • Embedding an HTTP server in Equinox
  • Writing a bundle-based server application
  • OSGi based JSP Support

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-348289-1-1.html 上篇帖子: JSPWiki安装指南 下篇帖子: (转载)Comet:基于 HTTP 长连接的“服务器推”技术
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表