KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > management > impl > MBeanRegistryImpl


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.management.impl;
16
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.management.DynamicMBean JavaDoc;
24 import javax.management.InstanceAlreadyExistsException JavaDoc;
25 import javax.management.InstanceNotFoundException JavaDoc;
26 import javax.management.JMException JavaDoc;
27 import javax.management.MBeanRegistrationException JavaDoc;
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.NotCompliantMBeanException JavaDoc;
30 import javax.management.ObjectInstance JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.StandardMBean JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.hivemind.ErrorHandler;
36 import org.apache.hivemind.events.RegistryShutdownListener;
37 import org.apache.hivemind.internal.ServicePoint;
38 import org.apache.hivemind.management.MBeanRegistry;
39 import org.apache.hivemind.management.ManagementMessages;
40 import org.apache.hivemind.management.ObjectNameBuilder;
41
42 /**
43  * Implementation of {@link MBeanRegistry}. Registers MBeans in an standard JMX MBeanServer Supports
44  * calling start methods, after the registration. MBeans can be provided as service references in a
45  * configuration. Standard MBeans must use the primitive service model. Any interceptor destroys JMX
46  * compliance due to naming conventions. Implements shutdown listener to unregisters all MBeans when
47  * the registry is shutdown
48  *
49  * @author Achim Huegen
50  * @since 1.1
51  */

