View Javadoc

1    package ch.qos.logback.access.filter;
2   
3   import ch.qos.logback.core.filter.Filter;
4   import ch.qos.logback.core.spi.FilterReply;
5   
6   import javax.management.MBeanServer;
7   import javax.management.ObjectName;
8   import javax.management.StandardMBean;
9   import java.lang.management.ManagementFactory;
10  
11  public class CountingFilter extends Filter {
12  
13    long total = 0;
14    final StatisticalViewImpl accessStatsImpl;
15    
16    String domain = "ch.qos.logback.access";
17    
18    public CountingFilter() {
19      accessStatsImpl = new StatisticalViewImpl(this);
20    }
21    
22    @Override
23    public FilterReply decide(Object event) {
24      total++;
25      accessStatsImpl.update();
26      return FilterReply.NEUTRAL;
27    }
28  
29    public long getTotal() {
30      return total;
31    }
32    
33    
34    @Override
35    public void start() {
36      MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
37      try {
38        ObjectName on = new ObjectName(domain+":Name="+getName());
39        StandardMBean mbean = new StandardMBean(accessStatsImpl, StatisticalView.class);
40        if (mbs.isRegistered(on)) {
41            mbs.unregisterMBean(on);
42        }
43        mbs.registerMBean(mbean, on);
44        super.start();
45      } catch (Exception e) {
46        addError("Failed to create mbean", e);
47      }
48    }
49    
50    @Override
51    public void stop() {
52      super.stop();
53      try {
54        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
55        ObjectName on = new ObjectName("totp:Filter=1");
56        mbs.unregisterMBean(on);
57      } catch(Exception e) {
58        addError("Failed to unregister mbean", e);
59      }
60    }
61  
62    public String getDomain() {
63      return domain;
64    }
65  
66    public void setDomain(String domain) {
67      this.domain = domain;
68    }
69    
70  }