Package org.tamacat.httpd.filter

Source Code of org.tamacat.httpd.filter.PerformanceCounterFilter

/*
* Copyright (c) 2010, TamaCat.org
* All rights reserved.
*/
package org.tamacat.httpd.filter;

import java.net.URL;

import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.protocol.HttpContext;
import org.tamacat.httpd.config.ServiceUrl;
import org.tamacat.httpd.jmx.BasicCounter;
import org.tamacat.httpd.jmx.URLBasicCounter;

public class PerformanceCounterFilter implements RequestFilter, ResponseFilter {

  private static final URLBasicCounter urlCounter = new URLBasicCounter();

  private ServiceUrl serviceUrl;
 
  /**
   * <p>Set the base ObjectName for JMX.
   * ObjectName is append the URL path.<br>
   * default: "org.tamacat.httpd:type=URL/${path}"
   * @param objectName
   */
  public void setObjectName(String objectName) {
    urlCounter.setObjectName(objectName);
  }
 
  @Override
  public void doFilter(HttpRequest request, HttpResponse response,
      HttpContext context) {
    BasicCounter counter = urlCounter.getCounter(getPath(serviceUrl));
    if (counter != null) counter.countUp();
  }

  @Override
  public void init(ServiceUrl serviceUrl) {
    this.serviceUrl = serviceUrl;
    urlCounter.register(getPath(serviceUrl));
  }

  @Override
  public void afterResponse(HttpRequest request, HttpResponse response,
      HttpContext context) {
    BasicCounter counter = urlCounter.getCounter(getPath(serviceUrl));
    if (counter != null) counter.countDown();
  }
 
  private static String getPath(ServiceUrl serviceUrl) {
    URL host = serviceUrl.getHost();
    String name = host != null?
      host.getHost() + serviceUrl.getPath() : serviceUrl.getPath();
    return name;
  }
}
TOP

Related Classes of org.tamacat.httpd.filter.PerformanceCounterFilter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.