KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > j2ee > factory > WebModuleFactory


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.factory;
23
24 import org.jboss.deployment.DeploymentInfo;
25 import org.jboss.logging.Logger;
26 import org.jboss.management.j2ee.Servlet;
27 import org.jboss.management.j2ee.WebModule;
28
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 /**
35  * A factory for mapping WARDeployer deployments to WebModules
36  *
37  * @author Scott.Stark@jboss.org
38  * @version $Revision: 37459 $
39  */

40 public class WebModuleFactory
41         implements ManagedObjectFactory
42 {
43    private static Logger log = Logger.getLogger(WebModuleFactory.class);
44    private HashMap JavaDoc deploymentToModuleNameMap = new HashMap JavaDoc();
45    private HashMap JavaDoc containerToModuleNameMap = new HashMap JavaDoc();
46
47    /**
48     * Create JSR-77 WebModule
49     *
50     * @param server the MBeanServer context
51     * @param data arbitrary data associated with the creation context
52     */

53    public ObjectName JavaDoc create(MBeanServer JavaDoc server, Object JavaDoc data)
54    {
55       if ((data instanceof DeploymentInfo) == false)
56          return null;
57
58       DeploymentInfo di = (DeploymentInfo) data;
59       String JavaDoc warName = di.shortName;
60       ObjectName JavaDoc webModuleService = di.deployedObject;
61       String JavaDoc earName = di.parent == null ? null : di.parent.shortName;
62       ObjectName JavaDoc jsr77Name = WebModule.create(server,
63               earName, warName, di.localUrl, webModuleService);
64       deploymentToModuleNameMap.put(di, jsr77Name);
65       Iterator JavaDoc servlets = di.mbeans.iterator();
66       while (servlets.hasNext())
67       {
68          Object JavaDoc next = servlets.next();
69          try
70          {
71             ObjectName JavaDoc servletName = (ObjectName JavaDoc) next;
72             createServlet(server, jsr77Name, servletName);
73          }
74          catch (Throwable JavaDoc e)
75          {
76             log.debug("Failed to create JSR-77 servlet: " + next, e);
77          }
78       }
79       return jsr77Name;
80    }
81
82    /**
83     * Destroy JSR-77 WebModule
84     *
85     * @param server the MBeanServer context
86     * @param data arbitrary data associated with the creation context
87     */

88    public void destroy(MBeanServer JavaDoc server, Object JavaDoc data)
89    {
90       if ((data instanceof DeploymentInfo) == false)
91          return;
92
93       DeploymentInfo di = (DeploymentInfo) data;
94       ObjectName JavaDoc jsr77Name = (ObjectName JavaDoc) deploymentToModuleNameMap.remove(di);
95
96       log.debug("Destroy module: " + jsr77Name);
97       Iterator JavaDoc servlets = di.mbeans.iterator();
98       while (servlets.hasNext())
99       {
100          Object JavaDoc next = servlets.next();
101          try
102          {
103             ObjectName JavaDoc servletName = (ObjectName JavaDoc) next;
104             destroyServlet(server, servletName);
105          }
106          catch (Throwable JavaDoc e)
107          {
108             log.debug("Failed to destroy JSR-77 servlet: " + next, e);
109          }
110       }
111
112       if (jsr77Name != null)
113       {
114          WebModule.destroy(server, jsr77Name);
115       }
116    }
117
118    /**
119     * Create JSR-77 Servlet
120     *
121     * @param mbeanServer the MBeanServer context
122     * @param webModuleName the JSR77 name of the servlet's WebModule
123     * @param servletServiceName The jboss servlet mbean name
124     */

125    public ObjectName JavaDoc createServlet(MBeanServer JavaDoc mbeanServer, ObjectName JavaDoc webModuleName,
126                                    ObjectName JavaDoc servletServiceName)
127    {
128       ObjectName JavaDoc jsr77Name = null;
129       // We don't currently have a web container mbean
130
ObjectName JavaDoc webContainerName = null;
131       try
132       {
133          log.debug("Creating servlet: " + servletServiceName);
134          String JavaDoc servletName = servletServiceName.getKeyProperty("name");
135          if (servletName != null)
136          {
137             // Only treat resources with names as potential servlets
138
jsr77Name = Servlet.create(mbeanServer, webModuleName,
139                     webContainerName, servletServiceName);
140             containerToModuleNameMap.put(servletServiceName, jsr77Name);
141             log.debug("Created servlet: " + servletServiceName + ", module: " + jsr77Name);
142          }
143       }
144       catch (Exception JavaDoc e)
145       {
146          log.debug("Failed to create servlet: " + servletServiceName, e);
147       }
148
149       return jsr77Name;
150    }
151
152    /**
153     * Destroy JSR-77 Servlet
154     *
155     * @param mbeanServer the MBeanServer context
156     * @param servletServiceName The jboss servlet mbean name
157     */

158    public void destroyServlet(MBeanServer JavaDoc server, ObjectName JavaDoc servletServiceName)
159    {
160       ObjectName JavaDoc jsr77Name = (ObjectName JavaDoc) containerToModuleNameMap.remove(servletServiceName);
161
162       log.debug("Destroy container: " + servletServiceName + ", module: " + jsr77Name);
163       if (jsr77Name != null)
164       {
165          Servlet.destroy(server, jsr77Name);
166       }
167    }
168
169 }
170
Popular Tags