use of javax.faces.el.VariableResolver 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();
}
use of javax.faces.el.VariableResolver in project ma-core-public by infiniteautomation.
the class JsfCreator method getInstance.
/* (non-Javadoc)
* @see org.directwebremoting.Creator#getInstance()
*/
public Object getInstance() throws InstantiationException {
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext == null) {
log.error("Object " + getManagedBeanName() + " cannot be created since the faces context is null");
return null;
}
Application application = facesContext.getApplication();
Object resolvedObject = null;
if (isVBExpression(getManagedBeanName())) {
ValueBinding vb = application.createValueBinding(getManagedBeanName());
if (vb != null) {
resolvedObject = vb.getValue(facesContext);
}
} else {
VariableResolver resolver = application.getVariableResolver();
resolvedObject = resolver.resolveVariable(facesContext, getManagedBeanName());
}
return resolvedObject;
}
Aggregations