public Object getValue(ELContext context, Object base, Object property) {
    // this resolver only resolves top level variable names to execution variable names.
    // only handle if this is a top level variable
    if (base == null) {
      // we assume a NPE-check for property is not needed
      // i don't think the next cast can go wrong.  can it?
      String name = (String) property;

      if (execution != null && NAME_EXECUTION.equals(name)) {
        context.setPropertyResolved(true);
        return execution;
      }

      if (processInstance != null && NAME_PROCESSINSTANCE.equals(name)) {
        context.setPropertyResolved(true);
        return processInstance;
      }

      if (task != null && NAME_TASK.equals(name)) {
        context.setPropertyResolved(true);
        return task;
      }
    }

    return null;
  }
Exemple #2
0
 public boolean isReadOnly(ELContext context, Object base, Object property) throws ELException {
   if (base != null) {
     return false;
   }
   if (property == null) {
     String message =
         MessageUtils.getExceptionMessageString(
             MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "property");
     throw new PropertyNotFoundException(message);
   }
   // return value will be ignored unless context.propertyResolved is
   // set to true.
   Integer index = IMPLICIT_OBJECTS.get(property.toString());
   if (index == null) {
     return false;
   }
   switch (index) {
     case FACES_CONTEXT:
       context.setPropertyResolved(true);
       return true;
     case VIEW:
       context.setPropertyResolved(true);
       return true;
     default:
       return false;
   }
 }
  @Override
  public void setValue(ELContext elContext, Object base, Object property, Object value) {

    if (elContext == null) {
      throw new NullPointerException();
    }

    if (base != null) {
      return;
    }

    elContext.setPropertyResolved(true);

    if (property instanceof String) {
      PageContext pageContext = (PageContext) elContext.getContext(JspContext.class);

      String attribute = (String) property;

      if (pageContext.getAttribute(attribute, PageContext.REQUEST_SCOPE) != null) {

        pageContext.setAttribute(attribute, value, PageContext.REQUEST_SCOPE);
      } else if (pageContext.getAttribute(attribute, PageContext.SESSION_SCOPE) != null) {

        pageContext.setAttribute(attribute, value, PageContext.SESSION_SCOPE);
      } else if (pageContext.getAttribute(attribute, PageContext.APPLICATION_SCOPE) != null) {

        pageContext.setAttribute(attribute, value, PageContext.APPLICATION_SCOPE);
      } else {
        pageContext.setAttribute(attribute, value, PageContext.PAGE_SCOPE);
      }
    }
  }
  @Override
  public Object getValue(ELContext context, Object base, Object property)
      throws NullPointerException, PropertyNotFoundException, ELException {
    context.setPropertyResolved(false);

    int start;
    Object result = null;

    if (base == null) {
      // call implicit and app resolvers
      int index = 1 /* implicit */ + appResolversSize;
      for (int i = 0; i < index; i++) {
        result = resolvers[i].getValue(context, base, property);
        if (context.isPropertyResolved()) {
          return result;
        }
      }
      // skip collection-based resolvers (map, resource, list, array, and
      // bean)
      start = index + 5;
    } else {
      // skip implicit resolver only
      start = 1;
    }

    for (int i = start; i < size; i++) {
      result = resolvers[i].getValue(context, base, property);
      if (context.isPropertyResolved()) {
        return result;
      }
    }

    return null;
  }
  @Override
  public Object invoke(
      ELContext context, Object base, Object method, Class<?>[] paramTypes, Object[] params) {
    String targetMethod = coerceToString(method);
    if (targetMethod.length() == 0) {
      throw new ELException(new NoSuchMethodException());
    }

    context.setPropertyResolved(false);

    Object result = null;

    // skip implicit and call app resolvers
    int index = 1 /* implicit */ + appResolversSize;
    for (int i = 1; i < index; i++) {
      result = resolvers[i].invoke(context, base, targetMethod, paramTypes, params);
      if (context.isPropertyResolved()) {
        return result;
      }
    }

    // skip map, resource, list, and array resolvers
    index += 4;
    // call bean and the rest of resolvers
    for (int i = index; i < size; i++) {
      result = resolvers[i].invoke(context, base, targetMethod, paramTypes, params);
      if (context.isPropertyResolved()) {
        return result;
      }
    }

    return null;
  }
  /**
   * Set the value of a scoped object for the specified name.
   *
   * @param context <code>ELContext</code> for evaluating this value
   * @param base Base object against which this evaluation occurs (must be null because we are
   *     evaluating a top level variable)
   * @param property Property name to be accessed
   * @param value New value to be set
   */
  public void setValue(ELContext context, Object base, Object property, Object value) {

    if (base != null) {
      return;
    }
    if (property == null) {
      throw new PropertyNotFoundException("No property specified");
    }

    context.setPropertyResolved(true);
    String key = property.toString();
    Object result = null;
    FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
    ExternalContext econtext = fcontext.getExternalContext();

    if (econtext.getRequestMap().containsKey(property)) {
      econtext.getRequestMap().put(key, value);
    } else if (econtext.getSessionMap().containsKey(property)) {
      econtext.getSessionMap().put(key, value);
    } else if (econtext.getApplicationMap().containsKey(property)) {
      econtext.getApplicationMap().put(key, value);
    } else {
      econtext.getRequestMap().put(key, value);
    }
  }
  /**
   * Return an existing scoped object for the specified name (if any); otherwise, return <code>null
   * </code>.
   *
   * @param context <code>ELContext</code> for evaluating this value
   * @param base Base object against which this evaluation occurs (must be null because we are
   *     evaluating a top level variable)
   * @param property Property name to be accessed
   */
  public Object getValue(ELContext context, Object base, Object property) {

    if (base != null) {
      return null;
    }
    if (property == null) {
      throw new PropertyNotFoundException("No property specified");
    }

    FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
    ExternalContext econtext = fcontext.getExternalContext();
    Object value = null;
    value = econtext.getRequestMap().get(property);
    if (value != null) {
      context.setPropertyResolved(true);
      return value;
    }
    value = econtext.getSessionMap().get(property);
    if (value != null) {
      context.setPropertyResolved(true);
      return value;
    }
    value = econtext.getApplicationMap().get(property);
    if (value != null) {
      context.setPropertyResolved(true);
      return value;
    }

    return null;
  }
