KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36
37 /**
38  * The JBoss JSR-77.3.16 implementation of the WebModule model
39  *
40  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</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 WebModule extends J2EEModule
46    implements WebModuleMBean
47 {
48
49    private static final String JavaDoc[] eventTypes = {NotificationConstants.OBJECT_CREATED,
50                                                NotificationConstants.OBJECT_DELETED};
51
52    // Attributes ----------------------------------------------------
53
private static Logger log = Logger.getLogger(WebModule.class);
54
55    /**
56     * list of Servlet names as strings
57     */

58    private List JavaDoc servletNames = new ArrayList JavaDoc();
59
60    private String JavaDoc jbossWebDD;
61
62    /**
63     * used to see if we should remove our parent when we are destroyed.
64     */

65    private static final Map JavaDoc fakeJ2EEApps = new HashMap JavaDoc();
66
67    // Static --------------------------------------------------------
68

69    /**
70     * Creates the JSR-77 WebModule
71     *
72     * @param mbeanServer MBeanServer the WebModule is created on
73     * @param earName Name of the Application but if null object then it
74     * is a standalone module (no EAR wrapper around)
75     * @param warName Name of the war
76     * @param pURL URL path to the local deployment of the module (where to find the DD file)
77     * @param webContainerName the JBoss web container mbean name
78     */

79    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer,
80                                    String JavaDoc earName,
81                                    String JavaDoc warName,
82                                    URL JavaDoc pURL,
83                                    ObjectName JavaDoc webContainerName)
84    {
85       String JavaDoc webXml = null;
86       String JavaDoc jbossWebXml = null;
87       ObjectName JavaDoc jsr77ParentName = null;
88       ObjectName JavaDoc lCreated = null;
89       ObjectName JavaDoc j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
90       ObjectName JavaDoc jsr77Name = null;
91       try
92       {
93          // Get the J2EEServer name
94
Hashtable JavaDoc props = j2eeServerName.getKeyPropertyList();
95          String JavaDoc j2eeServer = props.get(J2EEManagedObject.TYPE) + "=" +
96                  props.get("name");
97
98
99          if (earName == null)
100          {
101             // If there is no ear use the J2EEServer as the parent
102
jsr77ParentName = j2eeServerName;
103          }
104          else
105          {
106             // Query for the J2EEApplication matching earName
107
ObjectName JavaDoc lApplicationQuery = new ObjectName JavaDoc(J2EEDomain.getDomainName() + ":" +
108                     J2EEManagedObject.TYPE + "=" + J2EETypeConstants.J2EEApplication + "," +
109                     "name=" + earName + "," +
110                     j2eeServer + "," +
111                     "*");
112             Set JavaDoc lApplications = mbeanServer.queryNames(lApplicationQuery, null);
113
114             if (lApplications.isEmpty())
115             {
116                lCreated = J2EEApplication.create(mbeanServer,
117                        earName,
118                        null);
119                jsr77ParentName = lCreated;
120             } // end of if ()
121
else if (lApplications.size() == 1)
122             {
123                jsr77ParentName = (ObjectName JavaDoc) lApplications.iterator().next();
124             } // end of if ()
125
}
126
127          // Get the J2EE deployement descriptor
128
webXml = J2EEDeployedObject.getDeploymentDescriptor(pURL, J2EEDeployedObject.WEB);
129          // Get the JBoss Web deployement descriptor
130
jbossWebXml = J2EEDeployedObject.getDeploymentDescriptor(pURL,
131             J2EEDeployedObject.JBOSS_WEB);
132       }
133       catch (Exception JavaDoc e)
134       {
135          log.error("Could not create JSR-77 WebModule: " + warName, e);
136          return null;
137       }
138
139       try
140       {
141          // Get JVM of the j2eeServer
142
String JavaDoc[] jvms = (String JavaDoc[]) mbeanServer.getAttribute(j2eeServerName,
143                  "javaVMs");
144
145          WebModule webModule = new WebModule(warName, jsr77ParentName, jvms, webXml,
146                  webContainerName, jbossWebXml);
147          jsr77Name = webModule.getObjectName();
148          mbeanServer.registerMBean(webModule, jsr77Name);
149          //remember if we created our parent, if we did we have to kill it on destroy.
150
if (lCreated != null)
151          {
152             fakeJ2EEApps.put(jsr77Name, lCreated);
153          } // end of if ()
154
log.debug("Created JSR-77 WebModule: " + jsr77Name);
155       }
156       catch (Exception JavaDoc e)
157       {
158          log.debug("Could not create JSR-77 WebModule: " + warName, e);
159          return null;
160       }
161       return jsr77Name;
162    }
163
164    /**
165     * Destroy a JSR-77 WebModule
166     *
167     * @param mbeanServer The JMX MBeanServer the desired WebModule is registered on
168     * @param jsr77Name the JSR77 EJBModule component ObjectName
169     */

170    public static void destroy(MBeanServer JavaDoc mbeanServer, ObjectName JavaDoc jsr77Name)
171    {
172       try
173       {
174          mbeanServer.unregisterMBean(jsr77Name);
175          log.debug("Remove JSR-77 WebModule: " + jsr77Name);
176          ObjectName JavaDoc jsr77ParentName = (ObjectName JavaDoc) fakeJ2EEApps.get(jsr77Name);
177          if (jsr77ParentName != null)
178          {
179             log.debug("Remove fake JSR-77 parent Application: " + jsr77ParentName);
180             J2EEApplication.destroy(mbeanServer, jsr77ParentName);
181          }
182       }
183       catch (Exception JavaDoc e)
184       {
185          log.debug("Could not destroy JSR-77 WebModule: " + jsr77Name, e);
186       }
187    }
188
189    // Constructors --------------------------------------------------
190

