一、openfire的Eclipse环境搭配
1、官网http://www.igniterealtime.org/
2、Eclipse环境(包括JavaSE),Openfire的源代码包(例如:openfire_src_3_3_2.zip,可到jivesoftware官方网站下载),Mysql数据库V5以上(当然可以用其他数据库,但在此例中选用Mysql作为Openfire的后台数据库)。
3、将openfire_src_3_3_2.zip的源码包解压到一个目录里(包含四个子文件夹)
4、 其中有个 src目录:这个src文件夹就是我们想要的Openfire源代码了
5、在Eclipse中将此Java文件夹当成源代码文件夹新建工程,建立新工程后观察Openfire所import的包,发现Openfire用了许多开源工具,而这些库都在上文提到的Build目录的lib目录下(也可以到网上下载),将Openfire用到的库都添加到这个新建工程的Referenced Libraries中。或者如果这样一个一个添加比较麻烦的话还有一个方法就是从你安装好的Openfire服务器目录下的lib目录中(例如:”D:/Program Files/Openfire/lib”或 “/usr/local/openfire/lib”)找到openfire.jar,打开后将里面的org/jivesoftware删除,重新打包成jar并添加到这个新建工程的Referenced Libraries中,这样省去了很多工作。
6、openfire的起始类为org.jivesoftware.openfire.starter.ServerStarter.java 但是直接运行此类却有问题,因为此类是针对Openfire安装包而设计的,此类的功能是将所用到的Jar文件解压并将class文件加载到虚拟机中,而我们要用的却是源代码中我们自己编译好的class文件,所以我们需要一个新的启动类,一个简单的实现如下(当然最好是与ServerStarter.java中的方法一样,用自定义的ClassLoader来将XMPPServer.class加载到虚拟机中):
package org.jivesoftware.openfire.starter;
import org.jivesoftware.openfire.XMPPServer;
public class StandaloneStarter {
public static void main(String[] args) {
XMPPServer server = new XMPPServer();
}}
7、还有一个关键的一步是Openfire的Home设定
在XMPPServer类中有一个locateOpenfire方法,这个方法就是设置openfireHome属性。
具体修改如下:
// and just look for home in a standard sub-dir location and verify
// by looking for the config file
if (openfireHome == null) {
try {
//修改的是下面的代码,将".."替换为其他路径了
openfireHome = verifyHome("C://Program Files//Openfire", jiveConfigName).getCanonicalFile();
}
catch (FileNotFoundException fe) {
// Ignore.
}
catch (IOException ie) {
// Ignore.
}
}这部分默认是找当前路径,你可以修改它为你安装Openfire的路径,这样问题就解决了
二、openfire的svn源码下载
1、下载路径:
http://svn.igniterealtime.org/svn/repos/openfire/trunk
2、结点类包
起点 org.jivesoftware.openfire.starter.ServerStarter
服务器 org.jivesoftware.openfire.XMPPServer
用户验证包 里面有AuthProvier接口、几个实现类。。。
org.jivesoftware.openfire.auth
用户 org.jivesoftware.openfire.user
组 org.jivesoftware.openfire.group
好友列表org.jivesoftware.openfire.roster
开发插件会用到的接口和包
org.jivesoftware.openfire.container.Plugins
org.xmpp.component.Component
org.jivesoftware.openfire.event
org.jivesoftware.openfire.handler
拦截器
org.jivesoftware.openfire.interceptor、
三、openfire的Eclipse详细配置和编译
1. 从现有资源新建项目
(1) File->New->Java Project
(2) 在弹出的New Java Project对话框中输入工程名:openfire_src
(3) 选择Create project from existing source项,单击Browe,找到之前的解压目录 openfire_src
2. 编译工程
openfire是用ant构建的,所以要先设置使得eclipse中每次编译都使用内置的ant工具,保证编译的正确执行。同时,要在Eclipse中设置为使用JDK作为JVM,如果使用JRE作为JVM,编译就会失败。
因为openfire在Windows下是使用install4j打包成.exe格式的安装文件的,所以需要安装install4j。否则,在使用ant 进行编译的时候,在ant视图中可能会出现警告如下信息:taskdef class com.install4j.install4jtask cannot found。这主要是要安装用来打包Java应用程序的install4j.exe程序,而且将要安装到其默认的安装路径C:/Program Files/install4j下。
(1) 在eclipse中点击Window->Show View->Ant,打开Ant窗口
(2) 在Ant 视图中,单击右键,在弹出的快捷菜单中选择Add Buildfiles..
(3) 在弹出的对话框中,展开openfire_src/build文件夹,选择build目录下的build.xml,点击OK
(4) 在Ant视图中, 展开Openfire XMPP Server,双击 openfire_src(default)编译工程
(4) 如果在Console视图中显示“BUILD SUCCESSFUL”就表示编译成功
3. 创建项目Builder
(1) 在eclipse的菜单栏中,选择Run->Open Run Dialog...,在弹出的对话框左侧的树形结构中选择Java Application,单击右键,选择New创建启动配置
(2) 在Run窗口的Main选项卡中, 修改Name文本框中的值,改成包含要启动的类的工程名openfire_src
(3) 在Run窗口的Main选项卡中,点Browse按钮,选择openfire_src
(4) 在Run窗口的Main选项卡中,点Search按钮,选择Main class为org.jivesoftware.openfire.starter.ServerStarter,单击Apply按钮。(这是 openfire的启动类)
(5) 点击进入Arguments选项卡,在VM arguments文本框中输入
-DopenfireHome="${workspace_loc:openfire}/target/openfire"
单击Apply按钮。
这个是用于eclipse执行java命令时传递的参数,这样openfire程序可以通过 System.getProperty(“openfireHome”)得到openfire的本地位置。
(6) 点击进入Classpath选项卡,选中User Entries,这样Advanced...就处于可用状态;点击Advanced...按钮,在Advanced Options页面,选择Add Folders, 单击OK。(默认情况下,已经将工程openfire添加到了这里,而不需要进行该项操作,如果有多个工程的时候才需要执行该项操作。)选择 openfire_src/src/i18n, 点OK按钮将这个文件夹加入到Classpath选项卡中;同样的方式把openfire_src/src/resources/jar目录也加到 Classpath选项卡中。
(7) 在Common选项卡中,勾选Run复选框,单击Apply按钮。
设置完毕,这样以后在run这个工程的时候就会按照正确的配置进行了,debug的设置和run的设置类似,不用多说。
4. 运行并测试编译结果(openfire服务器)
(1) 在Eclipse菜单中,单击Run->Run(或快捷方式Ctrl+F11),在Console试图中出现如下所示的信息。
(2) 在Web浏览器中,输入http://127.0.0.1:9090/,如果可以出现配置的openfire的页面,就表示openfire编译、运行成功。
5. 自己定制openfire服务器时,在Eclipse对openfire源代码进行部署
openfire 的起始类为org.jivesoftware.openfire.starter.ServerStarter.java,但是直接运行此类却有问题,因为此类是针对Openfire安装包而设计的,此类的功能是将所用到的JAR文件解压并将class文件加载到虚拟机中,而我们要用的却是源代码中我们自己编译好的class文件。所以,我们需要一个新的启动类。
package org.jivesoftware.openfire.starter;
import org.jivesoftware.openfire.XMPPServer;
public class StandaloneStarter {
public static void main(String[] args) {
XMPPServer server = new XMPPServer();
}
}
这样程序就可以跑起来了,最后的问题就是配置文件路径的问题。
a) 在StandaloneStarter类的启动选项(VM arguments)中加入-DopenfireHome="${workspace_loc:openfire}/target/openfire",具体方法参见上文。
b) 点击进入Classpath选项卡,选中User Entries,这样Advanced...就处于可用状态;点击Advanced...按钮,在Advanced Options页面,选择Add Folders, 单击OK。(默认情况下,已经将工程openfire添加到了这里,而不需要进行该项操作,如果有多个工程的时候才需要执行该项操作。)选择 openfire_src/src/i18n, 点OK按钮将这个文件夹加入到Classpath选项卡中;同样的方式把openfire_src/src/resources/jar目录也加到 Classpath选项卡中。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wbw1985/archive/2010/04/15/5484350.aspx