Source Code Cross Referenced for BasicIntrospector.java in  » Web-Framework » vraptor » org » vraptor » introspector » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » vraptor » org.vraptor.introspector 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.vraptor.introspector;
002:
003:        import java.lang.reflect.Array;
004:        import java.util.ArrayList;
005:        import java.util.HashMap;
006:        import java.util.List;
007:        import java.util.Map;
008:        import java.util.Set;
009:        import java.util.TreeSet;
010:        import java.util.Map.Entry;
011:
012:        import org.apache.log4j.Logger;
013:        import org.vraptor.LogicRequest;
014:        import org.vraptor.annotations.In;
015:        import org.vraptor.component.Clazz;
016:        import org.vraptor.component.ComponentInstantiationException;
017:        import org.vraptor.component.FieldAnnotation;
018:        import org.vraptor.component.Outjectable;
019:        import org.vraptor.converter.ConversionException;
020:        import org.vraptor.converter.ConverterManager;
021:        import org.vraptor.i18n.ValidationMessage;
022:        import org.vraptor.reflection.GettingException;
023:        import org.vraptor.reflection.JPathExecutor;
024:        import org.vraptor.reflection.MethodInvocationException;
025:        import org.vraptor.reflection.ReflectionUtil;
026:        import org.vraptor.reflection.SettingException;
027:        import org.vraptor.scope.RequestContext;
028:        import org.vraptor.scope.ScopeType;
029:
030:        /**
031:         * The default introspector implementation.
032:         * 
033:         * @author Guilherme Silveira
034:         */
035:        public class BasicIntrospector implements  Introspector {
036:
037:            private static final Logger LOG = Logger
038:                    .getLogger(BasicIntrospector.class);
039:
040:            private final KeyExtractor keyExtractor = new KeyExtractor();
041:
042:            private BeanProvider beanProvider;
043:
044:            private final Map<Class, Clazz> clazzDirectory;
045:
046:            public BasicIntrospector() {
047:                beanProvider = new WebBeanProvider();
048:                clazzDirectory = new HashMap<Class, Clazz>();
049:            }
050:
051:            private Set<Parameter> addAll(Set<String> keys) {
052:                Set<Parameter> ts = new TreeSet<Parameter>();
053:                for (String key : keys) {
054:                    ts.add(new Parameter(key));
055:                }
056:                return ts;
057:            }
058:
059:            @SuppressWarnings("unchecked")
060:            public List<ValidationMessage> readParameters(
061:                    List<ReadParameter> parametersToRead, Object component,
062:                    LogicRequest logicRequest,
063:                    ConverterManager converterManager,
064:                    Object[] methodParamObjects) throws SettingException {
065:
066:                JPathExecutor executor = new JPathExecutor(converterManager,
067:                        logicRequest, methodParamObjects, component);
068:                RequestContext request = logicRequest.getRequestContext();
069:
070:                Map<String, Object> parameters = request.getParameterMap();
071:                List<ValidationMessage> problems = new ArrayList<ValidationMessage>();
072:
073:                out: for (Parameter p : addAll(parameters.keySet())) {
074:                    String parameter = p.getKey();
075:                    for (ReadParameter read : parametersToRead) {
076:                        if (!p.matches(read.getKey())) {
077:                            continue;
078:                        }
079:                        if (LOG.isDebugEnabled()) {
080:                            LOG
081:                                    .debug("Parameter " + parameter
082:                                            + " will be used on field "
083:                                            + read.getKey());
084:                        }
085:                        try {
086:                            // TODO: dont do this, cache it in request instead (or
087:                            // use the internal request itself)
088:                            executor.set(p.getPath(), singleValue(parameters
089:                                    .get(parameter)), arrayValue(parameters
090:                                    .get(parameter)), read);
091:                        } catch (ConversionException e) {
092:                            // validation problem...
093:                            if (LOG.isDebugEnabled()) {
094:                                LOG.debug(e.getMessage(), e);
095:                            }
096:                            ValidationMessage msg = e.getI18NMessage();
097:                            msg.setPath(parameter);
098:                            problems.add(msg);
099:                        }
100:                        continue out;
101:                    }
102:                    if (LOG.isDebugEnabled()) {
103:                        LOG.debug("Parameter not used: " + parameter);
104:                    }
105:                }
106:
107:                return problems;
108:
109:            }
110:
111:            private String[] arrayValue(Object val) {
112:                if (val.getClass().isArray()) {
113:                    return (String[]) val;
114:                }
115:                return new String[] { (String) val };
116:            }
117:
118:            private String singleValue(Object val) {
119:                if (val.getClass().isArray()) {
120:                    return (String) Array.get(val, 0);
121:                }
122:                return (String) val;
123:            }
124:
125:            public void inject(List<FieldAnnotation<In>> inAnnotations,
126:                    Object component, LogicRequest request)
127:                    throws ComponentInstantiationException, SettingException {
128:
129:                for (FieldAnnotation<In> info : inAnnotations) {
130:                    In in = info.getAnnotation();
131:                    String key = keyExtractor.extractInKey(info);
132:                    Object value = in.scope().getContext(request).getAttribute(
133:                            key);
134:                    setFieldForInjection(component, info, in, key, value);
135:                }
136:
137:            }
138:
139:            private void setFieldForInjection(Object component,
140:                    FieldAnnotation<In> info, In in, String key, Object value)
141:                    throws ComponentInstantiationException, SettingException {
142:
143:                if (LOG.isDebugEnabled()) {
144:                    LOG.debug("Injecting with key " + key + " from "
145:                            + in.scope());
146:                }
147:
148:                if (value == null) {
149:                    if (in.create()) {
150:                        if (LOG.isDebugEnabled()) {
151:                            LOG.debug("Trying to create "
152:                                    + info.getField().getType().getName());
153:                        }
154:                        value = getClazz(info.getField().getType())
155:                                .newInstance();
156:                    } else if (in.required()) {
157:                        throw new SettingException(
158:                                "Unable to fill inject value for field "
159:                                        + info.getField().getName());
160:                    } else {
161:                        return;
162:                    }
163:                }
164:
165:                ReflectionUtil.setField(component, info.getField(), value);
166:            }
167:
168:            /**
169:             * Returns the clazz instance for the specified type.
170:             * @param type	the type
171:             * @return	the clazz instance
172:             * @since 2.3.2
173:             */
174:            private Clazz getClazz(Class<?> type) {
175:                // TODO is sync really worth it?
176:                if (!clazzDirectory.containsKey(type)) {
177:                    clazzDirectory.put(type, new Clazz(type));
178:                }
179:                return clazzDirectory.get(type);
180:            }
181:
182:            public void outject(LogicRequest logicRequest, Object component,
183:                    Outjectable type) throws GettingException,
184:                    MethodInvocationException {
185:                for (ScopeType scope : ScopeType.values()) {
186:                    Map<String, Object> values = type.getOutjectedValues(
187:                            component, scope);
188:                    for (Entry<String, Object> value : values.entrySet()) {
189:                        if (LOG.isDebugEnabled()) {
190:                            LOG.debug("Outjecting key " + value.getKey()
191:                                    + " at " + scope);
192:                        }
193:                        scope.getContext(logicRequest).setAttribute(
194:                                value.getKey(), value.getValue());
195:                    }
196:                }
197:            }
198:
199:            public BeanProvider getBeanProvider() {
200:                return beanProvider;
201:            }
202:
203:            public void setBeanProvider(BeanProvider beanProvider) {
204:                this.beanProvider = beanProvider;
205:            }
206:
207:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.