KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MalformedObjectNameException JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.security.InvalidParameterException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37
38 /**
39  * Root class of the JBoss JSR-77 implementation of ResourceAdapterModule.
40  *
41  * @author <a HREF="mailto:mclaugs@comcast.net">Scott McLaughlin</a>.
42  * @author <a HREF="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
43  * @version $Revision: 40550 $
44  */

45 public class ResourceAdapterModule extends J2EEModule
46    implements ResourceAdapterModuleMBean
47 {
48
49    // Constants -----------------------------------------------------
50

51    // Attributes ----------------------------------------------------
52
private static Logger log = Logger.getLogger(ResourceAdapterModule.class);
53
54    // list of object names as strings
55
private List JavaDoc resourceAdapters = new ArrayList JavaDoc();
56
57    /**
58     * The JSR77 ObjectNames of fake J2EEApplications created by standalone jars
59     */

60    private static final Map JavaDoc fakeJ2EEApps = new HashMap JavaDoc();
61
62    // Static --------------------------------------------------------
63

64    /**
65     * Creates the JSR-77 EJBModule
66     *
67     * @param mbeanServer MBeanServer the EJBModule is created on
68     * @param earName the ear name unless null which indicates a standalone module (no EAR)
69     * @param rarName the RAR name
70     * @param pURL URL path to the local deployment of the module (where to find the DD file)
71     * @return the JSR77 ObjectName of the RARModule
72     */

73    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer, String JavaDoc earName,
74                                    String JavaDoc rarName, URL JavaDoc pURL)
75    {
76       String JavaDoc lDD = null;
77       ObjectName JavaDoc lParent = null;
78       ObjectName JavaDoc lCreated = null;
79       ObjectName JavaDoc jsr77Name = null;
80       // Get the J2EEServer name
81
ObjectName JavaDoc j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
82
83       try
84       {
85          Hashtable JavaDoc props = j2eeServerName.getKeyPropertyList();
86          String JavaDoc j2eeServer = props.get(J2EEManagedObject.TYPE) + "=" +
87                  props.get("name");
88
89          // if pName is equal to pApplicationName then we have
90
// a stand alone Module so do not create a J2EEApplication
91
if (earName == null)
92          {
93             // If there is no ear use the J2EEServer as the parent
94
lParent = j2eeServerName;
95          }
96          else
97          {
98             ObjectName JavaDoc parentAppQuery = new ObjectName JavaDoc(J2EEDomain.getDomainName() + ":" +
99                     J2EEManagedObject.TYPE + "=" + J2EETypeConstants.J2EEApplication + "," +
100                     "name=" + earName + "," +
101                     j2eeServer + "," +
102                     "*");
103             Set JavaDoc parentApps = mbeanServer.queryNames(parentAppQuery, null);
104
105             if (parentApps.size() == 0)
106             {
107                lCreated = J2EEApplication.create(mbeanServer,
108                        earName,
109                        null);
110                lParent = lCreated;
111             } // end of if ()
112
else if (parentApps.size() == 1)
113             {
114                lParent = (ObjectName JavaDoc) parentApps.iterator().next();
115             } // end of if ()
116
}
117
118          // Get the J2EE deployement descriptor
119
lDD = J2EEDeployedObject.getDeploymentDescriptor(pURL, J2EEDeployedObject.RAR);
120       }
121       catch (Exception JavaDoc e)
122       {
123          log.debug("Could not create JSR-77 ResourceAdapterModule: " + rarName, e);
124          return null;
125       }
126
127       try
128       {
129          // Get JVM of the j2eeServer
130
String JavaDoc[] jvms = (String JavaDoc[]) mbeanServer.getAttribute(j2eeServerName,
131                  "javaVMs");
132
133          // Now create the ResourceAdapterModule
134
ResourceAdapterModule rarModule = new ResourceAdapterModule(rarName,
135                  lParent, jvms, lDD);
136          jsr77Name = rarModule.getObjectName();
137          mbeanServer.registerMBean(rarModule, jsr77Name);
138
139          if (lCreated != null)
140          {
141             fakeJ2EEApps.put(jsr77Name, lCreated);
142          }
143          log.debug("Created JSR-77 EJBModule: " + jsr77Name);
144       }
145       catch (Exception JavaDoc e)
146       {
147          log.debug("Could not create JSR-77 ResourceAdapterModule: " + rarName, e);
148       }
149       return jsr77Name;
150    }
151
152    /**
153     * Destroyes the given JSR-77 RARModule
154     *
155     * @param mbeanServer The JMX MBeanServer the desired RARModule is registered on
156     * @param jsr77Name the JSR77 RARModule component ObjectName
157     */

