001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.faces;
017:
018: import javax.faces.application.Application;
019: import javax.faces.context.FacesContext;
020: import javax.faces.el.ValueBinding;
021: import javax.faces.el.VariableResolver;
022:
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.commons.logging.Log;
025: import org.directwebremoting.create.AbstractCreator;
026: import org.directwebremoting.extend.Creator;
027: import org.directwebremoting.util.LocalUtil;
028:
029: /**
030: * This is a DWR creator implementation, to allow dwr beans to be allocated into
031: * JSF scopes and into jeffs3 specific scope (i.e. the flow scope)
032: * @author Pierpaolo Follia
033: * @author Joe Walker [joe at getahead dot ltd dot uk]
034: * @author Vinicius Melo [vdmelo at gmail dot com]
035: */
036: public class JsfCreator extends AbstractCreator implements Creator {
037: /* (non-Javadoc)
038: * @see org.directwebremoting.Creator#getType()
039: */
040: public Class<?> getType() {
041: if (instanceType == null) {
042: try {
043: instanceType = getInstance().getClass();
044: } catch (InstantiationException ex) {
045: log.error(
046: "Failed to instansiate object to detect type.",
047: ex);
048: return Object.class;
049: }
050: }
051:
052: return instanceType;
053: }
054:
055: /* (non-Javadoc)
056: * @see org.directwebremoting.Creator#getInstance()
057: */
058: public Object getInstance() throws InstantiationException {
059: FacesContext facesContext = FacesContext.getCurrentInstance();
060: if (facesContext == null) {
061: log
062: .error("Object "
063: + getManagedBeanName()
064: + " cannot be created since the faces context is null");
065: return null;
066: }
067:
068: Application application = facesContext.getApplication();
069: Object resolvedObject = null;
070:
071: if (isVBExpression(getManagedBeanName())) {
072: ValueBinding vb = application
073: .createValueBinding(getManagedBeanName());
074: if (vb != null) {
075: resolvedObject = vb.getValue(facesContext);
076: }
077: } else {
078: VariableResolver resolver = application
079: .getVariableResolver();
080: resolvedObject = resolver.resolveVariable(facesContext,
081: getManagedBeanName());
082: }
083:
084: return resolvedObject;
085: }
086:
087: /**
088: * Determine whether String is a value binding expression or not.
089: * @param expression The expression to test for value bindingness
090: * @return true if the expression contains a VB expression
091: */
092: public static boolean isVBExpression(String expression) {
093: if (expression == null) {
094: return false;
095: }
096:
097: int start = expression.indexOf("#{");
098: int end = expression.indexOf('}');
099:
100: return start != -1 && start < end;
101: }
102:
103: /**
104: * @return Returns the managedBeanName.
105: */
106: public String getManagedBeanName() {
107: return managedBeanName;
108: }
109:
110: /**
111: * @param managedBeanName The managedBeanName to set.
112: */
113: public void setManagedBeanName(String managedBeanName) {
114: this .managedBeanName = managedBeanName;
115: }
116:
117: /**
118: * What sort of class do we create?
119: * @param classname The name of the class
120: */
121: public void setClass(String classname) {
122: try {
123: this .instanceType = LocalUtil.classForName(classname);
124: } catch (ClassNotFoundException ex) {
125: throw new IllegalArgumentException("Creator.ClassNotFound");
126: }
127: }
128:
129: /**
130: * The name of the bean to get from the FacesContext
131: */
132: private String managedBeanName;
133:
134: /**
135: * The cached type of bean that we are creating
136: */
137: private Class<?> instanceType;
138:
139: /**
140: * The log stream
141: */
142: private static final Log log = LogFactory.getLog(JsfCreator.class);
143: }
|