KickJava   Java API By Example, From Geeks To Geeks.

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


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.JMException JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MalformedObjectNameException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.security.InvalidParameterException JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.List JavaDoc;
35
36 /**
37  * Root class of the JBoss JSR-77 implementation of J2EEApplication.
38  *
39  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
40  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
41  * @author <a HREF="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
42  * @version $Revision: 40550 $
43  * @todo When all components of a J2EEApplication is state manageable
44  * this have to be too !!
45  */

46 public class J2EEApplication extends J2EEDeployedObject
47    implements J2EEApplicationMBean
48 {
49
50    // Attributes ----------------------------------------------------
51
private static Logger log = Logger.getLogger(J2EEApplication.class);
52    
53    /**
54     * The application module names as Strings
55     */

56    private List JavaDoc moduleNames = new ArrayList JavaDoc();
57
58    // Static --------------------------------------------------------
59

60    /**
61     * Create a JSR77 ear model instnace
62     *
63     * @param mbeanServer the MBeanServer to register with
64     * @param earName the name of the j2ee ear deployment
65     * @param url the ear URL, which may be null to represent a standalone
66     * jar/war/war without an ear
67     * @return the JSR77 ObjectName for the J2EEApplication
68     */

69    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer, String JavaDoc earName, URL JavaDoc url)
70    {
71       String JavaDoc lDD = null;
72       ObjectName JavaDoc jsr77Name = null;
73       ObjectName JavaDoc j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
74       // First get the deployement descriptor
75
lDD = J2EEDeployedObject.getDeploymentDescriptor(url, J2EEDeployedObject.APPLICATION);
76       try
77       {
78          // Now create the J2EEApplication
79
J2EEApplication j2eeApp = new J2EEApplication(earName, j2eeServerName, lDD);
80          jsr77Name = j2eeApp.getObjectName();
81          /* Check to see if the ear is already registered. This will occur when
82          an ear is deployed because we do not receive the ear module start
83          notification until its contained modules have started. The content
84          modules will have created a placeholder ear when they could not find
85          an existing J2EEApplication registered.
86          */

87          if (mbeanServer.isRegistered(jsr77Name) == true)
88          {
89             // We take the modules from the EAR placeholder
90
String JavaDoc[] tmpModules = (String JavaDoc[]) mbeanServer.getAttribute(jsr77Name, "modules");
91             // Remove the placeholder and register the j2eeApp
92
mbeanServer.unregisterMBean(jsr77Name);
93             mbeanServer.registerMBean(j2eeApp, jsr77Name);
94             // Add the
95
if (tmpModules != null)
96             {
97                for (int m = 0; m < tmpModules.length; m++)
98                   j2eeApp.addChild(newObjectName(tmpModules[m]));
99             }
100          }
101          else
102          {
103             mbeanServer.registerMBean(j2eeApp, jsr77Name);
104          }
105          log.debug("Created JSR-77 J2EEApplication: " + earName);
106       }
107       catch (Exception JavaDoc e)
108       {
109          log.debug("Could not create JSR-77 J2EEApplication: " + jsr77Name, e);
110       }
111       return jsr77Name;
112    }
113
114    /**
115     * Destroy the J2EEApplication component
116     *
117     * @param mbeanServer the MBeanServer used during create
118     * @param jsr77Name the JSR77 J2EEApplication component name
119     */

120    public static void destroy(MBeanServer JavaDoc mbeanServer, ObjectName JavaDoc jsr77Name)
121    {
122       try
123       {
124          mbeanServer.unregisterMBean(jsr77Name);
125          log.debug("Destroyed JSR-77 J2EEApplication: " + jsr77Name);
126       }
127       catch (javax.management.InstanceNotFoundException JavaDoc infe)
128       {
129       }
130       catch (Exception JavaDoc e)
131       {
132          log.debug("Could not destroy JSR-77 J2EEApplication: " + jsr77Name, e);
133       }
134    }
135
136    // Constructors --------------------------------------------------
137

