KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.net.URL JavaDoc;
25 import java.security.InvalidParameterException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.List JavaDoc;
29
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33
34 import org.jboss.logging.Logger;
35
36 /**
37  * Root class of the JBoss JSR-77 implementation of ServiceModule model.
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  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
43  * @version $Revision: 40550 $
44  */

45 public class ServiceModule extends J2EEModule
46    implements ServiceModuleMBean
47 {
48    private static final String JavaDoc[] eventTypes = {NotificationConstants.OBJECT_CREATED,
49                                                NotificationConstants.OBJECT_DELETED};
50
51    // Attributes ----------------------------------------------------
52
private static Logger log = Logger.getLogger(ServiceModule.class);
53
54    // list of object names as strings
55
private List JavaDoc mbeans = new ArrayList JavaDoc();
56
57    // Static --------------------------------------------------------
58

59    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer, String JavaDoc moduleName, URL JavaDoc url)
60    {
61       String JavaDoc lDD = null;
62       ObjectName JavaDoc jsr77Name = null;
63       ObjectName JavaDoc j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
64       
65       // First get the deployement descriptor
66
if (url != null && url.getFile().endsWith(".xml"))
67       {
68          // the url points to -service.xml or -deployer.xml, use as is
69
lDD = J2EEDeployedObject.getDeploymentDescriptor(url, null);
70       }
71       else
72       {
73          // the url points to .sar or .deployer, so look for META-INF/jboss-service.xml
74
lDD = J2EEDeployedObject.getDeploymentDescriptor(url, J2EEDeployedObject.SAR);
75       }
76
77       try
78       {
79          // Get JVM of the j2eeServer
80
String JavaDoc[] jvms = (String JavaDoc[]) mbeanServer.getAttribute(j2eeServerName, "javaVMs");
81          // Now create the ServiceModule
82
ServiceModule serviceModule = new ServiceModule(moduleName, j2eeServerName, jvms, lDD);
83          jsr77Name = serviceModule.getObjectName();
84          mbeanServer.registerMBean(serviceModule, jsr77Name);
85          log.debug("Created JSR-77 ServiceModule, name: " + moduleName);
86       }
87       catch (Exception JavaDoc e)
88       {
89          log.debug("Could not create JSR-77 ServiceModule: " + moduleName, e);
90       }
91       return jsr77Name;
92    }
93
94    public static void destroy(MBeanServer JavaDoc mbeanServer, String JavaDoc pModuleName)
95    {
96       try
97       {
98          log.debug("destroy(), remove Service Module: " + pModuleName);
99          // If Module Name already contains the JSR-77 Object Name String
100
if (pModuleName.indexOf(J2EEManagedObject.TYPE + "=" + J2EETypeConstants.ServiceModule) >= 0)
101          {
102             J2EEManagedObject.removeObject(mbeanServer, pModuleName);
103          }
104          else
105          {
106             J2EEManagedObject.removeObject(mbeanServer,
107                     pModuleName,
108                     J2EEDomain.getDomainName() + ":" +
109                     J2EEManagedObject.TYPE + "=" + J2EETypeConstants.ServiceModule +
110                     "," + "*");
111          }
112       }
113       catch (javax.management.InstanceNotFoundException JavaDoc infe)
114       {
115       }
116       catch (Throwable JavaDoc e)
117       {
118          log.error("Could not destroy JSR-77 ServiceModule: " + pModuleName, e);
119       }
120    }
121
122    // Constructors --------------------------------------------------
123

124    /**
125     * Constructor taking the Name of this Object
126     *
127     * @param moduleName the sar deployment module name
128     * @param j2eeServerName the J2EEServer ObjectName parent
129     * @param pDeploymentDescriptor
130     * @throws InvalidParameterException If the given Name is null
131     */

132    public ServiceModule(String JavaDoc moduleName, ObjectName JavaDoc j2eeServerName,
133                         String JavaDoc[] jvmNames, String JavaDoc pDeploymentDescriptor)
134            throws MalformedObjectNameException JavaDoc,
135            InvalidParentException
136    {
137       super(J2EETypeConstants.ServiceModule, moduleName, j2eeServerName, jvmNames, pDeploymentDescriptor);
138    }
139
140    // Public --------------------------------------------------------
141

142    // ResourceAdapterodule implementation --------------------------------------
143

144    /**
145     * @jmx:managed-attribute
146     */

147    public String JavaDoc[] getMBeans()
148    {
149       return (String JavaDoc[]) mbeans.toArray(new String JavaDoc[mbeans.size()]);
150    }
151
152    /**
153     * @jmx:managed-operation
154     */

155    public String JavaDoc getMBean(int pIndex)
156    {
157       if (pIndex >= 0 && pIndex < mbeans.size())
158       {
159          return (String JavaDoc) mbeans.get(pIndex);
160       }
161       else
162       {
163          return null;
164       }
165    }
166
167    // J2EEManagedObjectMBean implementation -------------------------
168

169    public void addChild(ObjectName JavaDoc pChild)
170    {
171       String JavaDoc lType = J2EEManagedObject.getType(pChild);
172       if (J2EETypeConstants.MBean.equals(lType))
173       {
174          mbeans.add(pChild.getCanonicalName());
175       }
176    }
177
178    public void removeChild(ObjectName JavaDoc pChild)
179    {
180       String JavaDoc lType = J2EEManagedObject.getType(pChild);
181       if (J2EETypeConstants.MBean.equals(lType))
182       {
183          mbeans.remove(pChild.getCanonicalName());
184       }
185    }
186
187    // javax.managment.j2ee.EventProvider implementation -------------
188

189    public String JavaDoc[] getEventTypes()
190    {
191       return eventTypes;
192    }
193
194    public String JavaDoc getEventType(int index)
195    {
196       String JavaDoc type = null;
197       if (index >= 0 && index < eventTypes.length)
198       {
199          type = eventTypes[index];
200       }
201       return type;
202    }
203
204
205    // org.jboss.ServiceMBean overrides ------------------------------------
206

207    public void postCreation()
208    {
209       sendNotification(NotificationConstants.OBJECT_CREATED, "SAR module created");
210    }
211
212    public void preDestruction()
213    {
214       sendNotification(NotificationConstants.OBJECT_DELETED, "SAR module destroyed");
215    }
216
217    // Object overrides ---------------------------------------------------
218

219    public String JavaDoc toString()
220    {
221       return "ServiceModule[ " + super.toString() +
222               "MBeans: " + mbeans +
223               " ]";
224    }
225
226    // Package protected ---------------------------------------------
227

228    // Protected -----------------------------------------------------
229

230    /**
231     * @return A hashtable with the J2EE-Application and J2EE-Server as parent
232     */

233    protected Hashtable JavaDoc getParentKeys(ObjectName JavaDoc pParent)
234    {
235       Hashtable JavaDoc lReturn = new Hashtable JavaDoc();
236       Hashtable JavaDoc lProperties = pParent.getKeyPropertyList();
237       lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get("name"));
238
239       return lReturn;
240    }
241 }
242
Popular Tags