Exemple #8
0
  // taken from TCK facesResourceBundleResolverFeatureDescriptorTest
  public String getFeatureDescriptorCorrectness() {
    StringBuilder builder = new StringBuilder();

    ELContext elContext = FacesContext.getCurrentInstance().getELContext();
    ELResolver resolver = elContext.getELResolver();
    boolean fd_Found = false;

    // Setup golden FeatureDescriptor.
    FeatureDescriptor controlDesc = new FeatureDescriptor();
    controlDesc.setValue("resolvable", Boolean.TRUE);
    controlDesc.setValue("type", ResourceBundle.class);
    controlDesc.setName("resourceBundle03");
    controlDesc.setDisplayName("simple");
    controlDesc.setExpert(false);
    controlDesc.setHidden(false);
    controlDesc.setPreferred(true);
    controlDesc.setShortDescription("");

    builder.append("<h1>getFeatureDescriptors output</h1>\n");
    for (Iterator i = resolver.getFeatureDescriptors(elContext, null); i.hasNext(); ) {
      FeatureDescriptor test = (FeatureDescriptor) i.next();
      builder
          .append("<p>Name: ")
          .append(test.getName())
          .append(" displayName: ")
          .append(test.getDisplayName())
          .append("</p>\n");
    }

    return builder.toString();
  }
Exemple #9
0
 /*
  * (non-Javadoc)
  *
  * @see javax.el.ValueExpression#setValue(javax.el.ELContext,
  *      java.lang.Object)
  */
 public void setValue(ELContext context, Object value) {
   Object base = this.orig.getValue(context);
   if (base != null) {
     context.setPropertyResolved(false);
     context.getELResolver().setValue(context, base, key, value);
   }
 }
Exemple #10
0
 /*
  * (non-Javadoc)
  *
  * @see javax.el.ValueExpression#isReadOnly(javax.el.ELContext)
  */
 public boolean isReadOnly(ELContext context) {
   Object base = this.orig.getValue(context);
   if (base != null) {
     context.setPropertyResolved(false);
     return context.getELResolver().isReadOnly(context, base, key);
   }
   return true;
 }
