一步步使用equinox建立服务器端应用(osgi)
原文地址:http://www.eclipse.org/equinox-portal/tutorials/server-side/
?
自己随便翻译一下主要为了练练英语,如果不对的欢迎指正。
?
使用eclipse equinox建立服务器端应用相对比较容易,建立一个非常简单的基于java servlets的应用是认识equinox建立服务端应用潜力的第一步。这个一步步建立服务端应用的向导,仅仅是使用equinox一种建立服务端应用的一个使用java servlets的一个例子。
?
为RCP/plug-in 开发安装配置eclipse。
Ganymede M5 或者之后的版本都可以运行本实例,你可以下载 EPP Ganymede Milestone Package Builds page.
?
帮助信息在 Eclipse for RCP/Plug-in Developers page.
?
?
?
运行eclipse,建立一个新的plug-in 项目(File > New > Project > Plug-in Project).
如下的向导页面,输入项目名称"org.eclipse.examples.servlet","Target Platform"选eclipse3.4.
?
第二页不要选择plugin-in options ,避免我们后面配置一些不需要的示例。
?
?
进入"Extensions"页面增加扩展org.eclipse.equinox.http.registry.servlets.(但是在我的galieo版本不是如下显示,建议直接编辑plugin.xml文件)
?
?
在"Extension Details" area,填入类名org.eclipse.examples.servlets.HelloServlet和你访问servlet的别名; 例如 "http://localhost/hello", 我们使用的别名"/hello"
?
override doGet方法,代码如下:
?
?
?
下面我们来运行它,你可以右键项目 "Run As... > OSGi Framework",当你运行你的bundle时,默认的机制会加载很多不需要的bundle。
当你在eclipse里运行程序时,经常需要配置launch configuration ,很多时候eclipse是自动配置的,所以很多人不一定用到。
建立一个launch configuration是比较容易的,点击‘Run--Configurations’
?
?
在Run Configurations对话框,如图配置,确定选中的bundle:org.eclipse.examples.servlet,
org.eclipse.equinox.http.jetty,然后点击“
Add Required Bundles”添加传递的依赖。然后按'Run'
?
?
如果你是linux下开发,可能会有权限限制你使用1024以下的端口,这里默认为80,所以你可以修改端口。
?
?
如果没有log诊断问题是个很大的挑战,当你运行一个equinox应用,log文件存放在 [workspace]/.metadata/.plugins/org.eclipse.pde.core/Equinox Servlet 目录,你也可以在你的launch configration的 "Program Arguments" 添加-consoleLog 参数,在控制台打出log.
org.mortbay.util.MultiException[java.net.BindException: Permission denied] 输出的日志表明了http server 没有权限绑定需要的端口。
?
端口设置方法如图:
-Dorg.eclipse.equinox.http.jetty.http.port=8080
?
?
开始运行后,你可以访问servlet,"http://localhost:8080/hello".
你可能会看到日志org.mortbay.util.MultiException[java.net.BindException: Address already in use],表明你的端口被占用了,你需要换一个。
?
建议新的plug-in project