KickJava   Java API By Example, From Geeks To Geeks.

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


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.JCAConnectionPoolStatsImpl;
26 import org.jboss.management.j2ee.statistics.JCAStatsImpl;
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 import java.util.ArrayList JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Set JavaDoc;
36
37 /**
38  * The JBoss JSR-77.3.22 JCAResource model implementation
39  *
40  * @author <a HREF="mailto:mclaugs@comcast.com">Scott McLaughlin</a>.
41  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
42  * @author <a HREF="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
43  * @version $Revision: 40550 $
44  */

45 public class JCAResource extends J2EEResource
46    implements JCAResourceMBean
47 {
48    // Constants -----------------------------------------------------
49
private static Logger log = Logger.getLogger(JCAResource.class);
50
51    // Attributes ----------------------------------------------------
52

53    // list of object names as strings
54
private List JavaDoc connectionFactories = new ArrayList JavaDoc();
55
56    private ObjectName JavaDoc cmServiceName;
57    private ObjectName JavaDoc poolServiceName;
58    private JCAStatsImpl stats;
59
60    // Static --------------------------------------------------------
61

62    /**
63     * Create a JCAResource
64     *
65     * @param mbeanServer
66     * @param resName
67     * @param jsr77RAParentName
68     * @param cmServiceName
69     * @param mcfServiceName
70     * @param poolServiceName
71     * @return
72     */

73    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer, String JavaDoc resName,
74                                    ObjectName JavaDoc jsr77RAParentName, ObjectName JavaDoc cmServiceName,
75                                    ObjectName JavaDoc mcfServiceName, ObjectName JavaDoc poolServiceName)
76    {
77       ObjectName JavaDoc jsr77Name = null;
78       try
79       {
80          JCAResource jcaRes = new JCAResource(resName, jsr77RAParentName,
81                  cmServiceName, poolServiceName);
82          jsr77Name = jcaRes.getObjectName();
83          mbeanServer.registerMBean(jcaRes, jsr77Name);
84          log.debug("Created JSR-77 JCAResource: " + resName);
85
86          // Create a JCAConnectionFactory and JCAManagedConnectionFactory
87
ObjectName JavaDoc jcaFactoryName = JCAConnectionFactory.create(mbeanServer,
88                  resName, jsr77Name, cmServiceName, mcfServiceName);
89       }
90       catch (Exception JavaDoc e)
91       {
92          log.debug("Could not create JSR-77 JCAResource: " + resName, e);
93       }
94       return jsr77Name;
95    }
96
97    public static void destroy(MBeanServer JavaDoc mbeanServer, String JavaDoc resName)
98    {
99       try
100       {
101          // Find the Object to be destroyed
102
ObjectName JavaDoc search = new ObjectName JavaDoc(J2EEDomain.getDomainName() + ":" +
103                  J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JCAResource + "," +
104                  "name=" + resName + "," +
105                  "*");
106          Set JavaDoc resNames = mbeanServer.queryNames(search, null);
107          if( resNames.isEmpty() == false )
108          {
109             ObjectName JavaDoc jcaRes = (ObjectName JavaDoc) resNames.iterator().next();
110             // Now check if the JCAResource does not contains another Connection Factory
111
String JavaDoc[] factories = (String JavaDoc[]) mbeanServer.getAttribute(jcaRes,
112                     "connectionFactories");
113             for(int n = 0; n < factories.length; n ++)
114             {
115                // Remove the JCAConnectionFactory
116
ObjectName JavaDoc cf = new ObjectName JavaDoc(factories[n]);
117                mbeanServer.unregisterMBean(cf);
118                /* Remove the JCAManagedConnectionFactory using a name of the
119                form *:J2EEServer=*,j2eeType=JCAManagedConnectionFactory,name=resName
120                */

121                Hashtable JavaDoc props = cf.getKeyPropertyList();
122                props.put("j2eeType", J2EETypeConstants.JCAManagedConnectionFactory);
123                props.remove(J2EETypeConstants.JCAResource);
124                ObjectName JavaDoc mcf = new ObjectName JavaDoc(cf.getDomain(), props);
125                mbeanServer.unregisterMBean(mcf);
126             }
127             mbeanServer.unregisterMBean(jcaRes);
128          }
129       }
130       catch (Exception JavaDoc e)
131       {
132          log.debug("Could not destroy JSR-77 JCAResource", e);
133       }
134    }
135
136    // Constructors --------------------------------------------------
137