138    /**
139     * Constructor taking the Name of this Object
140     *
141     * @param name Name to be set which must not be null
142     * @param pDeploymentDescriptor
143     * @throws InvalidParameterException If the given Name is null
144     */

145    public J2EEApplication(String JavaDoc name, ObjectName JavaDoc mbeanServer, String JavaDoc pDeploymentDescriptor)
146            throws MalformedObjectNameException JavaDoc, InvalidParentException
147    {
148       super(J2EETypeConstants.J2EEApplication, name, mbeanServer, pDeploymentDescriptor);
149    }
150
151    // Public --------------------------------------------------------
152

153    // J2EEApplication implementation --------------------------------
154

155    /**
156     * @jmx:managed-attribute
157     */

158    public String JavaDoc[] getmodules()
159    {
160       return (String JavaDoc[]) moduleNames.toArray(new String JavaDoc[moduleNames.size()]);
161    }
162
163    /**
164     * @jmx:managed-operation
165     */

166    public String JavaDoc getmodule(int pIndex)
167    {
168       if (pIndex >= 0 && pIndex < moduleNames.size())
169       {
170          return (String JavaDoc) moduleNames.get(pIndex);
171       }
172       return null;
173    }
174
175    // J2EEManagedObjectMBean implementation -------------------------
176

177    public void addChild(ObjectName JavaDoc pChild)
178    {
179       String JavaDoc lType = J2EEManagedObject.getType(pChild);
180       if (
181               J2EETypeConstants.EJBModule.equals(lType) ||
182               J2EETypeConstants.WebModule.equals(lType) ||
183               J2EETypeConstants.ResourceAdapterModule.equals(lType) ||
184               J2EETypeConstants.ServiceModule.equals(lType)
185       )
186       {
187          moduleNames.add(pChild.getCanonicalName());
188          try
189          {
190             // Now it also have to added as child to its
191
// parent
192
server.invoke(newObjectName(getparent()),
193                     "addChild",
194                     new Object JavaDoc[]{pChild},
195                     new String JavaDoc[]{ObjectName JavaDoc.class.getName()});
196          }
197          catch (JMException JavaDoc jme)
198          {
199             // Ignore it because parent has to be there
200
}
201       }
202    }
203
204    public void removeChild(ObjectName JavaDoc pChild)
205    {
206       String JavaDoc lType = J2EEManagedObject.getType(pChild);
207       if (
208               J2EETypeConstants.EJBModule.equals(lType) ||
209               J2EETypeConstants.WebModule.equals(lType) ||
210               J2EETypeConstants.ResourceAdapterModule.equals(lType) ||
211               J2EETypeConstants.ServiceModule.equals(lType)
212       )
213       {
214          moduleNames.remove(pChild.getCanonicalName());
215          try
216          {
217             // Now it also have to added as child to its
218
// parent
219
server.invoke(newObjectName(getparent()),
220                     "removeChild",
221                     new Object JavaDoc[]{pChild},
222                     new String JavaDoc[]{ObjectName JavaDoc.class.getName()});
223          }
224          catch (JMException JavaDoc jme)
225          {
226             // Ignore it because parent has to be there
227
}
228       }
229    }
230
231    // Object overrides ---------------------------------------------------
232

233    public String JavaDoc toString()
234    {
235       return "J2EEApplication { " + super.toString() + " } [ " +
236               "modules: " + moduleNames +
237               " ]";
238    }
239
240    // Package protected ---------------------------------------------
241

242    // Protected -----------------------------------------------------
243

244    /**
245     * @return A hashtable with the J2EE Server as parent
246     */

247    protected Hashtable JavaDoc getParentKeys(ObjectName JavaDoc pParent)
248    {
249       Hashtable JavaDoc lReturn = new Hashtable JavaDoc();
250       Hashtable JavaDoc lProperties = pParent.getKeyPropertyList();
251       lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get("name"));
252
253       return lReturn;
254    }
255
256    // Private -------------------------------------------------------
257

258    // Inner classes -------------------------------------------------
259
}
260
Popular Tags