mongodb 3.x driver Android compatibility

2019-01-12 04:40发布

问题:

I'm developing an Android app that use the java mongodb driver 3.0.3 to connect to every instance of mongodb.

The connector code to a generic istance is:

try{
    MongoCredential credential = MongoCredential.createCredential(user, dbname, pass.toCharArray());
    MongoClient mongoClient = new MongoClient( new ServerAddress(server , port ), Arrays.asList(credential));
    MongoDatabase db = mongoClient.getDatabase(dbname);
    System.out.println("Connect to database successfully ");
    Iterator i= mongoClient.listDatabaseNames().iterator();
    while (i.hasNext()){
        Log.d("DATABASE", (String) i.next());
    }
    Iterator ic= db.listCollectionNames().iterator();
    while (ic.hasNext()){
        Log.d("COLLECTION", (String) ic.next());
    }
}catch(Exception e){
    System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}

At runtime I have some error:

09-11 19:13:50.898 7418-7418/it.mysite.mongodbviewer W/org.bson.ObjectId﹕ Failed to get process identifier from JMX, using random number instead java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;

and

com.mongodb.MongoException: java.lang.NoClassDefFoundError: com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient

that generate a loop with this:

I/art﹕ Rejecting re-init on previously-failed class java.lang.Class

Anyone has a solution? java/lang/management/ManagementFactory seems not to exist for Android, how can you solve? If this is the center of the problem.

Thanks in advance, Matteo

PS: The complete log is:

09-11 19:13:50.898    7418-7418/it.mysite.mongodbviewer W/org.bson.ObjectId﹕ Failed to get process identifier from JMX, using random number instead
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
at org.bson.types.ObjectId.createProcessIdentifier(ObjectId.java:502)
at org.bson.types.ObjectId.<clinit>(ObjectId.java:460)
at com.mongodb.connection.ClusterId.<init>(ClusterId.java:47)
at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:40)
at com.mongodb.Mongo.createCluster(Mongo.java:660)
at com.mongodb.Mongo.createCluster(Mongo.java:646)
at com.mongodb.Mongo.<init>(Mongo.java:275)
at com.mongodb.MongoClient.<init>(MongoClient.java:184)
at com.mongodb.MongoClient.<init>(MongoClient.java:160)
at it.mysite.mongodbmanager.data.MongoDBDriver.connect(MongoDBDriver.java:102)
at it.mysite.mongodbmanager.fragments.MongoDBAccountDetailFragment.onClick(MongoDBAccountDetailFragment.java:101)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19865)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "java.lang.management.ManagementFactory" on path: DexPathList[[zip file "/data/app/it.mysite.mongodbviewer-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    at org.bson.types.ObjectId.createProcessIdentifier(ObjectId.java:502)
    at org.bson.types.ObjectId.<clinit>(ObjectId.java:460)
    at com.mongodb.connection.ClusterId.<init>(ClusterId.java:47)
    at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:40)
    at com.mongodb.Mongo.createCluster(Mongo.java:660)
    at com.mongodb.Mongo.createCluster(Mongo.java:646)
    at com.mongodb.Mongo.<init>(Mongo.java:275)
    at com.mongodb.MongoClient.<init>(MongoClient.java:184)
    at com.mongodb.MongoClient.<init>(MongoClient.java:160)
    at it.mysite.mongodbmanager.data.MongoDBDriver.connect(MongoDBDriver.java:102)
    at it.mysite.mongodbmanager.fragments.MongoDBAccountDetailFragment.onClick(MongoDBAccountDetailFragment.java:101)
    at android.view.View.performClick(View.java:4780)
    at android.view.View$PerformClick.run(View.java:19865)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Suppressed: java.lang.ClassNotFoundException: java.lang.management.ManagementFactory
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 22 more
 Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
09-11 19:13:50.901    7418-7418/it.mysite.mongodbviewer I/cluster﹕ Cluster created with settings {hosts=[192.168.1.74:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
09-11 19:13:50.918    7418-7418/it.mysite.mongodbviewer I/System.out﹕ Connect to database successfully
09-11 19:13:50.924    7418-7418/it.mysite.mongodbviewer I/cluster﹕ No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=192.168.1.74:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
09-11 19:13:50.954    7418-7487/it.mysite.mongodbviewer I/art﹕ Rejecting re-init on previously-failed class java.lang.Class<com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient>
09-11 19:13:50.954    7418-7487/it.mysite.mongodbviewer I/art﹕ Rejecting re-init on previously-failed class java.lang.Class<com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient>
09-11 19:13:50.955    7418-7487/it.mysite.mongodbviewer I/art﹕ Rejecting re-init on previously-failed class java.lang.Class<com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient>
09-11 19:13:50.956    7418-7487/it.mysite.mongodbviewer I/cluster﹕ Exception in monitor thread while connecting to server 192.168.1.74:27017
com.mongodb.MongoException: java.lang.NoClassDefFoundError: com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:125)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
at java.lang.Thread.run(Thread.java:818)
 Caused by: java.lang.NoClassDefFoundError: com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient
at com.mongodb.connection.ScramSha1Authenticator.createSaslClient(ScramSha1Authenticator.java:61)
at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:42)
at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32)
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99)
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
at java.lang.Thread.run(Thread.java:818)
09-11 19:13:51.479    7418-7487/it.mysite.mongodbviewer I/art﹕ Rejecting re-init on previously-failed class java.lang.Class<com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient>
09-11 19:13:52.106    7418-7487/it.mysite.mongodbviewer I/art﹕ Rejecting re-init on previously-failed class java.lang.Class<com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient>
09-11 19:13:52.637    7418-7487/it.mysite.mongodbviewer I/art﹕ Rejecting re-init on previously-failed class java.lang.Class<com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient>

