Skip to content

Commit

Permalink
Merge pull request #3872 from jbee/PAYARA-3468-generics
Browse files Browse the repository at this point in the history
PAYARA-3468 MP FT Generic types
  • Loading branch information
Pandrex247 authored Mar 29, 2019
2 parents 0860f07 + 40cbc2e commit fc4bfcf
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void postConstruct() {
}

@Override
public void event(Event event) {
public void event(Event<?> event) {
if (event.is(Deployment.APPLICATION_UNLOADED)) {
ApplicationInfo info = (ApplicationInfo) event.hook();
deregisterApplication(info.getName());
Expand All @@ -135,10 +135,9 @@ public Boolean isFaultToleranceEnabled(String applicationName, Config config) {
try {
if (faultToleranceObjects.containsKey(applicationName)) {
return faultToleranceObjects.get(applicationName).isEnabled();
} else {
initialiseFaultToleranceObject(applicationName, config);
return faultToleranceObjects.get(applicationName).isEnabled();
}
initialiseFaultToleranceObject(applicationName, config);
return faultToleranceObjects.get(applicationName).isEnabled();
} catch (NullPointerException npe) {
initialiseFaultToleranceObject(applicationName, config);
return faultToleranceObjects.get(applicationName).isEnabled();
Expand All @@ -156,10 +155,9 @@ public Boolean isFaultToleranceEnabled(String applicationName, Config config) {
public Boolean areFaultToleranceMetricsEnabled(String applicationName, Config config) {
if (faultToleranceObjects.containsKey(applicationName)) {
return faultToleranceObjects.get(applicationName).areMetricsEnabled();
} else {
initialiseFaultToleranceObject(applicationName, config);
return faultToleranceObjects.get(applicationName).areMetricsEnabled();
}
initialiseFaultToleranceObject(applicationName, config);
return faultToleranceObjects.get(applicationName).areMetricsEnabled();
}

/**
Expand Down Expand Up @@ -526,7 +524,7 @@ public String getApplicationName(InvocationManager invocationManager, Invocation
* @param annotatedMethod The annotated Method to generate the signature for
* @return A String in the format of CanonicalClassName#MethodName({ParameterTypes})>ReturnType
*/
private String getFullMethodSignature(Method annotatedMethod) {
private static String getFullMethodSignature(Method annotatedMethod) {
return annotatedMethod.getDeclaringClass().getCanonicalName()
+ "#" + annotatedMethod.getName()
+ "(" + Arrays.toString(annotatedMethod.getParameterTypes()) + ")"
Expand All @@ -547,7 +545,7 @@ public void endFaultToleranceSpan() {
}
}

private void addGenericFaultToleranceRequestTracingDetails(RequestTraceSpan span,
private static void addGenericFaultToleranceRequestTracingDetails(RequestTraceSpan span,
InvocationManager invocationManager, InvocationContext invocationContext) {
span.addSpanTag("App Name", invocationManager.getCurrentInvocation().getAppName());
span.addSpanTag("Component ID", invocationManager.getCurrentInvocation().getComponentId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@

import org.glassfish.api.admin.config.ConfigExtension;
import org.jvnet.hk2.config.Attribute;
import org.jvnet.hk2.config.ConfigBeanProxy;
import org.jvnet.hk2.config.Configured;

/**
* Configuration for the Fault Tolerance Service.
* @author Andrew Pielage
*/
@Configured(name = "microprofile-fault-tolerance-configuration")
public interface FaultToleranceServiceConfiguration extends ConfigBeanProxy, ConfigExtension {
public interface FaultToleranceServiceConfiguration extends ConfigExtension {

@Attribute(defaultValue = "concurrent/__defaultManagedExecutorService", dataType = String.class)
public String getManagedExecutorService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ <T> void processAnnotatedType(@Observes @WithAnnotations({ Asynchronous.class, B
* @param annotatedMethod The annotated method to validate
* @throws Exception
*/
private <T> void validateMethodAnnotations(AnnotatedMethod<T> annotatedMethod)
private static <T> void validateMethodAnnotations(AnnotatedMethod<T> annotatedMethod)
throws ClassNotFoundException, NoSuchMethodException {
Config config = ConfigProvider.getConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public static <A extends Annotation> A getAnnotation(BeanManager beanManager, Cl
* @param parameterType The type of the parameter to get the override value of
* @return
*/
public static <A extends Annotation> Optional getOverrideValue(Config config, Class<A> annotationClass,
String parameterName, String annotatedMethodName, String annotatedClassCanonicalName, Class parameterType) {
Optional value = Optional.empty();
public static <A extends Annotation, T> Optional<T> getOverrideValue(Config config, Class<A> annotationClass,
String parameterName, String annotatedMethodName, String annotatedClassCanonicalName, Class<T> parameterType) {
Optional<T> value = Optional.empty();

String annotationName = annotationClass.getSimpleName();

Expand Down Expand Up @@ -164,9 +164,9 @@ public static <A extends Annotation> Optional getOverrideValue(Config config, Cl
* @param parameterType The type of the parameter to get the override value of
* @return
*/
public static <A extends Annotation> Optional getOverrideValue(Config config, Class<A> annotationClass,
String parameterName, InvocationContext invocationContext, Class parameterType) {
Optional value = Optional.empty();
public static <A extends Annotation, T> Optional<T> getOverrideValue(Config config, Class<A> annotationClass,
String parameterName, InvocationContext invocationContext, Class<T> parameterType) {
Optional<T> value = Optional.empty();

// Get the annotation, method, and class names
String annotationName = annotationClass.getSimpleName();
Expand Down Expand Up @@ -220,9 +220,9 @@ public static <A extends Annotation> Optional getOverrideValue(Config config, Cl
* @param invocationContext The context of the invoking request
* @return
*/
public static <A extends Annotation> Optional getEnabledOverrideValue(Config config, Class<A> annotationClass,
public static <A extends Annotation> Optional<Boolean> getEnabledOverrideValue(Config config, Class<A> annotationClass,
InvocationContext invocationContext) {
Optional value = Optional.empty();
Optional<Boolean> value = Optional.empty();

// Get the annotation, method, and class names
String annotationName = annotationClass.getSimpleName();
Expand Down Expand Up @@ -270,7 +270,7 @@ public static <A extends Annotation> Optional getEnabledOverrideValue(Config con
* @param value The Optional value to check if an override was found for
* @param level A String denoting the level of override you were checking for
*/
private static void logOverride(Optional value, String level) {
private static void logOverride(Optional<?> value, String level) {
if (value.isPresent()) {
logger.log(Level.FINER, "{0} override found.", level);
} else {
Expand All @@ -286,9 +286,9 @@ private static void logOverride(Optional value, String level) {
* @param config The config to get the overriding enabled value from
* @return An overriding enabled value if there is one
*/
private static Optional checkAnnotatedMethodForEnabledOverride(String annotatedClassCanonicalName,
private static Optional<Boolean> checkAnnotatedMethodForEnabledOverride(String annotatedClassCanonicalName,
String annotationName, Config config) {
Optional value = checkForClassLevelOverride(annotatedClassCanonicalName, annotationName, "enabled",
Optional<Boolean> value = checkForClassLevelOverride(annotatedClassCanonicalName, annotationName, "enabled",
Boolean.class, config);

if (!value.isPresent()) {
Expand All @@ -307,11 +307,11 @@ private static Optional checkAnnotatedMethodForEnabledOverride(String annotatedC
* @param config The config to get the overriding value from
* @return An overriding value for the provided parameter if there is one
*/
private static Optional checkForClassLevelOverride(String annotatedClassCanonicalName, String annotationName,
String parameterName, Class parameterType, Config config) {
private static <T> Optional<T> checkForClassLevelOverride(String annotatedClassCanonicalName, String annotationName,
String parameterName, Class<T> parameterType, Config config) {
logger.log(Level.FINER, "No config override for annotated method, getting config override for the "
+ "annotated class...");
Optional value = config.getOptionalValue(annotatedClassCanonicalName + "/" + annotationName
Optional<T> value = config.getOptionalValue(annotatedClassCanonicalName + "/" + annotationName
+ "/" + parameterName, parameterType);
logOverride(value, "Class-level");

Expand All @@ -326,10 +326,10 @@ private static Optional checkForClassLevelOverride(String annotatedClassCanonica
* @param config The config to get the overriding value from
* @return
*/
private static Optional checkForGlobalLevelOverride(String annotationName, String parameterName, Class parameterType,
private static <T> Optional<T> checkForGlobalLevelOverride(String annotationName, String parameterName, Class<T> parameterType,
Config config) {
logger.log(Level.FINER, "No config override for annotated class, checking for global override.");
Optional value = config.getOptionalValue(annotationName + "/" + parameterName, parameterType);
Optional<T> value = config.getOptionalValue(annotationName + "/" + parameterName, parameterType);
logOverride(value, "Global");

return value;
Expand All @@ -343,15 +343,14 @@ private static Optional checkForGlobalLevelOverride(String annotationName, Strin
* @param annotationClass The class of the annotation
* @return The class of the annotated method
*/
public static <A extends Annotation> Class getAnnotatedMethodClass(InvocationContext invocationContext,
public static <A extends Annotation> Class<?> getAnnotatedMethodClass(InvocationContext invocationContext,
Class<A> annotationClass) {
Class targetClass = invocationContext.getTarget().getClass();
Class<?> targetClass = invocationContext.getTarget().getClass();

if (targetClass.isAnnotationPresent(annotationClass)) {
return targetClass;
} else {
return invocationContext.getMethod().getDeclaringClass();
}
return invocationContext.getMethod().getDeclaringClass();
}

/**
Expand All @@ -361,16 +360,15 @@ public static <A extends Annotation> Class getAnnotatedMethodClass(InvocationCon
* @param annotatedClass The class of the annotation
* @return The canonical name of the annotated method's class
*/
public static <A extends Annotation> String getAnnotatedMethodClassCanonicalName(Class annotatedClass) {
public static <A extends Annotation> String getAnnotatedMethodClassCanonicalName(Class<?> annotatedClass) {
String canonicalClassName = annotatedClass.getCanonicalName();

// If the class was a proxy from Weld, cut away the appended gubbins
// Author Note: There's probably a better way of doing this...
if (canonicalClassName.contains("$Proxy$_$$_WeldSubclass")) {
return canonicalClassName.split("\\$Proxy\\$_\\$\\$_WeldSubclass")[0];
} else {
return canonicalClassName;
}
return canonicalClassName;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,10 @@ public Object intercept(InvocationContext invocationContext) throws Exception {
// Attempt to proceed the InvocationContext with Asynchronous semantics if Fault Tolerance is enabled for
// this method
if (faultToleranceService.isFaultToleranceEnabled(appName, config)
&& ((Boolean) FaultToleranceCdiUtils.getEnabledOverrideValue(
&& (FaultToleranceCdiUtils.getEnabledOverrideValue(
config, Asynchronous.class, invocationContext)
.orElse(Boolean.TRUE))) {
Callable callable = () -> {
return invocationContext.proceed();
};
Callable<?> callable = () -> invocationContext.proceed();
logger.log(Level.FINER, "Proceeding invocation asynchronously");
proceededInvocationContext = new FutureDelegator(managedExecutorService.submit(callable));
} else {
Expand All @@ -126,7 +124,7 @@ public Object intercept(InvocationContext invocationContext) throws Exception {

// If the method was annotated with Fallback and the annotation is enabled, attempt it, otherwise just
// propagate the exception upwards
if (fallback != null && ((Boolean) FaultToleranceCdiUtils.getEnabledOverrideValue(
if (fallback != null && (FaultToleranceCdiUtils.getEnabledOverrideValue(
config, Fallback.class, invocationContext)
.orElse(Boolean.TRUE))) {
logger.log(Level.FINE, "Fallback annotation found on method - falling back from Asynchronous");
Expand Down Expand Up @@ -175,15 +173,14 @@ public Object get() throws InterruptedException, ExecutionException {

// If the result of future.get() is still a future, get it again
if (proceededInvocation instanceof Future) {
Future tempFuture = (Future) proceededInvocation;
Future<?> tempFuture = (Future<?>) proceededInvocation;
proceededInvocation = tempFuture.get();
}
} catch (InterruptedException | ExecutionException ex) {
if (ex.getCause() instanceof FaultToleranceException) {
throw (FaultToleranceException) ex.getCause();
} else {
throw ex;
}
throw ex;
}

return proceededInvocation;
Expand All @@ -198,15 +195,14 @@ public Object get(long timeout, TimeUnit unit) throws InterruptedException, Exec

// If the result of future.get() is still a future, get it again
if (proceededInvocation instanceof Future) {
Future tempFuture = (Future) proceededInvocation;
Future<?> tempFuture = (Future<?>) proceededInvocation;
proceededInvocation = tempFuture.get(timeout, unit);
}
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
if (ex.getCause() instanceof FaultToleranceException) {
throw new ExecutionException(ex.getCause());
} else {
throw ex;
}
throw ex;
}

return proceededInvocation;
Expand Down
Loading

0 comments on commit fc4bfcf

Please sign in to comment.