Exemple #11
0
 /*
  * (non-Javadoc)
  *
  * @see javax.el.ValueExpression#getType(javax.el.ELContext)
  */
 public Class getType(ELContext context) {
   Object base = this.orig.getValue(context);
   if (base != null) {
     context.setPropertyResolved(false);
     return context.getELResolver().getType(context, base, key);
   }
   return null;
 }
Exemple #12
0
  // taken from TCK facesResourceBundleResolverGetTypeTest
  public String getResourceBundleType() {
    FacesContext context = FacesContext.getCurrentInstance();
    ELContext elContext = context.getELContext();
    ELResolver elResolver = elContext.getELResolver();
    Class type = elResolver.getType(elContext, null, "resourceBundle03");

    return type.toString();
  }
  /**
   * @param result
   * @param element
   * @param dataList
   */
  public void processRepeat(
      Node result, Element element, Iterable<?> dataList, Runnable onEachLoop) {
    if (dataList == null) {
      return;
    }

    // Compute list size
    int size = Iterables.size(dataList);

    if (size > 0) {
      // Save the initial EL state
      Map<String, ? extends Object> oldContext = templateContext.getContext();
      Object oldCur = templateContext.getCur();
      ValueExpression oldVarExpression = null;

      // Set the new Context variable.  Copy the old context to preserve
      // any existing "index" variable
      Map<String, Object> loopData = Maps.newHashMap(oldContext);
      loopData.put(PROPERTY_COUNT, size);
      templateContext.setContext(loopData);

      // TODO: This means that any loop with @var doesn't make the loop
      // variable available in the default expression context.
      // Update the specification to make this explicit.
      Attr varAttr = element.getAttributeNode(ATTRIBUTE_VAR);
      if (varAttr == null) {
        oldCur = templateContext.getCur();
      } else {
        oldVarExpression = elContext.getVariableMapper().resolveVariable(varAttr.getValue());
      }

      Attr indexVarAttr = element.getAttributeNode(ATTRIBUTE_INDEX);
      String indexVar = indexVarAttr == null ? PROPERTY_INDEX : indexVarAttr.getValue();

      int index = 0;
      for (Object data : dataList) {
        loopData.put(indexVar, index++);

        // Set up context for rendering inner node
        templateContext.setCur(data);
        if (varAttr != null) {
          ValueExpression varExpression = expressions.constant(data, Object.class);
          elContext.getVariableMapper().setVariable(varAttr.getValue(), varExpression);
        }

        onEachLoop.run();
      }

      // Restore EL state
      if (varAttr == null) {
        templateContext.setCur(oldCur);
      } else {
        elContext.getVariableMapper().setVariable(varAttr.getValue(), oldVarExpression);
      }

      templateContext.setContext(oldContext);
    }
  }
  @Override
  @SuppressWarnings("deprecation")
  public Object getValue(ELContext context, Object base, Object property) throws ELException {

    // Don't call into the chain unless it's been decorated.
    if (legacyVR instanceof ChainAwareVariableResolver) {
      return null;
    }

    if (base != null) {
      return null;
    }
    if (base == null && property == null) {
      String message =
          MessageUtils.getExceptionMessageString(
              MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "base and property"); // ?????
      throw new PropertyNotFoundException(message);
    }
    context.setPropertyResolved(true);
    Object result = null;

    FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
    String propString = property.toString();
    Map<String, Object> stateMap = RequestStateManager.getStateMap(facesContext);
    try {
      // If we are already in the midst of an expression evaluation
      // that touched this resolver...
      //noinspection unchecked
      List<String> varNames = (List<String>) stateMap.get(RequestStateManager.REENTRANT_GUARD);
      if (varNames != null && !varNames.isEmpty() && varNames.contains(propString)) {
        // take no action and return.
        context.setPropertyResolved(false);
        return null;
      }
      // Make sure subsequent calls don't take action.
      if (varNames == null) {
        varNames = new ArrayList<>();
        stateMap.put(RequestStateManager.REENTRANT_GUARD, varNames);
      }
      varNames.add(propString);

      result = legacyVR.resolveVariable(facesContext, propString);
    } catch (EvaluationException ex) {
      context.setPropertyResolved(false);
      throw new ELException(ex);
    } finally {
      // Make sure to remove the guard after the call returns
      //noinspection unchecked
      List<String> varNames = (List<String>) stateMap.get(RequestStateManager.REENTRANT_GUARD);
      if (varNames != null && !varNames.isEmpty()) {
        varNames.remove(propString);
      }
      // Make sure that the ELContext "resolved" indicator is set
      // in accordance wth the result of the resolution.
      context.setPropertyResolved(result != null);
    }
    return result;
  }