回答1:

It looks to me like the ManagementFactory is a red herring, as the driver catches that exception and falls back to using a random number instead.

The real problem appears to be that the driver needs to authenticate with SCRAM-SHA-1, the implementation of which imports the following classes:

import javax.crypto.Mac;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.security.sasl.SaslClient;
import javax.security.sasl.SaslException;

I suspect that these are not available in the Android platform, which is not a complete implementation of the Java Runtime Environment.

One thing you could try would be to run against MongoDB 2.6, against which the driver's authentication implementation only relies on java.security.MessageDigest.

Another thing to think about is putting a REST service between the mobile app and MongoDB that is responsible for proxying all interactions with the database. The REST application can be run in an environment where the MongoDB driver will be fully functional.



回答2:

For anyone want to use github.com/mongodb/mongo-java-driver on Android, I have a solution!

I've forked from ufficial github.com/mongodb/mongo-java-driver and integrated classes from github.com/koterpillar/android-sasl to fix javax.security.sasl on Android.

Important: No driver-async is not ported because java.nio.channels.AsynchronousSocketChannel doesn't exist on Android and doesn't exist any porting.

The result is a working synchronous MongoDB Android (Java) driver that you can download from https://github.com/matfur92/mongo-java-driver

LAST UPDATE (5 November 2015) : 3.2.0-SNAPSHOT --> https://github.com/matfur92/mongo-java-driver/blob/gh-pages/JARs/mongo-java-driver-3.2.0-SNAPSHOT.jar?raw=true



回答3:

REST service workaround isn't the way, I must connect directly to mongodb instances.

EDIT:

Sync mongo-java-driver is correctly ported on Android with this porting: https://github.com/matfur92/mongo-java-driver

No driver-async is not ported because java.nio.channels.AsynchronousSocketChannel doesn't exist on Android and doesn't exist any porting.

If anyone has a solution to get driver-async working tell me please. The problem is in java.nio.channels.AsynchronousSocketChannel that not exist for Android