Search in sources :

Example 1 with EvaluationException

use of javax.faces.el.EvaluationException in project acs-community-packaging by Alfresco.

the class InvokeCommand method execute.

// ///////////////////////////////////////////////////////////////////////////
public void execute(final FacesContext facesContext, final String expression, final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    ResponseWriter writer = null;
    try {
        final int indexOfDot = expression.indexOf('.');
        final String variableName = expression.substring(0, indexOfDot);
        final String methodName = expression.substring(indexOfDot + 1);
        if (logger.isDebugEnabled())
            logger.debug("Invoking method represented by " + expression + " on variable " + variableName + " with method " + methodName);
        Object bean = null;
        if (Application.inPortalServer()) {
            // retrieve the managed bean, this is really weak but if the
            // request comes from a portal server the bean we need to get
            // is in the session with a prefix chosen by the portal vendor,
            // to cover this scenario we have to go through the names of
            // all the objects in the session to find the bean we want.
            String beanNameSuffix = "?" + variableName;
            Enumeration<?> enumNames = request.getSession().getAttributeNames();
            while (enumNames.hasMoreElements()) {
                String name = (String) enumNames.nextElement();
                if (name.endsWith(beanNameSuffix)) {
                    bean = request.getSession().getAttribute(name);
                    if (logger.isDebugEnabled())
                        logger.debug("Found bean " + bean + " in the session");
                    break;
                }
            }
        }
        // if we don't have the bean yet try and get it via the variable resolver
        if (bean == null) {
            VariableResolver vr = facesContext.getApplication().getVariableResolver();
            bean = vr.resolveVariable(facesContext, variableName);
            if (logger.isDebugEnabled())
                logger.debug("Created bean " + bean + " via the variable resolver");
        }
        final Method method = bean.getClass().getMethod(methodName);
        final String responseMimetype = (method.isAnnotationPresent(ResponseMimetype.class) ? method.getAnnotation(ResponseMimetype.class).value() : MimetypeMap.MIMETYPE_XML);
        if (logger.isDebugEnabled())
            logger.debug("invoking method " + method + " with repsonse mimetype  " + responseMimetype);
        writer = this.setupResponseWriter(responseMimetype, response, facesContext);
        // setup the transaction
        RetryingTransactionHelper txnHelper = Repository.getRetryingTransactionHelper(FacesContext.getCurrentInstance());
        final Object beanFinal = bean;
        RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>() {

            public Object execute() throws Throwable {
                // invoke the method
                try {
                    method.invoke(beanFinal);
                    return null;
                }// Let's prevent RuntimeExceptions being wrapped twice by unwrapping InvocationTargetExceptions
                 catch (InvocationTargetException e) {
                    if (e.getCause() != null) {
                        throw e.getCause();
                    }
                    throw e;
                }
            }
        };
        txnHelper.doInTransaction(callback);
    } catch (EvaluationException e) {
        Throwable err = e.getCause();
        if (err == null) {
            logger.error("Failed to execute method " + expression + ": " + e.getMessage(), e);
            throw e;
        } else {
            logger.error("Failed to execute method " + expression + ": " + err.getMessage(), err);
            if (err instanceof RuntimeException) {
                throw (RuntimeException) err;
            } else {
                throw new AlfrescoRuntimeException("Failed to execute method " + expression + ": " + err.getMessage(), err);
            }
        }
    } catch (RuntimeException err) {
        logger.error("Failed to execute method " + expression + ": " + err.getMessage(), err);
        throw err;
    } catch (Exception err) {
        logger.error("Failed to execute method " + expression + ": " + err.getMessage(), err);
        throw new AlfrescoRuntimeException("Failed to execute method " + expression + ": " + err.getMessage(), err);
    }
    // force the output back to the client
    writer.close();
}
Also used : RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) Method(java.lang.reflect.Method) EvaluationException(javax.faces.el.EvaluationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletException(javax.servlet.ServletException) EvaluationException(javax.faces.el.EvaluationException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ResponseWriter(javax.faces.context.ResponseWriter) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) VariableResolver(javax.faces.el.VariableResolver)

Example 2 with EvaluationException

use of javax.faces.el.EvaluationException in project acs-community-packaging by Alfresco.

the class FacesHelper method getManagedBean.

/**
 * Return a JSF managed bean reference.
 *
 * @param fc      FacesContext
 * @param name    Name of the managed bean to return
 *
 * @return the managed bean or null if not found
 */
public static Object getManagedBean(FacesContext fc, String name) {
    Object obj = null;
    try {
        ValueBinding vb = fc.getApplication().createValueBinding("#{" + name + "}");
        obj = vb.getValue(fc);
    } catch (EvaluationException ee) {
        // not much we can do here, just make sure return is null
        if (logger.isDebugEnabled())
            logger.debug("Failed to resolve managed bean: " + name, ee);
        obj = null;
    }
    return obj;
}
Also used : ValueBinding(javax.faces.el.ValueBinding) EvaluationException(javax.faces.el.EvaluationException)

Aggregations

EvaluationException (javax.faces.el.EvaluationException)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ResponseWriter (javax.faces.context.ResponseWriter)1 ValueBinding (javax.faces.el.ValueBinding)1 VariableResolver (javax.faces.el.VariableResolver)1 ServletException (javax.servlet.ServletException)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)1 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)1