Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit 6f758c3

Browse files
committed
Merge pull request #2 from mhurne/master
Support standard MongoDB connection strings
2 parents 2e79174 + f784f31 commit 6f758c3

File tree

7 files changed

+91
-26
lines changed

7 files changed

+91
-26
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
* 1.3.0 (2014-05-15)
4+
* Add support for [standard MongoDB connection strings](http://docs.mongodb.org/manual/reference/connection-string/)
5+
* Support for non-standard "gridfs://" connection strings is deprecated and may be removed in a future release
6+
37
* 1.2.0 (2014-02-17)
48
* Upgrade JClouds to version 1.7.1, in order to support changes between Java versions 1.7.0_45 and 1.7.0_51. See https://issues.apache.org/jira/browse/JCLOUDS-427
59

README.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Overview
22

3-
A JClouds BlobStore provider backed by MongoDB's GridFS. Not all capabilities are supported, but there should be enough for common use cases.
3+
A JClouds BlobStore provider backed by MongoDB's GridFS. Not all capabilities are supported, but there should be
4+
enough for common use cases.
45

56
# Usage
67

7-
First, add a dependency to your build file. Releases are published to [Bintray JCenter](https://bintray.com/bintray/jcenter). See the [changelog](CHANGES.md) for the latest version.
8+
First, add a dependency to your build file. Releases are published to
9+
[Bintray JCenter](https://bintray.com/bintray/jcenter). See the [changelog](CHANGES.md) for the latest version.
810

911
Gradle:
1012

@@ -27,17 +29,32 @@ Maven:
2729
</dependency>
2830
```
2931

30-
Next, obtain an instance of the `BlobStore`.
32+
Next, obtain an instance of the `BlobStore` using a
33+
[standard MongoDB connection string](http://docs.mongodb.org/manual/reference/connection-string/):
3134

3235
```java
3336
Properties overrides = new Properties();
34-
overrides.setProperty(Constants.PROPERTY_ENDPOINT, "gridfs://my_mongo_server:27017");
37+
overrides.setProperty(Constants.PROPERTY_ENDPOINT, "mongodb://my_mongo_server:27017/?maxPoolSize=50");
3538
BlobStoreContext context = ContextBuilder.newBuilder("gridfs").overrides(overrides)
3639
.buildView(BlobStoreContext.class);
3740
BlobStore blobStore = context.getBlobStore();
3841
```
3942

40-
Then, use the blob store to put/get blobs. The container is in the format **DB**[/**BUCKET**], where **DB** is the name of the database and **BUCKET** is the optional name of the GridFS bucket (which defaults to `fs`).
43+
You can also use a non-standard connection string (deprecated):
44+
45+
```java
46+
overrides.setProperty(Constants.PROPERTY_ENDPOINT, "gridfs://my_mongo_server:27017");
47+
```
48+
49+
To use a replica set when using a non-standard connection string, specify additional members as a comma or
50+
semicolon-separated list, like this:
51+
52+
```java
53+
overrides.setProperty(Constants.PROPERTY_ENDPOINT, "gridfs://node1:27017;node2:27017;node3:27017");
54+
```
55+
56+
Then, use the blob store to put/get blobs. The container is in the format **DB**[/**BUCKET**], where **DB** is the
57+
name of the database and **BUCKET** is the optional name of the GridFS bucket (which defaults to `fs`).
4158

4259
```java
4360
blobStore.createContainerInLocation(null, "blobStore");
@@ -51,9 +68,3 @@ Finally, close the context to allow it to properly clean up.
5168
```java
5269
context.close();
5370
```
54-
55-
To use a replica set, specify additional members as a comma or semicolon-separated list, like this:
56-
57-
```java
58-
overrides.setProperty(Constants.PROPERTY_ENDPOINT, "gridfs://node1:27017;node2:27017;node3:27017");
59-
```

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=1.2.1
1+
version=1.3.0
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package com.commercehub.jclouds.gridfs.blobstore;
22

33
class Constants {
4+
5+
/**
6+
* @deprecated Use a <a href="http://docs.mongodb.org/manual/reference/connection-string/">standard MongoDB
7+
* connection string</a> instead.
8+
*/
9+
@Deprecated
410
static final String GRIDFS_URI_SCHEME = "gridfs";
11+
512
}

src/main/java/com/commercehub/jclouds/gridfs/blobstore/GridFSBlobStore.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import com.mongodb.BasicDBObject;
55
import com.mongodb.DBObject;
66
import com.mongodb.MongoClient;
7-
import com.mongodb.MongoClientOptions;
8-
import com.mongodb.MongoCredential;
7+
import com.mongodb.MongoClientURI;
98
import com.mongodb.ServerAddress;
109
import com.mongodb.gridfs.GridFS;
1110
import com.mongodb.gridfs.GridFSDBFile;
@@ -32,10 +31,10 @@
3231

3332
import javax.inject.Inject;
3433
import java.net.UnknownHostException;
35-
import java.util.ArrayList;
3634
import java.util.List;
3735
import java.util.Set;
3836

37+
import static com.commercehub.jclouds.gridfs.blobstore.Constants.GRIDFS_URI_SCHEME;
3938
import static com.commercehub.jclouds.gridfs.blobstore.Util.parseGridFSIdentifier;
4039
import static com.commercehub.jclouds.gridfs.blobstore.Util.parseServerAddresses;
4140
import static com.google.common.base.Preconditions.checkNotNull;
@@ -60,15 +59,18 @@ protected GridFSBlobStore(ProviderMetadata providerMetadata, BlobStoreContext co
6059
this.dbFileToBlobMetadata = dbFileToBlobMetadata;
6160
this.locations = checkNotNull(locations, "locations");
6261

63-
List<ServerAddress> addresses = parseServerAddresses(providerMetadata.getEndpoint());
64-
List<MongoCredential> credentials = new ArrayList<>(); // TODO support credentials
65-
MongoClientOptions options = MongoClientOptions.builder().build(); // TODO support options configuration
66-
if (addresses.size() > 1) {
67-
this.mongo = new MongoClient(addresses, credentials, options);
62+
String endpoint = providerMetadata.getEndpoint();
63+
if (endpoint.startsWith(GRIDFS_URI_SCHEME)) {
64+
List<ServerAddress> addresses = parseServerAddresses(endpoint);
65+
if (addresses.size() > 1) {
66+
this.mongo = new MongoClient(addresses);
67+
} else {
68+
// If only one address, assume we want single-node mode.
69+
// You should always use multiple seeds with a replica set.
70+
this.mongo = new MongoClient(addresses.get(0));
71+
}
6872
} else {
69-
// If only one address, assume we want single-node mode.
70-
// You should always use multiple seeds with a replica set.
71-
this.mongo = new MongoClient(addresses.get(0), credentials, options);
73+
this.mongo = new MongoClient(new MongoClientURI(endpoint));
7274
}
7375
}
7476

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.commercehub.jclouds.gridfs.blobstore
2+
3+
import org.jclouds.ContextBuilder
4+
import org.jclouds.blobstore.BlobStore
5+
import org.jclouds.blobstore.BlobStoreContext
6+
import spock.lang.Specification
7+
8+
import static com.commercehub.jclouds.gridfs.blobstore.Constants.GRIDFS_URI_SCHEME
9+
import static org.jclouds.Constants.PROPERTY_ENDPOINT
10+
11+
class GridFSBlobStoreEndpointSpec extends Specification {
12+
private static final HOST = "localhost"
13+
private static final PORT = 27017
14+
15+
private BlobStoreContext context
16+
private BlobStore blobStore
17+
18+
private void initBlobStore(String endpoint) {
19+
// TODO: use embedded mongo
20+
def overrides = new Properties()
21+
overrides.setProperty(PROPERTY_ENDPOINT, endpoint)
22+
context = ContextBuilder.newBuilder("gridfs")
23+
.overrides(overrides)
24+
.buildView(BlobStoreContext)
25+
blobStore = context.getBlobStore()
26+
}
27+
28+
def cleanup() {
29+
if (context) {
30+
context.close()
31+
}
32+
}
33+
34+
def "can create blobStore with 'gridfs://' endpoint"() {
35+
expect:
36+
initBlobStore("${GRIDFS_URI_SCHEME}://${HOST}:${PORT}")
37+
}
38+
39+
def "can create blobStore with standard connection string endpoint"() {
40+
expect:
41+
initBlobStore("mongodb://${HOST}:${PORT}")
42+
}
43+
}

src/test/groovy/com/commercehub/jclouds/gridfs/blobstore/GridFSBlobStoreSpec.groovy

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import org.jclouds.blobstore.options.PutOptions
1515
import spock.lang.Shared
1616
import spock.lang.Specification
1717

18-
import static com.commercehub.jclouds.gridfs.blobstore.Constants.GRIDFS_URI_SCHEME
19-
2018
class GridFSBlobStoreSpec extends Specification {
2119
private static final DB_NAME = this.simpleName
2220
private static final BUCKET = "bk1"
@@ -40,7 +38,7 @@ class GridFSBlobStoreSpec extends Specification {
4038
mongo.getDB(DB_NAME).dropDatabase()
4139
// TODO: use embedded mongo
4240
def overrides = new Properties()
43-
overrides.setProperty(Constants.PROPERTY_ENDPOINT, "${GRIDFS_URI_SCHEME}://${host}:${port}");
41+
overrides.setProperty(Constants.PROPERTY_ENDPOINT, "mongodb://${host}:${port}");
4442
context = ContextBuilder.newBuilder("gridfs")
4543
.overrides(overrides)
4644
.buildView(BlobStoreContext)

0 commit comments

Comments
 (0)