import java.util.List;
public class MessageBean {
private String _id;
private String username;
private List<Integer> typeArry;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<Integer> getTypeArry() {
return typeArry;
}
public void setTypeArry(List<Integer> typeArry) {
this.typeArry = typeArry;
}
}
2.dao层
import java.util.List;
import org.jivesoftware.openfire.plugin.bean.MessageBean;
import org.jivesoftware.openfire.plugin.mongodb.MongDBManager;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
public class MessageMGManger {
private String _id;
private String username;
private List<Integer> typeArry;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<Integer> getTypeArry() {
return typeArry;
}
public void setTypeArry(List<Integer> typeArry) {
this.typeArry = typeArry;
}
final String DBNAME = "test";
public void add(){
DB db = MongDBManager.getDB(DBNAME);
DBCollection coll = db.getCollection("message");
DBObject statusObj = new BasicDBObject();
statusObj.put("_id", this._id);
statusObj.put("username", this.username);
statusObj.put("typeArry", this.typeArry);
coll.insert(statusObj);
}
public void add(MessageBean bean){
DB db = MongDBManager.getDB(DBNAME);
DBCollection coll = db.getCollection("message");
DBObject statusObj = new BasicDBObject();
statusObj.put("_id", bean.get_id());
statusObj.put("username", bean.getUsername());
statusObj.put("typeArry", bean.getTypeArry());
coll.insert(statusObj);
}
public org.jivesoftware.openfire.plugin.bean.MessageBean load(String _id){
DB db = MongDBManager.getDB(DBNAME);
DBCollection coll = db.getCollection("message");
DBObject dbo = coll.findOne(new BasicDBObject("_id","13"));
MessageBean bean = new MessageBean();
bean.set_id(dbo.get("_id").toString());
bean.setUsername(dbo.get("username").toString());
bean.setTypeArry((List<Integer>) dbo.get("typeArry"));
return bean;
}
}
import java.net.UnknownHostException;
import org.jivesoftware.util.Log;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class MongDBManager {
private static Mongo mongo = null;
private MongDBManager(){}
/**
* 根据名称获取DB(连接)
*
* @param dbName
* @return
*/
public static DB getDB(String dbName) {
if (mongo == null) {
init();
}
return mongo.getDB(dbName);
}
/**
* 初始化连接池,设置参数。
*/
private static void init() {
// String confFilePath = "";
String host = "127.0.0.1";// 主机名
int port = new Integer("27017");// 端口
// int poolSize = new Integer("20");// 连接数量
// int blockSize = new Integer("2"); // 等待队列长度
try {
mongo = new Mongo(host, port);
} catch (UnknownHostException e) {
Log.info("Couldn't connect to Mongo [/"+port+":"+port+"],UnknownHostException:"+e.getMessage());
Log.error(e);
} catch (MongoException e) {
Log.info("Couldn't connect to Mongo [/"+port+":"+port+"],MongoException:"+e.getMessage());
Log.error(e);
}
}
}
3.plugin:UserServiceBakPlugin
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.plugin.bean.MessageBean;
import org.jivesoftware.openfire.plugin.manager.MessageMGManger;
import org.jivesoftware.openfire.plugin.manager.PoemManager;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.PropertyEventDispatcher;
import org.jivesoftware.util.PropertyEventListener;
/**
* Plugin that allows the administration of users via HTTP requests.
*
* @author Justin Hunt
*/
public class UserServiceBakPlugin implements Plugin, PropertyEventListener {
private XMPPServer server;
private String title;
private String content;
private String author;
private String id;
private String username;
private PoemManager poemManager;
private MessageMGManger messageMGManger;
public void initializePlugin(PluginManager manager, File pluginDirectory) {
server = XMPPServer.getInstance();
poemManager = new PoemManager();
messageMGManger = new MessageMGManger();
id = JiveGlobals.getProperty("plugin.mongoServiceTest.id", "");
username = JiveGlobals.getProperty("plugin.mongoServiceTest.username", "");
title = JiveGlobals.getProperty("plugin.userservicebak.title", "");
content = JiveGlobals.getProperty("plugin.userservicebak.content", "");
author = JiveGlobals.getProperty("plugin.userservicebak.author", "");
PropertyEventDispatcher.addListener(this);
}
public void destroyPlugin() {
poemManager = null;
messageMGManger = null;
PropertyEventDispatcher.removeListener(this);
}
public void createPoem(String title,String content,String author){
poemManager.setAuthor(author);
poemManager.setContent(content);
poemManager.setTitle(title);
poemManager.savePoem();
}
public void createMessage(String _id,String username,List<Integer> typeArray){
org.jivesoftware.openfire.plugin.bean.MessageBean bean = new MessageBean();
bean.set_id(""+_id);
bean.setUsername(username);
if(typeArray!=null){
bean.setTypeArry(typeArray);
}else{
messageMGManger.setTypeArry(new ArrayList<Integer>());
}
bean.setTypeArry(new ArrayList<Integer>());
messageMGManger.add(bean);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
JiveGlobals.setProperty("plugin.userservicebak.title", title);
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
JiveGlobals.setProperty("plugin.userservicebak.content", content);
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
JiveGlobals.setProperty("plugin.userservicebak.author", author);
this.author = author;
}
public String getId() {
return id;
}
public void setId(String id) {
JiveGlobals.setProperty("plugin.mongoServiceTest.id", id);
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
JiveGlobals.setProperty("plugin.mongoServiceTest.username", username);
this.username = username;
}
public void propertySet(String property, Map<String, Object> params) {
if (property.equals("plugin.userservicebak.title")) {
this.title = (String)params.get("value");
}
else if (property.equals("plugin.userservicebak.content")) {
this.content = (String)params.get("value");
}
else if (property.equals("plugin.userservicebak.author")) {
this.author = (String)params.get("value");
}
else if (property.equals("plugin.mongoServiceTest.id")) {
this.id = (String)params.get("value");
}
else if (property.equals("plugin.mongoServiceTest.username")) {
this.username = (String)params.get("value");
}
}
public void propertyDeleted(String property, Map<String, Object> params) {
if (property.equals("plugin.userservicebak.title")) {
this.title= "";
}
else if (property.equals("plugin.userservicebak.content")) {
this.content = "";
}
else if (property.equals("plugin.userservicebak.author")) {
this.author = "";
}
}
public void xmlPropertySet(String property, Map<String, Object> params) {
// Do nothing
}
public void xmlPropertyDeleted(String property, Map<String, Object> params) {
// Do nothing
}
}
4.插件配置
plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<class>org.jivesoftware.openfire.plugin.UserServiceBakPlugin</class>
<name>User Service</name>
<description>Allows administration of users via HTTP requests.</description>
<author>Justin Hunt</author>
<version>1.3.0</version>
<date>10/12/2007</date>
<minServerVersion>3.3.0</minServerVersion>
<adminconsole>
<tab id="tab-server">
<sidebar id="sidebar-server-settings">
<item id="user-service" name="User Service" url="user-service.jsp"
description="Click to manage the service that allows remote admin of users" />
<item id="message-service" name="User Service" url="message-service.jsp"
description="MongoDB test" />
</sidebar>
</tab>
</adminconsole>
</plugin>
web-custom.xml
<?xml version='1.0' encoding='ISO-8859-1'?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- Servlets -->
<servlet>
<servlet-name>UserServiceBakServlet</servlet-name>
<servlet-class>org.jivesoftware.openfire.plugin.userServiceBak.UserServiceBakServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>MongoServiceTestServlet</servlet-name>
<servlet-class>org.jivesoftware.openfire.plugin.userServiceBak.MongoServiceTestServlet</servlet-class>
</servlet>
<!-- Servlet mappings -->
<servlet-mapping>
<servlet-name>MongoServiceTestServlet</servlet-name>
<url-pattern>/mongoServiceTest</url-pattern>
</servlet-mapping>
</web-app>
5.然后写测试页面,记得在lib目录中添加mongo的驱动。然后。。编译,打包。。。
大家多多指教,QQ:921471769