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: }
|