Coverage Report - net.admin4j.ui.servlets.ExceptionDisplayServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionDisplayServlet
41%
24/58
40%
8/20
4.5
 
 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 java.io.IOException;
 17  
 import java.util.ArrayList;
 18  
 import java.util.Collections;
 19  
 import java.util.HashMap;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 import java.util.Set;
 23  
 import java.util.StringTokenizer;
 24  
 
 25  
 import javax.servlet.ServletConfig;
 26  
 import javax.servlet.ServletException;
 27  
 import javax.servlet.http.HttpServletRequest;
 28  
 import javax.servlet.http.HttpServletResponse;
 29  
 
 30  
 import net.admin4j.config.Admin4JConfiguration;
 31  
 import net.admin4j.deps.commons.beanutils.BeanComparator;
 32  
 import net.admin4j.deps.commons.lang3.StringUtils;
 33  
 import net.admin4j.entity.ExceptionInfo;
 34  
 import net.admin4j.exception.ExceptionTracker;
 35  
 import net.admin4j.log.LogManager;
 36  
 import net.admin4j.log.LogManagerRegistry;
 37  
 import net.admin4j.util.Admin4jRuntimeException;
 38  
 import net.admin4j.util.FreemarkerUtils;
 39  
 import net.admin4j.util.GuiUtils;
 40  
 import net.admin4j.util.HostUtils;
 41  
 import net.admin4j.util.ServletUtils;
 42  
 import net.admin4j.vo.ExceptionStatisticVO;
 43  
 
 44  
 /**
 45  
  * Provides a browser for exception statistics intercepted by the Exception Tracker.  By default,
 46  
  * exceptions logged by Java Util Logging and Log4J are automatically tracked if they are present in the classpath.
 47  
  * 
 48  
  *  <p>Should you use other logging frameworks, you can have those exceptions tracked by writingand installing a custom
 49  
  *  appender for that framework.  See <a href="../../exception/Log4JExceptionAppender.html">Log4JExceptionAppender</a>
 50  
  *  for an example.</p>
 51  
  *  
 52  
  *  <p>You will need to map this servlet to an url pattern (e.g. '/admin4j/error').</p>
 53  
  *  <p>This servlet does <b>not</b> require other web resources.</p>
 54  
  *  
 55  
  *  <p>Init parameters for this servlet are as follows:</p>
 56  
  *  <li>tracking.time.in.days -- Optional.  Default 30 days.  Amount of time an exception will be tracked and reportable.</li>
 57  
  *  <li>exempted.exception.types -- Optional.  Comma delimited list of exception class names that will *not* be tracked.</li>
 58  
  * @author D. Ashmore
 59  
  * @see net.admin4j.exception.ExceptionTracker
 60  
  */
 61  6
 public class ExceptionDisplayServlet extends AdminDisplayServlet {
 62  
 
 63  
     private static final long serialVersionUID = -2262926722396897223L;
 64  
     
 65  
     public static final String PUBLIC_HANDLE="exception";
 66  
 
 67  
     /* (non-Javadoc)
 68  
      * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
 69  
      */
 70  
     @Override
 71  
     public void init(ServletConfig config) throws ServletException {
 72  6
         super.init(config);
 73  6
        for (LogManager logManager: LogManagerRegistry.getAvailableLogManagerSet()) {
 74  
            try {
 75  18
                logManager.installExceptionTrackingLogAppender();
 76  18
                logger.info("Exception tracking for {} installed.", logManager.getLoggerSoftwareProductName());
 77  
            }
 78  0
            catch (Throwable t) {
 79  0
                logger.error("Exception tracking for {} installed.", 
 80  
                        new Admin4jRuntimeException(t).addContextValue("log manager", logManager.getLoggerSoftwareProductName()));
 81  36
            }
 82  
        }
 83  
        
 84  6
        String trackingTimeStr = ServletUtils.getConfigurationSetting(
 85  
                new String[]{PUBLIC_HANDLE + ".tracking.time.in.days", 
 86  
                "tracking.time.in.days"}, config);
 87  
        Integer trackingTimeInDays;
 88  0
        if ( !StringUtils.isEmpty(trackingTimeStr)) {
 89  
            try {
 90  3
                trackingTimeInDays = new Integer(trackingTimeStr.trim());
 91  3
                ExceptionTracker.setPurgeThresholdInDays(trackingTimeInDays);
 92  
            }
 93  0
            catch (Throwable t) {
 94  0
                this.logger.error("Invalid tracking.time.in.millis parameter for ExceptionDisplayServlet.  Default used.  tracking.time.in.millis="+ trackingTimeStr, 
 95  
                        t);
 96  3
            }
 97  
        }
 98  3
        else if (Admin4JConfiguration.getExceptionTimeTrackingInDays() != null) {
 99  0
            ExceptionTracker.setPurgeThresholdInDays(Admin4JConfiguration.getExceptionTimeTrackingInDays());
 100  
        }
 101  
        
 102  6
        String exemptedExceptionStr = ServletUtils.getConfigurationSetting(
 103  
                new String[]{PUBLIC_HANDLE + ".exempted.exception.types", 
 104  
                "exempted.exception.types"}, config);
 105  0
        if ( !StringUtils.isEmpty(exemptedExceptionStr)) {
 106  0
            registerExceptedExceptionTypes(exemptedExceptionStr);
 107  
        }
 108  0
        else if ( !StringUtils.isEmpty(Admin4JConfiguration.getExceptionExemptedExceptionTypes() )) {
 109  0
            registerExceptedExceptionTypes(Admin4JConfiguration.getExceptionExemptedExceptionTypes());
 110  
        }
 111  6
     }
 112  
 
 113  
     private void registerExceptedExceptionTypes(String exemptedExceptionStr) {
 114  
         try {
 115  0
                StringTokenizer tok = new StringTokenizer(exemptedExceptionStr, ",");
 116  
                String exemptedClassName;
 117  0
                while (tok.hasMoreTokens()) {
 118  0
                    exemptedClassName = tok.nextToken();
 119  0
                    if (!StringUtils.isEmpty(exemptedClassName)) {
 120  0
                        ExceptionTracker.addExemptedExceptionClassName(exemptedClassName);
 121  0
                        this.logger.info("Exceptions of type {} will not be tracked.", exemptedClassName);
 122  
                    }
 123  
                }
 124  
            }
 125  0
            catch (Throwable t) {
 126  0
                this.logger.error("Invalid exempted.exception.types parameter for ExceptionDisplayServlet.  parm=" + exemptedExceptionStr, 
 127  
                        t);
 128  0
            }
 129  0
     }
 130  
 
 131  
     /* (non-Javadoc)
 132  
      * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 133  
      */
 134  
     @SuppressWarnings("unchecked")
 135  
     @Override
 136  
     protected void service(HttpServletRequest request, HttpServletResponse response)
 137  
             throws ServletException, IOException {
 138  
         
 139  3
         String actionStr = request.getParameter("action");
 140  0
         if ( !StringUtils.isEmpty(actionStr)) {
 141  
 //            if ("save".equalsIgnoreCase(actionStr)) {
 142  
 //                ExceptionTracker.recordUrl(request.getParameter("exceptionId"), request.getParameter("exceptionUrl"));
 143  
 //            }
 144  0
             if ("delete".equalsIgnoreCase(actionStr)) {
 145  0
                 ExceptionTracker.deleteException(request.getParameter("exceptionId"));
 146  
             }
 147  0
             if ("text".equalsIgnoreCase(actionStr)) {
 148  0
                 ExceptionInfo eInfo = ExceptionTracker.lookupException(request.getParameter("exceptionId"));
 149  
                 
 150  0
                 response.setContentType("application/octet-stream");                 
 151  0
                 response.setHeader("Content-disposition", "attachment; filename=\"exception.txt\"");
 152  
                 
 153  0
                 Map<String,Object> variableMap = new HashMap<String,Object>();
 154  0
                 variableMap.put("exception", eInfo);
 155  0
                 variableMap.put("host", HostUtils.getHostName());
 156  0
                 variableMap.put("GuiUtils", new GuiUtils());
 157  0
                 FreemarkerUtils.addConfiguration(variableMap);
 158  0
                 this.displayFreeMarkerResponse(request, response, "exceptionDownload.ftl", variableMap);
 159  
                 
 160  0
                 return;
 161  
             }
 162  
         }
 163  
         
 164  3
         Set<ExceptionStatisticVO> exceptionSet = ExceptionTracker.getExceptionStatisticSet();
 165  
         
 166  3
         List<ExceptionStatisticVO> exceptionList = new ArrayList<ExceptionStatisticVO>(exceptionSet);
 167  
         Collections.sort(exceptionList, new BeanComparator("totalNbrExceptions"));
 168  3
         Collections.reverse(exceptionList);
 169  
         
 170  3
         Map<String,Object> variableMap = new HashMap<String,Object>();
 171  3
         variableMap.put("exceptionList", exceptionList);
 172  3
         variableMap.put("GuiUtils", new GuiUtils());
 173  3
         FreemarkerUtils.addConfiguration(variableMap);
 174  
         
 175  3
         displayFreeMarkerPage(request, response, "exceptionDisplayServletDisplay.ftl", variableMap);
 176  3
     }
 177  
 
 178  
     /* (non-Javadoc)
 179  
      * @see net.admin4j.ui.servlets.Admin4JServlet#getServletLabel()
 180  
      */
 181  
     @Override
 182  
     public String getServletLabel() {
 183  3
         return "Exception Summary";
 184  
     }
 185  
 
 186  
 
 187  
 }