001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/jsf/tags/sakai_2-4-1/app/src/java/org/sakaiproject/jsf/app/SakaiVariableResolver.java $
003: * $Id: SakaiVariableResolver.java 9278 2006-05-10 23:29:21Z ray@media.berkeley.edu $
004: **********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.jsf.app;
021:
022: import javax.faces.context.FacesContext;
023: import javax.faces.el.EvaluationException;
024: import javax.faces.el.VariableResolver;
025: import javax.servlet.ServletContext;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.component.cover.ComponentManager;
030: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
031: import org.springframework.web.context.WebApplicationContext;
032: import org.springframework.web.context.support.WebApplicationContextUtils;
033:
034: /**
035: * <p>
036: * SakaiVariableResolver extends the standard variable resolved of the selected faced implementation. Special Sakai features include the ability to name any Sakai component (or Spring bean) by component (bean) name.
037: * </p>
038: *
039: * @author University of Michigan, Sakai Software Development Team
040: * @version $Revision: 9278 $
041: */
042: public class SakaiVariableResolver extends VariableResolver {
043: /** Our log (commons). */
044: private static Log M_log = LogFactory
045: .getLog(SakaiVariableResolver.class);
046:
047: /** The VariableResolver already in place that we add features to. */
048: protected VariableResolver m_resolver = null;
049:
050: /**
051: * Construct taking the VariableResolver alreay in place that we decorate.
052: *
053: * @param other
054: * The VariableResolver already in place.
055: */
056: public SakaiVariableResolver(VariableResolver other) {
057: m_resolver = other;
058: if (M_log.isDebugEnabled())
059: M_log.debug("constructed around: " + m_resolver);
060: }
061:
062: /**
063: * @inheritDoc
064: */
065: public Object resolveVariable(FacesContext context, String name)
066: throws EvaluationException {
067: if (M_log.isDebugEnabled())
068: M_log.debug("resolving: " + name);
069:
070: Object rv = null;
071:
072: // first, give the other a shot
073: if (m_resolver != null) {
074: rv = m_resolver.resolveVariable(context, name);
075: if (rv != null) {
076: if (M_log.isDebugEnabled())
077: M_log.debug("resolving: " + name
078: + " with other to: " + rv);
079: return rv;
080: }
081: }
082:
083: // now, we extend. Check for a component/bean, using the local Spring method to pick up local and global definitions.
084: WebApplicationContext wac = null;
085:
086: try {
087: wac = WebApplicationContextUtils
088: .getWebApplicationContext((ServletContext) context
089: .getExternalContext().getContext());
090: } catch (Exception e) {
091: e.printStackTrace();
092: }
093:
094: if (wac != null) {
095: // try the name as given
096: try {
097: rv = wac.getBean(name);
098: if (rv != null) {
099: if (M_log.isDebugEnabled())
100: M_log.debug("resolving: " + name
101: + " via spring to : " + rv);
102: return rv;
103: }
104: } catch (NoSuchBeanDefinitionException ignore) {
105: // the bean doesn't exist, but its not necessarily an error
106: }
107:
108: // since the jsf environment does not allow a dot in the name, convert from underbar to dot and try again
109: if (name.indexOf('_') != -1) {
110: String alternate = name.replace('_', '.');
111: try {
112: rv = wac.getBean(alternate);
113:
114: if (rv != null) {
115: if (M_log.isDebugEnabled())
116: M_log.debug("resolving: " + alternate
117: + " via spring to : " + rv);
118: return rv;
119: }
120: } catch (NoSuchBeanDefinitionException ignore) {
121: // the bean doesn't exist, but its not necessarily an error
122: }
123: }
124: }
125:
126: // if no wac, try using the component manager
127: else
128: rv = ComponentManager.get(name);
129: {
130: if (rv != null) {
131: if (M_log.isDebugEnabled())
132: M_log.debug("resolving: " + name
133: + " via component manager to : " + rv);
134: return rv;
135: }
136:
137: // since the jsf environment does not allow a dot in the name, convert from underbar to dot and try again
138: if (name.indexOf('_') != -1) {
139: String alternate = name.replace('_', '.');
140: rv = ComponentManager.get(alternate);
141:
142: if (rv != null) {
143: if (M_log.isDebugEnabled())
144: M_log.debug("resolving: " + alternate
145: + " via component manager to : " + rv);
146: return rv;
147: }
148: }
149: }
150:
151: if (M_log.isDebugEnabled())
152: M_log.debug("resolving: " + name + " unresolved!");
153: return null;
154: }
155: }
|