Skip to content

Commit

Permalink
Start progress on Petclinic sample / Refine MongoDB configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Feb 13, 2018
1 parent 113b254 commit d5de865
Show file tree
Hide file tree
Showing 41 changed files with 1,011 additions and 208 deletions.
4 changes: 0 additions & 4 deletions ROADMAP.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ class TypeConverterFactory {

At compile time we know the types but at runtime the lambda loses type information. We need to add an API to obtain type information for a bean from the factory.

#### Property Placeholder Support

Need to be able to specify placeholders in annotations and in configuration that are resolved at runtime.

#### AOP API

Expand All @@ -62,7 +59,6 @@ We need to implement AOP annotations for:

- Retry
- Hystrix
- Scheduled

#### Configuration Sharing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public InterceptorChain(Interceptor<B, R>[] interceptors,
this.isIntroduction = target instanceof Introduced;
if(isIntroduction) {
this.interceptors[this.interceptors.length-1] = context -> {
throw new UnsupportedOperationException("Introduction advice reached the end of the chain and possible implementations were found");
throw new UnsupportedOperationException("Introduction advice reached the end of the chain and no possible implementations were found");
};
}
else {
Expand Down
5 changes: 3 additions & 2 deletions configurations/mongo-reactive/build.gradle
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"
}
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 {
}
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();
}
}
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());
}

}

This file was deleted.

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 +
'}';
}
}
Loading

0 comments on commit d5de865

Please sign in to comment.