研究并用JAVA操作DOMINO的mtcstore.nsf数据库

时间:2022-10-30 19:14:18
首先我们要注意:
   要想得到相应的数据库, 数据库的路径必须是相对于Data目录下的 mtdata/mtstore.nsf文件。
     如果你要操作songlb这个用户的邮箱数据库那么就要指定mail/songlb.nsf这个文件目录。

以下是mtcstore.nsf的数据结构及含义


/**
 * 读取mtcstore.nsf数据使用的数据库接口
 *
 * @author songlb
 *
 * -----原始结构:---- 0DISPSTAT------[4.0] 1DISPTM------[2005-09-02 15:51:28 ZE8]
 * 2MTA------[ULTRAPOWER/ULTRANMS] 3NEXTMTA------[`z] 4PREVMTA------[`z]
 * 5NODELREA------[`z] 6MSGTM------[2005-09-02 15:51:23 ZE8]
 * 7MSGSZ------[1544.0] 8UNQMSGID------[OF46892529:11E0D26D ON48257070:002B2802]
 * 9INMSGID------[OFFC2465D0.7ED6D035-ON4825706F.0029D02C-4825706F.002A36F1]
 * 10OUTMSGID------[OFFC2465D0.7ED6D035-ON4825706F.0029D02C-4825706F.002A36F1]
 * 11INORG------[Mail Router] 12OUTORG------[Mail Router]
 * 13INRCP------[wanghp/ultranms@CHANGESELF]
 * 14OUTRCP------[wanghp/ultranms@CHANGESELF] 15SUBJ------[test]
 * 16MSGTYPE------[8.0] 17Form------[($MTRESP)] 18NUMENTRIES------[1.0]
 * 19EARLIESTMSGTM------[2005-09-02 15:51:23 ZE8] 20LATESTMSGTM------[2005-09-02
 * 15:51:23 ZE8] 21$UpdatedBy------[CN=ultrapower/O=ultranms]
 *
 * =====使用结构:===== [sender]:OUTORG [receiver]:OUTRCP [title]:SUBJ
 * [reason]:NODELREA [status]:DISPSTAT [time]:LATESTMSGTM [msgid]:UNQMSGID
 *
 *
 *
 * 其中:DISPSTAT: 1=unknown 2=inqueue 4=delivered 8=delivery_failed
 *
 * 16=transferred
 *
 * 32=transfer_failed 64=group_expanded 1073741824=deleted
 *
 * MSGTYPE: 1=unknown 2=memo 4=deliveryreport 8=nondeliveryreport
 * 16=returnreceipt 32=phonemessage 64=tracereport 128=notice

 */


以下是我启动MTC服务后,运行的一监控程序,不过现在没有环境不能保证程序的正确性,但是其中的原理可以给大家借鉴一下。



public class TraceMailDAO {

    private static String user = "songlb";

    private static String passwd = "123456789";

    static Logger log = Logger.getLogger("TraceMailDAO");

    private HashMap persons = new HashMap(); //要跟踪的用户集合

    /**
     * 得到相应的数据库 数据库的路径是相对于Data目录下的 mtdata/mtstore.nsf
     *
     * @param accessDB
     * @return
     * @throws NotesException
     */
    public static Database getDB(String accessDB) throws NotesException {

        lotus.domino.Session s = lotus.domino.NotesFactory.createSession(
                JDBCConstants.DOMINOHOST, user, passwd);

        log.info("Domino服务器主机名:" + s.getServerName() + "  版本号:"
                + s.getNotesVersion());

        lotus.domino.Database db = s.getDatabase(null, accessDB);
        if (!db.isOpen()) {
            db.open();
        }

        //打出数据库的标题和大小
        //System.out.println("title:" + db.getTitle() + " size:" +
        // db.getSize());
        return db;
    }

