1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.myfaces.custom.scope;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.faces.context.FacesContext;
25
26 /**
27 * Small helper to cope with the
28 * managed beans within the scope tag
29 * handler
30 *
31 * Thanks Derek Shen to allow me to relicense
32 * the Faces Utils code under Apache2
33 * this class is derived from it
34 *
35 * http://www.javaworld.com/javaworld/jw-07-2004/jw-0719-jsf.html
36 *
37 * @author Werner Punz werpu@gmx.at
38 *
39 */
40 public class ScopeUtils
41 {
42 private static final String EL_END = "}";
43 private static final String EL_BEGIN = "#{";
44 private static Log log = LogFactory.getLog(ScopeUtils.class);
45
46 /**
47 * el checker
48 *
49 * @param beanName
50 * @return true if the bean is a managed bean
51 */
52 public static final boolean isEl(String beanName)
53 {
54 beanName = beanName.trim();
55 return beanName.startsWith(EL_BEGIN) && beanName.endsWith(EL_END);
56 }
57
58 /**
59 * returns the managed bean from the given bean name
60 * @param beanName
61 * @return Object managed bean
62 */
63 public static Object getManagedBean(String beanName)
64 {
65 if (isEl(beanName))
66 {
67 if(FacesContext.getCurrentInstance() == null)
68 return null;
69
70 return FacesContext.getCurrentInstance().getApplication()
71 .createValueBinding(beanName).getValue(
72 FacesContext.getCurrentInstance());
73 }
74 else
75 {
76 try
77 {
78 if(FacesContext.getCurrentInstance() == null)
79 return null;
80
81 return FacesContext.getCurrentInstance().getApplication()
82 .createValueBinding(EL_BEGIN + beanName + EL_END).getValue(
83 FacesContext.getCurrentInstance());
84 }
85 catch(Exception ex)
86 {
87 log.info("ScopeContainer not found - has not been initialized.",ex);
88 return null;
89 }
90 }
91 }
92
93 }