Thursday, July 3, 2014

How to get access to Application Module Impl in Managed Bean

Welcome to my ADF blog. This is my first blog post on ADF so I wanted to start out with basics which is a simple blog post on ADF but often a handy code snippet when you are writing your backing bean or managed bean logic.

Sometimes there are scenarios where we want to get a handle to ApplicationModuleImpl class in the backing bean code itself. One reason why one would want to do this is to call framework methods of AMImpl class in the backing bean code. For example, to get a state of the DB connection being requested or released. Many such instances can be thought of where accessing ApplicationModuleImpl in managed bean or backing bean becomes necessary.
If so, this is how it can be done:


// 1. Access the binding context
// 2. Find datacontrol by name
// 3. Access the data control's application module data provider
FacesContext fctx = FacesContext.getCurrentInstance(); 
BindingContext bindingContext = BindingContext.getCurrent();
DCDataControl dc = bindingContext.findDataControl("MyModuleDataControl");
MyAMImpl am = (MyAMImpl)dc.getDataProvider();
Once you have an object of type AMImpl then you can call AM specific methods.

Note that it's always a best practice to create a data control binding for AM method by exposing it as a service method and then invoking Operation Binding on the method rather than doing it this way. Look out on my next blog on how to do that. But there are instances where you do want to invoke AM APIs like explained above. It is up to the developer to decide to make use of best coding practices as much as possible to minimize application performance and security issues.

Happy Coding!
public class Test {
  private String str = "My String Test";
  public void MyStr() {
    System.out.println(str);
  }
}