Save This Page
Home » mojarra-1.2_09-b02-FCS-source » com.sun.faces.el » [javadoc | source]
    1   /*
    2    * $Id: ManagedBeanELResolver.java,v 1.19.4.1 2007/12/03 18:33:51 rlubke Exp $
    3    */
    4   /*
    5    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    6    * 
    7    * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
    8    * 
    9    * The contents of this file are subject to the terms of either the GNU
   10    * General Public License Version 2 only ("GPL") or the Common Development
   11    * and Distribution License("CDDL") (collectively, the "License").  You
   12    * may not use this file except in compliance with the License. You can obtain
   13    * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
   14    * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
   15    * language governing permissions and limitations under the License.
   16    * 
   17    * When distributing the software, include this License Header Notice in each
   18    * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
   19    * Sun designates this particular file as subject to the "Classpath" exception
   20    * as provided by Sun in the GPL Version 2 section of the License file that
   21    * accompanied this code.  If applicable, add the following below the License
   22    * Header, with the fields enclosed by brackets [] replaced by your own
   23    * identifying information: "Portions Copyrighted [year]
   24    * [name of copyright owner]"
   25    * 
   26    * Contributor(s):
   27    * 
   28    * If you wish your version of this file to be governed by only the CDDL or
   29    * only the GPL Version 2, indicate your decision by adding "[Contributor]
   30    * elects to include this software in this distribution under the [CDDL or GPL
   31    * Version 2] license."  If you don't indicate a single choice of license, a
   32    * recipient has the option to distribute your version of this file under
   33    * either the CDDL, the GPL Version 2 or to extend the choice of license to
   34    * its licensees as provided above.  However, if you add GPL Version 2 code
   35    * and therefore, elected the GPL Version 2 license, then the option applies
   36    * only if the new code is made subject to such option by the copyright
   37    * holder.
   38    */
   39   
   40   package com.sun.faces.el;
   41   
   42   import com.sun.faces.application.ApplicationAssociate;
   43   import com.sun.faces.mgbean.BeanBuilder;
   44   import com.sun.faces.mgbean.BeanManager;
   45   import com.sun.faces.util.MessageUtils;
   46   import com.sun.faces.util.Util;
   47   
   48   import javax.el.ELContext;
   49   import javax.el.ELException;
   50   import javax.el.ELResolver;
   51   import javax.el.PropertyNotFoundException;
   52   import javax.faces.context.ExternalContext;
   53   import javax.faces.context.FacesContext;
   54   import java.beans.FeatureDescriptor;
   55   import java.util.ArrayList;
   56   import java.util.Collections;
   57   import java.util.Iterator;
   58   import java.util.List;
   59   import java.util.Map;
   60   
   61   public class ManagedBeanELResolver extends ELResolver {
   62   
   63       public ManagedBeanELResolver() {
   64       }
   65   
   66       public Object getValue(ELContext context, Object base, Object property)
   67           throws ELException {
   68           if (base != null) {
   69               return null;
   70           }
   71           if (property == null) {
   72               String message = MessageUtils.getExceptionMessageString
   73                   (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "property");
   74               throw new PropertyNotFoundException(message);
   75           }
   76   
   77           Object result = null;
   78           FacesContext facesContext = (FacesContext)
   79               context.getContext(FacesContext.class);
   80           BeanManager manager =
   81                ApplicationAssociate.getCurrentInstance().getBeanManager();
   82           String beanName = property.toString();
   83           if (manager != null
   84                 && manager.isManaged(beanName)
   85                 && !manager.isBeanInScope(beanName, facesContext)) {
   86   
   87               // no bean found in scope.  create a new instance
   88               result = manager.create(beanName, facesContext);
   89               context.setPropertyResolved(result != null);
   90           }
   91         
   92           return result;
   93       }
   94   
   95   
   96       public Class<?> getType(ELContext context, Object base, Object property)
   97           throws ELException {
   98   
   99           if (base == null && property == null) {
  100               String message = MessageUtils.getExceptionMessageString
  101                   (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "base and property"); // ?????
  102               throw new PropertyNotFoundException(message);
  103           }
  104   
  105           return null;
  106   
  107       }
  108   
  109       public void  setValue(ELContext context, Object base, Object property,
  110                             Object val) throws ELException {
  111   
  112           if (base == null && property == null) {
  113               String message = MessageUtils.getExceptionMessageString
  114                   (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "base and property"); // ?????
  115               throw new PropertyNotFoundException(message);
  116           }
  117   
  118           // The spec states that nothing ManagedBeanELResolver should
  119           // do nothing in setValue() so that the BeanELResolver can do its
  120           // thing, however, in 1.1, calling setValue() for a reference
  121           // that happened to be a managed bean cause the bean to be created
  122           // and pushed to scope if it wasn't already there.  So ultimately,
  123           // the user should be able to create a ValueExpression and call
  124           // setValue() and expect it to work without having to call getValue()
  125           // first.
  126           FacesContext facesContext = (FacesContext)
  127               context.getContext(FacesContext.class);
  128           BeanManager manager =
  129                ApplicationAssociate.getCurrentInstance().getBeanManager();
  130           String beanName = property.toString();
  131           if (manager != null
  132                 && manager.isManaged(beanName)
  133                 && !manager.isBeanInScope(beanName, facesContext)) {
  134   
  135               // no bean found in scope.  create a new instance
  136               manager.create(beanName, facesContext);
  137           }
  138   
  139       }
  140   
  141       public boolean isReadOnly(ELContext context, Object base, Object property)
  142           throws ELException {
  143           if (base != null) {
  144               return false;
  145           }
  146           if (property == null) {
  147               String message = MessageUtils.getExceptionMessageString
  148                   (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "property");
  149               throw new PropertyNotFoundException(message);
  150           }
  151   
  152           return false;
  153       }
  154   
  155       public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
  156   
  157           if (base != null) {
  158               return null;
  159           }
  160   
  161           FacesContext facesContext =
  162               (FacesContext) context.getContext(FacesContext.class);
  163           BeanManager beanManager =
  164                ApplicationAssociate.getCurrentInstance().getBeanManager();
  165           if (beanManager == null || beanManager.getRegisteredBeans().isEmpty()) {
  166               List<FeatureDescriptor> l = Collections.emptyList();
  167               return l.iterator();
  168           }
  169   
  170           Map<String,BeanBuilder> beans = beanManager.getRegisteredBeans();
  171           List<FeatureDescriptor> list =
  172                new ArrayList<FeatureDescriptor>(beans.size());
  173           // iterate over the list of managed beans
  174           for (Map.Entry<String,BeanBuilder> bean : beans.entrySet()) {
  175               String beanName = bean.getKey();
  176               BeanBuilder builder = bean.getValue();
  177               String loc = Util.getLocaleFromContextOrSystem(facesContext).toString();
  178               Map<String,String> descriptions = builder.getDescriptions();
  179   
  180               String description = null;
  181               if (descriptions != null) {
  182                   description = descriptions.get(loc);
  183                   if (description == null) {
  184                       description = descriptions.get("DEFAULT");
  185                   }
  186               }
  187               list.add(Util.getFeatureDescriptor(beanName,
  188                                                  beanName,
  189                                                  (description == null) ? "" : description,
  190                                                  false,
  191                                                  false,
  192                                                  true,
  193                                                  builder.getBeanClass(),
  194                                                  Boolean.TRUE));
  195           }
  196   
  197           return list.iterator();
  198       }
  199   
  200       public Class<?> getCommonPropertyType(ELContext context, Object base) {
  201           if (base != null) {
  202               return null;
  203           }
  204           return Object.class;
  205       }
  206   
  207   }

Save This Page
Home » mojarra-1.2_09-b02-FCS-source » com.sun.faces.el » [javadoc | source]