001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.cfg;
030:
031: import java.util.*;
032:
033: import javax.el.*;
034:
035: import javax.faces.*;
036: import javax.faces.application.*;
037: import javax.faces.component.*;
038: import javax.faces.component.html.*;
039: import javax.faces.context.*;
040: import javax.faces.convert.*;
041: import javax.faces.el.*;
042: import javax.faces.event.*;
043: import javax.faces.validator.*;
044:
045: import javax.xml.bind.annotation.*;
046:
047: import com.caucho.config.*;
048: import com.caucho.jsf.el.*;
049: import com.caucho.util.*;
050:
051: public class ManagedBeanConfig {
052: private static final L10N L = new L10N(ManagedBeanConfig.class);
053:
054: private String _configLocation;
055:
056: private String _id;
057:
058: private String _name;
059:
060: private String _typeName;
061: private Class _type;
062:
063: private ArrayList<BeanProgram> _program = new ArrayList<BeanProgram>();
064:
065: private Scope _scope = Scope.REQUEST;
066:
067: public void setId(String id) {
068: }
069:
070: public void setDescription(String description) {
071: }
072:
073: public void setManagedBeanName(String name) {
074: _name = name;
075: }
076:
077: public String getName() {
078: return _name;
079: }
080:
081: public void setConfigLocation(String location) {
082: _configLocation = location;
083: }
084:
085: @XmlElement(name="managed-bean-class")
086: public void setManagedBeanClass(String cl) {
087: _typeName = cl;
088: }
089:
090: public String getManagedBeanClass() {
091: return _typeName;
092: }
093:
094: public Class getType() {
095: if (_type == null) {
096: try {
097: ClassLoader loader = Thread.currentThread()
098: .getContextClassLoader();
099:
100: _type = Class.forName(_typeName, false, loader);
101: } catch (Exception e) {
102: throw ConfigException.create(e);
103: }
104: }
105:
106: return _type;
107: }
108:
109: @XmlElement(name="managed-bean-scope")
110: public void setManagedBeanScope(String scope) {
111: if ("request".equals(scope))
112: _scope = Scope.REQUEST;
113: else if ("session".equals(scope))
114: _scope = Scope.SESSION;
115: else if ("application".equals(scope))
116: _scope = Scope.APPLICATION;
117: else if ("none".equals(scope))
118: _scope = Scope.NONE;
119: else
120: throw new ConfigException(
121: L
122: .l(
123: "'{0}' is an unknown managed-bean-scope. Expected values are request, session, application, or none.",
124: scope));
125: }
126:
127: public String getManagedBeanScope() {
128: return _scope.toString();
129: }
130:
131: @XmlElement(name="managed-property")
132: public void setManagedProperty(ManagedProperty property) {
133: property.addProgram(_program, getType());
134: }
135:
136: public ManagedProperty getManagedProperty() {
137: throw new UnsupportedOperationException();
138: }
139:
140: public void setMapEntries(MappedEntries map) {
141: ArrayList<AbstractValue> keyList = map.getKeyList();
142: ArrayList<AbstractValue> valueList = map.getValueList();
143:
144: for (int i = 0; i < keyList.size(); i++) {
145: _program.add(new MapBeanProgram(keyList.get(i), valueList
146: .get(i)));
147: }
148: }
149:
150: public void setListEntries(ListEntries list) {
151: for (AbstractValue value : list.getListValues()) {
152: _program.add(new ListBeanProgram(value));
153: }
154: }
155:
156: public Object create(FacesContext context,
157: ManagedBeanELResolver.Scope createScope)
158: throws FacesException {
159: try {
160: ELContext elContext = context.getELContext();
161:
162: boolean isPropertyResolved = elContext.isPropertyResolved();
163:
164: Object value = getType().newInstance();
165:
166: if (createScope.getScope() < _scope.ordinal())
167: throw new FacesException(L.l(
168: "Scope '{0}' is long for enclosing bean.",
169: _scope));
170: else if (_scope.ordinal() < createScope.getScope())
171: createScope.setScope(_scope.ordinal());
172:
173: for (int i = 0; i < _program.size(); i++) {
174: _program.get(i).configure(context, value);
175: }
176:
177: ExternalContext extContext = context.getExternalContext();
178:
179: switch (_scope) {
180: case APPLICATION:
181: extContext.getApplicationMap().put(_name, value);
182: break;
183:
184: case SESSION:
185: extContext.getSessionMap().put(_name, value);
186: break;
187:
188: case REQUEST:
189: extContext.getRequestMap().put(_name, value);
190: break;
191: }
192:
193: elContext.setPropertyResolved(isPropertyResolved);
194:
195: return value;
196: } catch (RuntimeException e) {
197: throw e;
198: } catch (Exception e) {
199: throw new FacesException(e);
200: }
201: }
202:
203: enum Scope {
204: NONE, APPLICATION, SESSION, REQUEST,
205: };
206: }
|