导入Openfire源码
插件开发在src/plugins目录下进行
插件结构整体结构
plugin.xml 定义插件属性:
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<!-- Main plugin class -->
<class>com.roger.openfire.plugin.TreeStructurePlugin</class>
<name>Structure</name>
<description>Get Roger System Structure Tree</description>
<author>bin.xiao</author>
<version>1.0.5</version>
<date>3/19/2013</date>
<minServerVersion>3.7.0</minServerVersion>
<!--<databaseVersion>0</databaseVersion> --> <!-- 是否检查数据库 -->
</plugin>
初始化pluhin:
public class TreeStructurePlugin implements Plugin {
private XMPPServer server;
private TreeStructureService groupService;
private TreeStructureHandler treeStructureHandler = new TreeStructureHandler();
@Override
public void initializePlugin(PluginManager manager, File pluginDirectory) {
server = XMPPServer.getInstance();
server.getIQRouter().addHandler(treeStructureHandler);
groupService = new TreeStructureServiceImpl();
}
@Override
public void destroyPlugin() {
server.getIQRouter().removeHandler(treeStructureHandler);
server = null;
groupService = null;
}
}
//处理该插件事件
public class TreeStructureHandler extends IQHandler {
private static final Logger logger = LoggerFactory.getLogger(TreeStructureHandler.class);
private static final String MODULE_NAME = "Get Tree Structure";
private static final String NAME_SPACE = "com:roger:structure";
private IQHandlerInfo info;
private TreeStructureService service;
public TreeStructureHandler() {
super(MODULE_NAME);
info = new IQHandlerInfo("structure", NAME_SPACE);
service = new com.roger.openfire.plugin.impl.TreeStructureServiceImpl();
}
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
IQ result = IQ.createResultIQ(packet);
Element param = packet.getChildElement();
// String scope = param.elementText("scope");
// if(scope == null){
// //不包含Scope节点
// result.setChildElement(param.createCopy());
// result.setError(PacketError.Condition.bad_request);
// return result;
// }
String from = packet.getFrom().toString();
System.out.println(from);
List<Structure> gpst = service.getUserPowerGroup(from);
// List<Structure> gpst = service.getGroupRootFormatXML();
Map<String , List<treeUser>> userMap = service.getAllCompanyTreeUser();
buildResult(param, gpst ,userMap);
result.setChildElement(param.createCopy());
logger.info(result.toXML());
return result;
}
Element root = null;
private Element buildResult(Element param, List<Structure> gpst, Map<String,List<treeUser>> userMap){
for(Structure group : gpst){
root = param.addElement("structureNode")
.addAttribute("id", group.getId().toString())
.addAttribute("name", group.getName())
.addAttribute("parent", group.getParentId().toString())
.addAttribute("type","group");
List<treeUser> treeUser = userMap.get(group.getId().toString()); //加载用户
if(treeUser != null){
for (treeUser user : treeUser) {
root.addElement("userNode")
.addAttribute("jid", user.getLoginName())
.addAttribute("name", user.getUserName())
.addAttribute("companyId" , user.getCompanyId() + "")
.addAttribute("email", user.getEmail())
.addAttribute("sex", user.getSex())
.addAttribute("tel", user.getTel())
.addAttribute("type","user")
.addAttribute("company",group.getName());
}
}
if(group.getGroupRoot().isEmpty() && group.getGroupRoot().size() ==0){
continue;
}else{
buildResult(root, group.getGroupRoot(),userMap);
}
}
return root;
}
@Override
public IQHandlerInfo getInfo() {
return info;
}
}