001/*******************************************************************************
002The MIT License (MIT)
003
004Copyright (c) 2024 KILLCODING.COM
005
006Permission is hereby granted, free of charge, to any person obtaining a copy
007of this software and associated documentation files (the "Software"), to deal
008in the Software without restriction, including without limitation the rights
009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010copies of the Software, and to permit persons to whom the Software is
011furnished to do so, subject to the following conditions:
012
013The above copyright notice and this permission notice shall be included in
014all copies or substantial portions of the Software.
015
016THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
022THE SOFTWARE.
023*****************************************************************************/
024package com.killcoding.log;
025
026import java.util.concurrent.Executors;
027import com.killcoding.log.Logger;
028import java.util.Map;
029
030/**
031 * This abstract class is alert message base class
032 * Can notify administrators via email or other means
033 * */
034public class AlertMessage {
035    
036    public static Map<String, Object> msg = null;
037    protected static Runnable sendRun = null;
038
039        public AlertMessage() {
040                super();
041        }
042        
043    protected void send(Map<String, Object> msg){
044        AlertMessage.msg = msg;
045        if(AlertMessage.sendRun != null){
046            AlertMessage.sendRun.run();
047        }        
048    }   
049    
050    public static void send(Runnable sendRun){
051        AlertMessage.sendRun = sendRun;
052    }    
053        
054        public void push(Map<String, Object> msg) {
055                final Runnable runnable = new Runnable() {
056                        @Override
057                        public void run() {
058                                try{
059                                    send(msg);
060                                }catch(Exception e){
061                                    Logger.systemError(AlertMessage.class,e.getMessage(),e);
062                                }
063                        }
064                };
065                Executors.newSingleThreadExecutor().execute(runnable);
066        }
067
068}