My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package org.javasimon.jmx;

import org.javasimon.*;
import org.javasimon.callback.CallbackSkeleton;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.JMException;
import java.util.Set;
import java.util.Iterator;
import java.util.HashSet;
import java.lang.management.ManagementFactory;

/**
* Callback that registers MXBeans for Simons after their creation. It is
* advisable to register the callback as soon as possible otherwise MX Beans
* for some Simons may not be created. Class can be extended in order to
* override {@link #constructObjectName(Simon)}.
*
* @author <a href="mailto:virgo47@gmail.com">Richard "Virgo" Richter</a>
*/
public class JmxRegisterCallback extends CallbackSkeleton {
/**
* Domain part of the JMX object name - protected for subclasses.
*/
protected String domain;

/**
* MBean server instance specified for this callback (or default platform one) - protected for subclasses.
*/
protected MBeanServer mBeanServer;

private Set<String> registeredNames = new HashSet<String>();

/**
* Default constructor uses default MBeanServer.
*
* @param domain domain part of the object name
*/
public JmxRegisterCallback(String domain) {
this(ManagementFactory.getPlatformMBeanServer(), domain);
}

/**
* Constructor using specific MBeanServer.
*
* @param mBeanServer specific MBeanServer
* @param domain domain part of the object name
*/
public JmxRegisterCallback(MBeanServer mBeanServer, String domain) {
assert domain != null && !(domain.isEmpty());
this.mBeanServer = mBeanServer;
this.domain = domain;
}

/**
* After Simon is created respective MX bean is registered for it according to
* its type.
*
* @param simon created Simon
*/
@Override
public final void onSimonCreated(Simon simon) {
if (simon.getName() == null) {
return;
}
register(simon);
}

/**
* When the Simon is destroyed, its MX bean is unregistered.
*
* @param simon destroyed Simon
*/
@Override
public final void onSimonDestroyed(Simon simon) {
String name = constructObjectName(simon);
try {
ObjectName objectName = new ObjectName(name);
mBeanServer.unregisterMBean(objectName);
registeredNames.remove(name);
onManagerMessage("Unregistered Simon with the name: " + objectName);
} catch (JMException e) {
onManagerWarning("JMX unregistration failed for: " + name, e);
}
}

/**
* When the manager is cleared, all MX beans for its Simons are unregistered.
*/
@Override
public final void onManagerClear() {
Iterator<String> namesIter = registeredNames.iterator();
while (namesIter.hasNext()) {
String name = namesIter.next();
try {
ObjectName objectName = new ObjectName(name);
mBeanServer.unregisterMBean(objectName);
// here I have to use iterator.remove() - that's why I can't call common method
// for clear() and simonDestroyed(simon)
namesIter.remove();
onManagerMessage("Unregistered Simon with the name: " + objectName);
} catch (JMException e) {
onManagerWarning("JMX unregistration failed for: " + name, e);
}
}
}

/**
* Method registering Simon MX Bean - can not be overriden, but can be used in subclasses.
*
* @param simon Simon MX Bean to be registered
* @since 3.1
*/
protected final void register(Simon simon) {
Object mBean = constructObject(simon);
String name = constructObjectName(simon);
if (mBean != null && name != null) {
try {
ObjectName objectName = new ObjectName(name);
if (mBeanServer.isRegistered(objectName)) {
mBeanServer.unregisterMBean(objectName);
} else {
registeredNames.add(name);
}
mBeanServer.registerMBean(mBean, objectName);
onManagerMessage("Simon registered under the name: " + objectName);
} catch (JMException e) {
onManagerWarning("JMX registration failed for: " + name, e);
registeredNames.remove(name);
}
}
}

/**
* Constructs JMX object from Simon object. Method can be overridden.
*
* @param simon Simon object
* @return JMX object (=MBean) representing the Simon
*/
protected SimonSuperMXBean constructObject(Simon simon) {
SimonSuperMXBean simonMxBean;
if (simon instanceof Counter) {
simonMxBean = new CounterMXBeanImpl((Counter) simon);
} else if (simon instanceof Stopwatch) {
simonMxBean = new StopwatchMXBeanImpl((Stopwatch) simon);
} else {
onManagerWarning("Unknown type of Simon! " + simon, null);
simonMxBean = null;
}
return simonMxBean;
}

/**
* Constructs JMX object name from Simon object. Method can be overridden.
*
* @param simon Simon object
* @return object name in String form
*/
protected String constructObjectName(Simon simon) {
return domain + ":type=" + simonType(simon) + ",name=" + simon.getName();
}

/**
* Returns type of the simon as defined in {@link SimonInfo#COUNTER},
* {@link SimonInfo#STOPWATCH} or {@link SimonInfo#UNKNOWN}.
*
* @param simon Simon object
* @return type of the Simon as String
*/
protected String simonType(Simon simon) {
String type = SimonInfo.UNKNOWN;
if (simon instanceof Counter) {
type = SimonInfo.COUNTER;
} else if (simon instanceof Stopwatch) {
type = SimonInfo.STOPWATCH;
}
return type;
}
}

Change log

r419 by virgo47 on Jan 11, 2012   Diff
fix http://code.google.com/p/javasimon/iss
ues/detail?id=63
Go to: 
Project members, sign in to write a code review

Older revisions

r399 by virgo47 on Dec 31, 2011   Diff
a lot of javadoc/jhabit fixes
r394 by virgo47 on Dec 22, 2011   Diff
clean-up in JMX
Stopwatch/CounterMXBeans and examples
of their customization, most of the
final methods are back, but wrapped
stopwatch/counter is protected - one
...
r388 by virgo47 on Dec 17, 2011   Diff
name of the simon for EJB interceptor
is decided in protected method -
interceptor can be extended/overriden,
thanks Gerald
All revisions of this file

File info

Size: 5259 bytes, 180 lines
Powered by Google Project Hosting