Skip to content

Commit

Permalink
Merge pull request #404 from rareddy/TEIID-3334
Browse files Browse the repository at this point in the history
TEIID-3334: Adding connection semantics based on connection URL in addit...
  • Loading branch information
rareddy committed Feb 16, 2015
2 parents df66b30 + c08c6dd commit 777608a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 36 deletions.
Expand Up @@ -22,6 +22,7 @@

package org.teiid.resource.adapter.mongodb;

import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;

Expand All @@ -31,11 +32,7 @@
import org.teiid.mongodb.MongoDBConnection;
import org.teiid.resource.spi.BasicConnection;

import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.*;


public class MongoDBConnectionImpl extends BasicConnection implements MongoDBConnection {
Expand All @@ -54,6 +51,14 @@ public MongoDBConnectionImpl(String database, List<ServerAddress> servers,
}
this.database = database;
}

public MongoDBConnectionImpl(String database, MongoClientURI uri) throws UnknownHostException {
this.database = database;
if (uri.getDatabase() != null) {
this.database = database;
}
this.client = new MongoClient(uri);
}

@Override
public DB getDatabase() {
Expand Down
Expand Up @@ -34,6 +34,7 @@
import org.teiid.resource.spi.BasicManagedConnectionFactory;

import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

Expand All @@ -58,20 +59,34 @@ public BasicConnectionFactory<MongoDBConnectionImpl> createConnectionFactory() t
}

final List<ServerAddress> servers = getServers();

//TODO: need to define all the properties on the ra.xml and build this correctly
final MongoClientOptions options = MongoClientOptions.builder().build();

return new BasicConnectionFactory<MongoDBConnectionImpl>() {
@Override
public MongoDBConnectionImpl getConnection() throws ResourceException {
MongoCredential credential = null;
if (MongoDBManagedConnectionFactory.this.username != null && MongoDBManagedConnectionFactory.this.password != null) {
credential = MongoCredential.createMongoCRCredential(MongoDBManagedConnectionFactory.this.username, MongoDBManagedConnectionFactory.this.database, MongoDBManagedConnectionFactory.this.password.toCharArray());
}
return new MongoDBConnectionImpl(MongoDBManagedConnectionFactory.this.database, servers, credential, options);
}
};
if (servers != null) {
//if options needed then use URL format
final MongoClientOptions options = MongoClientOptions.builder().build();

return new BasicConnectionFactory<MongoDBConnectionImpl>() {
@Override
public MongoDBConnectionImpl getConnection() throws ResourceException {
MongoCredential credential = null;
if (MongoDBManagedConnectionFactory.this.username != null && MongoDBManagedConnectionFactory.this.password != null) {
credential = MongoCredential.createMongoCRCredential(MongoDBManagedConnectionFactory.this.username, MongoDBManagedConnectionFactory.this.database, MongoDBManagedConnectionFactory.this.password.toCharArray());
}
return new MongoDBConnectionImpl(MongoDBManagedConnectionFactory.this.database, servers, credential, options);
}
};
}

// Make connection using the URI format
return new BasicConnectionFactory<MongoDBConnectionImpl>() {
@Override
public MongoDBConnectionImpl getConnection() throws ResourceException {
try {
return new MongoDBConnectionImpl(MongoDBManagedConnectionFactory.this.database, getConnectionURI());
} catch (UnknownHostException e) {
throw new ResourceException(e);
}
}
};

}

/**
Expand Down Expand Up @@ -115,23 +130,34 @@ public void setDatabase(String database) {
this.database = database;
}

protected MongoClientURI getConnectionURI() {
String serverlist = getRemoteServerList();
if (serverlist.startsWith("mongodb://")) { //$NON-NLS-1$
return new MongoClientURI(getRemoteServerList());
}
return null;
}

protected List<ServerAddress> getServers() throws ResourceException {
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
StringTokenizer st = new StringTokenizer(getRemoteServerList(), ";"); //$NON-NLS-1$
while (st.hasMoreTokens()) {
String token = st.nextToken();
int idx = token.indexOf(':');
if (idx < 0) {
throw new InvalidPropertyException(UTIL.getString("no_database")); //$NON-NLS-1$
}
try {
addresses.add(new ServerAddress(token.substring(0, idx), Integer.valueOf(token.substring(idx+1))));
} catch(UnknownHostException e) {
throw new ResourceException(e);
}
}
return addresses;
String serverlist = getRemoteServerList();
if (!serverlist.startsWith("mongodb://")) { //$NON-NLS-1$
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
StringTokenizer st = new StringTokenizer(serverlist, ";"); //$NON-NLS-1$
while (st.hasMoreTokens()) {
String token = st.nextToken();
int idx = token.indexOf(':');
if (idx < 0) {
throw new InvalidPropertyException(UTIL.getString("no_database")); //$NON-NLS-1$
}
try {
addresses.add(new ServerAddress(token.substring(0, idx), Integer.valueOf(token.substring(idx+1))));
} catch(UnknownHostException e) {
throw new ResourceException(e);
}
}
return addresses;
}
return null;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions connectors/connector-mongodb/src/main/rar/META-INF/ra.xml
Expand Up @@ -37,7 +37,7 @@
<managedconnectionfactory-class>org.teiid.resource.adapter.mongodb.MongoDBManagedConnectionFactory</managedconnectionfactory-class>

<config-property>
<description>{$display:"Server List", $description:"Server List (host:port[;host:port...]) to connect to", $required:"true"}</description>
<description>{$display:"URL/Server List", $description:"Server List (host:port[;host:port...]) to connect to; if value is in URL format (mongodb://localhost:27017/test) then this will override the credentials and database name if provided", $required:"true"}</description>
<config-property-name>RemoteServerList</config-property-name>
<config-property-type>java.lang.String</config-property-type>
</config-property>
Expand All @@ -55,7 +55,7 @@
</config-property>

<config-property>
<description>{$display:"Database",$description:"MongoDB Database Name",$required:"true"}</description>
<description>{$display:"Database",$description:"MongoDB Database Name; Required when not defined on the URL",$required:"optional"}</description>
<config-property-name>Database</config-property-name>
<config-property-type>java.lang.String</config-property-type>
</config-property>
Expand Down

0 comments on commit 777608a

Please sign in to comment.