Exemple #15
0
  public void contextDestroyed(ServletContextEvent sce) {
    ServletContext context = sce.getServletContext();
    InitFacesContext initContext = null;
    try {
      initContext = new InitFacesContext(context);

      if (webAppListener != null) {
        webAppListener.contextDestroyed(sce);
        webAppListener = null;
      }
      if (webResourcePool != null) {
        webResourcePool.shutdownNow();
      }
      if (!ConfigManager.getInstance().hasBeenInitialized(context)) {
        return;
      }
      GroovyHelper helper = GroovyHelper.getCurrentInstance(context);
      if (helper != null) {
        helper.setClassLoader();
      }
      if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(
            Level.FINE, "ConfigureListener.contextDestroyed({0})", context.getServletContextName());
      }

      ELContext elctx = new ELContextImpl(initContext.getApplication().getELResolver());
      elctx.putContext(FacesContext.class, initContext);
      initContext.setELContext(elctx);
      Application app = initContext.getApplication();
      app.publishEvent(initContext, PreDestroyApplicationEvent.class, Application.class, app);

      Util.setNonFacesContextApplicationMap(null);

    } catch (Exception e) {
      if (LOGGER.isLoggable(Level.SEVERE)) {
        LOGGER.log(
            Level.SEVERE,
            "Unexpected exception when attempting to tear down the Mojarra runtime",
            e);
      }
    } finally {
      ApplicationAssociate.clearInstance(initContext.getExternalContext());
      ApplicationAssociate.setCurrentInstance(null);
      com.sun.faces.application.ApplicationImpl.clearInstance(initContext.getExternalContext());
      com.sun.faces.application.InjectionApplicationFactory.clearInstance(
          initContext.getExternalContext());
      // Release the initialization mark on this web application
      ConfigManager.getInstance().destory(context);
      if (initContext != null) {
        initContext.release();
      }
      ReflectionUtils.clearCache(Thread.currentThread().getContextClassLoader());
      WebConfiguration.clear(context);
    }
  }
  private Object evaluateProperty(Object base, String property) {
    ELContext elCtx = FacesContext.getCurrentInstance().getELContext();
    // simple property -> resolve value directly
    if (!property.contains(".")) {
      return elCtx.getELResolver().getValue(elCtx, base, property);
    }
    int index = property.indexOf('.');
    Object newBase = elCtx.getELResolver().getValue(elCtx, base, property.substring(0, index));

    return evaluateProperty(newBase, property.substring(index + 1));
  }
 @SuppressWarnings("unchecked")
 public <T> T getExpressionValue(FacesContext context, String exp, Class<T> returnType) {
   ELContext elContext = context.getELContext();
   boolean propertyResolved = elContext.isPropertyResolved();
   try {
     ValueExpression valueExpression = createElExpression(elContext, exp, returnType);
     return (T) valueExpression.getValue(elContext);
   } finally {
     elContext.setPropertyResolved(propertyResolved);
   }
 }
 protected <T> T findBean(FacesContext context, Class<T> type, String beanName) {
   ELContext elContext = context.getELContext();
   boolean propertyResolved = elContext.isPropertyResolved();
   try {
     return context
         .getApplication()
         .evaluateExpressionGet(
             context, "#" + "{" + beanName + "}", type); // Warning EL string is not closed
   } finally {
     elContext.setPropertyResolved(propertyResolved);
   }
 }
