Package: ManagementExporter

ManagementExporter

nameinstructionbranchcomplexitylinemethod
ManagementExporter(Object, String)
M: 19 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
afterPropertiesSet()
M: 32 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
createObjectName(String)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
destroy()
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
export()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
unExport()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.kernel.management;
13:
14: import java.lang.management.ManagementFactory;
15:
16: import javax.management.MBeanServer;
17: import javax.management.MalformedObjectNameException;
18: import javax.management.ObjectName;
19:
20: import org.slf4j.Logger;
21: import org.slf4j.LoggerFactory;
22: import org.springframework.beans.factory.DisposableBean;
23: import org.springframework.beans.factory.InitializingBean;
24: import org.springframework.context.ApplicationContext;
25:
26: /**
27: * Exports an object for management. It is useful to keep a handle to this exporter in order to {@link #unExport()} the
28: * object passed in. In the case where this class is created as part of a Spring bean definition, the object will be
29: * automatically exported and unExported in line with the lifecycle of the {@link ApplicationContext};
30: * <p />
31: *
32: * <strong>Concurrent Semantics</strong><br />
33: *
34: * Thread-safe.
35: *
36: * @param <T> The type of the object to export for management
37: *
38: */
39: public class ManagementExporter<T> implements InitializingBean, DisposableBean {
40:
41: private final Logger logger = LoggerFactory.getLogger(this.getClass());
42:
43: private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
44:
45: private final T object;
46:
47: private final ObjectName name;
48:
49: /**
50: * Create a {@link ManagementExporter}
51: *
52: * @param object The object to export
53: * @param name The name to export the object to
54: */
55: public ManagementExporter(T object, String name) {
56: this.object = object;
57: this.name = createObjectName(name);
58: }
59:
60: private ObjectName createObjectName(String name) {
61: try {
62: return new ObjectName(name);
63: } catch (MalformedObjectNameException e) {
64: throw new RuntimeException("Could not convert String '" + name + "' to ObjectName", e);
65: }
66: }
67:
68: /**
69: * Export the object
70: */
71: public void export() {
72: afterPropertiesSet();
73: }
74:
75: /**
76: * Unexport the object
77: */
78: public void unExport() {
79: destroy();
80: }
81:
82: public void afterPropertiesSet() {
83: try {
84: logger.debug("Registering object {} with name {} for management", object, name);
85: server.registerMBean(object, name);
86: } catch (Exception e) {
87: logger.error("Unable to register object {} with name {} for management", object, name);
88: throw new RuntimeException(e);
89: }
90: }
91:
92: public void destroy() {
93: try {
94: logger.debug("Unregistering bean {} with name {} from management", object, name);
95: server.unregisterMBean(name);
96: } catch (Exception e) {
97: logger.warn("Unable to unregister bean {} with name {} from management", object, name);
98: }
99: }
100:
101: }