KickJava   Java API By Example, From Geeks To Geeks.

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


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.BoundedRangeStatisticImpl;
26 import org.jboss.management.j2ee.statistics.CountStatisticImpl;
27 import org.jboss.management.j2ee.statistics.JCAConnectionPoolStatsImpl;
28 import org.jboss.management.j2ee.statistics.RangeStatisticImpl;
29 import org.jboss.management.j2ee.statistics.TimeStatisticImpl;
30
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.MalformedObjectNameException JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import java.util.Hashtable JavaDoc;
35
36 /**
37  * Root class of the JBoss JSR-77 implementation of JCAConnectionFactory.
38  *
39  * @author <a HREF="mailto:mclaugs@comcast.net">Scott McLaughlin</a>.
40  * @author <a HREF="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
41  * @version $Revision: 40550 $
42  */

43 public class JCAConnectionFactory extends J2EEManagedObject
44    implements JCAConnectionFactoryMBean
45 {
46    // Constants -----------------------------------------------------
47
private static Logger log = Logger.getLogger(JCAConnectionFactory.class);
48
49    // Attributes ----------------------------------------------------
50

51    /**
52     * The JBoss connection manager service name
53     */

54    private ObjectName JavaDoc cmServiceName;
55    
56    /**
57     * The JBoss managed connection service name
58     */

59    private ObjectName JavaDoc mcfServiceName;
60    private String JavaDoc jsr77MCFName;
61    private JCAConnectionPoolStatsImpl poolStats;
62
63    // Static --------------------------------------------------------
64

65    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer, String JavaDoc resName,
66                                    ObjectName JavaDoc jsr77ParentName, ObjectName JavaDoc ccmServiceNameName,
67                                    ObjectName JavaDoc mcfServiceName)
68    {
69       ObjectName JavaDoc jsr77Name = null;
70       try
71       {
72          JCAConnectionFactory jcaFactory = new JCAConnectionFactory(resName,
73                  jsr77ParentName, ccmServiceNameName, mcfServiceName);
74          jsr77Name = jcaFactory.getObjectName();
75          mbeanServer.registerMBean(jcaFactory, jsr77Name);
76          log.debug("Created JSR-77 JCAConnectionFactory: " + resName);
77          ObjectName JavaDoc jsr77MCFName = JCAManagedConnectionFactory.create(mbeanServer,
78                  resName, jsr77Name);
79          jcaFactory.setmanagedConnectionFactory(jsr77MCFName.getCanonicalName());
80       }
81       catch (Exception JavaDoc e)
82       {
83          log.debug("Could not create JSR-77 JCAConnectionFactory: " + resName, e);
84       }
85       return jsr77Name;
86    }
87
88    public static void destroy(MBeanServer JavaDoc mbeanServer, String JavaDoc resName)
89    {
90       try
91       {
92          // Remove the JCAConnectionFactory associated with resName
93
String JavaDoc connName = J2EEDomain.getDomainName() + ":"
94             + J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JCAConnectionFactory
95             + ",name=" + resName + ",*";
96          J2EEManagedObject.removeObject(mbeanServer, connName);
97       }
98       catch (javax.management.InstanceNotFoundException JavaDoc infe)
99       {
100       }
101       catch (Exception JavaDoc e)
102       {
103          log.debug("Could not destroy JSR-77 JCAConnectionFactory: " + resName, e);
104       }
105    }
106
107    // Constructors --------------------------------------------------
108

109
110    public JCAConnectionFactory(String JavaDoc resName, ObjectName JavaDoc jsr77ParentName,
111                                ObjectName JavaDoc ccmServiceNameName, ObjectName JavaDoc mcfServiceName)
112            throws MalformedObjectNameException JavaDoc, InvalidParentException
113    {
114       super(J2EETypeConstants.JCAConnectionFactory, resName, jsr77ParentName);
115       this.cmServiceName = ccmServiceNameName;
116       this.mcfServiceName = mcfServiceName;
117    }
118
119    // Public --------------------------------------------------------
120

