Skip to content

Commit 5664f79

Browse files
committed
JAVA-295: Further indirecting JMX dependencies to that driver will run on Android
1 parent cf314cc commit 5664f79

File tree

9 files changed

+271
-106
lines changed

9 files changed

+271
-106
lines changed

src/main/com/mongodb/DBPortPool.java

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919
package com.mongodb;
2020

2121
import com.mongodb.util.SimplePool;
22+
import com.mongodb.util.management.JMException;
23+
import com.mongodb.util.management.MBeanServerFactory;
2224

23-
import javax.management.JMException;
24-
import javax.management.MBeanServer;
25-
import javax.management.MalformedObjectNameException;
26-
import javax.management.ObjectName;
27-
import java.lang.management.ManagementFactory;
2825
import java.util.ArrayList;
2926
import java.util.Collections;
3027
import java.util.HashMap;
@@ -40,17 +37,6 @@ static class Holder {
4037

4138
Holder( MongoOptions options ){
4239
_options = options;
43-
{
44-
MBeanServer temp = null;
45-
try {
46-
temp = ManagementFactory.getPlatformMBeanServer();
47-
}
48-
catch ( Throwable t ){
49-
// ignore
50-
}
51-
52-
_server = temp;
53-
}
5440
}
5541

5642
DBPortPool get( ServerAddress addr ){
@@ -69,25 +55,20 @@ DBPortPool get( ServerAddress addr ){
6955
p = new DBPortPool( addr , _options );
7056
_pools.put( addr , p);
7157

72-
if ( _server != null ){
73-
try {
74-
ObjectName on = createObjectName( addr );
75-
if ( _server.isRegistered( on ) ){
76-
_server.unregisterMBean( on );
77-
Bytes.LOGGER.log( Level.INFO , "multiple Mongo instances for same host, jmx numbers might be off" );
78-
}
79-
_server.registerMBean( p , on );
80-
}
81-
catch ( JMException e ){
82-
Bytes.LOGGER.log( Level.WARNING , "jmx registration error: " + e + " continuing..." );
83-
}
84-
catch ( java.security.AccessControlException e ){
85-
Bytes.LOGGER.log( Level.WARNING , "jmx registration error: " + e + " continuing..." );
58+
try {
59+
String on = createObjectName(addr);
60+
if (MBeanServerFactory.getMBeanServer().isRegistered(on)) {
61+
MBeanServerFactory.getMBeanServer().unregisterMBean(on);
62+
Bytes.LOGGER.log(Level.INFO, "multiple Mongo instances for same host, jmx numbers might be off");
8663
}
64+
MBeanServerFactory.getMBeanServer().registerMBean(p, on);
65+
} catch (JMException e) {
66+
Bytes.LOGGER.log(Level.WARNING, "jmx registration error: " + e + " continuing...");
67+
} catch (java.security.AccessControlException e) {
68+
Bytes.LOGGER.log(Level.WARNING, "jmx registration error: " + e + " continuing...");
8769
}
88-
8970
}
90-
71+
9172
return p;
9273
}
9374

@@ -97,9 +78,9 @@ void close(){
9778
p.close();
9879

9980
try {
100-
ObjectName on = createObjectName( p._addr );
101-
if ( _server.isRegistered( on ) ){
102-
_server.unregisterMBean( on );
81+
String on = createObjectName( p._addr );
82+
if ( MBeanServerFactory.getMBeanServer().isRegistered(on) ){
83+
MBeanServerFactory.getMBeanServer().unregisterMBean(on);
10384
}
10485
} catch ( JMException e ){
10586
Bytes.LOGGER.log( Level.WARNING , "jmx de-registration error, continuing" , e );
@@ -108,16 +89,15 @@ void close(){
10889
}
10990
}
11091

111-
private ObjectName createObjectName( ServerAddress addr ) throws MalformedObjectNameException {
92+
private String createObjectName( ServerAddress addr ) {
11293
String name = "com.mongodb:type=ConnectionPool,host=" + addr.toString().replace( ":" , ",port=" ) + ",instance=" + _serial;
11394
if ( _options.description != null )
11495
name += ",description=" + _options.description;
115-
return new ObjectName( name );
96+
return name;
11697
}
11798

11899
final MongoOptions _options;
119-
final Map<ServerAddress,DBPortPool> _pools = Collections.synchronizedMap(new HashMap<ServerAddress, DBPortPool>());
120-
final MBeanServer _server;
100+
final Map<ServerAddress,DBPortPool> _pools = Collections.synchronizedMap( new HashMap<ServerAddress,DBPortPool>() );
121101
final int _serial = nextSerial.incrementAndGet();
122102

123103
// we use this to give each Holder a different mbean name

src/main/com/mongodb/util/SimplePool.java

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@
2727
import java.util.concurrent.Semaphore;
2828
import java.util.concurrent.TimeUnit;
2929

30-
import javax.management.Attribute;
31-
import javax.management.AttributeList;
32-
import javax.management.DynamicMBean;
33-
import javax.management.MBeanAttributeInfo;
34-
import javax.management.MBeanInfo;
35-
36-
public abstract class SimplePool<T> implements DynamicMBean {
30+
public abstract class SimplePool<T> implements SimplePoolMBean {
3731

3832
static final boolean TRACK_LEAKS = Boolean.getBoolean( "MONGO-TRACKLEAKS" );
3933
static final long _sleepTime = 2;
@@ -57,15 +51,6 @@ public SimplePool( String name , int maxToKeep , int maxTotal , boolean trackLea
5751
_maxTotal = maxTotal;
5852
_trackLeaks = trackLeaks || TRACK_LEAKS;
5953
_debug = debug;
60-
_mbeanInfo = new MBeanInfo( this.getClass().getName() , _name ,
61-
new MBeanAttributeInfo[]{
62-
new MBeanAttributeInfo( "name" , "java.lang.String" , "name of pool" , true , false , false ) ,
63-
new MBeanAttributeInfo( "size" , "java.lang.Integer" , "total size of pool" , true , false , false ) ,
64-
new MBeanAttributeInfo( "available" , "java.lang.Integer" , "total connections available" , true , false , false ) ,
65-
new MBeanAttributeInfo( "inUse" , "java.lang.Integer" , "number connections in use right now" , true , false , false ) ,
66-
new MBeanAttributeInfo( "everCreated" , "java.lang.Integer" , "number connections ever created" , true , false , false )
67-
} , null , null , null );
68-
6954
}
7055

7156
/** Creates a new object of this pool's type.
@@ -255,25 +240,33 @@ protected void clear(){
255240
}
256241
}
257242

258-
public int total(){
243+
@Override
244+
public String getName() {
245+
return _name;
246+
}
247+
248+
@Override
249+
public int getTotal(){
259250
return _all.size();
260251
}
261252

262-
public int inUse(){
253+
@Override
254+
public int getInUse(){
263255
return _all.size() - _avail.size();
264256
}
265257

266258
public Iterator<T> getAll(){
267259
return _all.getAll().iterator();
268260
}
269261

270-
public int available(){
262+
public int getAvailable(){
271263
if ( _maxTotal <= 0 )
272264
throw new IllegalStateException( "this pool has an infinite number of things available" );
273-
return _maxTotal - inUse();
265+
return _maxTotal - getInUse();
274266
}
275267

276-
public int everCreated(){
268+
@Override
269+
public int getEverCreated(){
277270
return _everCreated;
278271
}
279272

@@ -282,52 +275,11 @@ private void _debug( String msg ){
282275
System.out.println( "SimplePool [" + _name + "] : " + msg );
283276
}
284277

285-
public int maxToKeep(){
278+
@Override
279+
public int getSize(){
286280
return _maxToKeep;
287281
}
288282

289-
public Object getAttribute(String attribute){
290-
if ( attribute.equals( "name" ) )
291-
return _name;
292-
if ( attribute.equals( "size" ) )
293-
return _maxToKeep;
294-
if ( attribute.equals( "available" ) )
295-
return available();
296-
if ( attribute.equals( "inUse" ) )
297-
return inUse();
298-
if ( attribute.equals( "everCreated" ) )
299-
return _everCreated;
300-
301-
System.err.println( "com.mongo.util.SimplePool unknown attribute: " + attribute );
302-
throw new RuntimeException( "unknown attribute: " + attribute );
303-
}
304-
305-
public AttributeList getAttributes(String[] attributes){
306-
AttributeList l = new AttributeList();
307-
for ( int i=0; i<attributes.length; i++ ){
308-
String name = attributes[i];
309-
l.add( new Attribute( name , getAttribute( name ) ) );
310-
}
311-
return l;
312-
}
313-
314-
public MBeanInfo getMBeanInfo(){
315-
return _mbeanInfo;
316-
}
317-
318-
public Object invoke(String actionName, Object[] params, String[] signature){
319-
throw new RuntimeException( "not allowed to invoke anything" );
320-
}
321-
322-
public void setAttribute(Attribute attribute){
323-
throw new RuntimeException( "not allowed to set anything" );
324-
}
325-
326-
public AttributeList setAttributes(AttributeList attributes){
327-
throw new RuntimeException( "not allowed to set anything" );
328-
}
329-
330-
331283
public String toString(){
332284
StringBuilder buf = new StringBuilder();
333285
buf.append( "pool: " ).append( _name )
@@ -345,7 +297,6 @@ public String toString(){
345297
protected final int _maxTotal;
346298
protected final boolean _trackLeaks;
347299
protected final boolean _debug;
348-
protected final MBeanInfo _mbeanInfo;
349300

350301
private final List<T> _avail = new ArrayList<T>();
351302
protected final List<T> _availSafe = Collections.unmodifiableList( _avail );
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.mongodb.util;
2+
3+
/**
4+
* Copyright (c) 2008 - 2011 10gen, Inc. <http://10gen.com>
5+
* <p/>
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* <p/>
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* <p/>
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
public interface SimplePoolMBean {
19+
String getName();
20+
21+
int getTotal();
22+
23+
int getInUse();
24+
25+
int getEverCreated();
26+
27+
int getSize();
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2008 - 2011 10gen, Inc. <http://10gen.com>
3+
* <p/>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.util.management;
18+
19+
/**
20+
*
21+
* This class is NOT part of the public API. It may change at any time without notification.
22+
*/
23+
public class JMException extends Exception {
24+
static final long serialVersionUID = -2052972874393271421L;
25+
26+
public JMException(Throwable cause) {
27+
super(cause);
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2008 - 2011 10gen, Inc. <http://10gen.com>
3+
* <p/>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.util.management;
18+
19+
/**
20+
* This class is NOT part of the public API. It may change at any time without notification.
21+
*/
22+
public interface MBeanServer {
23+
boolean isRegistered(String mBeanName) throws JMException;
24+
25+
void unregisterMBean(String mBeanName) throws JMException;
26+
27+
void registerMBean(Object mBean, String mBeanName) throws JMException;
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (c) 2008 - 2011 10gen, Inc. <http://10gen.com>
3+
* <p/>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.util.management;
17+
18+
import com.mongodb.util.management.jmx.JMXMBeanServer;
19+
20+
/**
21+
* This class is NOT part of the public API. It may change at any time without notification.
22+
*
23+
* This class is used to insulate the rest of the driver from the possibility that JMX is not available,
24+
* as currently is the case on Android VM
25+
*/
26+
public class MBeanServerFactory {
27+
static {
28+
MBeanServer tmp;
29+
try {
30+
tmp = new JMXMBeanServer();
31+
} catch (Throwable e) {
32+
tmp = new NullMBeanServer();
33+
}
34+
35+
mBeanServer = tmp;
36+
}
37+
38+
public static MBeanServer getMBeanServer() {
39+
return mBeanServer;
40+
}
41+
42+
private static final MBeanServer mBeanServer;
43+
}

0 commit comments

Comments
 (0)