1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.testdoc.core.adapter;
17
18 import java.lang.management.ManagementFactory;
19
20 import javax.management.InstanceAlreadyExistsException;
21 import javax.management.JMX;
22 import javax.management.MBeanRegistrationException;
23 import javax.management.MBeanServer;
24 import javax.management.MalformedObjectNameException;
25 import javax.management.NotCompliantMBeanException;
26 import javax.management.ObjectName;
27
28 import de.smartics.testdoc.core.doc.Type;
29 import de.smartics.testdoc.core.doc.UnitTestDoc;
30 import de.smartics.testdoc.core.export.AbstractExportAdapter;
31 import de.smartics.testdoc.core.export.ExportException;
32
33
34
35
36
37
38
39
40
41
42 public class JmxExportAdapterClient extends AbstractExportAdapter
43 {
44
45
46
47
48
49
50
51 private static final long serialVersionUID = 1L;
52
53
54
55
56
57
58
59 public static final String OBJECT_NAME =
60 "de.smartics.testdoc.adapter:type=JmxExportAdapterClient,name=jmxExportAdapter";
61
62
63
64
65
66
67 private final ObjectName name;
68
69
70
71
72 private final JmxExportAdapterMBean delegate;
73
74
75
76
77
78
79
80
81
82
83 public JmxExportAdapterClient() throws MBeanRegistrationException
84 {
85 this.name = createObjectName();
86 this.delegate = createAndRegisterMBeanToPlatformServer();
87 }
88
89
90
91
92
93
94
95 private static ObjectName createObjectName() throws IllegalStateException
96 {
97 try
98 {
99 return new ObjectName(OBJECT_NAME);
100 }
101 catch (final MalformedObjectNameException e)
102 {
103 throw new IllegalStateException("The object name '" + OBJECT_NAME
104 + "' is invalid.", e);
105 }
106 }
107
108 private JmxExportAdapterMBean createAndRegisterMBeanToPlatformServer()
109 throws MBeanRegistrationException
110 {
111 JmxExportAdapterMBean delegate = new JmxExportAdapter();
112
113 final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
114 try
115 {
116 server.registerMBean(delegate, name);
117 }
118 catch (final InstanceAlreadyExistsException e)
119 {
120 delegate = fetchExportAdapterMBean(server, name);
121 }
122 catch (final NotCompliantMBeanException e)
123 {
124 throw new IllegalStateException(
125 "The instance to be registered with object name '" + name
126 + "' is not a valid MBean.", e);
127 }
128 return delegate;
129 }
130
131 private static JmxExportAdapterMBean fetchExportAdapterMBean(
132 final MBeanServer server, final ObjectName name)
133 {
134 return JMX.newMBeanProxy(server, name, JmxExportAdapterMBean.class);
135 }
136
137
138
139
140
141
142
143
144 public ObjectName getName()
145 {
146 return name;
147 }
148
149
150
151
152
153
154
155
156 @Override
157 public void export(final UnitTestDoc testDoc) throws ExportException
158 {
159 delegate.export(testDoc);
160 }
161
162
163
164
165
166
167 @Override
168 public void clear(final Type testCaseType)
169 {
170 delegate.clear(testCaseType);
171 }
172
173
174
175 }