001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.management.j2ee.factory;
023:
024: import org.jboss.deployment.DeploymentInfo;
025: import org.jboss.logging.Logger;
026: import org.jboss.management.j2ee.JCAResource;
027:
028: import javax.management.MBeanServer;
029: import javax.management.ObjectName;
030: import java.util.Iterator;
031:
032: /**
033: * A factory for mapping DataSourceDeployer deployments to JCAResource
034: *
035: * @author Scott.Stark@jboss.org
036: * @version $Revision: 57197 $
037: */
038: public class JCAResourceFactory implements ManagedObjectFactory {
039: private static Logger log = Logger
040: .getLogger(JCAResourceFactory.class);
041:
042: /**
043: * Creates a JCAResource
044: *
045: * @param mbeanServer
046: * @param data A MBeanServerNotification
047: * @return the JCAResource ObjectName
048: */
049: public ObjectName create(MBeanServer mbeanServer, Object data) {
050: if ((data instanceof DeploymentInfo) == false)
051: return null;
052:
053: DeploymentInfo di = (DeploymentInfo) data;
054: ObjectName jsr77Name = null;
055:
056: /* Get the RARDeployment service name by looking for the mbean in the
057: deployment with a name matching service=xxxDS. This relies on the naming
058: pattern created by the JCA CM deployer.
059: */
060: ObjectName rarDeployService = null;
061: ObjectName cmService = null;
062: ObjectName poolService = null;
063: Iterator iter = di.mbeans.iterator();
064: while (iter.hasNext()) {
065: ObjectName oname = (ObjectName) iter.next();
066: String name = oname.getKeyProperty("service");
067: if (name.equals("ManagedConnectionFactory")
068: || name.endsWith("DS"))
069: rarDeployService = oname;
070: else if (name.endsWith("CM"))
071: cmService = oname;
072: else if (name.endsWith("Pool"))
073: poolService = oname;
074: }
075: if (rarDeployService == null || cmService == null) {
076: log.debug("Failed to find a service=xxxDS match");
077: return null;
078: }
079:
080: try {
081: /* Now to tie this CM back to its rar query the rarDeployService for
082: the org.jboss.resource.RARDeployment service created by the RARDeployer.
083: */
084: ObjectName rarService = (ObjectName) mbeanServer
085: .getAttribute(rarDeployService, "OldRarDeployment");
086: // Get the ResourceAdapter JSR77 name
087: ObjectName jsr77RAName = RARModuleFactory
088: .getResourceAdapterName(rarService);
089: // Now build the JCAResource
090: String resName = rarDeployService.getKeyProperty("name");
091: jsr77Name = JCAResource.create(mbeanServer, resName,
092: jsr77RAName, cmService, rarDeployService,
093: poolService);
094: } catch (Exception e) {
095: log.debug("Failed to create JCAResource", e);
096: }
097:
098: return jsr77Name;
099: }
100:
101: /**
102: * Destroys the JCAResource
103: *
104: * @param mbeanServer
105: * @param data A MBeanServerNotification
106: */
107: public void destroy(MBeanServer mbeanServer, Object data) {
108: if ((data instanceof DeploymentInfo) == false)
109: return;
110:
111: DeploymentInfo di = (DeploymentInfo) data;
112: String resName = getDeploymentResName(mbeanServer, di);
113: JCAResource.destroy(mbeanServer, resName);
114: }
115:
116: private String getDeploymentResName(MBeanServer mbeanServer,
117: DeploymentInfo di) {
118: String resName = null;
119: /* Get the RARDeployment service name by looking for the mbean in the
120: deployment with a name matching service=xxxDS. This relies on the naming
121: pattern created by the JCA CM deployer.
122: */
123: ObjectName rarDeployService = null;
124: Iterator iter = di.mbeans.iterator();
125: while (iter.hasNext()) {
126: ObjectName oname = (ObjectName) iter.next();
127: String name = oname.getKeyProperty("service");
128: if (name.equals("ManagedConnectionFactory")
129: || name.endsWith("DS")) {
130: rarDeployService = oname;
131: resName = rarDeployService.getKeyProperty("name");
132: break;
133: }
134: }
135: return resName;
136: }
137: }
|