158    public static void destroy(MBeanServer JavaDoc mbeanServer, ObjectName JavaDoc jsr77Name)
159    {
160       try
161       {
162          log.debug("destroy(), remove RARModule: " + jsr77Name);
163          mbeanServer.unregisterMBean(jsr77Name);
164
165          ObjectName JavaDoc jsr77ParentName = (ObjectName JavaDoc) fakeJ2EEApps.get(jsr77Name);
166          if (jsr77ParentName != null)
167          {
168             log.debug("Remove fake JSR-77 parent Application: " + jsr77ParentName);
169             J2EEApplication.destroy(mbeanServer, jsr77ParentName);
170          }
171       }
172       catch (Exception JavaDoc e)
173       {
174          log.debug("Could not destroy JSR-77 RARModule: " + jsr77Name, e);
175       }
176    }
177
178    // Constructors --------------------------------------------------
179

180    /**
181     * Constructor taking the Name of this Object
182     *
183     * @param rarName Name to be set which must not be null
184     * @param jsr77ParentName ObjectName of the Parent this Module belongs
185     * too. Either it is a J2EEApplication or J2EEServer
186     * if a standalone module.
187     * @param pDeploymentDescriptor
188     * @throws InvalidParameterException If the given Name is null
189     */

190    public ResourceAdapterModule(String JavaDoc rarName, ObjectName JavaDoc jsr77ParentName,
191                                 String JavaDoc[] pJVMs, String JavaDoc pDeploymentDescriptor)
192            throws MalformedObjectNameException JavaDoc,
193            InvalidParentException
194    {
195       super(J2EETypeConstants.ResourceAdapterModule, rarName, jsr77ParentName, pJVMs, pDeploymentDescriptor);
196    }
197
198    // Public --------------------------------------------------------
199

200    // ResourceAdapterodule implementation --------------------------------------
201

202    /**
203     * @jmx:managed-attribute
204     */

205    public String JavaDoc[] getresourceAdapters()
206    {
207       return (String JavaDoc[]) resourceAdapters.toArray(new String JavaDoc[resourceAdapters.size()]);
208    }
209
210    /**
211     * @jmx:managed-operation
212     */

213    public String JavaDoc getresourceAdapter(int pIndex)
214    {
215       if (pIndex >= 0 && pIndex < resourceAdapters.size())
216       {
217          return (String JavaDoc) resourceAdapters.get(pIndex);
218       }
219       else
220       {
221          return null;
222       }
223    }
224
225    // J2EEManagedObjectMBean implementation -------------------------
226

227    public void addChild(ObjectName JavaDoc pChild)
228    {
229       String JavaDoc lType = J2EEManagedObject.getType(pChild);
230       if (J2EETypeConstants.ResourceAdapter.equals(lType))
231       {
232          resourceAdapters.add(pChild.getCanonicalName());
233       }
234    }
235
236    public void removeChild(ObjectName JavaDoc pChild)
237    {
238       String JavaDoc lType = J2EEManagedObject.getType(pChild);
239       if (J2EETypeConstants.ResourceAdapter.equals(lType))
240       {
241          resourceAdapters.remove(pChild.getCanonicalName());
242       }
243    }
244
245    // Object overrides ---------------------------------------------------
246

247    public String JavaDoc toString()
248    {
249       return "ResourceAdapterModule[ " + super.toString() +
250               "ResourceAdapters: " + resourceAdapters +
251               " ]";
252    }
253
254    // Package protected ---------------------------------------------
255

256    // Protected -----------------------------------------------------
257

258    /**
259     * @param jsr77ParentName the WebModule parent's JSR77 ObjectName
260     * @return A hashtable with the J2EE-Application and J2EE-Server as parent
261     */

262    protected Hashtable JavaDoc getParentKeys(ObjectName JavaDoc jsr77ParentName)
263    {
264       Hashtable JavaDoc parentKeys = new Hashtable JavaDoc();
265       Hashtable JavaDoc parentProps = jsr77ParentName.getKeyPropertyList();
266       String JavaDoc parentName = (String JavaDoc) parentProps.get("name");
267       String JavaDoc j2eeType = (String JavaDoc) parentProps.get(J2EEManagedObject.TYPE);
268
269       // Check if parent is a J2EEServer or J2EEApplication
270
if (j2eeType.equals(J2EETypeConstants.J2EEApplication) == false)
271       {
272          // J2EEServer
273
parentKeys.put(J2EETypeConstants.J2EEServer, parentName);
274          parentKeys.put(J2EETypeConstants.J2EEApplication, "null");
275       }
276       else
277       {
278          // J2EEApplication
279
parentKeys.put(J2EETypeConstants.J2EEApplication, parentName);
280          String JavaDoc j2eeServerName = (String JavaDoc) parentProps.get(J2EETypeConstants.J2EEServer);
281          parentKeys.put(J2EETypeConstants.J2EEServer, j2eeServerName);
282       }
283
284       return parentKeys;
285    }
286
287    // Private -------------------------------------------------------
288

289    // Inner classes -------------------------------------------------
290
}
291
Popular Tags