Source Code Cross Referenced for InstantiatingNullHandler.java in  » J2EE » webwork-2.2.6 » com » opensymphony » xwork » util » 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 » J2EE » webwork 2.2.6 » com.opensymphony.xwork.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2006 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        package com.opensymphony.xwork.util;
006:
007:        import com.opensymphony.xwork.ObjectFactory;
008:        import ognl.NullHandler;
009:        import ognl.Ognl;
010:        import ognl.OgnlRuntime;
011:        import org.apache.commons.logging.Log;
012:        import org.apache.commons.logging.LogFactory;
013:
014:        import java.beans.PropertyDescriptor;
015:        import java.util.*;
016:
017:        /**
018:         * <!-- START SNIPPET: javadoc -->
019:         *
020:         * Provided that the key {@link #CREATE_NULL_OBJECTS} is in the action context with a value of true (this key is set
021:         * only during the execution of the {@link com.opensymphony.xwork.interceptor.ParametersInterceptor}), OGNL expressions
022:         * that have caused a NullPointerException will be temporarily stopped for evaluation while the system automatically
023:         * tries to solve the null references by automatically creating the object.
024:         *
025:         * <p/> The following rules are used when handling null references:
026:         *
027:         * <ul>
028:         *
029:         * <li>If the property is declared <i>exactly</i> as a {@link Collection} or {@link List}, then an ArrayList shall be
030:         * returned and assigned to the null references.</li>
031:         *
032:         * <li>If the property is declared as a {@link Map}, then a HashMap will be returned and assigned to the null
033:         * references.</li>
034:         *
035:         * <li>If the null property is a simple bean with a no-arg constructor, it will simply be created using the {@link
036:         * ObjectFactory#buildBean(java.lang.Class, java.util.Map)} method.</li>
037:         *
038:         * </ul>
039:         *
040:         * <!-- END SNIPPET: javadoc -->
041:         *
042:         * <!-- START SNIPPET: example -->
043:         *
044:         * For example, if a form element has a text field named <b>person.name</b> and the expression <i>person</i> evaluates
045:         * to null, then this class will be invoked. Because the <i>person</i> expression evaluates to a <i>Person</i> class, a
046:         * new Person is created and assigned to the null reference. Finally, the name is set on that object and the overall
047:         * effect is that the system automatically created a Person object for you, set it by calling setPerson() and then
048:         * finally called getPerson().setName() as you would typically expect.
049:         *
050:         * <!-- END SNIPPET: example>
051:         *
052:         * @author Matt Ho
053:         * @author Patrick Lightbody
054:         */
055:        public class InstantiatingNullHandler implements  NullHandler {
056:
057:            public static final String CREATE_NULL_OBJECTS = "xwork.NullHandler.createNullObjects";
058:            private static final Log LOG = LogFactory
059:                    .getLog(InstantiatingNullHandler.class);
060:
061:            public Object nullMethodResult(Map context, Object target,
062:                    String methodName, Object[] args) {
063:                if (LOG.isDebugEnabled()) {
064:                    LOG.debug("Entering nullMethodResult ");
065:                }
066:
067:                return null;
068:            }
069:
070:            public Object nullPropertyValue(Map context, Object target,
071:                    Object property) {
072:                if (LOG.isDebugEnabled()) {
073:                    LOG.debug("Entering nullPropertyValue [target=" + target
074:                            + ", property=" + property + "]");
075:                }
076:
077:                boolean c = OgnlContextState.isCreatingNullObjects(context);
078:
079:                if (!c) {
080:                    return null;
081:                }
082:
083:                if ((target == null) || (property == null)) {
084:                    return null;
085:                }
086:
087:                try {
088:                    String propName = property.toString();
089:                    Object realTarget = OgnlUtil.getRealTarget(propName,
090:                            context, target);
091:                    Class clazz = null;
092:
093:                    if (realTarget != null) {
094:                        PropertyDescriptor pd = OgnlRuntime
095:                                .getPropertyDescriptor(realTarget.getClass(),
096:                                        propName);
097:                        if (pd == null) {
098:                            return null;
099:                        }
100:
101:                        clazz = pd.getPropertyType();
102:                    }
103:
104:                    if (clazz == null) {
105:                        // can't do much here!
106:                        return null;
107:                    }
108:
109:                    Object param = createObject(clazz, realTarget, propName,
110:                            context);
111:
112:                    Ognl.setValue(propName, context, realTarget, param);
113:
114:                    return param;
115:                } catch (Exception e) {
116:                    LOG
117:                            .error(
118:                                    "Could not create and/or set value back on to object",
119:                                    e);
120:                }
121:
122:                return null;
123:            }
124:
125:            private Object createObject(Class clazz, Object target,
126:                    String property, Map context) throws Exception {
127:                if (Collection.class.isAssignableFrom(clazz)) {
128:                    return new ArrayList();
129:                } else if (clazz == Map.class) {
130:                    return new HashMap();
131:                }
132:
133:                return ObjectFactory.getObjectFactory().buildBean(clazz,
134:                        context);
135:            }
136:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.