Coverage Report - net.admin4j.monitor.Detector
 
Classes in this File Line Coverage Branch Coverage Complexity
Detector
84%
11/13
N/A
1.667
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 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  
  * Base class for all Detectors.
 31  
  * @author D. Ashmore
 32  
  * @since 1.0
 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  
 }