52 public class MBeanRegistryImpl implements MBeanRegistry, RegistryShutdownListener
53 {
54     private ErrorHandler _errorHandler;
55
56     private Log _log;
57
58     private MBeanServer JavaDoc _beanServer;
59
60     private ObjectNameBuilder _objectNameBuilder;
61
62     private List JavaDoc _beans;
63
64     // Holds all registered MBean instances
65
private List JavaDoc _objectInstances = new ArrayList JavaDoc();
66
67     /**
68      * Creates new instance Registers all MBeans as defined in <code>beans</code>
69      *
70      * @param objectNameBuilder
71      * Service responsible for naming MBeans
72      * @param beans
73      * List with instances of {@link MBeanRegistrationContribution}. The specified
74      * services get registered as MBeans
75      */

76     public MBeanRegistryImpl(ErrorHandler errorHandler, Log log, MBeanServer JavaDoc beanServer,
77             ObjectNameBuilder objectNameBuilder, List JavaDoc beans)
78     {
79         _errorHandler = errorHandler;
80         _log = log;
81         _beanServer = beanServer;
82         _objectNameBuilder = objectNameBuilder;
83         _beans = beans;
84         if (_beans != null)
85             processContributions(_beans);
86     }
87
88     /**
89      * Registers all services as MBeans, specified in the contribution to this service
90      *
91      * @param beans
92      * List of MBeanRegistrationContribution
93      */

94     private void processContributions(List JavaDoc beans)
95     {
96         Iterator JavaDoc iter = beans.iterator();
97         while (iter.hasNext())
98         {
99             MBeanRegistrationContribution mbeanReg = (MBeanRegistrationContribution) iter.next();
100             registerServiceAsMBean(mbeanReg.getObjectName(), mbeanReg.getServicePoint(), mbeanReg
101                     .getStartMethod());
102         }
103     }
104
105     /**
106      * Registers a service as MBean. Retrieves an instance of the service by calling
107      * {@link ServicePoint#getService(Class)}
108      *
109      * @param objectName
110      * ObjectName for the MBean, if null the ObjectName is determined by the
111      * {@link ObjectNameBuilder}
112      * @param servicePoint
113      * ServicePoint
114      * @param startMethodName
115      * Name of the start method to call in the servicePoint after registration Can be
116      * null
117      */

118     private void registerServiceAsMBean(ObjectName JavaDoc objectName, ServicePoint servicePoint,
119             String JavaDoc startMethodName)
120     {
121         // By default the ObjectName is built by ObjectNameBuilder service
122
// but the name can be overriden in the contribution
123
if (objectName == null)
124             objectName = _objectNameBuilder.createServiceObjectName(servicePoint);
125
126         // Register the bean
127
Object JavaDoc mbean;
128         try
129         {
130             Class JavaDoc managementInterface = servicePoint.getDeclaredInterface();
131             // TODO: Check if ServiceModel is != pool and threaded
132
mbean = servicePoint.getService(managementInterface);
133             registerMBean(mbean, managementInterface, objectName);
134         }
135         catch (JMException JavaDoc e)
136         {
137             _errorHandler.error(
138                     _log,
139                     ManagementMessages.errorRegisteringMBean(objectName, e),
140                     null,
141                     e);
142             return;
143         }
144         // Call the start method if defined
145
try
146         {
147             if (startMethodName != null)
148                 invokeStartMethod(mbean, startMethodName);
149         }
150         catch (InvocationTargetException JavaDoc e)
151         {
152             _errorHandler.error(_log, ManagementMessages.errorStartMethodFailed(
153                     startMethodName,
154                     objectName,
155                     e.getTargetException()), null, e);
156             return;
157         }
158         catch (Exception JavaDoc e)
159         {
160             _errorHandler.error(_log, ManagementMessages.errorStartMethodFailed(
161                     startMethodName,
162                     objectName,
163                     e), null, e);
164             return;
165         }
166     }
167
168     /**
169      * @throws InstanceAlreadyExistsException
170      * @throws MBeanRegistrationException
171      * @throws NotCompliantMBeanException
172      * @see MBeanRegistry#registerMBean(Object, Class, ObjectName)
173      */

174     public ObjectInstance JavaDoc registerMBean(Object JavaDoc obj, Class JavaDoc managementInterface, ObjectName JavaDoc objectName)
175             throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc,
176             NotCompliantMBeanException JavaDoc
177     {
178         ObjectInstance JavaDoc instance = null;
179         try
180         {
181             if (_log.isDebugEnabled())
182             {
183                 _log.debug("Trying to register MBean " + objectName);
184             }
185             instance = _beanServer.registerMBean(obj, objectName);
186         }
187         catch (NotCompliantMBeanException JavaDoc e)
188         {
189             if (_log.isDebugEnabled())
190             {
191                 _log.debug("MBean " + objectName + " is not compliant. Registering"
192                         + " using StandardMBean");
193             }
194             if (DynamicMBean JavaDoc.class.isAssignableFrom(obj.getClass()) || managementInterface == null)
195                 throw e;
196             // if the object is a Standard MBean that is surrounded by
197
// a proxy or an interceptor it is not compliant since
198
// the naming conventions are not fulfilled.
199
// Now we use the StandardMBean class to adapt the MBean to the
200
// DynamicMBean interface which is not restricted by these
201
// naming conventions
202
StandardMBean JavaDoc standardMBean = new StandardMBean JavaDoc(obj, managementInterface);
203             instance = _beanServer.registerMBean(standardMBean, objectName);
204         }
205         _objectInstances.add(instance);
206         return instance;
207     }
208
209     /**
210      * @see org.apache.hivemind.management.MBeanRegistry#unregisterMBean(javax.management.ObjectName)
211      */

212     public void unregisterMBean(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc,
213             MBeanRegistrationException JavaDoc
214     {
215         ObjectInstance JavaDoc instance = _beanServer.getObjectInstance(objectName);
216         _objectInstances.remove(instance);
217         _beanServer.unregisterMBean(objectName);
218     }
219
220     /**
221      * Calls the start method of an mbean
222      */

223     private void invokeStartMethod(Object JavaDoc mbean, String JavaDoc methodName) throws IllegalAccessException JavaDoc,
224             InvocationTargetException JavaDoc, NoSuchMethodException JavaDoc
225     {
226         Class JavaDoc serviceClass = mbean.getClass();
227         Method JavaDoc m = serviceClass.getMethod(methodName, (Class JavaDoc []) null);
228         m.invoke(mbean, (Object JavaDoc []) null);
229     }
230
231     /**
232      * Unregisters all registered MBeans
233      */

234     public void registryDidShutdown()
235     {
236         // Unregister objects in reversed order. Otherwise the
237
// Jsr 160 connector gets problems after the namingservice is unregistered
238
for (int i = _objectInstances.size() - 1; i >= 0; i--)
239         {
240             ObjectInstance JavaDoc objectInstance = (ObjectInstance JavaDoc) _objectInstances.get(i);
241             try
242             {
243                 _beanServer.unregisterMBean(objectInstance.getObjectName());
244             }
245             catch (JMException JavaDoc e)
246             {
247                 // Uncritical error, just warn
248
_log.warn(ManagementMessages.errorUnregisteringMBean(
249                         objectInstance.getObjectName(),
250                         e));
251             }
252         }
253     }
254
255 }
Popular Tags