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         * @og.rev 6.9.7.0 (2018/05/14) PMD Each class should declare at least one constructor
052         */
053        public MailDaemon_SendGridAPI() { super(); }            // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
054
055        /**
056         * このタイマータスクによって初期化されるアクションです。
057         * パラメータを使用した初期化を行います。
058         *
059         */
060        @Override
061        public void initDaemon() {
062                // 何もありません。(PMD エラー回避)
063        }
064
065        /**
066         * タイマータスクのデーモン処理の開始ポイントです。
067         *
068         */
069        @Override
070        protected void startDaemon() {
071                // SendGridのAPIキー
072                final String sendGridApiKey = HybsSystem.sys("MAIL_SENDGRID_APIKEY");
073                if(sendGridApiKey == null || sendGridApiKey.length() == 0){
074                        // SendGridのAPIキーが設定されていない場合は終了
075                        return;
076                }
077
078                if( loopCnt % LOOP_COUNTER == 0 ) {
079                        loopCnt = 1;
080                        System.out.println( toString() + " " + new Date()  + " " );
081                }
082                else {
083                        String systemId = null;
084                        try {
085                                systemId = getValue( "SYSTEM_ID" );
086                                // SengGridAPI利用のメール送信機能呼び出し
087                                final MailManager_DB_SendGridAPI manager = new MailManager_DB_SendGridAPI();
088                                manager.sendDBMail( systemId );
089                        }
090                        catch( final RuntimeException rex ) {
091//                              final String errMsg = "メール送信失敗しました。SYSTEM_ID=" + systemId ;
092                                final String errMsg = "メール送信失敗しました。SYSTEM_ID=" + systemId + " , ERR=" +  rex.getMessage();
093                                LogWriter.log( errMsg );
094                        }
095                        loopCnt++ ;
096                }
097        }
098}