⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mailreport.java.040128

📁 java编写的OCR软件
💻 040128
字号:
/*
    Jacson
    Copyright (C) 2003 Patrick Carl, patrick.carl@web.de
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
    $Id: $
 
 */

package de.spieleck.app.jacson.report;


import de.spieleck.app.jacson.JacsonConfigException;
import de.spieleck.app.jacson.JacsonException;

import de.spieleck.config.ConfigNode;
import de.spieleck.app.jacson.util.ConfigUtil;
import de.spieleck.util.StringUtil;
import de.spieleck.util.Mailer;

import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Set;


/**
 * Report which sends a mail. Since it is a sub class of PrintingReport
 * it prints it output in the same way, only sending the text via mail 
 * to configurable receivers.
 * The report uses a file to cache its content or stores the 
 * contents in memory. This depends whether the filename config parameter
 * is given or not.
 * @author  pcs_org
 */
public class MailReport extends PrintingReport {
    
    // used config nodes
    public final static String SMTP_SERVER_HOST_NODE = "smtphost";
    public final static String SMTP_SERVER_PORT_NODE = "smtpport";
    public final static String MAIL_USER_NAME_NODE = "user";
    public final static String MAIL_PASSWORD_NODE = "password";
    public final static String MAIL_SENDER_NODE = "sender";
    public final static String MAIL_RECEIVERS_NODE = "receivers";
    public final static String MAIL_SUBJECT_NODE = "subject";
    
    
    // default values
    public final static String DEFAULT_SMTP_SERVER_HOST = "localhost";
    public final static int DEFAULT_SMTP_SERVER_PORT = 25;
    public final static String DEFAULT_MAIL_USER = null;
    public final static String DEFAULT_MAIL_PASSWORD = null;
    public final static String DEFAULT_MAIL_SENDER = "Jacson@do.not.reply";
    
    
    private String smtpServerHost;
    private int smtpServerPort;
    private String sender;
    private String user;
    private String password;
    private String receivers;
    private String subject;
    private boolean caching;
    
    private ByteArrayOutputStream out;
    private String fileName;
    
    /** Creates a new instance of MailReport */
    public MailReport() {
        super();
    }
    
    /**
     * finishes the Report and sends a mail
     */ 
    public void finish(boolean close) {
        System.out.println("MailReport finish start");
        super.finish(close);
        String text;
        try{
            text = getMessageText();
        } catch(JacsonException je){
            je.printStackTrace();
            return;
        }
        Set receivers = StringUtil.getTokensAsSet(this.receivers, ';');
        String[] to = (String[]) receivers.toArray(new String[]{});
        try{
        Mailer.sendMail(sender, to, subject, getMessageText(), 
        smtpServerHost, smtpServerPort, user, password);
        } catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("MailReport finish end");
        
        
        // now let's do something for mailing!
    }
    
    
    /**
    * returns the text of the message
    **/
    private String getMessageText() throws JacsonException{
        // if we cached the message text we have to reload it
        if(caching){
            try{
            FileReader in = new FileReader(fileName);
            StringBuffer sb = new StringBuffer(1024);
            char[] c = new char[1024];
            while(in.read(c) >= 0)
                sb = sb.append(c);
            return sb.toString();
            } catch(IOException ie){
                throw new JacsonException("An error occured during reading" 
                + "cached file " + fileName, ie);
            }
        } else
            return out.toString();
    }
    
    public void init(ConfigNode config) throws JacsonConfigException {
        // I'd prefer super.init(config) but since the PrintingReport does not
        // know about the configuration parameters of a MailReport
        // it would not work :(
        
        // but I thought it should work because of polymorphism
        
        System.out.println("MailReport init start");
        super.init(config);
        receivers = config.getString(MAIL_RECEIVERS_NODE, null);
        if(receivers == null)
            throw new JacsonConfigException("MailReport needs a receiver.");
        sender = config.getString(MAIL_SENDER_NODE, DEFAULT_MAIL_SENDER);
        smtpServerHost = config.getString(SMTP_SERVER_HOST_NODE,
        DEFAULT_SMTP_SERVER_HOST);
        smtpServerPort = config.getInt(SMTP_SERVER_PORT_NODE,
        DEFAULT_SMTP_SERVER_PORT);
        user = config.getString(MAIL_USER_NAME_NODE, DEFAULT_MAIL_USER);
        password = config.getString(MAIL_PASSWORD_NODE, DEFAULT_MAIL_PASSWORD);
        subject = "Jacson Mail Report: " +
        config.getString(MAIL_SUBJECT_NODE, "");
        ConfigUtil.verify(config, this);
        System.out.println("MailReport init end");
    }
    
    
        
    public boolean accept(ConfigNode node) {
        if(node == null)
            return false;
        String name = node.getName();
        System.out.println(this + ":" + name);
        return (super.accept(node) ||
        name.equals( SMTP_SERVER_HOST_NODE)
        || name.equals(SMTP_SERVER_PORT_NODE)
        || name.equals(MAIL_PASSWORD_NODE) 
        || name.equals(MAIL_SENDER_NODE) || name.equals(MAIL_SUBJECT_NODE)
        || name.equals(MAIL_RECEIVERS_NODE) || name.equals(MAIL_USER_NAME_NODE));
    }
    
    protected void useFilename(ConfigNode config)
    throws JacsonConfigException {
        fileName = config.getString(FILENAME_NODE, null);
        caching = (fileName != null);
        if(!caching)
            out = new ByteArrayOutputStream(1024);
        useFilename(this, config, out);
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -