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;
023:
024: import org.jboss.logging.Logger;
025:
026: import javax.management.MBeanServer;
027: import javax.management.MalformedObjectNameException;
028: import javax.management.ObjectName;
029: import java.net.URL;
030: import java.security.InvalidParameterException;
031: import java.util.ArrayList;
032: import java.util.HashMap;
033: import java.util.Hashtable;
034: import java.util.List;
035: import java.util.Map;
036: import java.util.Set;
037:
038: /**
039: * Root class of the JBoss JSR-77 implementation of ResourceAdapterModule.
040: *
041: * @author <a href="mailto:mclaugs@comcast.net">Scott McLaughlin</a>.
042: * @author <a href="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
043: * @version $Revision: 57197 $
044: */
045: public class ResourceAdapterModule extends J2EEModule implements
046: ResourceAdapterModuleMBean {
047:
048: // Constants -----------------------------------------------------
049:
050: // Attributes ----------------------------------------------------
051: private static Logger log = Logger
052: .getLogger(ResourceAdapterModule.class);
053:
054: // list of object names as strings
055: private List resourceAdapters = new ArrayList();
056:
057: /**
058: * The JSR77 ObjectNames of fake J2EEApplications created by standalone jars
059: */
060: private static final Map fakeJ2EEApps = new HashMap();
061:
062: // Static --------------------------------------------------------
063:
064: /**
065: * Creates the JSR-77 EJBModule
066: *
067: * @param mbeanServer MBeanServer the EJBModule is created on
068: * @param earName the ear name unless null which indicates a standalone module (no EAR)
069: * @param rarName the RAR name
070: * @param pURL URL path to the local deployment of the module (where to find the DD file)
071: * @return the JSR77 ObjectName of the RARModule
072: */
073: public static ObjectName create(MBeanServer mbeanServer,
074: String earName, String rarName, URL pURL) {
075: String lDD = null;
076: ObjectName lParent = null;
077: ObjectName lCreated = null;
078: ObjectName jsr77Name = null;
079: // Get the J2EEServer name
080: ObjectName j2eeServerName = J2EEDomain
081: .getDomainServerName(mbeanServer);
082:
083: try {
084: Hashtable props = j2eeServerName.getKeyPropertyList();
085: String j2eeServer = props.get(J2EEManagedObject.TYPE) + "="
086: + props.get("name");
087:
088: // if pName is equal to pApplicationName then we have
089: // a stand alone Module so do not create a J2EEApplication
090: if (earName == null) {
091: // If there is no ear use the J2EEServer as the parent
092: lParent = j2eeServerName;
093: } else {
094: ObjectName parentAppQuery = new ObjectName(J2EEDomain
095: .getDomainName()
096: + ":"
097: + J2EEManagedObject.TYPE
098: + "="
099: + J2EETypeConstants.J2EEApplication
100: + ","
101: + "name="
102: + earName
103: + ","
104: + j2eeServer
105: + ","
106: + "*");
107: Set parentApps = mbeanServer.queryNames(parentAppQuery,
108: null);
109:
110: if (parentApps.size() == 0) {
111: lCreated = J2EEApplication.create(mbeanServer,
112: earName, null);
113: lParent = lCreated;
114: } // end of if ()
115: else if (parentApps.size() == 1) {
116: lParent = (ObjectName) parentApps.iterator().next();
117: } // end of if ()
118: }
119:
120: // Get the J2EE deployement descriptor
121: lDD = J2EEDeployedObject.getDeploymentDescriptor(pURL,
122: J2EEDeployedObject.RAR);
123: } catch (Exception e) {
124: log.debug("Could not create JSR-77 ResourceAdapterModule: "
125: + rarName, e);
126: return null;
127: }
128:
129: try {
130: // Get JVM of the j2eeServer
131: String[] jvms = (String[]) mbeanServer.getAttribute(
132: j2eeServerName, "javaVMs");
133:
134: // Now create the ResourceAdapterModule
135: ResourceAdapterModule rarModule = new ResourceAdapterModule(
136: rarName, lParent, jvms, lDD);
137: jsr77Name = rarModule.getObjectName();
138: mbeanServer.registerMBean(rarModule, jsr77Name);
139:
140: if (lCreated != null) {
141: fakeJ2EEApps.put(jsr77Name, lCreated);
142: }
143: log.debug("Created JSR-77 EJBModule: " + jsr77Name);
144: } catch (Exception e) {
145: log.debug("Could not create JSR-77 ResourceAdapterModule: "
146: + rarName, e);
147: }
148: return jsr77Name;
149: }
150:
151: /**
152: * Destroyes the given JSR-77 RARModule
153: *
154: * @param mbeanServer The JMX MBeanServer the desired RARModule is registered on
155: * @param jsr77Name the JSR77 RARModule component ObjectName
156: */
157: public static void destroy(MBeanServer mbeanServer,
158: ObjectName jsr77Name) {
159: try {
160: log.debug("destroy(), remove RARModule: " + jsr77Name);
161: mbeanServer.unregisterMBean(jsr77Name);
162:
163: ObjectName jsr77ParentName = (ObjectName) fakeJ2EEApps
164: .get(jsr77Name);
165: if (jsr77ParentName != null) {
166: log.debug("Remove fake JSR-77 parent Application: "
167: + jsr77ParentName);
168: J2EEApplication.destroy(mbeanServer, jsr77ParentName);
169: }
170: } catch (Exception e) {
171: log.debug("Could not destroy JSR-77 RARModule: "
172: + jsr77Name, e);
173: }
174: }
175:
176: // Constructors --------------------------------------------------
177:
178: /**
179: * Constructor taking the Name of this Object
180: *
181: * @param rarName Name to be set which must not be null
182: * @param jsr77ParentName ObjectName of the Parent this Module belongs
183: * too. Either it is a J2EEApplication or J2EEServer
184: * if a standalone module.
185: * @param pDeploymentDescriptor
186: * @throws InvalidParameterException If the given Name is null
187: */
188: public ResourceAdapterModule(String rarName,
189: ObjectName jsr77ParentName, String[] pJVMs,
190: String pDeploymentDescriptor)
191: throws MalformedObjectNameException, InvalidParentException {
192: super (J2EETypeConstants.ResourceAdapterModule, rarName,
193: jsr77ParentName, pJVMs, pDeploymentDescriptor);
194: }
195:
196: // Public --------------------------------------------------------
197:
198: // ResourceAdapterodule implementation --------------------------------------
199:
200: /**
201: * @jmx:managed-attribute
202: */
203: public String[] getresourceAdapters() {
204: return (String[]) resourceAdapters
205: .toArray(new String[resourceAdapters.size()]);
206: }
207:
208: /**
209: * @jmx:managed-operation
210: */
211: public String getresourceAdapter(int pIndex) {
212: if (pIndex >= 0 && pIndex < resourceAdapters.size()) {
213: return (String) resourceAdapters.get(pIndex);
214: } else {
215: return null;
216: }
217: }
218:
219: // J2EEManagedObjectMBean implementation -------------------------
220:
221: public void addChild(ObjectName pChild) {
222: String lType = J2EEManagedObject.getType(pChild);
223: if (J2EETypeConstants.ResourceAdapter.equals(lType)) {
224: resourceAdapters.add(pChild.getCanonicalName());
225: }
226: }
227:
228: public void removeChild(ObjectName pChild) {
229: String lType = J2EEManagedObject.getType(pChild);
230: if (J2EETypeConstants.ResourceAdapter.equals(lType)) {
231: resourceAdapters.remove(pChild.getCanonicalName());
232: }
233: }
234:
235: // Object overrides ---------------------------------------------------
236:
237: public String toString() {
238: return "ResourceAdapterModule[ " + super .toString()
239: + "ResourceAdapters: " + resourceAdapters + " ]";
240: }
241:
242: // Package protected ---------------------------------------------
243:
244: // Protected -----------------------------------------------------
245:
246: /**
247: * @param jsr77ParentName the WebModule parent's JSR77 ObjectName
248: * @return A hashtable with the J2EE-Application and J2EE-Server as parent
249: */
250: protected Hashtable getParentKeys(ObjectName jsr77ParentName) {
251: Hashtable parentKeys = new Hashtable();
252: Hashtable parentProps = jsr77ParentName.getKeyPropertyList();
253: String parentName = (String) parentProps.get("name");
254: String j2eeType = (String) parentProps
255: .get(J2EEManagedObject.TYPE);
256:
257: // Check if parent is a J2EEServer or J2EEApplication
258: if (j2eeType.equals(J2EETypeConstants.J2EEApplication) == false) {
259: // J2EEServer
260: parentKeys.put(J2EETypeConstants.J2EEServer, parentName);
261: parentKeys.put(J2EETypeConstants.J2EEApplication, "null");
262: } else {
263: // J2EEApplication
264: parentKeys.put(J2EETypeConstants.J2EEApplication,
265: parentName);
266: String j2eeServerName = (String) parentProps
267: .get(J2EETypeConstants.J2EEServer);
268: parentKeys
269: .put(J2EETypeConstants.J2EEServer, j2eeServerName);
270: }
271:
272: return parentKeys;
273: }
274:
275: // Private -------------------------------------------------------
276:
277: // Inner classes -------------------------------------------------
278: }
|