001: package org.vraptor.component;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008:
009: import org.vraptor.annotations.In;
010: import org.vraptor.interceptor.InterceptorType;
011: import org.vraptor.introspector.ReadParameter;
012: import org.vraptor.reflection.GettingException;
013: import org.vraptor.reflection.MethodInvocationException;
014: import org.vraptor.reflection.ReflectionUtil;
015: import org.vraptor.scope.ScopeType;
016:
017: /**
018: * Metadata information about a component class. This class is used to deal with
019: * component classes.
020: *
021: * @author Guilherme Silveira
022: */
023: public class DefaultComponentType implements ComponentType {
024:
025: private final Map<String, ? extends LogicMethod> actions;
026:
027: private final List<FieldAnnotation<In>> inAnnotations;
028:
029: private final List<Outjecter> outjections;
030:
031: private final List<ReadParameter> readParameters;
032:
033: private final List<InterceptorType> interceptors;
034:
035: private final Class<?> clazz;
036:
037: private final String name;
038:
039: private final BeanConstructor constructor;
040:
041: private final ScopeType scope;
042:
043: private final String destroyLogicName;
044:
045: public <T> DefaultComponentType(Class<T> type, String name,
046: ScopeType scope, BeanConstructor constructor,
047: Map<String, ? extends LogicMethod> actions,
048: List<FieldAnnotation<In>> ins,
049: List<InterceptorType> interceptors,
050: String destroyLogicName, List<ReadParameter> parameters) {
051: this .name = name;
052: this .actions = actions;
053: this .scope = scope;
054: this .inAnnotations = ins;
055: this .interceptors = interceptors;
056: this .clazz = type;
057: this .constructor = constructor;
058: this .readParameters = parameters;
059: this .destroyLogicName = destroyLogicName;
060: this .outjections = ReflectionUtil.loadOutjecters(type);
061: }
062:
063: public String getName() {
064: return this .name;
065: }
066:
067: public LogicMethod getLogic(String key)
068: throws LogicNotFoundException {
069: if (!this .actions.containsKey(key)) {
070: throw new LogicNotFoundException("Unable to find logic "
071: + key);
072: }
073: return this .actions.get(key);
074: }
075:
076: public List<InterceptorType> getInterceptors() {
077: return this .interceptors;
078: }
079:
080: public List<FieldAnnotation<In>> getInAnnotations() {
081: return this .inAnnotations;
082: }
083:
084: public List<ReadParameter> getReadParameters() {
085: return this .readParameters;
086: }
087:
088: public Collection<LogicMethod> getLogics() {
089: return new ArrayList<LogicMethod>(this .actions.values());
090: }
091:
092: public Class getComponentClass() {
093: return this .clazz;
094: }
095:
096: public ScopeType getScope() {
097: return this .scope;
098: }
099:
100: public String getKey() {
101: return "component."
102: + Character.toUpperCase(getComponentClass()
103: .getSimpleName().charAt(0))
104: + getComponentClass().getSimpleName().substring(1);
105: }
106:
107: public String getDestroyLogicName() {
108: return this .destroyLogicName;
109: }
110:
111: public Map<String, Object> getOutjectedValues(Object comp,
112: ScopeType scope) throws GettingException,
113: MethodInvocationException {
114: Map<String, Object> map = new HashMap<String, Object>();
115: for (Outjecter out : outjections) {
116: if (out.getScope().equals(scope)) {
117: String key = out.getKey();
118: Object obj = out.getValue(comp);
119: map.put(key, obj);
120: }
121: }
122: return map;
123: }
124:
125: public BeanConstructor getConstructor() {
126: return constructor;
127: }
128:
129: }
|