191    /**
192     * Constructor taking the Name of this Object
193     *
194     * @param warName Name to be set which must not be null
195     * @param j2eeAppName the name of the parent JSR77 model component
196     * @param jvms the names of the deployment env JVM JSR77 model components
197     * @param webDD the web.xml descriptor text
198     * @param webContainerName the JBoss web container service name for the war
199     * @param jbossWebDD the jboss-web.xml descriptor text
200     */

201    public WebModule(String JavaDoc warName, ObjectName JavaDoc j2eeAppName, String JavaDoc[] jvms,
202                     String JavaDoc webDD, ObjectName JavaDoc webContainerName, String JavaDoc jbossWebDD)
203            throws MalformedObjectNameException JavaDoc,
204            InvalidParentException
205    {
206       super(J2EETypeConstants.WebModule, warName, j2eeAppName, jvms, webDD);
207       this.jbossWebDD = (jbossWebDD == null ? "" : jbossWebDD);
208    }
209
210    // Public --------------------------------------------------------
211

212    /**
213     * Return the associated servlet names as Strings.
214     *
215     * @jmx:managed-attribute
216     */

217    public String JavaDoc[] getservlets()
218    {
219       String JavaDoc[] servlets = new String JavaDoc[servletNames.size()];
220       servletNames.toArray(servlets);
221       return servlets;
222    }
223
224    /**
225     * @jmx:managed-operation
226     */

227    public String JavaDoc getservlet(int pIndex)
228    {
229       if (pIndex >= 0 && pIndex < servletNames.size())
230       {
231          return (String JavaDoc) servletNames.get(pIndex);
232       }
233       else
234       {
235          return null;
236       }
237    }
238
239    /**
240     * @jmx:managed-attribute
241     */

242    public String JavaDoc getjbossWebDeploymentDescriptor()
243    {
244       return jbossWebDD;
245    }
246
247    // J2EEManagedObjectMBean implementation -------------------------
248

249    public void addChild(ObjectName JavaDoc pChild)
250    {
251       String JavaDoc lType = J2EEManagedObject.getType(pChild);
252       if (J2EETypeConstants.Servlet.equals(lType)
253       )
254       {
255          servletNames.add(pChild.getCanonicalName());
256       }
257    }
258
259    public void removeChild(ObjectName JavaDoc pChild)
260    {
261       String JavaDoc lType = J2EEManagedObject.getType(pChild);
262       if (J2EETypeConstants.Servlet.equals(lType))
263       {
264          servletNames.remove(pChild.getCanonicalName());
265       }
266    }
267
268    // javax.managment.j2ee.EventProvider implementation -------------
269

270    public String JavaDoc[] getEventTypes()
271    {
272       return eventTypes;
273    }
274
275    public String JavaDoc getEventType(int index)
276    {
277       String JavaDoc type = null;
278       if (index >= 0 && index < eventTypes.length)
279       {
280          type = eventTypes[index];
281       }
282       return type;
283    }
284
285    public void postCreation()
286    {
287       sendNotification(NotificationConstants.OBJECT_CREATED, "Web module created");
288    }
289
290    public void preDestruction()
291    {
292       sendNotification(NotificationConstants.OBJECT_DELETED, "Web module destroyed");
293    }
294
295    // Object overrides ---------------------------------------------------
296

297    public String JavaDoc toString()
298    {
299       return "WebModule[ " + super.toString() +
300               ", Servlets: " + servletNames +
301               ", JBoss-Web-DD: " + jbossWebDD +
302               " ]";
303    }
304
305    // Package protected ---------------------------------------------
306

307    // Protected -----------------------------------------------------
308

309    /**
310     * @param jsr77ParentName the WebModule parent's JSR77 ObjectName
311     * @return A hashtable with the J2EE-Application and J2EE-Server as parent
312     */

313    protected Hashtable JavaDoc getParentKeys(ObjectName JavaDoc jsr77ParentName)
314    {
315       Hashtable JavaDoc parentKeys = new Hashtable JavaDoc();
316       Hashtable JavaDoc parentProps = jsr77ParentName.getKeyPropertyList();
317       String JavaDoc parentName = (String JavaDoc) parentProps.get("name");
318       String JavaDoc j2eeType = (String JavaDoc) parentProps.get(J2EEManagedObject.TYPE);
319
320       // Check if parent is a J2EEServer or J2EEApplication
321
if (j2eeType.equals(J2EETypeConstants.J2EEApplication) == false)
322       {
323          // J2EEServer
324
parentKeys.put(J2EETypeConstants.J2EEServer, parentName);
325          parentKeys.put(J2EETypeConstants.J2EEApplication, "null");
326       }
327       else
328       {
329          // J2EEApplication
330
parentKeys.put(J2EETypeConstants.J2EEApplication, parentName);
331          String JavaDoc j2eeServerName = (String JavaDoc) parentProps.get(J2EETypeConstants.J2EEServer);
332          parentKeys.put(J2EETypeConstants.J2EEServer, j2eeServerName);
333       }
334
335       return parentKeys;
336    }
337
338    // Private -------------------------------------------------------
339

340    // Inner classes -------------------------------------------------
341

342 }
343
344
Popular Tags