PDA

View Full Version : 如何实现BS的在线统计


kadahai
08-12-06, 02:59 PM
在Lotus BS中和实现一个在线用户的统计,Domino65环境,请高手赐教!

xdw076
08-12-06, 03:32 PM
(Java代理)查询Domino服务器在线人数
import lotus.domino.*;
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;

public class JavaAgent
extends AgentBase {

private static final String CONSOLE_COMMAND =
"Show Inetusers -xml >HttpUsers.xml";
private static final String OUTPUT_FILE_NAME = "HttpUsers.xml";

public void NotesMain() {

try {
Session session = getSession();

//取Domino服务器版本,版本低于6.5则退出
String dominoVersion = session.getNotesVersion();
if (dominoVersion.indexOf("6.5") < 0) {
return;
}

AgentContext agentContext = session.getAgentContext();
Database curDB = agentContext.getCurrentDatabase();
String server = curDB.getServer();

// (Your code goes here)
//接收控制台返回的字符串
String returnString = session.sendConsoleCommand(server, CONSOLE_COMMAND);
if (returnString.equals("") || returnString.indexOf("<") < 0 ||
returnString.indexOf(">") < 0) {
return;
}

//输出的文件存在则先删除
File file = new File(OUTPUT_FILE_NAME);
if (file.exists()) {
file.delete();
}

//将控制台返回的字符串写到文件中
FileOutputStream fos = new FileOutputStream(file);
fos.write(returnString.getBytes());
fos.flush();
fos.close();

//分析XML文件
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.
newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
org.w3c.dom.Document document = builder.parse(file);
Element rootElement = document.getDocumentElement();

NodeList userDataList = rootElement.getElementsByTagName("userdata");
if (userDataList == null || userDataList.getLength() == 0) {
return;
}

Node userDataNode = null;
Node userNameNode = null;
for (int i = 0; i < userDataList.getLength(); i++) {
userDataNode = userDataList.item(i);
String netaddress = getNodeProperty(userDataNode, "netaddress");
String login = getNodeProperty(userDataNode, "login");
login = stringToDateTimeString(login);

String expires = getNodeProperty(userDataNode, "expires");
expires = stringToDateTimeString(expires);

userNameNode = userDataNode.getChildNodes().item(1);
if (userNameNode == null) {
continue;
}
if (!userNameNode.hasChildNodes()) {
continue;
}

String userName = getNodeValue(userNameNode);
String printString = "用户名=" + userName + ", 地址=" + netaddress +
", 登陆时间=" + login + ", 过期时间=" + expires;
System.out.println(printString);
}

}
catch (Exception e) {
e.printStackTrace();
}
}

public String getNodeValue(Node node) {
Node firstNode = node.getFirstChild();
if (firstNode == null) {
return "";
}

return firstNode.getNodeValue();
}

public String getNodeProperty(Node node, String propertyName) {
NamedNodeMap namedNodeMap = node.getAttributes();
if (namedNodeMap == null || namedNodeMap.getLength() == 0) {
return "";
}

for (int i = 0; i < namedNodeMap.getLength(); i++) {
Node childNode = namedNodeMap.item(i);
if (childNode.getNodeName().equals(propertyName)) {
return childNode.getNodeValue();
}
}

return "";
}

public String stringToDateTimeString(String string) {
int pos = string.indexOf("T");
String single = string.substring(0, 4);
single += "年";
single += string.substring(4, 6);
single += "月";
single += string.substring(6, 8);
single += " ";

String sHour = string.substring(pos + 1, pos + 3);
String sMinute = string.substring(pos + 3, pos + 5);
String sSecond = string.substring(pos + 5, pos + 7);

single += sHour + ":" + sMinute + ":" + sSecond;
int hour = Integer.parseInt(sHour);

if (hour < 12) {
single += " 早上";
}
else {
single += " 下午";
}

return single;
}
}

这个论坛找的!!

kadahai
14-12-06, 04:55 PM
有没有LS的在线统计