KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > stats > TxStatisticsInterceptor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.varia.stats;
23
24 import org.jboss.ejb.plugins.AbstractInterceptor;
25 import org.jboss.invocation.Invocation;
26 import org.jboss.invocation.InvocationType;
27 import org.jboss.metadata.XmlLoadable;
28 import org.jboss.metadata.BeanMetaData;
29 import org.jboss.deployment.DeploymentException;
30 import org.jboss.mx.util.MBeanServerLocator;
31 import org.w3c.dom.Element JavaDoc;
32
33 import javax.management.MBeanServer JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36
37
38 /**
39  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
40  * @version <tt>$Revision: 37459 $</tt>
41  */

42 public class TxStatisticsInterceptor
43    extends AbstractInterceptor
44    implements XmlLoadable
45 {
46    private MBeanServer JavaDoc server;
47    private ObjectName JavaDoc serviceName;
48
49    private String JavaDoc local;
50    private String JavaDoc localHome;
51    private String JavaDoc remote;
52    private String JavaDoc home;
53
54    // XmlLoadable implementation
55

56    public void importXml(Element JavaDoc element) throws Exception JavaDoc
57    {
58       String JavaDoc service = element.getAttribute("service");
59       if(service == null || service.trim().length() == 0)
60       {
61          throw new DeploymentException("Required attribute 'service' is not set.");
62       }
63
64       serviceName = new ObjectName JavaDoc(service);
65       server = MBeanServerLocator.locateJBoss();
66    }
67
68    // Interceptor implementation
69

70    public void start()
71    {
72       BeanMetaData bean = container.getBeanMetaData();
73       local = bean.getLocal();
74       localHome = bean.getLocalHome();
75       remote = bean.getRemote();
76       home = bean.getHome();
77    }
78
79    public Object JavaDoc invokeHome(final Invocation mi) throws Exception JavaDoc
80    {
81       Method JavaDoc method = mi.getMethod();
82       if(method != null)
83       {
84          String JavaDoc className = mi.getType() == InvocationType.LOCALHOME ? localHome : home;
85          logInvocation(className + "." + method.getName());
86       }
87
88       return super.invokeHome(mi);
89    }
90
91    public Object JavaDoc invoke(final Invocation mi) throws Exception JavaDoc
92    {
93       Method JavaDoc method = mi.getMethod();
94       if(method != null)
95       {
96          String JavaDoc className = mi.getType() == InvocationType.LOCAL ? local : remote;
97          logInvocation(className + "." + method.getName());
98       }
99
100       return super.invoke(mi);
101    }
102
103    // Private
104

105    private void logInvocation(String JavaDoc method)
106    {
107       try
108       {
109          StatisticalItem item = new TxReport.MethodStats(method);
110          server.invoke(serviceName, "addStatisticalItem",
111             new Object JavaDoc[]{item},
112             new String JavaDoc[]{StatisticalItem.class.getName()});
113       }
114       catch(Exception JavaDoc e)
115       {
116          log.error("Failed to add invocation.", e);
117       }
118    }
119 }
120
Popular Tags