壹佰网|ERP100 - 企业信息化知识门户

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1963|回复: 0

[Domino管理] 通过JAVA代理查询Domino服务器用户在线人数

[复制链接]
发表于 2012/9/11 13:39:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。如果您注册时有任何问题请联系客服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;
  }
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|手机版|壹佰网 ERP100 ( 京ICP备19053597号-2 )

Copyright © 2005-2012 北京海之大网络技术有限责任公司 服务器托管由互联互通
手机:13911575376
网站技术点击发送消息给对方83569622   广告&合作 点击发送消息给对方27675401   点击发送消息给对方634043306   咨询及人才点击发送消息给对方138011526

GMT+8, 2025/11/29 01:39 , Processed in 0.010887 second(s), 14 queries , File On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表