Skip to content

Commit

Permalink
Merge pull request #3 from BuyerQuest/dih-variable-substitution-doesn…
Browse files Browse the repository at this point in the history
…t-work

Core properties are not substituted for mongo datasource
  • Loading branch information
serhii-shnurenko committed Aug 16, 2021
2 parents 1ee00e3 + 4cdbecd commit ea845f6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>net.buyerquest</groupId>
<artifactId>SolrMongoImporter</artifactId>
<name>MongoDB Data Importer for SOLR Data Import Handler</name>
<version>1.2.1</version>
<version>1.2.2</version>
<url>https://github.com/BuyerQuest/SolrMongoImporter</url>
<properties>
<solr.version>3.6.2</solr.version>
Expand Down Expand Up @@ -81,8 +81,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ public class MongoDataSource extends DataSource<Iterator<Map<String, Object>>> {
*/
@Override
@SuppressWarnings({ "PMD.CommentRequired", "PMD.AvoidDuplicateLiterals" })
public void init(final Context context, final Properties mongoInitProperties) {
public void init(final Context context, final Properties initProps) {
LOG.debug("init() - start");

final String databaseName = mongoInitProperties.getProperty(DATABASE);
resolveVariables(context, initProps);

final String databaseName = initProps.getProperty(DATABASE);
LOG.debug("Using databaseName of " + databaseName);

// The database name parameter is required, throw
Expand All @@ -58,11 +60,11 @@ public void init(final Context context, final Properties mongoInitProperties) {
if (LOG.isDebugEnabled()) {
LOG.debug("Initialization properties:");
for (@SuppressWarnings("rawtypes")
final Map.Entry entry : mongoInitProperties.entrySet()) {
final Map.Entry entry : initProps.entrySet()) {
LOG.debug(" - " + entry.getKey() + " = " + entry.getValue());
}
}
this.client = this.getClient(mongoInitProperties);
this.client = this.getClient(initProps);
this.database = this.client.getDatabase(databaseName)
.withReadPreference(ReadPreference.secondaryPreferred());

Expand All @@ -71,34 +73,34 @@ public void init(final Context context, final Properties mongoInitProperties) {

/**
* Builds and returns the {@link MongoClient} used to connect to the Mongo instances specified in
* {@code mongoInitProperties} parameter. Supports seeding the client with muliple instances via
* {@code initProps} parameter. Supports seeding the client with muliple instances via
* the use of a Mongo JDBC style URI.
*
* @param mongoInitProperties the initialization properties passed in from the SOLR DIH
* @param initProps the initialization properties passed in from the SOLR DIH
* @return the {@link MongoClient} to be used when querying the Mongo data store
*/
public MongoClient getClient(final Properties mongoInitProperties) {
public MongoClient getClient(final Properties initProps) {
LOG.debug("getClient() - start");
final String databaseName = mongoInitProperties.getProperty(DATABASE);
final String databaseName = initProps.getProperty(DATABASE);

// Default to using a URL, which allows for all kinds of Mongo configurations including
// standalone, replica sets, shards, etc.
if (mongoInitProperties.containsKey(URI)
&& !StringUtils.isEmpty(mongoInitProperties.getProperty(URI))) {
if (initProps.containsKey(URI)
&& !StringUtils.isEmpty(initProps.getProperty(URI))) {

// Build the URI used to connect to Mongo
final String uri = mongoInitProperties.getProperty(URI);
final String uri = initProps.getProperty(URI);

LOG.debug("Returning getClientFromURI()");
return this.getClientFromURI(uri);

} else {
// Assume a single server is being used; if nothing is specified, then assume it is a
// developer running Mongo on localhost.
final String host = mongoInitProperties.getProperty(HOST, "localhost");
final Integer port = Integer.valueOf(mongoInitProperties.getProperty(PORT, "27017"));
final String username = mongoInitProperties.getProperty(USERNAME);
final String password = mongoInitProperties.getProperty(PASSWORD);
final String host = initProps.getProperty(HOST, "localhost");
final Integer port = Integer.valueOf(initProps.getProperty(PORT, "27017"));
final String username = initProps.getProperty(USERNAME);
final String password = initProps.getProperty(PASSWORD);

LOG.debug("Returning getClient() using host, port, datbaseName, username, and password");
return this.getClient(host, port, databaseName, username, password);
Expand Down Expand Up @@ -271,6 +273,15 @@ public Map<String, Object> next() {
return result;
}
}

private void resolveVariables(Context ctx, Properties initProps) {
for (Map.Entry<Object, Object> entry : initProps.entrySet()) {
if (entry.getValue() != null) {
entry.setValue(ctx.replaceTokens((String) entry.getValue()));
}
}
}

public static final String DATABASE = "database";
public static final String HOST = "host";
public static final String PORT = "port";
Expand Down

0 comments on commit ea845f6

Please sign in to comment.