Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.servlet;
 
 
 
 
 public class StatisticsServlet extends HttpServlet
 {
     private static final Logger LOG = Log.getLogger(StatisticsServlet.class);
 
     boolean _restrictToLocalhost = true// defaults to true
     private StatisticsHandler _statsHandler;
     private MemoryMXBean _memoryBean;
     private Connector[] _connectors;
 
     public void init() throws ServletException
     {
         ServletContext context = getServletContext();
         ContextHandler.Context scontext = (ContextHandler.Contextcontext;
         Server _server = scontext.getContextHandler().getServer();
 
         Handler handler = _server.getChildHandlerByClass(StatisticsHandler.class);
 
         if (handler != null)
         {
              = (StatisticsHandlerhandler;
         }
         else
         {
             .warn("Statistics Handler not installed!");
             return;
         }
         
          = ManagementFactory.getMemoryMXBean();
          = _server.getConnectors();
 
         if (getInitParameter("restrictToLocalhost") != null)
         {
              = "true".equals(getInitParameter("restrictToLocalhost"));
         }
 
     }
 
     public void doPost(HttpServletRequest sreqHttpServletResponse sresthrows ServletExceptionIOException
     {
         doGet(sreqsres);
     }
 
     protected void doGet(HttpServletRequest reqHttpServletResponse respthrows ServletExceptionIOException
     {
         if ( == null)
         {
             .warn("Statistics Handler not installed!");
             resp.sendError(.);
             return;
         }
         if ()
         {
             if (!"127.0.0.1".equals(req.getRemoteAddr()))
             {
                 resp.sendError(.);
                 return;
             }
         }
 
         String wantXml = req.getParameter("xml");
        if (wantXml == null)
          wantXml = req.getParameter("XML");
        if (wantXml != null && "true".equalsIgnoreCase(wantXml))
        {
            sendXmlResponse(resp);
        }
        else
        {
            sendTextResponse(resp);
        }
    }
    private void sendXmlResponse(HttpServletResponse responsethrows IOException
    {
        StringBuilder sb = new StringBuilder();
        sb.append("<statistics>\n");
        sb.append("  <requests>\n");
        sb.append("    <statsOnMs>").append(.getStatsOnMs()).append("</statsOnMs>\n");
        
        sb.append("    <requests>").append(.getRequests()).append("</requests>\n");
        sb.append("    <requestsActive>").append(.getRequestsActive()).append("</requestsActive>\n");
        sb.append("    <requestsActiveMax>").append(.getRequestsActiveMax()).append("</requestsActiveMax>\n");
        sb.append("    <requestsTimeTotal>").append(.getRequestTimeTotal()).append("</requestsTimeTotal>\n");
        sb.append("    <requestsTimeMean>").append(.getRequestTimeMean()).append("</requestsTimeMean>\n");
        sb.append("    <requestsTimeMax>").append(.getRequestTimeMax()).append("</requestsTimeMax>\n");
        sb.append("    <requestsTimeStdDev>").append(.getRequestTimeStdDev()).append("</requestsTimeStdDev>\n");
        sb.append("    <dispatched>").append(.getDispatched()).append("</dispatched>\n");
        sb.append("    <dispatchedActive>").append(.getDispatchedActive()).append("</dispatchedActive>\n");
        sb.append("    <dispatchedActiveMax>").append(.getDispatchedActiveMax()).append("</dispatchedActiveMax>\n");
        sb.append("    <dispatchedTimeTotal>").append(.getDispatchedTimeTotal()).append("</dispatchedTimeTotal>\n");
        sb.append("    <dispatchedTimeMean").append(.getDispatchedTimeMean()).append("</dispatchedTimeMean>\n");
        sb.append("    <dispatchedTimeMax>").append(.getDispatchedTimeMax()).append("</dispatchedTimeMax>\n");
        sb.append("    <dispatchedTimeStdDev").append(.getDispatchedTimeStdDev()).append("</dispatchedTimeStdDev>\n");
        
        sb.append("    <requestsSuspended>").append(.getSuspends()).append("</requestsSuspended>\n");
        sb.append("    <requestsExpired>").append(.getExpires()).append("</requestsExpired>\n");
        sb.append("    <requestsResumed>").append(.getResumes()).append("</requestsResumed>\n");
        sb.append("  </requests>\n");
        sb.append("  <responses>\n");
        sb.append("    <responses1xx>").append(.getResponses1xx()).append("</responses1xx>\n");
        sb.append("    <responses2xx>").append(.getResponses2xx()).append("</responses2xx>\n");
        sb.append("    <responses3xx>").append(.getResponses3xx()).append("</responses3xx>\n");
        sb.append("    <responses4xx>").append(.getResponses4xx()).append("</responses4xx>\n");
        sb.append("    <responses5xx>").append(.getResponses5xx()).append("</responses5xx>\n");
        sb.append("    <responsesBytesTotal>").append(.getResponsesBytesTotal()).append("</responsesBytesTotal>\n");
        sb.append("  </responses>\n");
        sb.append("  <connections>\n");
        for (Connector connector : )
        {
        	sb.append("    <connector>\n");
        	sb.append("      <name>").append(connector.getName()).append("</name>\n");
        	sb.append("      <statsOn>").append(connector.getStatsOn()).append("</statsOn>\n");
            if (connector.getStatsOn())
            {
            	sb.append("    <statsOnMs>").append(connector.getStatsOnMs()).append("</statsOnMs>\n");
            	sb.append("    <connections>").append(connector.getConnections()).append("</connections>\n");
            	sb.append("    <connectionsOpen>").append(connector.getConnectionsOpen()).append("</connectionsOpen>\n");
            	sb.append("    <connectionsOpenMax>").append(connector.getConnectionsOpenMax()).append("</connectionsOpenMax>\n");
            	sb.append("    <connectionsDurationTotal>").append(connector.getConnectionsDurationTotal()).append("</connectionsDurationTotal>\n");
            	sb.append("    <connectionsDurationMean>").append(connector.getConnectionsDurationMean()).append("</connectionsDurationMean>\n");
            	sb.append("    <connectionsDurationMax>").append(connector.getConnectionsDurationMax()).append("</connectionsDurationMax>\n");
                sb.append("    <connectionsDurationStdDev>").append(connector.getConnectionsDurationStdDev()).append("</connectionsDurationStdDev>\n");
                sb.append("    <requests>").append(connector.getRequests()).append("</requests>\n");
                sb.append("    <connectionsRequestsMean>").append(connector.getConnectionsRequestsMean()).append("</connectionsRequestsMean>\n");
                sb.append("    <connectionsRequestsMax>").append(connector.getConnectionsRequestsMax()).append("</connectionsRequestsMax>\n");
                sb.append("    <connectionsRequestsStdDev>").append(connector.getConnectionsRequestsStdDev()).append("</connectionsRequestsStdDev>\n");
            }
            sb.append("    </connector>\n");
        }
        sb.append("  </connections>\n");
        sb.append("  <memory>\n");
        sb.append("    <heapMemoryUsage>").append(.getHeapMemoryUsage().getUsed()).append("</heapMemoryUsage>\n");
        sb.append("    <nonHeapMemoryUsage>").append(.getNonHeapMemoryUsage().getUsed()).append("</nonHeapMemoryUsage>\n");
        sb.append("  </memory>\n");
        sb.append("</statistics>\n");
        response.setContentType("text/xml");
        PrintWriter pout = response.getWriter();
        pout.write(sb.toString());
    }
    private void sendTextResponse(HttpServletResponse responsethrows IOException
    {
        StringBuilder sb = new StringBuilder();
        sb.append(.toStatsHTML());
        sb.append("<h2>Connections:</h2>\n");
        for (Connector connector : )
        {
            sb.append("<h3>").append(connector.getName()).append("</h3>");
            if (connector.getStatsOn())
            {
                sb.append("Statistics gathering started ").append(connector.getStatsOnMs()).append("ms ago").append("<br />\n");
                sb.append("Total connections: ").append(connector.getConnections()).append("<br />\n");
                sb.append("Current connections open: ").append(connector.getConnectionsOpen());
                sb.append("Max concurrent connections open: ").append(connector.getConnectionsOpenMax()).append("<br />\n");
                sb.append("Total connections duration: ").append(connector.getConnectionsDurationTotal()).append("<br />\n");
                sb.append("Mean connection duration: ").append(connector.getConnectionsDurationMean()).append("<br />\n");
                sb.append("Max connection duration: ").append(connector.getConnectionsDurationMax()).append("<br />\n");
                sb.append("Connection duration standard deviation: ").append(connector.getConnectionsDurationStdDev()).append("<br />\n");
                sb.append("Total requests: ").append(connector.getRequests()).append("<br />\n");
                sb.append("Mean requests per connection: ").append(connector.getConnectionsRequestsMean()).append("<br />\n");
                sb.append("Max requests per connection: ").append(connector.getConnectionsRequestsMax()).append("<br />\n");
                sb.append("Requests per connection standard deviation: ").append(connector.getConnectionsRequestsStdDev()).append("<br />\n");
            }
            else
            {
                sb.append("Statistics gathering off.\n");
            }
        }
        sb.append("<h2>Memory:</h2>\n");
        sb.append("Heap memory usage: ").append(.getHeapMemoryUsage().getUsed()).append(" bytes").append("<br />\n");
        sb.append("Non-heap memory usage: ").append(.getNonHeapMemoryUsage().getUsed()).append(" bytes").append("<br />\n");
        response.setContentType("text/html");
        PrintWriter pout = response.getWriter();
        pout.write(sb.toString());
    }
New to GrepCode? Check out our FAQ X