Exemple #19
0
  public ExpressionBuilder(String expression, ELContext ctx) throws ELException {
    this.expression = expression;

    FunctionMapper ctxFn = ctx.getFunctionMapper();
    VariableMapper ctxVar = ctx.getVariableMapper();

    if (ctxFn != null) {
      this.fnMapper = new FunctionMapperFactory(ctxFn);
    }
    if (ctxVar != null) {
      this.varMapper = new VariableMapperFactory(ctxVar);
    }
  }
  protected FacesContext getFacesContext(
      PortletRequest portletRequest, PortletResponse portletResponse, Lifecycle lifecycle) {

    FacesContext newFacesContext =
        getFacesContextFactory()
            .getFacesContext(portletContext, portletRequest, portletResponse, lifecycle);

    // TCK TestPage203 (JSF_ELTest) ensure that the #{facesContext} implicit object is set to the
    // current instance.
    ELContext elContext = newFacesContext.getELContext();
    elContext.putContext(FacesContext.class, newFacesContext);

    return newFacesContext;
  }
Exemple #21
0
 /** 返回上下文中唯一的文档对象. */
 public static Document getContextDocument(ELContext elctx) {
   Document doc = (Document) elctx.getContext(Document.class);
   if (doc == null) {
     try {
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       DocumentBuilder db = dbf.newDocumentBuilder();
       doc = db.newDocument();
       elctx.putContext(Document.class, doc);
     } catch (ParserConfigurationException ex) {
       throw new ELException(ex);
     }
   }
   return doc;
 }
 @Override
 public Class<?> getType(ELContext context, Object base, Object property) {
   Class<?> type = null;
   if (base instanceof DocumentModel) {
     try {
       type = super.getType(context, base, property);
     } catch (PropertyNotFoundException e) {
       type = DocumentPropertyContext.class;
       context.setPropertyResolved(true);
     }
   } else if (base instanceof DocumentPropertyContext || base instanceof Property) {
     type = Object.class;
     if (base instanceof DocumentPropertyContext) {
       DocumentPropertyContext ctx = (DocumentPropertyContext) base;
       try {
         Property docProperty = getDocumentProperty(ctx, property);
         if (docProperty.isContainer()) {
           Property subProperty = getDocumentProperty(docProperty, property);
           if (subProperty.isList()) {
             type = List.class;
           }
         } else if (docProperty instanceof ArrayProperty) {
           type = List.class;
         }
       } catch (PropertyException pe) {
         // avoid errors, return Object
         log.warn(pe.toString());
       }
     } else if (base instanceof Property) {
       try {
         Property docProperty = (Property) base;
         Property subProperty = getDocumentProperty(docProperty, property);
         if (subProperty.isList()) {
           type = List.class;
         }
       } catch (PropertyException pe) {
         try {
           // try property getters to resolve
           // doc.schema.field.type for instance
           type = super.getType(context, base, property);
         } catch (PropertyNotFoundException e) {
           // avoid errors, log original error and return Object
           log.warn(pe.toString());
         }
       }
     }
     context.setPropertyResolved(true);
   }
   return type;
 }
  /** @see javax.el.ELResolver#getValue(ELContext, Object, Object) */
  @Override
  public Object getValue(ELContext context, Object base, Object property) {
    if (property != null) {
      String propertyString = property.toString();
      Namespace namespace = null;
      if (base == null) {
        if (manager.getRootNamespace().contains(propertyString)) {
          context.setPropertyResolved(true);
          return manager.getRootNamespace().get(propertyString);
        }
      } else if (base instanceof Namespace) {
        namespace = (Namespace) base;
        // We're definitely the responsible party
        context.setPropertyResolved(true);
        if (namespace.contains(propertyString)) {
          // There is a child namespace
          return namespace.get(propertyString);
        }
      } else {
        // let the standard EL resolver chain handle the property
        return null;
      }
      final String name;
      if (namespace != null) {
        // Try looking in the manager for a bean
        name = namespace.qualifyName(propertyString);
      } else {
        name = propertyString;
      }
      Object value = null;
      try {

        Bean<?> bean = manager.resolve(manager.getBeans(name));
        CreationalContext<?> creationalContext = manager.createCreationalContext(bean);
        if (bean != null) {
          value = manager.getReference(bean, creationalContext);
        }
        creationalContext.release();
      } catch (Exception e) {
        throw new RuntimeException(
            "Error resolving property " + propertyString + " against base " + base, e);
      }
      if (value != null) {
        context.setPropertyResolved(true);
        return value;
      }
    }
    return null;
  }
 @Override
 public boolean isReadOnly(ELContext context, Object base, Object property) {
   boolean readOnly = false;
   try {
     readOnly = super.isReadOnly(context, base, property);
   } catch (PropertyNotFoundException e) {
     if (base instanceof DocumentModel || base instanceof DocumentPropertyContext) {
       readOnly = false;
       context.setPropertyResolved(true);
     } else if (base instanceof Property) {
       readOnly = ((Property) base).isReadOnly();
       context.setPropertyResolved(true);
     }
   }
   return readOnly;
 }
