Skip to content

Commit e0bb838

Browse files
committed
MBeanServerFactoryBean returns JDK 1.5 platform MBeanServer for agent id "" (SPR-5909)
1 parent 8fb53c8 commit e0bb838

File tree

5 files changed

+44
-25
lines changed

5 files changed

+44
-25
lines changed

org.springframework.context/src/main/java/org/springframework/jmx/access/MBeanClientInterceptor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public void setEnvironment(Map<String, ?> environment) {
161161
* attempt being made to locate the attendant MBeanServer, unless
162162
* the {@link #setServiceUrl "serviceUrl"} property has been set.
163163
* @see javax.management.MBeanServerFactory#findMBeanServer(String)
164+
* <p>Specifying the empty String indicates the platform MBeanServer.
164165
*/
165166
public void setAgentId(String agentId) {
166167
this.agentId = agentId;

org.springframework.context/src/main/java/org/springframework/jmx/access/NotificationListenerRegistrar.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -105,6 +105,7 @@ public void setServiceUrl(String url) throws MalformedURLException {
105105
* attempt being made to locate the attendant MBeanServer, unless
106106
* the {@link #setServiceUrl "serviceUrl"} property has been set.
107107
* @see javax.management.MBeanServerFactory#findMBeanServer(String)
108+
* <p>Specifying the empty String indicates the platform MBeanServer.
108109
*/
109110
public void setAgentId(String agentId) {
110111
this.agentId = agentId;

org.springframework.context/src/main/java/org/springframework/jmx/support/JmxUtils.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,31 @@ public static MBeanServer locateMBeanServer() throws MBeanServerNotFoundExceptio
9191
* <code>MBeanServer</code> can be found. Logs a warning if more than one
9292
* <code>MBeanServer</code> found, returning the first one from the list.
9393
* @param agentId the agent identifier of the MBeanServer to retrieve.
94-
* If this parameter is <code>null</code>, all registered MBeanServers are
95-
* considered.
94+
* If this parameter is <code>null</code>, all registered MBeanServers are considered.
95+
* If the empty String is given, the platform MBeanServer will be returned.
9696
* @return the <code>MBeanServer</code> if found
9797
* @throws org.springframework.jmx.MBeanServerNotFoundException
9898
* if no <code>MBeanServer</code> could be found
9999
* @see javax.management.MBeanServerFactory#findMBeanServer(String)
100100
*/
101101
public static MBeanServer locateMBeanServer(String agentId) throws MBeanServerNotFoundException {
102-
List servers = MBeanServerFactory.findMBeanServer(agentId);
103-
104102
MBeanServer server = null;
105-
if (servers != null && servers.size() > 0) {
106-
// Check to see if an MBeanServer is registered.
107-
if (servers.size() > 1 && logger.isWarnEnabled()) {
108-
logger.warn("Found more than one MBeanServer instance" +
109-
(agentId != null ? " with agent id [" + agentId + "]" : "") +
110-
". Returning first from list.");
103+
104+
// null means any registered server, but "" specifically means the platform server
105+
if (!"".equals(agentId)) {
106+
List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(agentId);
107+
if (servers != null && servers.size() > 0) {
108+
// Check to see if an MBeanServer is registered.
109+
if (servers.size() > 1 && logger.isWarnEnabled()) {
110+
logger.warn("Found more than one MBeanServer instance" +
111+
(agentId != null ? " with agent id [" + agentId + "]" : "") +
112+
". Returning first from list.");
113+
}
114+
server = servers.get(0);
111115
}
112-
server = (MBeanServer) servers.get(0);
113116
}
114117

115-
if (server == null && agentId == null) {
118+
if (server == null && !StringUtils.hasLength(agentId)) {
116119
// Attempt to load the PlatformMBeanServer.
117120
try {
118121
server = ManagementFactory.getPlatformMBeanServer();

org.springframework.context/src/main/java/org/springframework/jmx/support/MBeanServerFactoryBean.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public void setLocateExistingServerIfPossible(boolean locateExistingServerIfPoss
8484
* and (importantly) if said MBeanServer cannot be located no
8585
* attempt will be made to create a new MBeanServer (and an
8686
* MBeanServerNotFoundException will be thrown at resolution time).
87+
* <p>Specifying the empty String indicates the platform MBeanServer.
8788
* @see javax.management.MBeanServerFactory#findMBeanServer(String)
8889
*/
8990
public void setAgentId(String agentId) {

org.springframework.context/src/test/java/org/springframework/jmx/support/MBeanServerFactoryBeanTests.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2002-2005 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
66
* the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
@@ -17,22 +17,23 @@
1717
package org.springframework.jmx.support;
1818

1919
import java.util.List;
20-
20+
import java.lang.management.ManagementFactory;
2121
import javax.management.MBeanServer;
2222
import javax.management.MBeanServerFactory;
2323

2424
import junit.framework.TestCase;
2525

2626
/**
2727
* @author Rob Harrop
28+
* @author Juergen Hoeller
2829
*/
2930
public class MBeanServerFactoryBeanTests extends TestCase {
3031

3132
public void testGetObject() throws Exception {
3233
MBeanServerFactoryBean bean = new MBeanServerFactoryBean();
3334
bean.afterPropertiesSet();
3435
try {
35-
MBeanServer server = (MBeanServer) bean.getObject();
36+
MBeanServer server = bean.getObject();
3637
assertNotNull("The MBeanServer should not be null", server);
3738
}
3839
finally {
@@ -45,7 +46,7 @@ public void testDefaultDomain() throws Exception {
4546
bean.setDefaultDomain("foo");
4647
bean.afterPropertiesSet();
4748
try {
48-
MBeanServer server = (MBeanServer) bean.getObject();
49+
MBeanServer server = bean.getObject();
4950
assertEquals("The default domain should be foo", "foo", server.getDefaultDomain());
5051
}
5152
finally {
@@ -60,7 +61,7 @@ public void testWithLocateExistingAndExistingServer() {
6061
bean.setLocateExistingServerIfPossible(true);
6162
bean.afterPropertiesSet();
6263
try {
63-
MBeanServer otherServer = (MBeanServer) bean.getObject();
64+
MBeanServer otherServer = bean.getObject();
6465
assertSame("Existing MBeanServer not located", server, otherServer);
6566
}
6667
finally {
@@ -72,12 +73,24 @@ public void testWithLocateExistingAndExistingServer() {
7273
}
7374
}
7475

75-
public void testWithLocateExistingAndNoExistingServer() {
76+
public void testWithLocateExistingAndFallbackToPlatformServer() {
7677
MBeanServerFactoryBean bean = new MBeanServerFactoryBean();
7778
bean.setLocateExistingServerIfPossible(true);
7879
bean.afterPropertiesSet();
7980
try {
80-
assertNotNull("MBeanServer not created", bean.getObject());
81+
assertSame(ManagementFactory.getPlatformMBeanServer(), bean.getObject());
82+
}
83+
finally {
84+
bean.destroy();
85+
}
86+
}
87+
88+
public void testWithEmptyAgentIdAndFallbackToPlatformServer() {
89+
MBeanServerFactoryBean bean = new MBeanServerFactoryBean();
90+
bean.setAgentId("");
91+
bean.afterPropertiesSet();
92+
try {
93+
assertSame(ManagementFactory.getPlatformMBeanServer(), bean.getObject());
8194
}
8295
finally {
8396
bean.destroy();
@@ -98,12 +111,12 @@ private void testCreation(boolean referenceShouldExist, String failMsg) throws E
98111
bean.afterPropertiesSet();
99112

100113
try {
101-
MBeanServer server = (MBeanServer) bean.getObject();
102-
List servers = MBeanServerFactory.findMBeanServer(null);
114+
MBeanServer server = bean.getObject();
115+
List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
103116

104117
boolean found = false;
105-
for (int x = 0; x < servers.size(); x++) {
106-
if (servers.get(x) == server) {
118+
for (MBeanServer candidate : servers) {
119+
if (candidate == server) {
107120
found = true;
108121
break;
109122
}

0 commit comments

Comments
 (0)