|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
   package net.admin4j.monitor;  | 
  |  15 |     | 
     | 
  |  16 |     | 
   import java.io.ByteArrayOutputStream;  | 
  |  17 |     | 
   import java.io.OutputStreamWriter;  | 
  |  18 |     | 
   import java.util.Map;  | 
  |  19 |     | 
     | 
  |  20 |     | 
   import org.slf4j.Logger;  | 
  |  21 |     | 
   import org.slf4j.LoggerFactory;  | 
  |  22 |     | 
     | 
  |  23 |     | 
   import net.admin4j.deps.commons.lang3.Validate;  | 
  |  24 |     | 
   import net.admin4j.util.Admin4jRuntimeException;  | 
  |  25 |     | 
   import net.admin4j.util.FreemarkerUtils;  | 
  |  26 |     | 
   import net.admin4j.util.notify.Notifier;  | 
  |  27 |     | 
   import freemarker.template.Template;  | 
  |  28 |     | 
     | 
  |  29 |     | 
     | 
  |  30 |     | 
     | 
  |  31 |     | 
     | 
  |  32 |     | 
     | 
  |  33 |     | 
     | 
  |  34 |     | 
   public abstract class Detector implements Runnable { | 
  |  35 |     | 
         | 
  |  36 |     | 
       protected Notifier notifier;  | 
  |  37 |    39 |        protected Logger logger = LoggerFactory.getLogger(this.getClass());  | 
  |  38 |     | 
         | 
  |  39 |    39 |        public Detector(Notifier emailNotifier) { | 
  |  40 |     | 
           Validate.notNull(emailNotifier, "Null emailNotifier not allowed");  | 
  |  41 |    39 |            this.notifier = emailNotifier;  | 
  |  42 |    39 |        }  | 
  |  43 |     | 
     | 
  |  44 |     | 
       protected Notifier getNotifier() { | 
  |  45 |    681 |                return notifier;  | 
  |  46 |     | 
       }  | 
  |  47 |     | 
     | 
  |  48 |     | 
       protected void sendMessage(String subject, String templateName, Map<String, Object> variableMap) { | 
  |  49 |     | 
     | 
  |  50 |    213 |            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();  | 
  |  51 |     | 
           try { | 
  |  52 |    213 |                Template temp = FreemarkerUtils.createConfiguredTemplate(this.getClass(), templateName);  | 
  |  53 |    213 |                temp.process(variableMap, new OutputStreamWriter(outputStream));  | 
  |  54 |     | 
           }  | 
  |  55 |    0 |            catch (Exception e) { | 
  |  56 |    0 |                throw new Admin4jRuntimeException(e);  | 
  |  57 |    213 |            }  | 
  |  58 |     | 
             | 
  |  59 |    213 |            this.getNotifier().notify(subject, outputStream.toString());  | 
  |  60 |    213 |        }  | 
  |  61 |     | 
     | 
  |  62 |     | 
   }  |