Skip to content

Commit

Permalink
master: Add annotation support for all methods. Allow for pure annota…
Browse files Browse the repository at this point in the history
…tions implementation.
  • Loading branch information
Ozzy Espaillat committed Dec 14, 2024
1 parent 6b52a1b commit 1f8767f
Show file tree
Hide file tree
Showing 17 changed files with 318 additions and 82 deletions.
24 changes: 24 additions & 0 deletions jpos/src/main/java/org/jpos/annotation/Abort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jpos.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.jpos.transaction.TransactionConstants;
import org.jpos.transaction.TransactionParticipant;

/**
* Indicates that the annotated method is called in the preparation phase of transaction
* processing, specifying the expected outcome through the {@code result} attribute.
* This replaces the {@link TransactionParticipant} prepare method.
*
* @see {@linkplain https://marqeta.atlassian.net/wiki/spaces/~62f54a31d49df231b62a575d/blog/2023/12/01/3041525965/AutoWiring+Participants+with+jPos+-+Part+I}
*
* Usage: Used in conjunction with {@link AnnotatedParticipant} annotations.
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Abort {
}
24 changes: 24 additions & 0 deletions jpos/src/main/java/org/jpos/annotation/Commit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jpos.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.jpos.transaction.TransactionConstants;
import org.jpos.transaction.TransactionParticipant;

/**
* Indicates that the annotated method is called in the preparation phase of transaction
* processing, specifying the expected outcome through the {@code result} attribute.
* This replaces the {@link TransactionParticipant} prepare method.
*
* @see {@linkplain https://marqeta.atlassian.net/wiki/spaces/~62f54a31d49df231b62a575d/blog/2023/12/01/3041525965/AutoWiring+Participants+with+jPos+-+Part+I}
*
* Usage: Used in conjunction with {@link AnnotatedParticipant} annotations.
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Commit {
}
25 changes: 25 additions & 0 deletions jpos/src/main/java/org/jpos/annotation/PrepareForAbort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jpos.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.jpos.transaction.TransactionConstants;
import org.jpos.transaction.TransactionParticipant;

/**
* Indicates that the annotated method is called in the preparation phase of transaction
* processing, specifying the expected outcome through the {@code result} attribute.
* This replaces the {@link TransactionParticipant} prepare method.
*
* @see {@linkplain https://marqeta.atlassian.net/wiki/spaces/~62f54a31d49df231b62a575d/blog/2023/12/01/3041525965/AutoWiring+Participants+with+jPos+-+Part+I}
*
* Usage: Used in conjunction with {@link AnnotatedParticipant} annotations.
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PrepareForAbort {
public int result() default TransactionConstants.PREPARED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public boolean isMatch(Throwable e) {
}

@Override
public int doReturn(TransactionParticipant p, Context ctx, Throwable t) {
public int doReturn(Object p, Context ctx, Throwable t) {
ctx.log("prepare exception in " + this.getClass().getName());
ctx.log(t);
setResultCode(ctx, CMF.INTERNAL_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public interface ReturnExceptionHandler {
boolean isMatch(Throwable e);
int doReturn(TransactionParticipant p, Context ctx, Throwable obj);
int doReturn(Object p, Context ctx, Throwable obj);

default void configure(Method m) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public void configure(Parameter f) throws ConfigurationException {
}
}
@Override
public <T> T getValue(TransactionParticipant participant, Context ctx) {
public <T> T getValue(Object participant, Context ctx) {
return (T) new ContextView(ctx, readWrite, readOnly, writeOnly);
}

};
} else {
return new Resolver() {
@Override
public <T> T getValue(TransactionParticipant participant, Context ctx) {
public <T> T getValue(Object participant, Context ctx) {
return (T) ctx;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void configure(Parameter f) {
}

@Override
public <T> T getValue(TransactionParticipant participant, Context ctx) {
public <T> T getValue(Object participant, Context ctx) {
return ctx.get(ctxKey);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void configure(Parameter f) throws ConfigurationException {
}

@Override
public <T> T getValue(TransactionParticipant participant, Context ctx) {
public <T> T getValue(Object participant, Context ctx) {
return NameRegistrar.getIfExists(registryKey);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
public interface Resolver {
default void configure(Parameter f) throws ConfigurationException {
}
<T> T getValue(TransactionParticipant participant, Context ctx);
<T> T getValue(Object participant, Context ctx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ public boolean isMatch(Method m) {
public ReturnHandler resolve(Method m) {
Return r = m.getAnnotation(Return.class);
final String[] keys = r.value();
final int jPosRes = m.getAnnotation(Prepare.class).result();
return (participant, ctx, res) -> {
Map resMap = (Map) res;
for(String key: keys) {
if (resMap != null && resMap.containsKey(key)) {
ctx.put(key, resMap.get(key));
}
}
return jPosRes;
return getJPosResult(m);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.jpos.transaction.TransactionParticipant;

public interface ReturnHandler {
int doReturn(TransactionParticipant p, Context ctx, Object obj);
int doReturn(Object p, Context ctx, Object obj);

default void configure(Method m) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

import java.lang.reflect.Method;

import org.jpos.annotation.Prepare;
import org.jpos.annotation.PrepareForAbort;
import org.jpos.annotation.resolvers.Priority;

public interface ReturnHandlerProvider extends Priority {
boolean isMatch(Method m);

ReturnHandler resolve(Method m);

default int getJPosResult(Method m) {
int jPosRes = 0;
if (m.isAnnotationPresent(Prepare.class)) {
jPosRes = m.getAnnotation(Prepare.class).result();
} else if (m.isAnnotationPresent(PrepareForAbort.class)) {
jPosRes = m.getAnnotation(PrepareForAbort.class).result();
}
return jPosRes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ public boolean isMatch(Method m) {
public ReturnHandler resolve(Method m) {
Return r = m.getAnnotation(Return.class);
final String key = r.value()[0];
final int jPosRes = m.getAnnotation(Prepare.class).result();
return (participant, ctx, res) -> {
if (res != null) {
ctx.put(key, res);
}
return jPosRes;
return getJPosResult(m);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public boolean isMatch(Method m) {

@Override
public ReturnHandler resolve(Method m) {
final int jPosRes = m.getAnnotation(Prepare.class).result();
return (participant, ctx, res) -> jPosRes;
return (participant, ctx, res) -> getJPosResult(m);
}
}
Loading

0 comments on commit 1f8767f

Please sign in to comment.