121    // javax.management.j2ee.JCAConnectionFactory implementation -----------------
122

123    /**
124     * @jmx:managed-attribute
125     */

126    public String JavaDoc getmanagedConnectionFactory()
127    {
128       return jsr77MCFName;
129    }
130
131    void setmanagedConnectionFactory(String JavaDoc jsr77MCFName)
132    {
133       this.jsr77MCFName = jsr77MCFName;
134    }
135
136    /**
137     * @jmx:managed-operation
138     */

139    public JCAConnectionPoolStatsImpl getPoolStats(ObjectName JavaDoc poolServiceName)
140    {
141       TimeStatisticImpl waitTime = null;
142       TimeStatisticImpl useTime = null;
143       CountStatisticImpl closeCount = null;
144       CountStatisticImpl createCount = null;
145       BoundedRangeStatisticImpl freePoolSize = null;
146       BoundedRangeStatisticImpl poolSize = null;
147       RangeStatisticImpl waitingThreadCount = null;
148       try
149       {
150          if (poolStats == null)
151          {
152             Integer JavaDoc max = (Integer JavaDoc) server.getAttribute(poolServiceName, "MaxSize");
153             freePoolSize = new BoundedRangeStatisticImpl("FreePoolSize", "1",
154                     "The free connection count", 0, max.longValue());
155             poolSize = new BoundedRangeStatisticImpl("PoolSize", "1",
156                     "The connection count", 0, max.longValue());
157             poolStats = new JCAConnectionPoolStatsImpl(getobjectName(), jsr77MCFName,
158                     waitTime, useTime, closeCount, createCount, freePoolSize, poolSize,
159                     waitingThreadCount);
160          }
161          createCount = (CountStatisticImpl) poolStats.getCreateCount();
162          closeCount = (CountStatisticImpl) poolStats.getCloseCount();
163          freePoolSize = (BoundedRangeStatisticImpl) poolStats.getFreePoolSize();
164          poolSize = (BoundedRangeStatisticImpl) poolStats.getPoolSize();
165
166          // Update the stats
167
Integer JavaDoc isize = (Integer JavaDoc) server.getAttribute(poolServiceName, "ConnectionCreatedCount");
168          createCount.set(isize.longValue());
169          isize = (Integer JavaDoc) server.getAttribute(poolServiceName, "ConnectionDestroyedCount");
170          closeCount.set(isize.longValue());
171          isize = (Integer JavaDoc) server.getAttribute(poolServiceName, "ConnectionCount");
172          poolSize.set(isize.longValue());
173          Long JavaDoc lsize = (Long JavaDoc) server.getAttribute(poolServiceName, "AvailableConnectionCount");
174          freePoolSize.set(lsize.longValue());
175       }
176       catch (Exception JavaDoc e)
177       {
178          log.debug("Failed to update JCAConnectionPoolStats", e);
179       }
180
181       return poolStats;
182    }
183
184    // java.lang.Object overrides ------------------------------------
185

186    public String JavaDoc toString()
187    {
188       return "JCAConnectionFactory { " + super.toString() + " } [ " +
189               " ]";
190    }
191
192    // Package protected ---------------------------------------------
193

194    // Protected -----------------------------------------------------
195

196    /**
197     * @return A hashtable with the JCAResource and J2EEServer
198     */

199    protected Hashtable JavaDoc getParentKeys(ObjectName JavaDoc parentName)
200    {
201       Hashtable JavaDoc keys = new Hashtable JavaDoc();
202       Hashtable JavaDoc nameProps = parentName.getKeyPropertyList();
203       String JavaDoc factoryName = (String JavaDoc) nameProps.get("name");
204       String JavaDoc serverName = (String JavaDoc) nameProps.get(J2EETypeConstants.J2EEServer);
205       keys.put(J2EETypeConstants.J2EEServer, serverName);
206       keys.put(J2EETypeConstants.JCAResource, factoryName);
207       return keys;
208    }
209
210 }
211
Popular Tags