001: package org.vraptor.interceptor;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Map;
007: import java.util.concurrent.ConcurrentHashMap;
008: import java.util.concurrent.ConcurrentMap;
009:
010: import org.apache.log4j.Logger;
011: import org.vraptor.Interceptor;
012: import org.vraptor.LogicRequest;
013: import org.vraptor.annotations.In;
014: import org.vraptor.annotations.InterceptedBy;
015: import org.vraptor.component.Clazz;
016: import org.vraptor.component.ComponentInstantiationException;
017: import org.vraptor.component.FieldAnnotation;
018: import org.vraptor.component.InvalidComponentException;
019: import org.vraptor.component.Outjectable;
020: import org.vraptor.component.Outjecter;
021: import org.vraptor.introspector.BeanProvider;
022: import org.vraptor.reflection.GettingException;
023: import org.vraptor.reflection.MethodInvocationException;
024: import org.vraptor.reflection.ReflectionUtil;
025: import org.vraptor.scope.ScopeType;
026:
027: /**
028: * Interceptor class to deal with interceptors instances
029: *
030: * @author Guilherme Silveira
031: * @author Paulo Silveira
032: */
033: public class InterceptorType implements Outjectable {
034:
035: private static final Logger LOG = Logger
036: .getLogger(InterceptorType.class);
037:
038: private static final ConcurrentMap<Class, InterceptorType> types = new ConcurrentHashMap<Class, InterceptorType>();
039:
040: private final Clazz type;
041:
042: private final List<Outjecter> outjections;
043:
044: /** In annotations */
045: private final List<FieldAnnotation<In>> inAnnotations;
046:
047: /**
048: * New interceptor class
049: *
050: * @param type
051: * class
052: */
053: private InterceptorType(Class<? extends Interceptor> type) {
054: this .type = new Clazz(type);
055: this .inAnnotations = ReflectionUtil.readAnnotations(this .type
056: .getType(), In.class);
057: this .outjections = ReflectionUtil.loadOutjecters(type);
058: }
059:
060: /**
061: * Returns a new instance of this interceptor, using the specified bean
062: * provider.
063: *
064: * @since 2.3.2
065: */
066: public Interceptor newInstance(LogicRequest logicRequest,
067: BeanProvider provider)
068: throws ComponentInstantiationException,
069: InvalidComponentException {
070: return (Interceptor) type.findSingleConstructor().newInstance(
071: logicRequest, provider);
072: }
073:
074: /**
075: * @return Returns the inAnnotations.
076: */
077: public List<FieldAnnotation<In>> getInAnnotations() {
078: return this .inAnnotations;
079: }
080:
081: public Class getInterceptorClass() {
082: return this .type.getType();
083: }
084:
085: /**
086: * Returns an InterceptorType which represents the given interceptor class.
087: *
088: * @param c
089: * @return
090: */
091: public static InterceptorType getType(Class<? extends Interceptor> c) {
092: if (!types.containsKey(c)) {
093: LOG.debug("creating interceptor type for " + c);
094: types.put(c, new InterceptorType(c));
095: }
096: return types.get(c);
097: }
098:
099: /**
100: * Returns an InterceptorType which represents the given interceptor
101: *
102: * @param interceptor
103: * @return
104: */
105: public static InterceptorType getType(Interceptor interceptor) {
106: return getType(interceptor.getClass());
107: }
108:
109: /**
110: * Returns a list of interceptors for the specified component class, parsing
111: * its InterceptedBy annotations
112: *
113: * @param clazz
114: * class
115: * @return interceptor type list
116: */
117: public static List<InterceptorType> getInterceptors(Class<?> clazz) {
118: List<InterceptorType> types = new ArrayList<InterceptorType>();
119: if (clazz.isAnnotationPresent(InterceptedBy.class)) {
120: @SuppressWarnings("unchecked")
121: InterceptedBy annotation = clazz
122: .getAnnotation(InterceptedBy.class);
123: for (Class<? extends Interceptor> c : annotation.value()) {
124: InterceptorType type = InterceptorType.getType(c);
125: types.add(type);
126: }
127: }
128: return types;
129: }
130:
131: public Map<String, Object> getOutjectedValues(Object comp,
132: ScopeType scope) throws GettingException,
133: MethodInvocationException {
134: Map<String, Object> map = new HashMap<String, Object>();
135: for (Outjecter out : outjections) {
136: if (out.getScope().equals(scope)) {
137: String key = out.getKey();
138: Object obj = out.getValue(comp);
139: map.put(key, obj);
140: }
141: }
142: return map;
143: }
144:
145: public boolean equals(Object o) {
146: if (this == o)
147: return true;
148: if (o instanceof InterceptorType) {
149: InterceptorType other = (InterceptorType) o;
150: return this .type.equals(other.type);
151: } else {
152: return false;
153: }
154: }
155:
156: public int hashCode() {
157: return this.type.hashCode();
158: }
159: }
|