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

Implement JsonbComponentInstanceCreator #2769

Merged
merged 1 commit into from
Jun 11, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
<jboss-logmanager.version>1.0.3</jboss-logmanager.version>
<jetty.version>9.4.18.v20190429</jetty.version>
<flyway.version>5.2.4</flyway.version>
<yasson.version>1.0.3</yasson.version>
<yasson.version>1.0.4</yasson.version>
<!-- Used for integration tests, to make sure webjars work-->
<bootstrap.version>3.1.0</bootstrap.version>
<keycloak.version>6.0.1</keycloak.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.util.List;
import java.util.Map;

import javax.servlet.Servlet;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
Expand Down Expand Up @@ -82,4 +80,4 @@ public String getEffectiveServletName(final String key) {

}

}
}
4 changes: 4 additions & 0 deletions extensions/jsonb/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jsonp-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,51 @@
*/
package io.quarkus.jsonb.deployment;

import java.util.function.Predicate;

import javax.json.bind.adapter.JsonbAdapter;

import org.eclipse.yasson.JsonBindingProvider;
import org.eclipse.yasson.spi.JsonbComponentInstanceCreator;
import org.jboss.jandex.DotName;

import io.quarkus.arc.deployment.BeanArchiveIndexBuildItem;
import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
import io.quarkus.arc.processor.BeanInfo;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.substrate.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.substrate.ServiceProviderBuildItem;
import io.quarkus.deployment.builditem.substrate.SubstrateResourceBundleBuildItem;
import io.quarkus.jsonb.QuarkusJsonbComponentInstanceCreator;

public class JsonbProcessor {

static final DotName JSONB_ADAPTER_NAME = DotName.createSimple(JsonbAdapter.class.getName());

@BuildStep
void build(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<SubstrateResourceBundleBuildItem> resourceBundle) {
BuildProducer<SubstrateResourceBundleBuildItem> resourceBundle,
BuildProducer<ServiceProviderBuildItem> serviceProvider) {
reflectiveClass.produce(new ReflectiveClassBuildItem(false, false,
JsonBindingProvider.class.getName()));

resourceBundle.produce(new SubstrateResourceBundleBuildItem("yasson-messages"));

serviceProvider.produce(new ServiceProviderBuildItem(JsonbComponentInstanceCreator.class.getName(),
QuarkusJsonbComponentInstanceCreator.class.getName()));
}

@BuildStep
void unremovableJsonbAdapters(BuildProducer<UnremovableBeanBuildItem> unremovableBeans,
BeanArchiveIndexBuildItem beanArchiveIndex) {
unremovableBeans.produce(new UnremovableBeanBuildItem(new Predicate<BeanInfo>() {

@Override
public boolean test(BeanInfo bean) {
return bean.isClassBean() && bean.hasType(JSONB_ADAPTER_NAME);
}
}));
}

}
4 changes: 4 additions & 0 deletions extensions/jsonb/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jsonp</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.quarkus.jsonb;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.yasson.spi.JsonbComponentInstanceCreator;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InstanceHandle;

public class QuarkusJsonbComponentInstanceCreator implements JsonbComponentInstanceCreator {

private final Map<Class<?>, Object> components;

// Instance handles are used to destroy the CDI-based components correctly
private final List<InstanceHandle<?>> beanHandles;

public QuarkusJsonbComponentInstanceCreator() {
this.beanHandles = new ArrayList<>();
this.components = new ConcurrentHashMap<>();
}

@Override
public void close() throws IOException {
beanHandles.forEach(InstanceHandle::close);
beanHandles.clear();
components.clear();
}

@SuppressWarnings("unchecked")
@Override
public <T> T getOrCreateComponent(Class<T> componentClass) {
return (T) components.computeIfAbsent(componentClass, c -> {
InstanceHandle<T> beanHandle = Arc.container().instance(componentClass);
if (beanHandle.isAvailable()) {
beanHandles.add(beanHandle);
return beanHandle.get();
}
try {
return componentClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException("Cannot instantiate Jsonb component: " + componentClass, e);
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.jsonb.QuarkusJsonbComponentInstanceCreator
2 changes: 1 addition & 1 deletion independent-projects/arc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<!-- Versions -->
<version.cdi>2.0.SP1</version.cdi>
<version.jandex>2.1.0.Final</version.jandex>
<version.jandex>2.1.1.Final</version.jandex>
<version.junit4>4.12</version.junit4>
<version.maven>3.5.2</version.maven>
<version.jboss-logging>3.3.2.Final</version.jboss-logging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ public Set<Type> getTypes() {
return types;
}

public boolean hasType(DotName typeName) {
for (Type type : types) {
if (type.name().equals(typeName)) {
return true;
}
}
return false;
}

public Set<AnnotationInstance> getQualifiers() {
return qualifiers;
}
Expand Down