Coverage Report - net.admin4j.ui.servlets.MemoryMonitorStartupServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
MemoryMonitorStartupServlet
62%
42/67
75%
15/20
3.429
 
 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.ui.servlets;
 15  
 
 16  
 import javax.servlet.ServletConfig;
 17  
 import javax.servlet.ServletException;
 18  
 
 19  
 import net.admin4j.config.Admin4JConfiguration;
 20  
 import net.admin4j.monitor.Detector;
 21  
 import net.admin4j.monitor.LowMemoryDetector;
 22  
 import net.admin4j.util.Daemon;
 23  
 import net.admin4j.util.ServletUtils;
 24  
 import net.admin4j.util.notify.NotifierUtils;
 25  
 
 26  
 
 27  
 /**
 28  
  * This servlet will start a memory monitor that will notify you of memory issues.
 29  
  * <p>Init parameters for this servlet are as follows:</p>
 30  
  * <li>sleep.interval.millis -- Optional.  Default 30000.  Amount of time in millis monitor will sleep between checks.</li>
 31  
  * <li>memory.threshold.pct -- Optional.  Default 90. The percentage of memory that must be allocated before low memory warnings are issued.</li>
 32  
  * <li>nbr.intervals.between.warnings -- Optional.  Default 30. Number of intervals low memory must be experienced before repeat warnings are issued.</li>
 33  
  * <li>notifier -- Required.  Handles admin notification.  See documentation for the Notifier you're using
 34  
  * for any additional configuration requirements.  For example, 'net.admin4j.util.notify.EmailNotifier'.</li>
 35  
  * @author D. Ashmore
 36  
  * @since 1.0
 37  
  */
 38  6
 public class MemoryMonitorStartupServlet extends Admin4JServlet {
 39  
 
 40  
     private static final long serialVersionUID = 694573116545416300L;
 41  
     public static final String PUBLIC_HANDLE="memory";
 42  
     
 43  6
     private long sleepIntervalMillis = LowMemoryDetector.DEFAULT_SLEEP_INTERVAL;
 44  6
     private int memoryThresholdPct = LowMemoryDetector.DEFAULT_MEMORY_USAGE_THRESHOLD_PCT;
 45  6
     private int nbrIntervalsBetweenWarnings = LowMemoryDetector.DEFAULT_NBR_INTERVALS_BETWEEN_WARNINGS;
 46  6
     private int nbrLowWatermarkIntervals = LowMemoryDetector.DEFAULT_NBR_LOW_WATERMARK_INTERVALS;
 47  6
     private long lowWatermarkMonitorIntervalInMillis = LowMemoryDetector.DEFAULT_LOW_WATERMARK_MONITOR_INTERVAL_IN_MILLIS;
 48  
     
 49  
     @SuppressWarnings("unused")
 50  6
     private static Daemon memoryMonitorDaemon = null;
 51  
     
 52  
     @Override
 53  
     public void init(ServletConfig config) throws ServletException {
 54  6
         super.init(config);
 55  
         try {
 56  
                   
 57  6
             String sleepIntervalMillisStr = ServletUtils.getConfigurationSetting(
 58  
                     new String[]{PUBLIC_HANDLE + ".sleep.interval.millis", 
 59  
                     "sleep.interval.millis"}, config);
 60  6
             if (sleepIntervalMillisStr != null) {
 61  
                 try {
 62  3
                     sleepIntervalMillis = Long.parseLong(sleepIntervalMillisStr);
 63  0
                 } catch (Exception e) {
 64  0
                     sleepIntervalMillis = LowMemoryDetector.DEFAULT_SLEEP_INTERVAL;
 65  0
                     this.logger.error("Invalid sleep.interval.millis parameter for MemoryMonitorStartupServlet.  Default used.  sleep.interval.millis={}", 
 66  
                             sleepIntervalMillisStr, e);
 67  3
                 }
 68  
             }
 69  3
             else if ( Admin4JConfiguration.getMemorySleepIntervalMillis() != null ) {
 70  0
                 sleepIntervalMillis = Admin4JConfiguration.getMemorySleepIntervalMillis();
 71  
             }
 72  
             
 73  6
             String memoryThresholdPctStr = ServletUtils.getConfigurationSetting(
 74  
                     new String[]{PUBLIC_HANDLE + ".threshold.pct"}, config);
 75  6
             if (memoryThresholdPctStr != null) {
 76  
                 try {
 77  3
                     memoryThresholdPct = Integer.parseInt(memoryThresholdPctStr);
 78  0
                 } catch (Exception e) {
 79  0
                     memoryThresholdPct = LowMemoryDetector.DEFAULT_MEMORY_USAGE_THRESHOLD_PCT;
 80  0
                     this.logger.error("Invalid memory.threshold.pct parameter for MemoryMonitorStartupServlet.  Default used.  memory.threshold.pct={}", 
 81  
                             memoryThresholdPctStr, e);
 82  3
                 }
 83  
             }
 84  3
             else if ( Admin4JConfiguration.getMemoryThresholdPct() != null ) {
 85  0
                 memoryThresholdPct = Admin4JConfiguration.getMemoryThresholdPct();
 86  
             }
 87  
             
 88  6
             String nbrIntervalsBetweenWarningsStr = ServletUtils.getConfigurationSetting(
 89  
                     new String[]{PUBLIC_HANDLE + ".nbr.intervals.between.warnings", 
 90  
                     "nbr.intervals.between.warnings"}, config);
 91  6
             if (nbrIntervalsBetweenWarningsStr != null) {
 92  
                 try {
 93  3
                     nbrIntervalsBetweenWarnings = Integer.parseInt(nbrIntervalsBetweenWarningsStr);
 94  0
                 } catch (Exception e) {
 95  0
                     nbrIntervalsBetweenWarnings = LowMemoryDetector.DEFAULT_NBR_INTERVALS_BETWEEN_WARNINGS;
 96  0
                     this.logger.error("Invalid nbr.intervals.between.warnings for MemoryMonitorStartupServlet.  Default used.  nbr.intervals.between.warnings={}", 
 97  
                             nbrIntervalsBetweenWarningsStr, e);
 98  3
                 }
 99  
             }
 100  3
             else if ( Admin4JConfiguration.getMemoryNbrIntervalsBetweenWarnings() != null ) {
 101  0
                 nbrIntervalsBetweenWarnings = Admin4JConfiguration.getMemoryNbrIntervalsBetweenWarnings();
 102  
             }
 103  
             
 104  6
             String lowWatermarkMonitorIntervalStr = ServletUtils.getConfigurationSetting(
 105  
                     new String[]{PUBLIC_HANDLE + ".low.watermark.monitor.interval.millis"}, config);
 106  6
             if (lowWatermarkMonitorIntervalStr != null) {
 107  
                 try {
 108  3
                     lowWatermarkMonitorIntervalInMillis = Long.parseLong(lowWatermarkMonitorIntervalStr);
 109  0
                 } catch (Exception e) {
 110  0
                     lowWatermarkMonitorIntervalInMillis = LowMemoryDetector.DEFAULT_LOW_WATERMARK_MONITOR_INTERVAL_IN_MILLIS;
 111  0
                     this.logger.error("Invalid low.watermark.monitor.interval.millis parameter for MemoryMonitorStartupServlet.  Default used.  low.watermark.monitor.interval.millis={}", 
 112  
                             lowWatermarkMonitorIntervalStr, e);
 113  3
                 }
 114  
             }
 115  3
             else if ( Admin4JConfiguration.getMemoryLowWatermarkMonitorIntervalInMillis() != null ) {
 116  0
                 lowWatermarkMonitorIntervalInMillis = Admin4JConfiguration.getMemoryLowWatermarkMonitorIntervalInMillis();
 117  
             }
 118  
             
 119  6
             String nbrLowWatermarkIntervalsStr = ServletUtils.getConfigurationSetting(
 120  
                     new String[]{PUBLIC_HANDLE + ".nbr.low.watermark.intervals"}, config);
 121  6
             if (nbrLowWatermarkIntervalsStr != null) {
 122  
                 try {
 123  3
                     nbrLowWatermarkIntervals = Integer.parseInt(nbrLowWatermarkIntervalsStr);
 124  0
                 } catch (Exception e) {
 125  0
                     nbrLowWatermarkIntervals = LowMemoryDetector.DEFAULT_NBR_LOW_WATERMARK_INTERVALS;
 126  0
                     this.logger.error("Invalid memory.threshold.pct parameter for MemoryMonitorStartupServlet.  Default used.  memory.threshold.pct={}", 
 127  
                             nbrLowWatermarkIntervalsStr, e);
 128  3
                 }
 129  
             }
 130  3
             else if ( Admin4JConfiguration.getMemoryNbrLowWatermarkIntervals() != null ) {
 131  0
                 nbrLowWatermarkIntervals = Admin4JConfiguration.getMemoryNbrLowWatermarkIntervals();
 132  
             }
 133  
             
 134  6
             String notifierClassName = config.getInitParameter("notifier");
 135  6
             Detector lowMemoryDetector = new LowMemoryDetector(NotifierUtils.configure(config, notifierClassName)
 136  
                     , memoryThresholdPct
 137  
                     , nbrIntervalsBetweenWarnings
 138  
                     , nbrLowWatermarkIntervals
 139  
                     , lowWatermarkMonitorIntervalInMillis);
 140  6
             memoryMonitorDaemon = new Daemon( lowMemoryDetector, "Admin4j Low Memory Detector", sleepIntervalMillis);
 141  
         }
 142  0
         catch (Throwable t) {
 143  0
             logger.error("Error in boot sequence", t);
 144  0
             throw new ServletException(t);
 145  6
         }
 146  6
     }
 147  
 
 148  
     /* (non-Javadoc)
 149  
      * @see net.admin4j.ui.servlets.Admin4JServlet#hasDisplay()
 150  
      */
 151  
     @Override
 152  
     public boolean hasDisplay() {
 153  0
         return false;
 154  
     }
 155  
 
 156  
     protected long getSleepIntervalMillis() {
 157  0
         return sleepIntervalMillis; // Here for testing
 158  
     }
 159  
 
 160  
     protected int getMemoryThresholdPct() {
 161  3
         return memoryThresholdPct; // Here for testing
 162  
     }
 163  
 
 164  
     protected int getNbrIntervalsBetweenWarnings() {
 165  3
         return nbrIntervalsBetweenWarnings; // Here for testing
 166  
     }
 167  
 
 168  
     protected int getNbrLowWatermarkIntervals() {
 169  3
         return nbrLowWatermarkIntervals; // Here for testing
 170  
     }
 171  
 
 172  
     protected long getLowWatermarkMonitorIntervalInMillis() {
 173  3
         return lowWatermarkMonitorIntervalInMillis; // Here for testing
 174  
     }
 175  
 
 176  
 }