KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ws > mbean > AbstractWebServiceMBean


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id$
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.ws.mbean;
26
27 import java.util.Iterator 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 import javax.management.modelmbean.ModelMBean JavaDoc;
34
35 import org.apache.commons.modeler.ManagedBean;
36 import org.apache.commons.modeler.Registry;
37
38 import org.objectweb.jonas.common.Log;
39 import org.objectweb.jonas.jmx.JmxService;
40 import org.objectweb.jonas.management.JonasMBeanTools;
41 import org.objectweb.jonas.management.j2eemanagement.J2EEManagedObject;
42 import org.objectweb.jonas.service.ServiceManager;
43
44 import org.objectweb.util.monolog.api.BasicLevel;
45 import org.objectweb.util.monolog.api.Logger;
46
47 /**
48  * Base Class for WebServices MBeans. It makes registration/unregistration
49  * of hierarchical MBeans very easy. Needs to be subclassed for each WebService MBean.
50  *
51  * @author Guillaume Sauthier
52  */

53 public abstract class AbstractWebServiceMBean extends J2EEManagedObject {
54
55     /**
56      * logger
57      */

58     private static Logger log = Log.getLogger(Log.JONAS_WS_PREFIX);
59
60     /**
61      * ObjectName instance : avoid recreating an ObjectName each time we want one
62      */

63     private ObjectName JavaDoc realObjectName = null;
64
65     /**
66      * Constructor for default J2eeManagedObject
67      * @param objectName the MBean's ObjectName
68      */

69     public AbstractWebServiceMBean(String JavaDoc objectName) {
70         super(objectName);
71         try {
72             realObjectName = ObjectName.getInstance(objectName);
73         } catch (MalformedObjectNameException JavaDoc e) {
74             // Cannot go here
75
realObjectName = null;
76         }
77     }
78
79     /**
80      * Registers the MBean (and its childs) in the default MBeanServer
81      */

82     public void register() {
83         register(getMBeanServer());
84     }
85
86     /**
87      * Registers the MBean (and its childs) in the specified MBeanServer
88      * @param server the MBeanServer where ON will be registered
89      */

90     public void register(MBeanServer JavaDoc server) {
91
92         // get common-modeler Registry
93
Registry registry = JonasMBeanTools.getRegistry();
94         // get the ManagedBean given the MBean type (WebServices, PortComponent, ...)
95
ManagedBean managed = registry.findManagedBean(getMBeanType());
96         try {
97             // instantiate and register the Model
98
ModelMBean JavaDoc model = managed.createMBean(this);
99             server.registerMBean(model, realObjectName);
100         } catch (Exception JavaDoc e) {
101             // should never goes here
102
log.log(BasicLevel.DEBUG, "Should never goes here", e);
103         }
104
105         // register "childs"
106
List JavaDoc childs = getChildsMBeans();
107         if (childs != null) {
108             for (Iterator JavaDoc i = childs.iterator(); i.hasNext();) {
109                 AbstractWebServiceMBean child = (AbstractWebServiceMBean) i.next();
110                 child.register(server);
111             }
112         }
113     }
114
115     /**
116      * @return Returns the MBean type (as used in mbean-descriptor.xml)
117      */

118     protected abstract String JavaDoc getMBeanType();
119
120     /**
121      * Unregisters the MBean (and its childs) from the default MBeanServer
122      */

123     public void unregister() {
124         unregister(getMBeanServer());
125     }
126
127     /**
128      * Unregisters the MBean (and its childs) from the specified MBeanServer
129      * @param server the MBeanServer where ON will be unregistered
130      */

131     public void unregister(MBeanServer JavaDoc server) {
132         // unregister MBean
133
try {
134             server.unregisterMBean(ObjectName.getInstance(getObjectName()));
135         } catch (Exception JavaDoc e) {
136             // nothing to do, we simply continue the unregistering process
137
log.log(BasicLevel.DEBUG, "Should never goes here", e);
138         }
139
140         // unregister "childs"
141
List JavaDoc childs = getChildsMBeans();
142         if (childs != null) {
143             for (Iterator JavaDoc i = childs.iterator(); i.hasNext();) {
144                 AbstractWebServiceMBean child = (AbstractWebServiceMBean) i.next();
145                 child.unregister(server);
146             }
147         }
148     }
149
150     /**
151      * @return Returns the Child List
152      */

153     protected abstract List JavaDoc getChildsMBeans();
154
155     /**
156      * @return Returns the MBeanServer
157      */

158     private MBeanServer JavaDoc getMBeanServer() {
159         ServiceManager sm;
160         try {
161             sm = ServiceManager.getInstance();
162         } catch (Exception JavaDoc e) {
163             // we should never goes here
164
throw new IllegalStateException JavaDoc(
165                     "Cannot get ServiceManager inside JOnAS !");
166         }
167         MBeanServer JavaDoc server = ((JmxService) sm.getJmxService()).getJmxServer();
168         return server;
169     }
170
171     /**
172      * @return Returns the ObjectName instance
173      */

174     public ObjectName JavaDoc getRealObjectName() {
175         return this.realObjectName;
176     }
177 }
178
Popular Tags