forked from micronaut-projects/micronaut-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start progress on Petclinic sample / Refine MongoDB configuration
- Loading branch information
1 parent
113b254
commit d5de865
Showing
41 changed files
with
1,011 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
dependencies { | ||
compile project(":inject") | ||
compile project(":runtime") | ||
compile project(":validation") | ||
compileOnly project(":inject-java") | ||
compile "org.mongodb:mongodb-driver-reactivestreams:1.7.0" | ||
|
||
compileOnly "de.flapdoodle.embed:de.flapdoodle.embed.mongo:2.0.1" | ||
testCompile "de.flapdoodle.embed:de.flapdoodle.embed.mongo:2.0.1" | ||
testCompile "io.reactivex.rxjava2:rxjava:$rxJava2Version" | ||
} |
25 changes: 25 additions & 0 deletions
25
...n/java/org/particleframework/configuration/mongo/reactive/AbstractMongoClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2018 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.particleframework.configuration.mongo.reactive; | ||
|
||
import com.mongodb.reactivestreams.client.MongoClient; | ||
|
||
/** | ||
* @author graemerocher | ||
* @since 1.0 | ||
*/ | ||
public abstract class AbstractMongoClientFactory { | ||
} |
123 changes: 123 additions & 0 deletions
123
...n/java/org/particleframework/configuration/mongo/reactive/AbstractMongoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2018 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.particleframework.configuration.mongo.reactive; | ||
|
||
import com.mongodb.ConnectionString; | ||
import com.mongodb.ServerAddress; | ||
import com.mongodb.async.client.MongoClientSettings; | ||
import com.mongodb.connection.*; | ||
import com.mongodb.reactivestreams.client.MongoClients; | ||
import org.bson.codecs.pojo.PojoCodecProvider; | ||
import org.particleframework.core.util.StringUtils; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.bson.codecs.configuration.CodecRegistries.fromProviders; | ||
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries; | ||
|
||
/** | ||
* Abstract Mongo configuration type | ||
* | ||
* @author graemerocher | ||
* @since 1.0 | ||
*/ | ||
abstract class AbstractMongoConfiguration { | ||
|
||
protected String uri; | ||
|
||
/** | ||
* @return The MongoDB URI | ||
*/ | ||
@NotBlank | ||
public String getUri() { | ||
return uri; | ||
} | ||
|
||
/** | ||
* @return The {@link ClusterSettings#builder()} | ||
*/ | ||
public abstract ClusterSettings.Builder getClusterSettings(); | ||
|
||
/** | ||
* @return The {@link MongoClientSettings#builder()} | ||
*/ | ||
public abstract MongoClientSettings.Builder getClientSettings(); | ||
|
||
public abstract ServerSettings.Builder getServerSettings(); | ||
|
||
public abstract ConnectionPoolSettings.Builder getPoolSettings(); | ||
|
||
public abstract SocketSettings.Builder getSocketSettings(); | ||
|
||
public abstract SslSettings.Builder getSslSettings(); | ||
|
||
/** | ||
* Sets the MongoDB URI | ||
* @param uri The MongoDB URI | ||
*/ | ||
public void setUri(String uri) { | ||
this.uri = uri; | ||
} | ||
|
||
|
||
/** | ||
* @return The MongoDB {@link ConnectionString} | ||
*/ | ||
public Optional<ConnectionString> getConnectionString() { | ||
if(StringUtils.isNotEmpty(uri)) { | ||
return Optional.of(new ConnectionString(uri)); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
/** | ||
* @return Builds the {@link MongoClientSettings} | ||
*/ | ||
public MongoClientSettings buildSettings() { | ||
Optional<ConnectionString> connectionString = getConnectionString(); | ||
ClusterSettings.Builder clusterSettings = getClusterSettings(); | ||
SslSettings.Builder sslSettings = getSslSettings(); | ||
ConnectionPoolSettings.Builder poolSettings = getPoolSettings(); | ||
SocketSettings.Builder socketSettings = getSocketSettings(); | ||
ServerSettings.Builder serverSettings = getServerSettings(); | ||
|
||
if(connectionString.isPresent()) { | ||
ConnectionString cs = connectionString.get(); | ||
|
||
serverSettings.applyConnectionString(cs); | ||
clusterSettings.applyConnectionString(cs); | ||
poolSettings.applyConnectionString(cs); | ||
sslSettings.applyConnectionString(cs); | ||
socketSettings.applyConnectionString(cs); | ||
} | ||
MongoClientSettings.Builder clientSettings = getClientSettings(); | ||
clientSettings.clusterSettings(clusterSettings.build()) | ||
.serverSettings(serverSettings.build()) | ||
.connectionPoolSettings(poolSettings.build()) | ||
.socketSettings(socketSettings.build()) | ||
.sslSettings(sslSettings.build()) ; | ||
|
||
clientSettings.codecRegistry( | ||
fromRegistries(MongoClients.getDefaultCodecRegistry(), | ||
fromProviders(PojoCodecProvider.builder().automatic(true).build())) | ||
|
||
); | ||
return clientSettings.build(); | ||
} | ||
} |
35 changes: 26 additions & 9 deletions
35
...in/java/org/particleframework/configuration/mongo/reactive/DefaultMongoClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,43 @@ | ||
/* | ||
* Copyright 2018 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.particleframework.configuration.mongo.reactive; | ||
|
||
import com.mongodb.reactivestreams.client.MongoClient; | ||
import com.mongodb.reactivestreams.client.MongoClients; | ||
import org.particleframework.context.annotation.*; | ||
|
||
import javax.inject.Singleton; | ||
import org.particleframework.context.annotation.Bean; | ||
import org.particleframework.context.annotation.Factory; | ||
import org.particleframework.context.annotation.Primary; | ||
import org.particleframework.context.annotation.Requires; | ||
import org.particleframework.runtime.context.scope.Refreshable; | ||
|
||
/** | ||
* Factory for the default {@link MongoClient}. Creates the injectable {@link Primary} bean | ||
* | ||
* @author Graeme Rocher | ||
* @since 1.0 | ||
*/ | ||
@Requires(property = "particle.mongo.uri") | ||
@Requires(missingProperty = "particle.mongo.uris") | ||
@Requires(beans = MongoConfiguration.class) | ||
@Factory | ||
public class DefaultMongoClientFactory { | ||
public class DefaultMongoClientFactory extends AbstractMongoClientFactory { | ||
|
||
@Bean(preDestroy = "close") | ||
@Singleton | ||
@Refreshable | ||
@Primary | ||
MongoClient mongoClient(@Value("particle.mongo.uri") String uri) { | ||
return MongoClients.create(uri); | ||
MongoClient mongoClient(MongoConfiguration mongoConfiguration) { | ||
return MongoClients.create(mongoConfiguration.buildSettings()); | ||
} | ||
|
||
} |
35 changes: 0 additions & 35 deletions
35
.../org/particleframework/configuration/mongo/reactive/DefaultMongoClusterClientFactory.java
This file was deleted.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
.../src/main/java/org/particleframework/configuration/mongo/reactive/MongoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2018 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.particleframework.configuration.mongo.reactive; | ||
|
||
import com.mongodb.ConnectionString; | ||
import com.mongodb.async.client.MongoClientSettings; | ||
import com.mongodb.connection.*; | ||
import org.particleframework.context.annotation.ConfigurationBuilder; | ||
import org.particleframework.context.annotation.ConfigurationProperties; | ||
import org.particleframework.context.annotation.Requires; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* The default MongoDB configuration class | ||
* | ||
* @author graemerocher | ||
* @since 1.0 | ||
*/ | ||
@Requires(property = MongoSettings.MONGODB_URI) | ||
@Requires(missingProperty = MongoSettings.MONGODB_ADDRESSES) | ||
@ConfigurationProperties(MongoSettings.PREFIX) | ||
public class MongoConfiguration extends AbstractMongoConfiguration { | ||
|
||
@ConfigurationBuilder(prefixes = "") | ||
protected MongoClientSettings.Builder clientSettings = MongoClientSettings.builder(); | ||
|
||
@ConfigurationBuilder(prefixes = "", configurationPrefix = "cluster") | ||
protected ClusterSettings.Builder clusterSettings = ClusterSettings.builder(); | ||
|
||
@ConfigurationBuilder(prefixes = "", configurationPrefix = "server") | ||
protected ServerSettings.Builder serverSettings = ServerSettings.builder(); | ||
|
||
@ConfigurationBuilder(prefixes = "", configurationPrefix = "connectionPool") | ||
protected ConnectionPoolSettings.Builder poolSettings = ConnectionPoolSettings.builder(); | ||
|
||
@ConfigurationBuilder(prefixes = "", configurationPrefix = "socket") | ||
protected SocketSettings.Builder socketSettings = SocketSettings.builder(); | ||
|
||
@ConfigurationBuilder(prefixes = "", configurationPrefix = "ssl") | ||
protected SslSettings.Builder sslSettings = SslSettings.builder(); | ||
|
||
|
||
/** | ||
* @return The {@link ClusterSettings#builder()} | ||
*/ | ||
@Override | ||
public ClusterSettings.Builder getClusterSettings() { | ||
return clusterSettings; | ||
} | ||
|
||
/** | ||
* @return The {@link MongoClientSettings#builder()} | ||
*/ | ||
@Override | ||
public MongoClientSettings.Builder getClientSettings() { | ||
return clientSettings; | ||
} | ||
|
||
|
||
@Override | ||
public ServerSettings.Builder getServerSettings() { | ||
return serverSettings; | ||
} | ||
|
||
@Override | ||
public ConnectionPoolSettings.Builder getPoolSettings() { | ||
return poolSettings; | ||
} | ||
|
||
@Override | ||
public SocketSettings.Builder getSocketSettings() { | ||
return socketSettings; | ||
} | ||
|
||
@Override | ||
public SslSettings.Builder getSslSettings() { | ||
return sslSettings; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "MongoConfiguration{" + | ||
"uri='" + getUri() + '\'' + | ||
", clientSettings=" + clientSettings + | ||
", clusterSettings=" + clusterSettings + | ||
'}'; | ||
} | ||
} |
Oops, something went wrong.