|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MBeanRegistryImpl.java | 87.5% | 76.5% | 85.7% | 79.7% |
|
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;
|
|
18 |
import java.lang.reflect.Method;
|
|
19 |
import java.util.ArrayList;
|
|
20 |
import java.util.Iterator;
|
|
21 |
import java.util.List;
|
|
22 |
|
|
23 |
import javax.management.DynamicMBean;
|
|
24 |
import javax.management.InstanceAlreadyExistsException;
|
|
25 |
import javax.management.InstanceNotFoundException;
|
|
26 |
import javax.management.JMException;
|
|
27 |
import javax.management.MBeanRegistrationException;
|
|
28 |
import javax.management.MBeanServer;
|
|
29 |
import javax.management.NotCompliantMBeanException;
|
|
30 |
import javax.management.ObjectInstance;
|
|
31 |
import javax.management.ObjectName;
|
|
32 |
import javax.management.StandardMBean;
|
|
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 _beanServer;
|
|
59 |
|
|
60 |
private ObjectNameBuilder _objectNameBuilder;
|
|
61 |
|
|
62 |
private List _beans;
|
|
63 |
|
|
64 |
// Holds all registered MBean instances
|
|
65 |
private List _objectInstances = new ArrayList(); |
|
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 | 7 |
public MBeanRegistryImpl(ErrorHandler errorHandler, Log log, MBeanServer beanServer,
|
77 |
ObjectNameBuilder objectNameBuilder, List beans) |
|
78 |
{ |
|
79 | 7 |
_errorHandler = errorHandler; |
80 | 7 |
_log = log; |
81 | 7 |
_beanServer = beanServer; |
82 | 7 |
_objectNameBuilder = objectNameBuilder; |
83 | 7 |
_beans = beans; |
84 | 7 |
if (_beans != null) |
85 | 5 |
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 | 5 |
private void processContributions(List beans) |
95 |
{ |
|
96 | 5 |
Iterator iter = beans.iterator(); |
97 | 5 |
while (iter.hasNext())
|
98 |
{ |
|
99 | 9 |
MBeanRegistrationContribution mbeanReg = (MBeanRegistrationContribution) iter.next(); |
100 | 9 |
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 | 9 |
private void registerServiceAsMBean(ObjectName objectName, ServicePoint servicePoint, |
119 |
String startMethodName) |
|
120 |
{ |
|
121 |
// By default the ObjectName is built by ObjectNameBuilder service
|
|
122 |
// but the name can be overriden in the contribution
|
|
123 | 9 |
if (objectName == null) |
124 | 8 |
objectName = _objectNameBuilder.createServiceObjectName(servicePoint); |
125 |
|
|
126 |
// Register the bean
|
|
127 | 9 |
Object mbean; |
128 | 9 |
try
|
129 |
{ |
|
130 | 9 |
Class managementInterface = servicePoint.getServiceInterface(); |
131 |
// TODO: Check if ServiceModel is != pool and threaded
|
|
132 | 9 |
mbean = servicePoint.getService(managementInterface); |
133 | 9 |
registerMBean(mbean, managementInterface, objectName); |
134 |
} |
|
135 |
catch (JMException e)
|
|
136 |
{ |
|
137 | 0 |
_errorHandler.error( |
138 |
_log, |
|
139 |
ManagementMessages.errorRegisteringMBean(objectName, e), |
|
140 |
null,
|
|
141 |
e); |
|
142 | 0 |
return;
|
143 |
} |
|
144 |
// Call the start method if defined
|
|
145 | 9 |
try
|
146 |
{ |
|
147 | 9 |
if (startMethodName != null) |
148 | 7 |
invokeStartMethod(mbean, startMethodName); |
149 |
} |
|
150 |
catch (InvocationTargetException e)
|
|
151 |
{ |
|
152 | 0 |
_errorHandler.error(_log, ManagementMessages.errorStartMethodFailed( |
153 |
startMethodName, |
|
154 |
objectName, |
|
155 |
e.getTargetException()), null, e);
|
|
156 | 0 |
return;
|
157 |
} |
|
158 |
catch (Exception e)
|
|
159 |
{ |
|
160 | 0 |
_errorHandler.error(_log, ManagementMessages.errorStartMethodFailed( |
161 |
startMethodName, |
|
162 |
objectName, |
|
163 |
e), null, e);
|
|
164 | 0 |
return;
|
165 |
} |
|
166 |
} |
|
167 |
|
|
168 |
/**
|
|
169 |
* @throws InstanceAlreadyExistsException
|
|
170 |
* @throws MBeanRegistrationException
|
|
171 |
* @throws NotCompliantMBeanException
|
|
172 |
* @see MBeanRegistry#registerMBean(Object, Class, ObjectName)
|
|
173 |
*/
|
|
174 | 12 |
public ObjectInstance registerMBean(Object obj, Class managementInterface, ObjectName objectName)
|
175 |
throws InstanceAlreadyExistsException, MBeanRegistrationException,
|
|
176 |
NotCompliantMBeanException |
|
177 |
{ |
|
178 | 12 |
ObjectInstance instance = null;
|
179 | 12 |
try
|
180 |
{ |
|
181 | 12 |
if (_log.isDebugEnabled())
|
182 |
{ |
|
183 | 0 |
_log.debug("Trying to register MBean " + objectName);
|
184 |
} |
|
185 | 12 |
instance = _beanServer.registerMBean(obj, objectName); |
186 |
} |
|
187 |
catch (NotCompliantMBeanException e)
|
|
188 |
{ |
|
189 | 2 |
if (_log.isDebugEnabled())
|
190 |
{ |
|
191 | 0 |
_log.debug("MBean " + objectName + " is not compliant. Registering" |
192 |
+ " using StandardMBean");
|
|
193 |
} |
|
194 | 2 |
if (DynamicMBean.class.isAssignableFrom(obj.getClass()) || managementInterface == null) |
195 | 1 |
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 | 1 |
StandardMBean standardMBean = new StandardMBean(obj, managementInterface);
|
203 | 1 |
instance = _beanServer.registerMBean(standardMBean, objectName); |
204 |
} |
|
205 | 11 |
_objectInstances.add(instance); |
206 | 11 |
return instance;
|
207 |
} |
|
208 |
|
|
209 |
/**
|
|
210 |
* @see org.apache.hivemind.management.MBeanRegistry#unregisterMBean(javax.management.ObjectName)
|
|
211 |
*/
|
|
212 | 0 |
public void unregisterMBean(ObjectName objectName) throws InstanceNotFoundException, |
213 |
MBeanRegistrationException |
|
214 |
{ |
|
215 | 0 |
ObjectInstance instance = _beanServer.getObjectInstance(objectName); |
216 | 0 |
_objectInstances.remove(instance); |
217 | 0 |
_beanServer.unregisterMBean(objectName); |
218 |
} |
|
219 |
|
|
220 |
/**
|
|
221 |
* Calls the start method of an mbean
|
|
222 |
*/
|
|
223 | 7 |
private void invokeStartMethod(Object mbean, String methodName) throws IllegalAccessException, |
224 |
InvocationTargetException, NoSuchMethodException |
|
225 |
{ |
|
226 | 7 |
Class serviceClass = mbean.getClass(); |
227 | 7 |
Method m = serviceClass.getMethod(methodName, null);
|
228 | 7 |
m.invoke(mbean, null);
|
229 |
} |
|
230 |
|
|
231 |
/**
|
|
232 |
* Unregisters all registered MBeans
|
|
233 |
*/
|
|
234 | 2 |
public void registryDidShutdown() |
235 |
{ |
|
236 |
// Unregister objects in reversed order. Otherwise the
|
|
237 |
// Jsr 160 connector gets problems after the namingservice is unregistered
|
|
238 | 2 |
for (int i = _objectInstances.size() - 1; i >= 0; i--) |
239 |
{ |
|
240 | 4 |
ObjectInstance objectInstance = (ObjectInstance) _objectInstances.get(i); |
241 | 4 |
try
|
242 |
{ |
|
243 | 4 |
_beanServer.unregisterMBean(objectInstance.getObjectName()); |
244 |
} |
|
245 |
catch (JMException e)
|
|
246 |
{ |
|
247 |
// Uncritical error, just warn
|
|
248 | 0 |
_log.warn(ManagementMessages.errorUnregisteringMBean( |
249 |
objectInstance.getObjectName(), |
|
250 |
e)); |
|
251 |
} |
|
252 |
} |
|
253 |
} |
|
254 |
|
|
255 |
} |
|