Exemple #25
0
  public Class<?> getType(ELContext context, Object base, Object property) throws ELException {
    if (base != null) {
      return null;
    }
    if (property == null) {
      String message =
          MessageUtils.getExceptionMessageString(
              MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "property");
      throw new PropertyNotFoundException(message);
    }

    Integer index = IMPLICIT_OBJECTS.get(property.toString());

    if (index == null) {
      return null;
    }
    switch (index) {
      case FACES_CONTEXT:
      case VIEW:
        context.setPropertyResolved(true);
        return null;
      default:
        return null;
    }
  }
 // Capture the base and property rather than write the value
 @Override
 public void setValue(ELContext context, Object base, Object property, Object value) {
   if (base != null && property != null) {
     context.setPropertyResolved(true);
     valueReference = new ValueReference(base, property.toString());
   }
 }
  /**
   * Evaluate the expr as an object.
   *
   * @param env the page context
   */
  @Override
  public Object getValue(ELContext env) throws ELException {
    if (!(env instanceof ServletELContext))
      return env.getELResolver().getValue(env, null, "session");

    env.setPropertyResolved(true);

    ServletELContext servletEnv = (ServletELContext) env;

    HttpServletRequest req = servletEnv.getRequest();

    HttpSession session = req.getSession(false);

    if (session != null) return session.getAttribute(_field);
    else return null;
  }
  @Test
  public void testInvoke() {
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("B");

    context
        .getVariableMapper()
        .setVariable("beanB", factory.createValueExpression(beanB, TesterBeanB.class));

    MethodExpression me1 =
        factory.createMethodExpression(
            context, "${beanB.getName}", String.class, new Class<?>[] {});
    MethodExpression me2 =
        factory.createMethodExpression(
            context, "${beanB.sayHello('JUnit')}", String.class, new Class<?>[] {String.class});
    MethodExpression me3 =
        factory.createMethodExpression(
            context, "${beanB.sayHello}", String.class, new Class<?>[] {String.class});

    assertEquals("B", me1.invoke(context, null));
    assertEquals("Hello JUnit from B", me2.invoke(context, null));
    assertEquals("Hello JUnit from B", me2.invoke(context, new Object[] {"JUnit2"}));
    assertEquals("Hello JUnit2 from B", me3.invoke(context, new Object[] {"JUnit2"}));
    assertEquals("Hello JUnit from B", me2.invoke(context, new Object[] {null}));
    assertEquals("Hello  from B", me3.invoke(context, new Object[] {null}));
  }
  /**
   * Return <code>true</code> if the specified property is read only.
   *
   * @param context <code>ELContext</code> for evaluating this value
   * @param base Base object against which this evaluation occurs (must be null because we are
   *     evaluating a top level variable)
   * @param property Property name to be accessed
   */
  public boolean isReadOnly(ELContext context, Object base, Object property) {

    if (base == null) {
      context.setPropertyResolved(true);
      return false;
    }
    return false;
  }
Exemple #30
0
 /*
  * (non-Javadoc)
  *
  * @see javax.el.ValueExpression#getValue(javax.el.ELContext)
  */
 public Object getValue(ELContext context) {
   Object base = this.orig.getValue(context);
   if (base != null) {
     context.setPropertyResolved(true);
     return new Entry((Map) base, key);
   }
   return null;
 }