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