001: package org.vraptor.interceptor;
002:
003: import java.util.LinkedList;
004: import java.util.List;
005:
006: import org.apache.log4j.Logger;
007: import org.vraptor.Interceptor;
008: import org.vraptor.LogicRequest;
009: import org.vraptor.component.ComponentInstantiationException;
010: import org.vraptor.component.InvalidComponentException;
011: import org.vraptor.introspector.Introspector;
012: import org.vraptor.reflection.GettingException;
013: import org.vraptor.reflection.MethodInvocationException;
014: import org.vraptor.reflection.SettingException;
015:
016: /**
017: * Deals with the list of interceptors to be executed.
018: *
019: * @author Guilherme Silveira
020: * @author Paulo Silveira
021: * @since 2.3.2
022: */
023: public class InterceptorDealer {
024:
025: private static final Logger LOG = Logger
026: .getLogger(InterceptorDealer.class);
027:
028: private final Introspector introspector;
029:
030: private final LinkedList<Interceptor> before;
031:
032: private final LinkedList<Interceptor> after;
033:
034: private final LinkedList<InterceptorType> main;
035:
036: private final LogicRequest logicRequest;
037:
038: public InterceptorDealer(LogicRequest logicRequest,
039: Introspector introspector, List<Interceptor> before,
040: List<InterceptorType> main, List<Interceptor> after) {
041: this .logicRequest = logicRequest;
042: this .introspector = introspector;
043: this .before = new LinkedList<Interceptor>(before);
044: this .after = new LinkedList<Interceptor>(after);
045: this .main = new LinkedList<InterceptorType>(main);
046: }
047:
048: public void outject(Interceptor interceptor, LogicRequest context)
049: throws GettingException, MethodInvocationException {
050: InterceptorType type = InterceptorType.getType(interceptor);
051: introspector.outject(context, interceptor, type);
052: }
053:
054: public void inject(Interceptor interceptor, LogicRequest context)
055: throws ComponentInstantiationException, SettingException {
056: this .introspector.inject(InterceptorType.getType(interceptor)
057: .getInAnnotations(), interceptor, context);
058: }
059:
060: public Interceptor poll() throws InterceptorInstantiationException {
061: if (!before.isEmpty()) {
062: return before.poll();
063: } else {
064: if (!main.isEmpty()) {
065: try {
066: InterceptorType type = main.poll();
067: Interceptor interceptor = type.newInstance(
068: logicRequest, introspector
069: .getBeanProvider());
070: if (LOG.isDebugEnabled()) {
071: LOG.debug("Adding interceptor "
072: + interceptor.getClass().getName());
073: }
074: return interceptor;
075: } catch (ComponentInstantiationException e) {
076: throw new InterceptorInstantiationException(e
077: .getMessage(), e);
078: } catch (InvalidComponentException e) {
079: throw new InterceptorInstantiationException(e
080: .getMessage(), e);
081: }
082: } else {
083: if (!after.isEmpty()) {
084: return after.poll();
085: } else {
086: throw new IllegalStateException(
087: "logic flow execution being called more than"
088: + " than the number of interceptors. The last interceptor"
089: + "must not continue the flow!");
090: }
091: }
092: }
093: }
094:
095: /**
096: * Adds an interceptor to the top.
097: * @param i the interceptor to add
098: */
099: public void add(InterceptorType i) {
100: main.addFirst(i);
101: }
102:
103: }
|