01: /*
02: * (C) Copyright 2006 Nabh Information Systems, Inc.
03: *
04: * All copyright notices regarding Nabh's products MUST remain
05: * intact in the scripts and in the outputted HTML.
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public License
08: * as published by the Free Software Foundation; either version 2.1
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: *
20: */
21: package com.nabhinc.portlet.mvcportlet.common;
22:
23: import javax.portlet.PortletException;
24: import javax.portlet.PortletRequest;
25: import javax.portlet.PortletSession;
26:
27: import org.w3c.dom.Element;
28:
29: import com.nabhinc.portlet.mvcportlet.core.Constants;
30: import com.nabhinc.portlet.mvcportlet.util.ConfigUtil;
31:
32: /**
33: * A ObjectRegistry class that returns bean from specified attribute name and scope.
34: * This class has two optional attributes, <code>attributeNames</code> and
35: * <code>scope></code>. The default <code>attributeName</code> is <code>mvcportlet.bean</code> and
36: * <code>scope</code> is <code>request</code>. The <code>scope</code>'s value must be one of the
37: * following: <code>portlet_session</code>, <code>portlet_context</code> and <code>application_session</code>.
38: * This class serves as default bean-creator class for ParameterSetter action processor.
39: *
40: * @author Padmanabh Dabke
41: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
42: * @since 1.1
43: */
44: public class AttributeBeanCreator implements BeanCreator {
45: private String abcAttributeName = "mvcportlet.bean";
46: private int abcScope = Constants.SCOPE_REQUEST;
47:
48: public void setAttributeName(String attribName) {
49: abcAttributeName = attribName;
50: }
51:
52: public Object getBean(PortletRequest request)
53: throws PortletException {
54:
55: switch (abcScope) {
56: case Constants.SCOPE_REQUEST:
57: return request.getAttribute(abcAttributeName);
58: case Constants.SCOPE_PORTLET_SESSION:
59: return request.getPortletSession().getAttribute(
60: abcAttributeName, PortletSession.PORTLET_SCOPE);
61: case Constants.SCOPE_APPLICATION_SESSION:
62: return request.getPortletSession().getAttribute(
63: abcAttributeName, PortletSession.APPLICATION_SCOPE);
64: case Constants.SCOPE_PORTLET_CONTEXT:
65: return request.getPortletSession().getPortletContext()
66: .getAttribute(abcAttributeName);
67:
68: }
69: throw new IllegalArgumentException("Invalid attribute scope: "
70: + abcScope);
71: }
72:
73: public void init(Element config) throws Exception {
74: String attribName = config.getAttribute("attributeName");
75: if (attribName != null && attribName.trim().length() > 0)
76: abcAttributeName = attribName.trim();
77: String scopeStr = config.getAttribute("scope");
78: if (scopeStr != null && (!scopeStr.equals(""))) {
79: abcScope = ConfigUtil.getScope(scopeStr);
80: }
81:
82: }
83:
84: }
|