Saturday, January 21, 2012

How-to: Accessing EL expressions from java code

If you need evaluate EL expression directly from backin bean code, you can do it in three steps:
 
//get current EL context
javax.el.ELContext elContext = FacesContext.getCurrentInstance().getELContext();
 
//get the expression factory (for JSF or ADF).
javax.el.ExpressionFactory expressionFactory = 
FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
 
//Create value expression as the EL I'm evaluating is a value e.g. #{bean.property}. 
//Create MethodExpression if the EL is a method e.g. #{bean.method()}
javax.el.ValueExpression valueExpression = 
expressionFactory.createValueExpression(elContext, "#{your.expression}, 
WhateverYouAreExpecting.class);
 
// get value and dont' forget to cast.
 
But if you're a little lazy, you can use JSFUtil class, which provides various utility methods that are handy to have around when working with JSF EL. The class is available to download from https://sourceforge.net/projects/wshfaces (likaonJsfUtils.jar).

The class provides methods such as:
  • public static Object getValueEl(String el) (Usage example: JSFUtil.getValueEl("#{bean.property}")
  • Programmatic invocation of a method that an EL evaluates to: public static Object invokeEL(String el)
  •  Method which sets a value into an EL object: public static void setValueEl(String el, Object val)
  •  Function which returns managed bean object: public static Object getManagedBean(String beanName), usage example: JSFUtil.getManagedBean("#{SessionBean1}")
  • and so on:)

Friday, January 20, 2012

How to: ADFc : impossible to call directly the task flow '/WEB-INF/flows/ my-flow-def.xml #my-flow-def' with the help of URL

If  you can’t call directly your task flow with the URL, and JDeveloper reports error: <SecurityUtils><invokeURLAllowed> ADFc : impossible to call directly the task flow '/WEB-INF/flows/ my-flow-def.xml #my-flow-def' with the help of UR, check if URL Invoke property of the taskflow is set to url-invoke-allowed, as shown bellow:



URL Invoke taskflow property was introduced in last ADF/JDeveloper release or patchset.

ADF: Oracle ADF XML Files

ADF framework bases on a huge amount of XML configuration files. In my opinion it isn't nice idea, but I mus live with it.
Short and unfortunatelly not detailed reference "Oracle ADF XML Files" you can find here: http://docs.oracle.com/cd/E23549_01/web.1111/b31974/appendixa.htm.

Wednesday, January 18, 2012

How-to: "Servlet access to JSF context" or "Accessing a JSF managedBean from a Servlet"

There have been several posts asking how to get a Faces Context or Faces managed bean from outside of JSF such as in a servlet.

After obtaining Faces Context from servlet or filter, you can:
  • access managed beans in application or session scope from your custom servlet or servlet filter
  • get or set the current view id
  • perform authentication and session management
  • generate additional content to be displayed in a faces component
  • and so on
There is in the web many better and worse solutions of this dilema. My approach bases on my custom JSF util library, which is available to download from SourceForge. LikaonJsfUtil.jar provides functionalities we can use to solve our issue.
Class AbstractFacesServlet provides extra methods to establish communication between our custom server and Faces Controller:


protected javax.faces.context.FacesContext getFacesContext(javax.servlet.http.HttpServletRequest request, 

javax.servlet.http.HttpServletResponse response);
 

protected javax.faces.application.Application getApplication(javax.faces.context.FacesContext ctx);
 

protected java.lang.Object getManagedBean(java.lang.String beanName, javax.faces.context.FacesContext ctx);

The AbstractServletRequestWrapper class we can use in our filters also provides simillar methods.

How to use cited classes:

public class FileUploadServlet extends AbstractFacesServlet {
    private static final String CONTENT_TYPE = "text/html; charset=UTF-8";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {
        /* Bean ApplicationBean1 is application scope bean registered in faces-config .xml or via annotation*/
        ApplicationBean1 myBean = (ApplicationBean1) this.getManagedBean("ApplicationBean1", this.getFacesContext(request, response));
        /* and so on */
    }
}


Enjoy:)

JDeveloper: JPS-01520: Cannot initialize identity store.

Error JPS-01520: Cannot initialize identity store
at oracle.adf.share.security.providers.jps.JpsUtil.getDefaultIdentityStore(JpsUtil.java:339)
at oracle.adf.share.security.providers.jps.JpsIdentityManagementProvider.initialize(JpsIdentityManagementProvider.java:90)
at oracle.adf.share.security.providers.jps.JpsIdentityManagementProvider.<init>(JpsIdentityManagementProvider.java:69)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)


was reported from JDeveloper version 11.1.1.3, but till now (11.1.1.5) this is not solved.

After some investigation I found simple solution. The DefaultDomain configuration file config.xml (in my Windows in C:\Users\mariusz\AppData\Roaming\JDeveloper\system11.1.1.5.37.60.13\DefaultDomain\config\config.xml) should be modified. I put "localhost" in "<listen-address></listen-address>" section.

Config file before modification:

and after modification



After modification domain restart is needed?