001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.plugin.daemon;
017
018import org.opengion.fukurou.system.OgRuntimeException ;         // 6.4.2.0 (2016/01/29)
019import org.opengion.hayabusa.common.HybsSystem;
020import org.opengion.hayabusa.report.GE50Access;
021import org.opengion.fukurou.system.LogWriter;
022import org.opengion.fukurou.util.StringUtil;
023import org.opengion.fukurou.system.ThrowUtil;                                                   // 6.4.2.0 (2016/01/29)
024import org.opengion.fukurou.util.HybsTimerTask;
025
026import org.opengion.fukurou.mail.MailReceiveListener ;
027import org.opengion.fukurou.mail.MailRX ;
028
029import java.util.Date;
030
031/**
032 * 【メールデーモン】メールサーバーを監視して、EXCELファイルのDB登録処理のデーモンです。
033 * 指定の条件でメールサーバーを監視し、添付のEXCELファイルを取り出します。
034 * 添付ファイルは、EXCEL取り込みインターフェースに則り、要求番号.xls にリネームして
035 * 所定のディレクトリ(EXCEL_IN_FILE_URLで定義)にセーブし、帳票要求テーブルに登録します。(GE50)
036 * それ以降の処理は、EXCEL帳票デーモン以下の共通処理で処理されます。
037 * EXCEL取り込み処理は、このメール自動取り込みと、ファイルアップロードの方式を
038 * サポートしています。
039 *
040 * @og.rev 3.8.0.0 (2005/06/07) 新規追加
041 * @og.rev 4.3.4.4 (2009/01/01) プラグイン化
042 * @og.group デーモン
043 *
044 * @version  4.0
045 * @author   Kazuhiko Hasegawa
046 * @since    JDK5.0,
047 */
048public class Daemon_MailReceive extends HybsTimerTask {
049        /** このプログラムのVERSION文字列を設定します。   {@value} */
050        private static final String VERSION = "6.4.2.0 (2016/01/29)" ;
051
052        private static final int LOOP_COUNTER = 24;             // カウンタを24回に設定
053
054        private int             loopCnt         ;
055        private MailRX  mailRX          ;
056
057        /**
058         * デフォルトコンストラクター
059         *
060         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
061         */
062        public Daemon_MailReceive() { super(); }                // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
063
064        /**
065         * このタイマータスクによって初期化されるアクションです。
066         * パラメータを使用した初期化を行います。
067         *
068         */
069        @Override
070        public void initDaemon() {
071
072                final String    host     = StringUtil.nval( getValue( "HOST"   ) , HybsSystem.sys( "COMMON_MAIL_SERVER" ) );
073                final String    user     = StringUtil.nval( getValue( "USER"   ) , HybsSystem.sys( "MAIL_DAEMON_DEFAULT_USER" ) );
074                final String    passwd   = StringUtil.nval( getValue( "PASSWD" ) , HybsSystem.sys( "MAIL_DAEMON_DEFAULT_PASS" ) );
075
076                mailRX = new MailRX();
077                mailRX.setHost( host );
078                mailRX.setUser( user );
079                mailRX.setPasswd( passwd );
080                mailRX.setDelete( true );
081
082        // 現段階では、Subjectによるメール取得振分は行いません。
083        // 条件に合わないエラーメールを取り出す機会がなくなるためです。
084        //      String  subject  = getValue( "SUBJECT" );
085        //      if( subject != null && subject.length() > 0 ) {
086        //              HybsEntry entry = new HybsEntry( "Subject",subject );
087        //              mailRX.addMatchTerm( entry );
088        //      }
089
090                final String    receiveListener  = getValue( "RECEIVE_LISTENER" );
091                final MailReceiveListener listener = HybsSystem.newInstance( receiveListener );
092                mailRX.setMailReceiveListener( listener );
093        }
094
095        /**
096         * タイマータスクのデーモン処理の開始ポイントです。
097         *
098         * @og.rev 5.3.0.0 (2010/12/01) エラーハンドリングを修正
099         * @og.rev 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs)
100         * @og.rev 6.4.2.0 (2016/01/29) StringUtil#stringStackTrace(Throwable) を、ThrowUtil#ogStackTrace(String,Throwable) に置き換え。
101         */
102        @Override
103        protected void startDaemon() {
104                if( loopCnt % LOOP_COUNTER == 0 ) {
105                        loopCnt = 1;
106                        System.out.println();
107                        System.out.print( toString() + " " + new Date()  + " " );
108                }
109                else {
110                        System.out.print( "." );
111                        loopCnt++ ;
112                }
113
114                // 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs)
115                if( mailRX == null ) {
116                        final String errMsg = "#initDaemon()を先に実行しておいてください。" ;
117                        throw new OgRuntimeException( errMsg );
118                }
119
120                try {
121                        mailRX.start();
122                }
123
124                // 5.3.0.0 (2010/12/01) エラーハンドリングを修正
125                catch( final Throwable ex ) {
126                        final String errMsg = ThrowUtil.ogStackTrace( ex ) ;                                    // 6.4.2.0 (2016/01/29)
127                        System.out.println( errMsg );
128                        LogWriter.log( errMsg );
129
130                        final GE50Access ge50 = new GE50Access( "CYYYYY","M_Daemon","MailReceive" );
131
132                        ge50.setSystemId( "ERR" );
133                        ge50.makeYkno();
134
135                        ge50.insertErrorGE56( errMsg );
136                        ge50.insertGE50( GE50Access.FG_ERR1 );
137
138                }
139        }
140}