
nablarch.fw.Interceptor Maven / Gradle / Ivy
The newest version!
package nablarch.fw;
import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import nablarch.core.repository.SystemRepository;
import nablarch.core.util.annotation.Published;
/**
* {@link Handler#handle(Object, ExecutionContext)}メソッドに対するインターセプタに付与する
* メタアノテーション。
*
* インターセプタを作成するには、このメタアノテーションを付与したアノテーションを作成し、
* {@link Interceptor}の属性には、インターセプト処理を実装するクラスを指定する。
* この実装クラスは、{@link Interceptor.Impl} を継承して作成する。
*
* 以下は、インターセプタ"@AroundAdvice"の実装例である。
*
*
* {@code @Target}(ElementType.METHOD)
* {@code @Retention}(RetentionPolicy.RUNTIME)
* {@code @Interceptor}(AroundAdvice.Impl.class)
* public {@code @interface} AroundAdvice {
* public static class Impl extends Interceptor.Impl {
* public HttpResponse handle(HttpRequest req, ExecutionContext ctx) {
* doBeforeAdvice(req, ctx);
* HttpResponse res = getOriginalHandler().handle(req, ctx);
* doAfterAdvice(req, ctx);
* return res;
* }
* void doBeforeAdvice(HttpRequest req, ExecutionContext ctx) {
* //......
* }
* void doAfterAdvice(HttpRequest req, ExecutionContext ctx) {
* //......
* }
* }
* }
*
*
* @author Iwauo Tajima
*/
@Documented
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Published(tag = "architect")
public @interface Interceptor {
/**
* このインターセプタが付与されたメソッドに対して実行される
* インターセプト処理を実装するクラス。
*
* @see Interceptor.Impl
*/
@SuppressWarnings("rawtypes") Class extends Interceptor.Impl> value();
/**
* {@link Interceptor}の処理内容を実装するクラスの抽象基底クラスとなるリクエストハンドラ。
*
* 各インターセプションが付与されたときに行われるインターセプト処理の内容は、
* このクラスを継承して作成する。
*
* 各{@link Interceptor}の値に指定されるクラスは、このクラスのサブクラスであり、
* インターセプトが行われると、そのサブクラスが実装する{@link Handler#handle(Object, ExecutionContext)}に処理が委譲される。
* この際、ラップされる前のリクエストハンドラを{@link #getOriginalHandler()}で取得できるので、
* 各インターセプタ固有の処理を以下の例のように実装することができる。
*
* public HttpResponse handle(HttpRequest req, ExecutionContext ctx) {
* try {
* doBeforeAdvice(); // インターセプタによる前処理
* return getOriginalHandler().handle(req, ctx); // 本処理
*
* } catch(RuntimeException e) {
* doErrorHandling(); // インターセプタによる例外ハンドリング
* throw e;
*
* } finally {
* doAfterAdvice(); // インターセプタによる終端処理
* }
* }
*
*
* @author Iwauo Tajima
*/
public abstract static class Impl
implements HandlerWrapper {
@Published(tag = "architect")
public Impl() {
}
/**
* 処理対象の{@link Interceptor}を設定する。
*
* @param annotation このクラスが実装する{@link Interceptor}
*/
@SuppressWarnings("unchecked")
public void setInterceptor(Annotation annotation) {
this.interceptor = (T) annotation;
}
/**
* 処理対象の{@link Interceptor}を取得する。
*
* @return 処理対象の{@link Interceptor}を取得する。
*/
@Published(tag = "architect")
public T getInterceptor() {
return interceptor;
}
/** 処理対象の{@link Interceptor}アノテーション */
private T interceptor;
/**
* インターセプト対象のリクエストハンドラを取得する。
*
* @return インターセプト対象のリクエストハンドラ
*/
@Published(tag = "architect")
public Handler getOriginalHandler() {
return originalHandler;
}
/**
* インターセプト対象のリクエストハンドラを設定する。
*
* @param originalHandler インターセプト対象のリクエストハンドラ
*/
public void setOriginalHandler(
Handler originalHandler) {
this.originalHandler = originalHandler;
}
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy