Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

import javax.inject.Inject;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.CompositeIndex;
import org.jboss.jandex.DotName;
Expand Down Expand Up @@ -86,6 +90,11 @@ public void registerField(FieldInfo fieldInfo) {
processorContext.addReflectiveField(fieldInfo);
}
});
for (BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>> transformer : beanDeployment
.getAnnotationTransformers()) {
builder.addAnnotationTransformer(transformer);
}

builder.setOutput(new ResourceOutput() {
@Override
public void writeResource(Resource resource) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,62 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;

public class BeanDeployment {

private final List<String> additionalBeans = new ArrayList<>();

private final Map<String, byte[]> generatedBeans = new HashMap<>();

// Lite profile
private final List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> annotationTransformers = new ArrayList<>();

// Full profile
private final List<String> extensions = new ArrayList<>();

public void addAdditionalBean(Class<?>... beanClass) {
additionalBeans.addAll(Arrays.stream(beanClass).map(Class::getName).collect(Collectors.toList()));
}

public void addAdditionalBean(String... beanClass) {
additionalBeans.addAll(Arrays.stream(beanClass).collect(Collectors.toList()));
}

public void addGeneratedBean(String name, byte[] bean) {
generatedBeans.put(name, bean);
}

public void addAnnotationTransformer(BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>> transformer) {
annotationTransformers.add(transformer);
}

public void addExtension(String extensionClass) {
extensions.add(extensionClass);
}

public List<String> getAdditionalBeans() {
return additionalBeans;
}

public Map<String, byte[]> getGeneratedBeans() {
return generatedBeans;
}

public List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> getAnnotationTransformers() {
return annotationTransformers;
}

public List<String> getExtensions() {
return extensions;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class RuntimePriority {

public static final int UNDERTOW_CREATE_DEPLOYMENT = 100;
public static final int UNDERTOW_REGISTER_SERVLET = 200;
public static final int FAULT_TOLERANCE_DEPLOYMENT = 250;
public static final int HEALTH_DEPLOYMENT = 260;
public static final int WELD_DEPLOYMENT = 300;
public static final int JAXRS_DEPLOYMENT = 350;
Expand Down
5 changes: 5 additions & 0 deletions examples/permissive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<artifactId>shamrock-openapi-deployment</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-fault-tolerance-deployment</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jboss.shamrock.example.faulttolerance;

import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;

import org.eclipse.microprofile.faulttolerance.Retry;

@ApplicationScoped
public class Service {

static final int THRESHOLD = 2;

private String name;

@PostConstruct
void init() {
name = "Lucie";
}

@Retry(maxRetries = 10)
public String getName(AtomicInteger counter) {
if (counter.incrementAndGet() >= THRESHOLD) {
return name;
}
throw new IllegalStateException("Counter=" + counter.get());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jboss.shamrock.example.faulttolerance;

import java.util.concurrent.atomic.AtomicInteger;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/ft")
public class TestResource {

@Inject
Service service;

@GET
public String getName() {
AtomicInteger counter = new AtomicInteger();
String name = service.getName(counter);
return counter + ":" + name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.jboss.shamrock.example.test;

import org.jboss.shamrock.junit.GraalTest;
import org.junit.runner.RunWith;

@RunWith(GraalTest.class)
public class FaultToleranceITCase extends FaultToleranceTestCase {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jboss.shamrock.example.test;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.jboss.shamrock.junit.ShamrockTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(ShamrockTest.class)
public class FaultToleranceTestCase {

@Test
public void testRetry() throws Exception {
URL uri = new URL("http://localhost:8080/rest/ft");
URLConnection connection = uri.openConnection();
InputStream in = connection.getInputStream();
byte[] buf = new byte[100];
int r;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((r = in.read(buf)) > 0) {
out.write(buf, 0, r);
}
Assert.assertEquals("2:Lucie", new String(out.toByteArray()));
}
}
5 changes: 5 additions & 0 deletions examples/strict/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
<artifactId>shamrock-rest-client-deployment</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-fault-tolerance-deployment</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql-protean</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jboss.protean.arc.processor;

import java.util.Collection;
import java.util.List;
import java.util.function.BiFunction;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;

public class AnnotationTransformer {

private List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> transformers = null;

Collection<AnnotationInstance> getAnnotations(AnnotationTarget target) {
Collection<AnnotationInstance> annotations = null;
for (BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>> transformer : transformers) {
annotations = transformer.apply(target, annotations);
}
return annotations;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

import javax.enterprise.inject.spi.DefinitionException;
Expand Down Expand Up @@ -49,7 +50,10 @@ public class BeanDeployment {

private final InterceptorResolver interceptorResolver;

BeanDeployment(IndexView index, Collection<DotName> additionalBeanDefiningAnnotations) {
private final List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> annotationTransformers;

BeanDeployment(IndexView index, Collection<DotName> additionalBeanDefiningAnnotations,
List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> annotationTransformers) {
long start = System.currentTimeMillis();
this.index = index;
this.qualifiers = findQualifiers(index);
Expand All @@ -61,6 +65,7 @@ public class BeanDeployment {
this.beans = findBeans(initBeanDefiningAnnotations(additionalBeanDefiningAnnotations), observers);
this.observers = observers;
this.interceptorResolver = new InterceptorResolver(this);
this.annotationTransformers = annotationTransformers;
LOGGER.infof("Build deployment created in %s ms", System.currentTimeMillis() - start);
}

Expand Down Expand Up @@ -96,6 +101,28 @@ ClassInfo getInterceptorBinding(DotName name) {
return interceptorBindings.get(name);
}

Collection<AnnotationInstance> getAnnotations(AnnotationTarget target) {
Collection<AnnotationInstance> annotations = null;
switch (target.kind()) {
case CLASS:
annotations = target.asClass().classAnnotations();
break;
case METHOD:
annotations = target.asMethod().annotations();
case FIELD:
annotations = target.asField().annotations();
default:
throw new UnsupportedOperationException();
}
if (annotationTransformers == null || annotationTransformers.isEmpty()) {
return annotations;
}
for (BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>> transformer : annotationTransformers) {
annotations = transformer.apply(target, annotations);
}
return annotations;
}

void init() {
long start = System.currentTimeMillis();
for (BeanInfo bean : beans) {
Expand Down Expand Up @@ -142,8 +169,8 @@ private List<BeanInfo> findBeans(List<DotName> beanDefiningAnnotations, List<Obs
continue;
}
if (beanClass.nestingType().equals(NestingType.ANONYMOUS) || beanClass.nestingType().equals(NestingType.LOCAL)
|| (beanClass.nestingType().equals(NestingType.INNER) && !Modifier.isStatic(beanClass.flags())) ||
Modifier.isInterface(beanClass.flags())) {
|| (beanClass.nestingType().equals(NestingType.INNER) && !Modifier.isStatic(beanClass.flags()))
|| Modifier.isInterface(beanClass.flags())) {
// Skip interfaces, annonymous, local and inner classes
continue;
}
Expand Down Expand Up @@ -190,7 +217,7 @@ private List<BeanInfo> findBeans(List<DotName> beanDefiningAnnotations, List<Obs
beans.add(Beans.createProducerMethod(producerMethod, declaringBean, this, findDisposer(declaringBean, producerMethod, disposers)));
}
}

for (FieldInfo producerField : producerFields) {
BeanInfo declaringBean = beanClassToBean.get(producerField.declaringClass());
if (declaringBean != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class BeanInfo {
private Map<MethodInfo, List<InterceptorInfo>> interceptedMethods;

private Map<InterceptionType, List<InterceptorInfo>> lifecycleInterceptors;

private final Integer alternativePriority;

/**
Expand Down Expand Up @@ -201,11 +201,11 @@ List<InterceptorInfo> getBoundInterceptors() {
DisposerInfo getDisposer() {
return disposer;
}

boolean isAlternative() {
return alternativePriority != null;
}

Integer getAlternativePriority() {
return alternativePriority;
}
Expand Down Expand Up @@ -278,7 +278,7 @@ private void putLifecycleInterceptors(Map<InterceptionType, List<InterceptorInfo
}

private void addClassLevelBindings(ClassInfo classInfo, Collection<AnnotationInstance> bindings) {
classInfo.classAnnotations().stream()
beanDeployment.getAnnotations(classInfo).stream()
.filter(a -> beanDeployment.getInterceptorBinding(a.name()) != null && bindings.stream().noneMatch(e -> e.name().equals(a.name())))
.forEach(a -> bindings.add(a));
if (classInfo.superClassType() != null && !classInfo.superClassType().name().equals(DotNames.OBJECT)) {
Expand Down
Loading