Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth code changes to enable compatibility with new mongo versions and… #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 47 additions & 33 deletions src/main/org/apache/solr/handler/dataimport/MongoDataSource.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package org.apache.solr.handler.dataimport;


import com.mongodb.*;
import com.mongodb.util.JSON;
import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;
import static org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;
import static org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.MongoException;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.util.JSON;

/**
* User: James
Expand Down Expand Up @@ -41,29 +53,31 @@ public void init(Context context, Properties initProps) {
String username = initProps.getProperty(USERNAME);
String password = initProps.getProperty(PASSWORD);

if (databaseName == null) {
throw new DataImportHandlerException(SEVERE
, "Database must be supplied");
}

try {
Mongo mongo = new Mongo(host, Integer.parseInt(port));
mongo.setReadPreference(ReadPreference.secondaryPreferred());

this.mongoConnection = mongo;
this.mongoDb = mongo.getDB(databaseName);

if (username != null) {
if (this.mongoDb.authenticate(username, password.toCharArray()) == false) {
throw new DataImportHandlerException(SEVERE
, "Mongo Authentication Failed");
}
}

} catch (UnknownHostException e) {
throw new DataImportHandlerException(SEVERE
, "Unable to connect to Mongo");
}
if (databaseName == null) {
throw new DataImportHandlerException(SEVERE, "Database must be supplied");
}
MongoClient mongoclient=null;
try {
if (username == null) {
mongoclient = new MongoClient(new ServerAddress(host, Integer.valueOf(port)));
mongoclient.setReadPreference(ReadPreference.secondaryPreferred());
}
else if (username != null) {
List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
MongoCredential mc = MongoCredential.createCredential(username, databaseName, password.toCharArray());
credentialsList.add(mc);

mongoclient = new MongoClient(new ServerAddress(host, Integer.valueOf(port)), credentialsList);
mongoclient.setReadPreference(ReadPreference.secondaryPreferred());
}

this.mongoConnection = mongoclient;
this.mongoDb = ((Mongo) mongoclient).getDB(databaseName);

} catch (Exception e) {
throw new DataImportHandlerException(SEVERE, e.getMessage());
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.apache.solr.handler.dataimport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;

import java.util.Map;

import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;
import org.apache.solr.handler.dataimport.Context;
import org.apache.solr.handler.dataimport.DataImportHandlerException;
import org.apache.solr.handler.dataimport.DataImporter;
import org.apache.solr.handler.dataimport.EntityProcessorBase;
import org.apache.solr.handler.dataimport.MongoDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* User: James
Expand Down