KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > j2ee > JTAResource


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.management.j2ee;
23
24 import org.jboss.logging.Logger;
25 import org.jboss.management.j2ee.statistics.CountStatisticImpl;
26 import org.jboss.management.j2ee.statistics.JTAStatsImpl;
27
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.j2ee.statistics.Stats JavaDoc;
32
33 /**
34  * The JBoss JSR-77.3.30 implementation of the JTAResource model
35  *
36  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
37  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
38  * @author <a HREF="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
39  * @version $Revision: 40550 $
40  */

41 public class JTAResource extends J2EEResource
42    implements JTAResourceMBean
43 {
44    // Constants -----------------------------------------------------
45
private static Logger log = Logger.getLogger(JTAResource.class);
46
47    // Attributes ----------------------------------------------------
48

49    private ObjectName JavaDoc jtaServiceName;
50    private JTAStatsImpl stats;
51
52    // Static --------------------------------------------------------
53

54    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer, String JavaDoc resName,
55                                    ObjectName JavaDoc jtaServiceName)
56    {
57       ObjectName JavaDoc j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
58       ObjectName JavaDoc jsr77Name = null;
59       try
60       {
61          JTAResource jtaRes = new JTAResource(resName, j2eeServerName, jtaServiceName);
62          jsr77Name = jtaRes.getObjectName();
63          mbeanServer.registerMBean(jtaRes, jsr77Name);
64          log.debug("Created JSR-77 JTAResource: " + resName);
65       }
66       catch (Exception JavaDoc e)
67       {
68          log.debug("Could not create JSR-77 JTAResource: " + resName, e);
69       }
70       return jsr77Name;
71    }
72
73    public static void destroy(MBeanServer JavaDoc mbeanServer, String JavaDoc resName)
74    {
75       try
76       {
77          J2EEManagedObject.removeObject(mbeanServer,
78                  J2EEDomain.getDomainName() + ":" +
79                  J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JTAResource + "," +
80                  "name=" + resName + "," +
81                  "*");
82       }
83       catch (Exception JavaDoc e)
84       {
85          log.error("Could not destroy JSR-77 JTAResource: " + resName, e);
86       }
87    }
88
89    // Constructors --------------------------------------------------
90

91    /**
92     * @param resName Name of the JTAResource
93     * @throws InvalidParameterException If list of nodes or ports was null or empty
94     */

95    public JTAResource(String JavaDoc resName, ObjectName JavaDoc j2eeServerName, ObjectName JavaDoc jtaServiceName)
96            throws
97            MalformedObjectNameException JavaDoc,
98            InvalidParentException
99    {
100       super(J2EETypeConstants.JTAResource, resName, j2eeServerName);
101       if (log.isDebugEnabled())
102          log.debug("Service name: " + jtaServiceName);
103       this.jtaServiceName = jtaServiceName;
104       stats = new JTAStatsImpl();
105    }
106
107    // Begin StatisticsProvider interface methods
108

109    /**
110     * Obtain the Stats from the StatisticsProvider.
111     *
112     * @return An EJBStats subclass
113     * @jmx:managed-attribute
114     */

115    public Stats JavaDoc getstats()
116    {
117       try
118       {
119          CountStatisticImpl readyCount = (CountStatisticImpl) stats.getActiveCount();
120          Long JavaDoc count = (Long JavaDoc) server.getAttribute(jtaServiceName, "TransactionCount");
121          readyCount.set(count.longValue());
122          CountStatisticImpl commitCount = (CountStatisticImpl) stats.getCommittedCount();
123          count = (Long JavaDoc) server.getAttribute(jtaServiceName, "CommitCount");
124          commitCount.set(count.longValue());
125          CountStatisticImpl rollbackCount = (CountStatisticImpl) stats.getRolledbackCount();
126          count = (Long JavaDoc) server.getAttribute(jtaServiceName, "RollbackCount");
127          rollbackCount.set(count.longValue());
128       }
129       catch (Exception JavaDoc e)
130       {
131          log.debug("Failed to retrieve stats", e);
132       }
133       return stats;
134    }
135
136    public void resetStats()
137    {
138       stats.reset();
139    }
140
141    // End StatisticsProvider interface methods
142

143    // javax.managment.j2ee.EventProvider implementation -------------
144

145    public String JavaDoc[] getEventTypes()
146    {
147       return StateManagement.stateTypes;
148    }
149
150    public String JavaDoc getEventType(int pIndex)
151    {
152       if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length)
153       {
154          return StateManagement.stateTypes[pIndex];
155       }
156       else
157       {
158          return null;
159       }
160    }
161
162    // java.lang.Object overrides ------------------------------------
163

164    public String JavaDoc toString()
165    {
166       return "JTAResource { " + super.toString() + " } [ " +
167               " ]";
168    }
169
170 }
171
Popular Tags