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     */
016    package org.opengion.plugin.daemon;
017    
018    import org.opengion.hayabusa.common.HybsSystem;
019    import org.opengion.hayabusa.report.GE50Access;
020    import org.opengion.fukurou.util.LogWriter;
021    import org.opengion.fukurou.util.StringUtil;
022    import org.opengion.fukurou.util.HybsTimerTask;
023    
024    import org.opengion.fukurou.mail.MailReceiveListener ;
025    import org.opengion.fukurou.mail.MailRX ;
026    
027    import java.util.Date;
028    
029    /**
030     * ã€ãƒ¡ãƒ¼ãƒ«ãƒ??モン】メールサーãƒã?を監視ã—ã¦ã€EXCELファイルã®DB登録処ç??ãƒ??モンã§ã™ã?
031     * æŒ?®šã?æ¡ä»¶ã§ãƒ¡ãƒ¼ãƒ«ã‚µãƒ¼ãƒã?を監視ã—ã€æ·»ä»˜ã?EXCELファイルをå–りå?ã—ã¾ã™ã?
032     * 添付ファイルã¯ã€EXCELå–り込ã¿ã‚¤ãƒ³ã‚¿ãƒ¼ãƒ•ェースã«å‰?‚Šã€è¦æ±‚番å·.xls ã«ãƒªãƒã?ãƒ?—ã¦
033     * æ‰?®šã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒª(EXCEL_IN_FILE_URLã§å®šç¾©)ã«ã‚»ãƒ¼ãƒ–ã—ã€å¸³ç¥¨è¦æ±‚テーブルã«ç™»éŒ²ã—ã¾ã™ã?(GE50)
034     * ãれ以é™ã?処ç??ã€EXCEL帳票ãƒ??モン以下ã?共通å?ç?§å‡¦ç?•れã¾ã™ã?
035     * EXCELå–り込ã¿å‡¦ç??ã€ã“ã®ãƒ¡ãƒ¼ãƒ«è‡ªå‹•å–り込ã¿ã¨ã€ãƒ•ァイルアãƒ??ロードã?æ–¹å¼ã‚’
036     * サãƒã?トã—ã¦ã?¾ã™ã?
037     *
038     * @og.rev 3.8.0.0 (2005/06/07) æ–°è¦è¿½åŠ?
039     * @og.rev 4.3.4.4 (2009/01/01) プラグイン�
040     * @og.group �?モン
041     *
042     * @version  4.0
043     * @author   Kazuhiko Hasegawa
044     * @since    JDK5.0,
045     */
046    public class Daemon_MailReceive extends HybsTimerTask {
047            //* ã“ã?プログラãƒ??VERSIONæ–?­—å?を設定ã—ã¾ã™ã?       {@value} */
048            private static final String VERSION = "5.3.0.0 (2010/12/01)" ;
049    
050            private int                     loopCnt         = 0;
051            private static final int LOOP_COUNTER = 24;             // カウンタã‚?4回ã«è¨­å®?
052    
053            private MailRX  mailRX          = null;
054    
055            /**
056             * ã“ã?タイマã?タスクã«ã‚ˆã£ã¦åˆæœŸåŒ–ã•れるアクションã§ã™ã?
057             * パラメータを使用ã—ãŸåˆæœŸåŒ–を行ã„ã¾ã™ã?
058             *
059             */
060            @Override
061            public void initDaemon() {
062    
063                    String  host     = StringUtil.nval( getValue( "HOST"   ) , HybsSystem.sys( "COMMON_MAIL_SERVER" ) );
064                    String  user     = StringUtil.nval( getValue( "USER"   ) , HybsSystem.sys( "MAIL_DAEMON_DEFAULT_USER" ) );
065                    String  passwd   = StringUtil.nval( getValue( "PASSWD" ) , HybsSystem.sys( "MAIL_DAEMON_DEFAULT_PASS" ) );
066    
067                    mailRX = new MailRX();
068                    mailRX.setHost( host );
069                    mailRX.setUser( user );
070                    mailRX.setPasswd( passwd );
071                    mailRX.setDelete( true );
072    
073            // ç¾æ®µéšŽã§ã¯ã€Subjectã«ã‚ˆã‚‹ãƒ¡ãƒ¼ãƒ«å–得振åˆ??行ã„ã¾ã›ã‚“ã€?
074            // æ¡ä»¶ã«åˆã‚ãªã?‚¨ãƒ©ãƒ¼ãƒ¡ãƒ¼ãƒ«ã‚’å–りå?ã™æ©Ÿä¼šãŒãªããªã‚‹ãŸã‚ã§ã™ã?
075            //      String  subject  = getValue( "SUBJECT" );
076            //      if( subject != null && subject.length() > 0 ) {
077            //              HybsEntry entry = new HybsEntry( "Subject",subject );
078            //              mailRX.addMatchTerm( entry );
079            //      }
080    
081                    String  receiveListener  = getValue( "RECEIVE_LISTENER" );
082                    MailReceiveListener listener = (MailReceiveListener)HybsSystem.newInstance( receiveListener );
083                    mailRX.setMailReceiveListener( listener );
084            }
085    
086            /**
087             * タイマã?タスクã®ãƒ??モン処ç??é–‹å§‹ã?イントã§ã™ã?
088             *
089             * @og.rev 5.3.0.0 (2010/12/01) エラーãƒãƒ³ãƒ‰ãƒªãƒ³ã‚°ã‚’修正
090             *
091             */
092            @Override
093            protected void startDaemon() {
094                    if( loopCnt % LOOP_COUNTER == 0 ) {
095                            loopCnt = 1;
096                            System.out.println();
097                            System.out.print( toString() + " " + new Date()  + " " );
098                    }
099                    else {
100                            System.out.print( "." );
101                            loopCnt++ ;
102                    }
103    
104                    try {
105                            mailRX.start();
106                    }
107    //              catch( Throwable ex ) {
108    //                      GE50Access GE50 = new GE50Access( "CYYYYY","M_Daemon","MailReceive" );
109    //
110    //                      GE50.setSystemId( "ERR" );
111    //                      GE50.makeYkno();
112    //
113    //                      GE50.insertGE50( GE50Access.FG_ERR1 );
114    //
115    //                      String errMsg = StringUtil.stringStackTrace( ex );
116    //                      GE50.insertErrorGE56( errMsg );
117    //
118    //                      LogWriter.log( errMsg );
119    //              }
120                    // 5.3.0.0 (2010/12/01) エラーãƒãƒ³ãƒ‰ãƒªãƒ³ã‚°ã‚’修正
121                    catch( Throwable ex ) {
122                            String errMsg = StringUtil.stringStackTrace( ex ) ;
123                            System.out.println( errMsg );
124                            LogWriter.log( errMsg );
125    
126                            GE50Access ge50 = new GE50Access( "CYYYYY","M_Daemon","MailReceive" );
127    
128                            ge50.setSystemId( "ERR" );
129                            ge50.makeYkno();
130    
131                            ge50.insertErrorGE56( errMsg );
132                            ge50.insertGE50( GE50Access.FG_ERR1 );
133    
134                    }
135            }
136    }