|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。如果您注册时有任何问题请联系客服QQ: 83569622 。
您需要 登录 才可以下载或查看,没有帐号?注册
x
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;
}
} |
|