文章目录



作用:初始化时在控制台打印测试消息


开发环境win7/eclipse/jdk1.6/mysql5.0/openfire3.7


 


开发步骤


1. 布署openfire3.7源码 部署方法链接:

Openfire源代码部署




 




~~
~~
2. src/plugins目录下新建文件夹chatHistory。并按下图的目录结构新建对应的文件

 






 


plugin.xml文件代码如下:




1. <?xml version="1.0" encoding="UTF-8"?>

<plugin>

<class>com.d3.xmpp.ChatHistoryPlugin</class>

<name>chatHistroyRecord</name>

    <description>save chatRecord</description>

    <author>mzh</author>

    <version>1.0.0</version>

    <date>23/08/2013</date>

    <databaseKey>chatHistory</databaseKey>

    <databaseVersion>0</databaseVersion>

    <minServerVersion>3.7.0</minServerVersion>

    

    <adminconsole>

        <tab id="tab-users">

<sidebar id="sidebar-users">

<item id="chathistory" name=""

url="chatHistory.jsp"

description="chatRecord" />

</sidebar>

        </tab>

    </adminconsole>

</plugin>
2.



ChatHistoryInterceptor.java文件代码如下

 





1. public class ChatHistoryInterceptor implements PacketInterceptor {

private static Connection con = null;



@Override

public void interceptPacket(Packet packet, Session session,

boolean incoming, boolean processed) throws PacketRejectedException {

if (processed || !(packet instanceof Message) || !incoming || Message.Type.chat != ((Message) packet).getType())

return;

PreparedStatement pstmt = null;

try

        {

            if(con == null || con.isClosed())

                con = DbConnectionManager.getConnection();

            pstmt = con.prepareStatement("INSERT INTO ofChatHistory (fromJID, toJID, message, sendtime) VALUES (?, ?, ?, ?)");

            pstmt.setString(1, ((Message)packet).getFrom().toBareJID());

            pstmt.setString(2, ((Message)packet).getTo().toBareJID());

            pstmt.setString(3, ((Message)packet).getBody());

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String ly_time = sdf.format(new Date());

            pstmt.setString(4, ly_time);

            pstmt.executeUpdate();

        }

        catch(Exception ex)

        {

            System.out.println(ex.getMessage());

            DbConnectionManager.closeConnection(pstmt, con);

            con = null;

        }

}

}
2.


最后输出语句“测试插件启动成功”在控制台显示乱码,将此改为""


ChatHistoryPlugin.java文件代码如下:



1. public class ChatHistoryPlugin implements Plugin {



private ChatHistoryInterceptor chatHistoryInterceptor = null; 

@Override

public void destroyPlugin() {

if(chatHistoryInterceptor != null){

InterceptorManager.getInstance().removeInterceptor(chatHistoryInterceptor);

}

}

@Override

public void initializePlugin(PluginManager manager, File pluginDirectory) {

chatHistoryInterceptor = new ChatHistoryInterceptor();

InterceptorManager.getInstance().addInterceptor(chatHistoryInterceptor);

}

}
2.


3.打包插件


a.第一次打包需配置ant工具。在环境变量中

添加 path=C:Program FilesJavajdk1.5.0_09;D:Program FilesMyEclipse6.5eclipsepluginsorg.apache.ant_1.7.0.v200706080842bin (如果原来有path,在变量值的尾部添加半角分号 ; 然后再加入上面的jdk和ant路径 )

b.编译

进入cmd,进入 openfire/build所在目录:输入命令:ant plugins 等待执行完,而且没有报错,说明打包成功。在openfire3.6.3targetopenfireplugins目录下就生成了chathistory.jar


4.测试

启动openfire ,如发现控制台输出“测试插件启动成功”,说明插件开发成功。

 

 

注:这是第一次开发插件,纠结中成功!但是控制台没有打印出“测试插件启动成功”信息!待研究。。。

问题解决:

主要是控制台不打印汉字,改为英语,一切OK!以下是运行效果:





文章目录