    /**
     * 取mcstore.nsf数据库的alluser表的数据 是对每天Mail Router 系统退信进行筛选。 1、程序每天只取本天的退信
     * 2、取得跟踪的人员的邮件
     */
    public List getMTCDatas() {

        ArrayList ls = new ArrayList();
        try {

            Database db = TraceMailDAO.getDB(JDBCConstants.DB);//得到数据库

            View view = (View) db.getView(JDBCConstants.DBVIEW);//取得自己想要的MAIL视图

            Document doc = view.getFirstDocument();
            Vector columnNames = view.getColumns(); //得到所有列名
            Nsfmail entity = null;
            boolean flag = false;

            while (doc != null) {
                entity = new Nsfmail();

                Vector values = doc.getColumnValues();
                String tmp = null;
                String tmpStr = "";

                for (int j = 0; j < columnNames.size(); j++) {
                    String columnName = ((String) columnNames.elementAt(j)
                            .toString()).trim();

                    if (columnName.length() != 0) {
                        //log.info(values.get(j).getClass());
                        //ViewColumn column = (ViewColumn)
                        // columnNames.elementAt(j);
                        if (columnName.equals("sender")) {

                            if(values.get(j) instanceof Vector){
                                tmp = ((Vector) values.get(j)).toString();
                                /**
                                 * //1、判断是系统退信
                                 */
                                if (tmp.indexOf("Mail Router") > -1) {
                                } else {
                                    //flag = false;
                                   // break;
                                }
                                entity.setSender(tmp);
                            }else{
                                tmp=(String) values.get(j);
                            }

                        } else if (columnName.equals("receiver")) {
                            if(values.get(j) instanceof Vector){
                                tmp = ((Vector) values.get(j)).toString();
                            }else{
                                tmp=(String) values.get(j);
                            }
                            /**
                             * //2、判断是否是要跟踪的用户
                             */
                            if (persons.containsKey(tmp)) {
                            } else {
                               // flag = false;
                               // break;
                            }
                            entity.setReceiver(tmp);

                        } else if (columnName.equals("title")) {
                            if(values.get(j) instanceof Vector){
                                tmp = ((Vector) values.get(j)).toString();
                            }else{
                                tmp=(String) values.get(j);
                            }
                            entity.setTitle(tmp);
                        } else if (columnName.equals("reason")) {
                            if(values.get(j) instanceof Vector){
                                tmp = ((Vector) values.get(j)).toString();
                            }else{
                                tmp=(String) values.get(j);
                            }
                            entity.setReason(tmp);
                        } else if (columnName.equals("status")) {
                            if(values.get(j) instanceof Vector){
                                tmp = ((Vector) values.get(j)).toString();
                            }else{
                                tmp=String.valueOf(values.get(j));//单一是Double类型
                            }

                            /**
                             * 3、判断是不是传送失败或邮递失败的邮件
                             */
                            if (tmp.indexOf(JDBCConstants.DELIVERY_FAILED) > -1
                                    || tmp
                                            .indexOf(JDBCConstants.TRANSFER_FAILED) > -1) {
                            } else {
                               flag = false;
                               // break;
                            }
                            entity.setStatus(tmp);
                        } else if (columnName.equals("time")) {
                            tmpStr = String.valueOf((DateTime) values.get(j));
                            tmpStr = tmpStr.substring(0, 19);

                            /**
                             * //4、判断是否是今天的退信信息
                             */
                            String nowTime = DateUtil.timesLong2Date(new Date()
                                    .getTime());
                            String[] rt = nowTime.split(" ");
                            nowTime = rt[0] + " 00:00:00";
                            long t1 = DateUtil.StringToDate(nowTime).getTime();
                            //log.info("t1:"+t1);
                            //log.info("tD1:"+DateUtil.StringToDate(tmp));
                            nowTime = rt[0] + " 23:59:59";
                            long t2 = DateUtil.StringToDate(nowTime).getTime();

                            long t3 = (DateUtil.StringToDate(tmpStr)).getTime();
                            if (t3 >= t1 && t3 <= t2) {
                            } else {
                               // flag = false;
                               // break;
                            }

                            entity.setTime(t3);
                        } else if (columnName.equals("msgid")) {
                            if(values.get(j) instanceof Vector){
                                tmp = ((Vector) values.get(j)).toString();
                            }else{
                                tmp=(String) values.get(j);
                            }
                            entity.setMsgid(tmp);
                        }

                    }

                }

                if (flag) {
                    ls.add(entity);
                    log.info("888888");
                }
                doc = view.getNextDocument(doc);
                //   }

                System.out.println("");
            }



        } catch (lotus.domino.NotesException e) {
            e.printStackTrace();

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

        }

        return ls;

    }

    public static void main(String[] args) {
        TraceMailDAO dao = new TraceMailDAO();

        ArrayList ls=(ArrayList)dao.getMTCDatas();
        for(int i=0;i<ls.size();i++){
            log.info(ls.get(i).toString());
        }

    }
}