Source Code Cross Referenced for BaseBeanPopulator.java in  » Portal » stringbeans-3.5 » com » nabhinc » portlet » mvcportlet » common » 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 » Portal » stringbeans 3.5 » com.nabhinc.portlet.mvcportlet.common 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * (C) Copyright 2005 Nabh Information Systems, Inc.
003:         *
004:         * All copyright notices regarding Nabh's products MUST remain
005:         * intact in the scripts and in the outputted HTML.
006:         * This program is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public License
008:         * as published by the Free Software Foundation; either version 2.1 
009:         * of the License, or (at your option) any later version.
010:         *
011:         * This program is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014:         * GNU Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public License
017:         * along with this program; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
019:         *
020:         */
021:        package com.nabhinc.portlet.mvcportlet.common;
022:
023:        import java.io.IOException;
024:        import java.lang.reflect.InvocationTargetException;
025:        import java.util.HashMap;
026:        import java.util.Iterator;
027:        import java.util.Map;
028:
029:        import javax.portlet.PortletException;
030:        import javax.portlet.PortletRequest;
031:        import javax.portlet.PortletResponse;
032:        import javax.portlet.PortletSession;
033:
034:        import org.apache.commons.beanutils.BeanUtils;
035:        import org.w3c.dom.Element;
036:
037:        import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
038:        import com.nabhinc.portlet.mvcportlet.core.Constants;
039:        import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
040:        import com.nabhinc.portlet.mvcportlet.util.ConfigUtil;
041:        import com.nabhinc.util.XMLUtil;
042:
043:        /**
044:         * Creates a Java bean if not already created, populates it based on the
045:         * request parameters, and sets it as an attribute value under specified scope.
046:         *  
047:         * @author Padmanabh Dabke
048:         * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
049:         * @since 1.0
050:         */
051:        public class BaseBeanPopulator extends BaseRequestProcessor {
052:
053:            private HashMap bpParamNameMap = new HashMap();
054:            private int bpScope = Constants.SCOPE_PORTLET_SESSION;
055:            private String bpBeanClass = null;
056:            private String bpBeanAttribute = null;
057:
058:            public void init(Element config,
059:                    ControllerPortletConfig controllerConfig)
060:                    throws PortletException {
061:                super .init(config, controllerConfig);
062:                bpBeanClass = XMLUtil.getSubElementText(config, "beanClass");
063:                if (bpBeanClass == null) {
064:                    throw new PortletException("beanClass must be specified.");
065:                }
066:                String scopeStr = XMLUtil.getSubElementText(config, "scope");
067:                if (scopeStr != null) {
068:                    bpScope = ConfigUtil.getScope(scopeStr);
069:                }
070:
071:                bpBeanAttribute = XMLUtil.getSubElementText(config,
072:                        "beanAttribute");
073:                if (bpBeanAttribute == null)
074:                    throw new PortletException("You must specify beanAttribute");
075:
076:                Element[] paramMapElems = XMLUtil.getSubElements(config,
077:                        "param-name-mapping");
078:                if (paramMapElems != null && paramMapElems.length != 0) {
079:                    for (int i = 0; i < paramMapElems.length; i++) {
080:                        String paramName = paramMapElems[i]
081:                                .getAttribute("paramName");
082:                        String propertyName = paramMapElems[i]
083:                                .getAttribute("propertyName");
084:                        if (paramName == null || paramName.length() == 0
085:                                || propertyName == null
086:                                || propertyName.length() == 0) {
087:                            throw new PortletException(
088:                                    "Parameter name mapping must specify paramName and propertyName.");
089:                        }
090:                        bpParamNameMap.put(paramName, propertyName);
091:                    }
092:                }
093:            }
094:
095:            /**
096:             * @param request
097:             * @param response
098:             * @return <code>success</code>
099:             * @throws PortletException
100:             * @throws IOException
101:             */
102:            public String process(PortletRequest request,
103:                    PortletResponse response) throws PortletException,
104:                    IOException {
105:
106:                try {
107:                    Object bean = getBean(request);
108:                    Map paramMap = request.getParameterMap();
109:                    // Check if parameter -> bean property mapping is specified. If
110:                    // it is specified, we have to replace the map keys corresponding
111:                    // the parameters with property keys
112:                    if (bpParamNameMap.size() == 0) {
113:                        BeanUtils.populate(bean, paramMap);
114:                    } else {
115:                        HashMap newMap = new HashMap();
116:                        Iterator iter = paramMap.entrySet().iterator();
117:                        while (iter.hasNext()) {
118:                            Map.Entry entry = (Map.Entry) iter.next();
119:                            String paramName = (String) entry.getKey();
120:                            String propName = (String) bpParamNameMap
121:                                    .get(paramName);
122:                            if (propName == null) {
123:                                newMap.put(paramName, entry.getValue());
124:                            } else {
125:                                newMap.put(propName, entry.getValue());
126:                            }
127:                        }
128:
129:                        BeanUtils.populate(bean, newMap);
130:                    }
131:                    setBeanAttribute(request, bean);
132:                    return "success";
133:                } catch (ClassNotFoundException ex) {
134:                    throw new PortletException("Bean class not found.", ex);
135:                } catch (IllegalAccessException ex) {
136:                    throw new PortletException(
137:                            "Access exception in bean instantiation.", ex);
138:                } catch (InstantiationException ex) {
139:                    throw new PortletException("Failed to instantiate bean.",
140:                            ex);
141:                } catch (InvocationTargetException ex) {
142:                    throw new PortletException("Failed to populate bean.");
143:                }
144:
145:            }
146:
147:            private Object getBean(PortletRequest request)
148:                    throws InstantiationException, IllegalAccessException,
149:                    ClassNotFoundException {
150:                Object bean = null;
151:                switch (bpScope) {
152:                case Constants.SCOPE_REQUEST:
153:                    bean = request.getAttribute(bpBeanAttribute);
154:                    break;
155:                case Constants.SCOPE_PORTLET_CONTEXT:
156:                    bean = request.getPortletSession().getPortletContext()
157:                            .getAttribute(bpBeanAttribute);
158:                    break;
159:                case Constants.SCOPE_APPLICATION_SESSION:
160:                    bean = request.getPortletSession().getAttribute(
161:                            bpBeanAttribute, PortletSession.APPLICATION_SCOPE);
162:                    break;
163:                case Constants.SCOPE_PORTLET_SESSION:
164:                    bean = request.getPortletSession().getAttribute(
165:                            bpBeanAttribute);
166:
167:                }
168:
169:                if (bean == null)
170:                    bean = Class.forName(bpBeanClass).newInstance();
171:                return bean;
172:
173:            }
174:
175:            private void setBeanAttribute(PortletRequest request, Object bean) {
176:                switch (bpScope) {
177:                case Constants.SCOPE_REQUEST:
178:                    request.setAttribute(bpBeanAttribute, bean);
179:                    break;
180:                case Constants.SCOPE_PORTLET_CONTEXT:
181:                    request.getPortletSession().getPortletContext()
182:                            .setAttribute(bpBeanAttribute, bean);
183:                    break;
184:                case Constants.SCOPE_APPLICATION_SESSION:
185:                    request.getPortletSession().setAttribute(bpBeanAttribute,
186:                            bean, PortletSession.APPLICATION_SCOPE);
187:                    break;
188:                case Constants.SCOPE_PORTLET_SESSION:
189:                    request.getPortletSession().setAttribute(bpBeanAttribute,
190:                            bean);
191:                }
192:            }
193:
194:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.