138    /**
139     * @param resName
140     * @param jsr77ParentName
141     * @param cmServiceName
142     * @param poolServiceName
143     * @throws MalformedObjectNameException
144     * @throws InvalidParentException
145     */

146    public JCAResource(String JavaDoc resName, ObjectName JavaDoc jsr77ParentName,
147                       ObjectName JavaDoc cmServiceName, ObjectName JavaDoc poolServiceName)
148            throws MalformedObjectNameException JavaDoc, InvalidParentException
149    {
150       super(J2EETypeConstants.JCAResource, resName, jsr77ParentName);
151       this.cmServiceName = cmServiceName;
152       this.poolServiceName = poolServiceName;
153    }
154
155    // Public --------------------------------------------------------
156

157    // javax.management.j2ee.JCAResource implementation ---------------------
158

159    /**
160     * @jmx:managed-attribute
161     */

162    public String JavaDoc[] getconnectionFactories()
163    {
164       return (String JavaDoc[]) connectionFactories.toArray(new String JavaDoc[connectionFactories.size()]);
165    }
166
167    /**
168     * @jmx:managed-operation
169     */

170    public String JavaDoc getconnectionFactory(int i)
171    {
172       if (i >= 0 && i < connectionFactories.size())
173       {
174          return (String JavaDoc) connectionFactories.get(i);
175       }
176       else
177       {
178          return null;
179       }
180    }
181
182    // J2EEManagedObjectMBean implementation -------------------------
183

184    public void addChild(ObjectName JavaDoc pChild)
185    {
186       String JavaDoc lType = J2EEManagedObject.getType(pChild);
187       if (J2EETypeConstants.JCAConnectionFactory.equals(lType))
188       {
189          connectionFactories.add(pChild.getCanonicalName());
190       }
191    }
192
193    public void removeChild(ObjectName JavaDoc pChild)
194    {
195       String JavaDoc lType = J2EEManagedObject.getType(pChild);
196       if (J2EETypeConstants.JCAConnectionFactory.equals(lType))
197       {
198          connectionFactories.remove(pChild.getCanonicalName());
199       }
200    }
201
202    // Begin StatisticsProvider interface methods
203

204    /**
205     * Obtain the Stats from the StatisticsProvider.
206     *
207     * @return An JCAStats implementation
208     * @jmx:managed-attribute
209     */

210    public Stats JavaDoc getstats()
211    {
212       try
213       {
214          ObjectName JavaDoc jsr77CFName = newObjectName(getconnectionFactory(0));
215          Object JavaDoc[] params = {poolServiceName};
216          String JavaDoc[] sig = {ObjectName JavaDoc.class.getName()};
217          JCAConnectionPoolStatsImpl cfStats = (JCAConnectionPoolStatsImpl)
218                  server.invoke(jsr77CFName, "getPoolStats", params, sig);
219          JCAConnectionPoolStatsImpl[] poolStats = {cfStats};
220          stats = new JCAStatsImpl(null, poolStats);
221       }
222       catch (Exception JavaDoc e)
223       {
224          log.debug("Failed to create JCAStats", e);
225       }
226       return stats;
227    }
228
229    /**
230     * Reset all statistics in the StatisticsProvider
231     *
232     * @jmx:managed-operation
233     */

234    public void resetStats()
235    {
236       if (stats != null)
237          stats.reset();
238    }
239    // End StatisticsProvider interface methods
240

241    // java.lang.Object overrides ------------------------------------
242

243    public String JavaDoc toString()
244    {
245       return "JCAResource { " + super.toString() + " } [ " +
246               "Connection Factories: " + connectionFactories +
247               " ]";
248    }
249
250    /**
251     * @return A hashtable with the J2EEServer and ResourceAdapter
252     */

253    protected Hashtable JavaDoc getParentKeys(ObjectName JavaDoc parentName)
254    {
255       Hashtable JavaDoc keys = new Hashtable JavaDoc();
256       Hashtable JavaDoc nameProps = parentName.getKeyPropertyList();
257       String JavaDoc adapterName = (String JavaDoc) nameProps.get("name");
258       String JavaDoc serverName = (String JavaDoc) nameProps.get(J2EETypeConstants.J2EEServer);
259       keys.put(J2EETypeConstants.J2EEServer, serverName);
260       keys.put(J2EETypeConstants.ResourceAdapter, adapterName);
261       return keys;
262    }
263 }
264
Popular Tags