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 java.util.Date;
019
020import org.opengion.fukurou.util.HybsTimerTask;
021import org.opengion.fukurou.system.LogWriter;                                   // Ver6
022import org.opengion.hayabusa.common.HybsSystem;
023import org.opengion.plugin.cloud.MailManager_DB_SendGridAPI;
024
025/**
026 * メールパラメータテーブルを監視して、SengGridAPIを利用したメール送信プログラムを呼び出します。
027 * このクラスは、HybsTimerTask を継承した タイマータスククラスです。
028 * startDaemon() がタイマータスクによって、呼び出されます。
029 * 
030 * 通常のメールモジュールはSMTPを利用した送信で、hayabusa.mail内のクラスをCallしていますが、
031 * こちらはplugin.cloud内のsendGridのAPIをCallしてメールを送信します。
032 * そのため、sendGridを利用出来る状態(jarファイルの配備等)となっている必要があります。
033 * 
034 * システムリソースにMAIL_SENDGRID_APIKEYが登録されていない場合は動作しません。
035 *
036 * @og.group メールモジュール
037 *
038 * @og.rev 5.9.26.0 (2017/11/02) 新規作成
039 * @author   T.OTA
040 * @since    JDK1.7
041 */
042public class MailDaemon_SendGridAPI extends HybsTimerTask {
043
044        private int loopCnt ;
045
046        private static final int LOOP_COUNTER = 24; // カウンタを24回に設定
047
048        /**
049         * このタイマータスクによって初期化されるアクションです。
050         * パラメータを使用した初期化を行います。
051         *
052         */
053        @Override
054        public void initDaemon() {
055                // 何もありません。(PMD エラー回避)
056        }
057
058        /**
059         * タイマータスクのデーモン処理の開始ポイントです。
060         *
061         */
062        @Override
063        protected void startDaemon() {
064                // SendGridのAPIキー
065                final String sendGridApiKey = HybsSystem.sys("MAIL_SENDGRID_APIKEY");
066                if(sendGridApiKey == null || sendGridApiKey.length() == 0){
067                        // SendGridのAPIキーが設定されていない場合は終了
068                        return;
069                }
070
071                if( loopCnt % LOOP_COUNTER == 0 ) {
072                        loopCnt = 1;
073                        System.out.println( toString() + " " + new Date()  + " " );
074                }
075                else {
076                        String systemId = null;
077                        try {
078                                systemId = getValue( "SYSTEM_ID" );
079                                // SengGridAPI利用のメール送信機能呼び出し
080                                final MailManager_DB_SendGridAPI manager = new MailManager_DB_SendGridAPI();
081                                manager.sendDBMail( systemId );
082                        }
083                        catch( RuntimeException rex ) {
084                                final String errMsg = "メール送信失敗しました。"
085                                                        + " SYSTEM_ID=" + systemId ;
086                                LogWriter.log( errMsg );
087                        }
088                        loopCnt++ ;
089                }
090        }
091}