Skip to content

Commit

Permalink
Remove MongoDB with Panache deprecated classes
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Oct 19, 2021
1 parent 36dee61 commit 69d270b
Show file tree
Hide file tree
Showing 20 changed files with 44 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public class SplitPackageProcessor {
public boolean test(String packageName) {
// Remove the elements from this list when the original issue is fixed
// so that we can detect further issues.
return packageName.startsWith("io.fabric8.kubernetes")
|| packageName.equals("io.quarkus.mongodb.panache.reactive")
|| packageName.equals("io.quarkus.mongodb.panache");
return packageName.startsWith("io.fabric8.kubernetes");
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
import io.quarkus.jsonb.spi.JsonbSerializerBuildItem;
import io.quarkus.mongodb.deployment.MongoClientNameBuildItem;
import io.quarkus.mongodb.deployment.MongoUnremovableClientsBuildItem;
import io.quarkus.mongodb.panache.MongoEntity;
import io.quarkus.mongodb.panache.ProjectionFor;
import io.quarkus.mongodb.panache.common.PanacheMongoRecorder;
import io.quarkus.mongodb.panache.jackson.ObjectIdDeserializer;
import io.quarkus.mongodb.panache.jackson.ObjectIdSerializer;
Expand All @@ -70,10 +68,8 @@ public abstract class BasePanacheMongoResourceProcessor {
public static final DotName BSON_IGNORE = createSimple(BsonIgnore.class.getName());
public static final DotName BSON_PROPERTY = createSimple(BsonProperty.class.getName());
public static final DotName MONGO_ENTITY = createSimple(io.quarkus.mongodb.panache.common.MongoEntity.class.getName());
public static final DotName DEPRECATED_MONGO_ENTITY = createSimple(MongoEntity.class.getName());
public static final DotName OBJECT_ID = createSimple(ObjectId.class.getName());
public static final DotName PROJECTION_FOR = createSimple(io.quarkus.mongodb.panache.common.ProjectionFor.class.getName());
public static final DotName DEPRECATED_PROJECTION_FOR = createSimple(ProjectionFor.class.getName());

@BuildStep
public void buildImperative(CombinedIndexBuildItem index,
Expand Down Expand Up @@ -225,27 +221,6 @@ protected void handleProjectionFor(CombinedIndexBuildItem index,
transformers.produce(new BytecodeTransformerBuildItem(info.name().toString(), fieldEnhancer));
}

// Register for building the property mapping cache
propertyMappingClass
.produce(new PropertyMappingClassBuildStep(targetClass.name().toString(),
annotationInstance.target().asClass().name().toString()));
}
for (AnnotationInstance annotationInstance : index.getIndex().getAnnotations(DEPRECATED_PROJECTION_FOR)) {
Type targetClass = annotationInstance.value().asClass();
ClassInfo target = index.getIndex().getClassByName(targetClass.name());
Map<String, String> classPropertyMapping = new HashMap<>();
extractMappings(classPropertyMapping, target, index);
propertyMapping.put(targetClass.name(), classPropertyMapping);
}
for (AnnotationInstance annotationInstance : index.getIndex().getAnnotations(DEPRECATED_PROJECTION_FOR)) {
Type targetClass = annotationInstance.value().asClass();
Map<String, String> targetPropertyMapping = propertyMapping.get(targetClass.name());
if (targetPropertyMapping != null && !targetPropertyMapping.isEmpty()) {
ClassInfo info = annotationInstance.target().asClass();
ProjectionForEnhancer fieldEnhancer = new ProjectionForEnhancer(targetPropertyMapping);
transformers.produce(new BytecodeTransformerBuildItem(info.name().toString(), fieldEnhancer));
}

// Register for building the property mapping cache
propertyMappingClass
.produce(new PropertyMappingClassBuildStep(targetClass.name().toString(),
Expand All @@ -265,13 +240,6 @@ public void mongoClientNames(ApplicationArchivesBuildItem applicationArchivesBui
values.add(clientName.asString());
}
}
instances = indexView.getAnnotations(DEPRECATED_MONGO_ENTITY);
for (AnnotationInstance annotation : instances) {
AnnotationValue clientName = annotation.value("clientName");
if ((clientName != null) && !clientName.asString().isEmpty()) {
values.add(clientName.asString());
}
}
for (String value : values) {
// we don't want the qualifier @MongoClientName qualifier added
// as these clients will only be looked up programmatically via name
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* Use one of its methods to perform the update query.
*/
public interface PanacheUpdate extends io.quarkus.mongodb.panache.PanacheUpdate {
public interface PanacheUpdate {

/**
* Execute the update query with the update document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* Use one of its methods to perform the update query.
*/
public interface ReactivePanacheUpdate extends io.quarkus.mongodb.panache.reactive.ReactivePanacheUpdate {
public interface ReactivePanacheUpdate {
/**
* Execute the update query with the update document.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import com.mongodb.client.model.ReplaceOptions;
import com.mongodb.client.model.WriteModel;

import io.quarkus.mongodb.panache.MongoEntity;
import io.quarkus.mongodb.panache.binder.NativeQueryBinder;
import io.quarkus.mongodb.panache.binder.PanacheQlQueryBinder;
import io.quarkus.mongodb.panache.common.MongoEntity;
import io.quarkus.mongodb.reactive.ReactiveMongoClient;
import io.quarkus.mongodb.reactive.ReactiveMongoCollection;
import io.quarkus.mongodb.reactive.ReactiveMongoDatabase;
Expand Down Expand Up @@ -214,24 +214,17 @@ public Uni<Void> delete(Object entity) {
}

public ReactiveMongoCollection mongoCollection(Class<?> entityClass) {
MongoEntity legacyEntity = entityClass.getAnnotation(MongoEntity.class);
io.quarkus.mongodb.panache.common.MongoEntity mongoEntity = entityClass
.getAnnotation(io.quarkus.mongodb.panache.common.MongoEntity.class);
ReactiveMongoDatabase database = mongoDatabase(legacyEntity, mongoEntity);
if (legacyEntity != null && !legacyEntity.collection().isEmpty()) {
return database.getCollection(legacyEntity.collection(), entityClass);
}
MongoEntity mongoEntity = entityClass.getAnnotation(MongoEntity.class);
ReactiveMongoDatabase database = mongoDatabase(mongoEntity);
if (mongoEntity != null && !mongoEntity.collection().isEmpty()) {
return database.getCollection(mongoEntity.collection(), entityClass);
}
return database.getCollection(entityClass.getSimpleName(), entityClass);
}

public ReactiveMongoDatabase mongoDatabase(Class<?> entityClass) {
MongoEntity legacyEntity = entityClass.getAnnotation(MongoEntity.class);
io.quarkus.mongodb.panache.common.MongoEntity mongoEntity = entityClass
.getAnnotation(io.quarkus.mongodb.panache.common.MongoEntity.class);
return mongoDatabase(legacyEntity, mongoEntity);
MongoEntity mongoEntity = entityClass.getAnnotation(MongoEntity.class);
return mongoDatabase(mongoEntity);
}

//
Expand Down Expand Up @@ -316,21 +309,17 @@ private ReactiveMongoCollection mongoCollection(Object entity) {
return mongoCollection(entityClass);
}

private ReactiveMongoDatabase mongoDatabase(MongoEntity legacyEntity,
io.quarkus.mongodb.panache.common.MongoEntity mongoEntity) {
ReactiveMongoClient mongoClient = clientFromArc(legacyEntity, mongoEntity, ReactiveMongoClient.class, true);
if (legacyEntity != null && !legacyEntity.database().isEmpty()) {
return mongoClient.getDatabase(legacyEntity.database());
}
String databaseName = getDefaultDatabaseName(legacyEntity, mongoEntity);
private ReactiveMongoDatabase mongoDatabase(MongoEntity mongoEntity) {
ReactiveMongoClient mongoClient = clientFromArc(mongoEntity, ReactiveMongoClient.class, true);
String databaseName = getDefaultDatabaseName(mongoEntity);
return mongoClient.getDatabase(databaseName);
}

private String getDefaultDatabaseName(MongoEntity legacyEntity, io.quarkus.mongodb.panache.common.MongoEntity mongoEntity) {
return defaultDatabaseName.computeIfAbsent(beanName(legacyEntity, mongoEntity), new Function<String, String>() {
private String getDefaultDatabaseName(MongoEntity mongoEntity) {
return defaultDatabaseName.computeIfAbsent(beanName(mongoEntity), new Function<String, String>() {
@Override
public String apply(String beanName) {
return getDatabaseName(legacyEntity, mongoEntity, beanName);
return getDatabaseName(mongoEntity, beanName);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.quarkus.arc.Arc;
import io.quarkus.arc.InjectableBean;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.mongodb.panache.MongoEntity;
import io.quarkus.mongodb.panache.common.MongoEntity;
import io.quarkus.mongodb.runtime.MongoClientBeanUtil;
import io.quarkus.mongodb.runtime.MongoClientConfig;
import io.quarkus.mongodb.runtime.MongoClients;
Expand All @@ -17,28 +17,24 @@ public final class BeanUtils {
private BeanUtils() {
}

public static String beanName(MongoEntity legacyEntity, io.quarkus.mongodb.panache.common.MongoEntity entity) {
public static String beanName(MongoEntity entity) {
if (entity != null && !entity.clientName().isEmpty()) {
return entity.clientName();
}
if (legacyEntity != null && !legacyEntity.clientName().isEmpty()) {
return legacyEntity.clientName();
}

return MongoClientBeanUtil.DEFAULT_MONGOCLIENT_NAME;
}

public static <T> T clientFromArc(MongoEntity legacyEntity, io.quarkus.mongodb.panache.common.MongoEntity entity,
public static <T> T clientFromArc(MongoEntity entity,
Class<T> clientClass, boolean isReactive) {
T mongoClient = Arc.container()
.instance(clientClass, MongoClientBeanUtil.clientLiteral(beanName(legacyEntity, entity), isReactive))
.instance(clientClass, MongoClientBeanUtil.clientLiteral(beanName(entity), isReactive))
.get();
if (mongoClient != null) {
return mongoClient;
}

if ((entity == null || entity.clientName().isEmpty())
&& (legacyEntity == null || legacyEntity.clientName().isEmpty())) {
if ((entity == null || entity.clientName().isEmpty())) {
// this case happens when there are multiple instances because they are all annotated with @Named
for (InstanceHandle<T> handle : Arc.container().select(clientClass).handles()) {
InjectableBean<T> bean = handle.getBean();
Expand All @@ -53,17 +49,13 @@ public static <T> T clientFromArc(MongoEntity legacyEntity, io.quarkus.mongodb.p
}
}
throw new IllegalStateException(String.format("Unable to find default %s bean", clientClass.getSimpleName()));
} else if (entity != null && !entity.clientName().isEmpty()) {
throw new IllegalStateException(
String.format("Unable to find %s bean for entity %s", clientClass.getSimpleName(), entity));
} else {
throw new IllegalStateException(
String.format("Unable to find %s bean for entity %s", clientClass.getSimpleName(), legacyEntity));
String.format("Unable to find %s bean for entity %s", clientClass.getSimpleName(), entity));
}
}

public static String getDatabaseName(MongoEntity legacyEntity, io.quarkus.mongodb.panache.common.MongoEntity mongoEntity,
String clientBeanName) {
public static String getDatabaseName(MongoEntity mongoEntity, String clientBeanName) {
MongoClients mongoClients = Arc.container().instance(MongoClients.class).get();
MongoClientConfig matchingMongoClientConfig = mongoClients.getMatchingMongoClientConfig(clientBeanName);
if (matchingMongoClientConfig.database.isPresent()) {
Expand All @@ -78,23 +70,14 @@ public static String getDatabaseName(MongoEntity legacyEntity, io.quarkus.mongod
}
}

if (legacyEntity == null && mongoEntity == null) {
if (mongoEntity == null) {
throw new IllegalArgumentException(
"The database property was not configured for the default Mongo Client (via 'quarkus.mongodb.database'");
}
if (legacyEntity != null && legacyEntity.clientName().isEmpty()) {
if (mongoEntity.clientName().isEmpty()) {
throw new IllegalArgumentException("The database attribute was not set for the @MongoEntity annotation "
+ "and neither was the database property configured for the default Mongo Client (via 'quarkus.mongodb.database')");
}
if (mongoEntity != null && mongoEntity.clientName().isEmpty()) {
throw new IllegalArgumentException("The database attribute was not set for the @MongoEntity annotation "
+ "and neither was the database property configured for the default Mongo Client (via 'quarkus.mongodb.database')");
}
if (legacyEntity != null) {
throw new IllegalArgumentException(String.format(
"The database attribute was not set for the @MongoEntity annotation neither was the database property configured for the named Mongo Client (via 'quarkus.mongodb.%s.database')",
legacyEntity.clientName()));
}
throw new IllegalArgumentException(String.format(
"The database attribute was not set for the @MongoEntity annotation neither was the database property configured for the named Mongo Client (via 'quarkus.mongodb.%s.database')",
mongoEntity.clientName()));
Expand Down
Loading

0 comments on commit 69d270b

Please sign in to comment.