KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > faces > JsfCreator


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.faces;
17
18 import javax.faces.application.Application;
19 import javax.faces.context.FacesContext;
20 import javax.faces.el.ValueBinding;
21 import javax.faces.el.VariableResolver;
22
23 import org.directwebremoting.create.AbstractCreator;
24 import org.directwebremoting.extend.Creator;
25 import org.directwebremoting.util.LocalUtil;
26 import org.directwebremoting.util.Logger;
27
28 /**
29  * This is a DWR creator implementation, to allow dwr beans to be allocated into
30  * JSF scopes and into jeffs3 specific scope (i.e. the flow scope)
31  * @author Pierpaolo Follia
32  * @author Joe Walker [joe at getahead dot ltd dot uk]
33  * @author Vinicius Melo [vdmelo at gmail dot com]
34  */

35 public class JsfCreator extends AbstractCreator implements Creator
36 {
37     /* (non-Javadoc)
38      * @see org.directwebremoting.Creator#getType()
39      */

40     public Class JavaDoc getType()
41     {
42         if (instanceType == null)
43         {
44             try
45             {
46                 instanceType = getInstance().getClass();
47             }
48             catch (InstantiationException JavaDoc ex)
49             {
50                 log.error("Failed to instansiate object to detect type.", ex);
51                 return Object JavaDoc.class;
52             }
53         }
54
55         return instanceType;
56     }
57
58     /* (non-Javadoc)
59      * @see org.directwebremoting.Creator#getInstance()
60      */

61     public Object JavaDoc getInstance() throws InstantiationException JavaDoc
62     {
63         FacesContext facesContext = FacesContext.getCurrentInstance();
64         if (facesContext == null)
65         {
66             log.error("Object " + getManagedBeanName() + " cannot be created since the faces context is null");
67             return null;
68         }
69
70         Application application = facesContext.getApplication();
71         Object JavaDoc resolvedObject = null;
72
73         if (isVBExpression(getManagedBeanName()))
74         {
75             ValueBinding vb = application.createValueBinding(getManagedBeanName());
76             if (vb != null)
77             {
78                 resolvedObject = vb.getValue(facesContext);
79             }
80         }
81         else
82         {
83             VariableResolver resolver = application.getVariableResolver();
84             resolvedObject = resolver.resolveVariable(facesContext, getManagedBeanName());
85         }
86
87         return resolvedObject;
88     }
89
90     /**
91      * Determine whether String is a value binding expression or not.
92      * @param expression The expression to test for value bindingness
93      * @return true if the expression contains a VB expression
94      */

95     public static boolean isVBExpression(String JavaDoc expression)
96     {
97         if (expression == null)
98         {
99             return false;
100         }
101
102         int start = expression.indexOf("#{");
103         int end = expression.indexOf('}');
104
105         return start != -1 && start < end;
106     }
107
108     /**
109      * @return Returns the managedBeanName.
110      */

111     public String JavaDoc getManagedBeanName()
112     {
113         return managedBeanName;
114     }
115
116     /**
117      * @param managedBeanName The managedBeanName to set.
118      */

119     public void setManagedBeanName(String JavaDoc managedBeanName)
120     {
121         this.managedBeanName = managedBeanName;
122     }
123
124     /**
125      * What sort of class do we create?
126      * @param classname The name of the class
127      */

128     public void setClass(String JavaDoc classname)
129     {
130         try
131         {
132             this.instanceType = LocalUtil.classForName(classname);
133         }
134         catch (ClassNotFoundException JavaDoc ex)
135         {
136             throw new IllegalArgumentException JavaDoc("Creator.ClassNotFound");
137         }
138     }
139
140     /**
141      * The name of the bean to get from the FacesContext
142      */

143     private String JavaDoc managedBeanName;
144
145     /**
146      * The cached type of bean that we are creating
147      */

148     private Class JavaDoc instanceType;
149
150     /**
151      * The log stream
152      */

153     private static final Logger log = Logger.getLogger(JsfCreator.class);
154 }
155
Popular Tags