Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register classes with Solr Field annotations for reflection #2342

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
*/
package org.apache.camel.quarkus.component.solr.deployment;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import org.jboss.logging.Logger;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;

class SolrProcessor {

private static final Logger LOG = Logger.getLogger(SolrProcessor.class);
private static final String FEATURE = "camel-solr";
private static final DotName FIELD_DOT_NAME = DotName.createSimple("org.apache.solr.client.solrj.beans.Field");

@BuildStep
FeatureBuildItem feature() {
Expand All @@ -35,4 +40,27 @@ FeatureBuildItem feature() {
ExtensionSslNativeSupportBuildItem activateSslNativeSupport() {
return new ExtensionSslNativeSupportBuildItem(FEATURE);
}

@BuildStep
void registerForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveClass, CombinedIndexBuildItem combinedIndex) {
// Register any classes within the application archive that contain the Solr Field annotation
combinedIndex.getIndex()
.getAnnotations(FIELD_DOT_NAME)
.stream()
.map(annotationInstance -> {
AnnotationTarget target = annotationInstance.target();
AnnotationTarget.Kind kind = target.kind();
if (kind.equals(AnnotationTarget.Kind.FIELD)) {
ClassInfo classInfo = target.asField().declaringClass();
return new ReflectiveClassBuildItem(false, true, classInfo.name().toString());
} else if (kind.equals(AnnotationTarget.Kind.METHOD)) {
ClassInfo classInfo = target.asMethod().declaringClass();
return new ReflectiveClassBuildItem(true, false, classInfo.name().toString());
} else {
throw new RuntimeException(
jamesnetherton marked this conversation as resolved.
Show resolved Hide resolved
FIELD_DOT_NAME.toString() + " cannot be applied to " + target.kind().toString());
}
})
.forEach(reflectiveClass::produce);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.ws.rs.DELETE;
Expand Down Expand Up @@ -64,15 +65,17 @@ public abstract class SolrCommonResource {

@PUT
@Path("bean")
public Response addBean(Item bean) {
public Response addBean(String name) {
Item bean = createItem(name);
producerTemplate.sendBodyAndHeader(solrComponentURI, bean, SolrConstants.OPERATION, SolrConstants.OPERATION_ADD_BEAN);
solrCommit();
return Response.accepted().build();
}

@PUT
@Path("beans")
public Response addBeans(List<Item> beans) {
public Response addBeans(List<String> names) {
List<Item> beans = names.stream().map(this::createItem).collect(Collectors.toList());
producerTemplate.sendBodyAndHeader(solrComponentURI, beans, SolrConstants.OPERATION, SolrConstants.OPERATION_ADD_BEANS);
solrCommit();
return Response.accepted().build();
Expand Down Expand Up @@ -165,12 +168,19 @@ private void solrCommit() {

@GET
@Path("bean/{id}")
public Item getBeanById(@PathParam("id") String id) throws IOException, SolrServerException {
public String getBeanById(@PathParam("id") String id) throws IOException, SolrServerException {
SolrQuery solrQuery = new SolrQuery();
solrQuery.set("q", "id:" + id);
QueryRequest queryRequest = new QueryRequest(solrQuery);
QueryResponse response = queryRequest.process(solrClient);
List<Item> responses = response.getBeans(Item.class);
return responses.size() != 0 ? responses.get(0) : new Item();
return responses.size() != 0 ? responses.get(0).getId() : "";
}

private Item createItem(String id) {
Item item = new Item();
item.setId(id);
item.setCategories(new String[] { "aaa", "bbb", "ccc" });
return item;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

public class Item {

@Field
String id;

@Field("cat")
Expand All @@ -33,6 +32,7 @@ public String getId() {
return id;
}

@Field
public void setId(String id) {
this.id = id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import org.apache.camel.quarkus.component.solr.it.bean.Item;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -50,18 +49,17 @@ private static Stream<String> resources() {
@MethodSource("resources")
public void testSingleBean(String resource) {
// create a bean item
Item item = createItem("test1");
given()
.contentType(ContentType.JSON)
.body(item)
.body("test1")
.put(resource + "/bean")
.then()
.statusCode(202);
// verify existing bean
given()
.get(resource + "/bean/test1")
.then()
.body("id", equalTo("test1"));
.body(equalTo("test1"));

// delete bean by id
given()
Expand All @@ -74,21 +72,21 @@ public void testSingleBean(String resource) {
given()
.get(resource + "/bean/test1")
.then()
.body("id", emptyOrNullString());
.body(emptyOrNullString());
}

@ParameterizedTest
@MethodSource("resources")
public void testMultipleBeans(String resource) {
// create list of beans
List<Item> beans = new ArrayList<>();
beans.add(createItem("bean1"));
beans.add(createItem("bean2"));
List<String> beanNames = new ArrayList<>();
beanNames.add("bean1");
beanNames.add("bean2");

// add beans with camel
given()
.contentType(ContentType.JSON)
.body(beans)
.body(beanNames)
.put(resource + "/beans")
.then()
.statusCode(202);
Expand All @@ -97,11 +95,11 @@ public void testMultipleBeans(String resource) {
given()
.get(resource + "/bean/bean1")
.then()
.body("id", equalTo("bean1"));
.body(equalTo("bean1"));
given()
.get(resource + "/bean/bean2")
.then()
.body("id", equalTo("bean2"));
.body(equalTo("bean2"));

// delete all beans that has id begins with bean
given()
Expand All @@ -115,11 +113,11 @@ public void testMultipleBeans(String resource) {
given()
.get(resource + "/bean/bean1")
.then()
.body("id", emptyOrNullString());
.body(emptyOrNullString());
given()
.get(resource + "/bean/bean2")
.then()
.body("id", emptyOrNullString());
.body(emptyOrNullString());
}

@ParameterizedTest
Expand All @@ -140,7 +138,7 @@ public void testInsertId(String resource) {
given()
.get(resource + "/bean/id1")
.then()
.body("id", equalTo("id1"));
.body(equalTo("id1"));

}

Expand All @@ -160,7 +158,7 @@ public void testOptimize(String resource) {
given()
.get(resource + "/bean/opt1")
.then()
.body("id", emptyOrNullString());
.body(emptyOrNullString());
// optimize
given()
.get(resource + "/optimize")
Expand All @@ -170,7 +168,7 @@ public void testOptimize(String resource) {
given()
.get(resource + "/bean/opt1")
.then()
.body("id", equalTo("opt1"));
.body(equalTo("opt1"));

}

Expand All @@ -191,7 +189,7 @@ public void testRollback(String resource) {
given()
.get(resource + "/bean/roll1")
.then()
.body("id", emptyOrNullString());
.body(emptyOrNullString());
// rollback
given()
.get(resource + "/rollback")
Expand All @@ -206,7 +204,7 @@ public void testRollback(String resource) {
given()
.get(resource + "/bean/roll1")
.then()
.body("id", emptyOrNullString());
.body(emptyOrNullString());
}

@ParameterizedTest
Expand All @@ -225,7 +223,7 @@ public void testSoftCommit(String resource) {
given()
.get(resource + "/bean/com1")
.then()
.body("id", emptyOrNullString());
.body(emptyOrNullString());
// soft commit
given()
.get(resource + "/softcommit")
Expand All @@ -235,7 +233,7 @@ public void testSoftCommit(String resource) {
given()
.get(resource + "/bean/com1")
.then()
.body("id", equalTo("com1"));
.body(equalTo("com1"));
}

@ParameterizedTest
Expand All @@ -260,13 +258,6 @@ public void testInsertStreaming(String resource) throws InterruptedException {
given()
.get(resource + "/bean/stream1")
.then()
.body("id", equalTo("stream1"));
}

private Item createItem(String id) {
Item item = new Item();
item.setId(id);
item.setCategories(new String[] { "aaa", "bbb", "ccc" });
return item;
.body(equalTo("stream1"));
}
}