Pages

Sunday, January 23, 2011

Some handy code for your managed Beans ( ADF & JSF )

Back in 2009, I already a made a blogpost about some handy code which you can use in your ADF Web Application. You can say this blogspot is part 2 and here I will show you the code, I use most  in my own managed Beans.

I start with FacesContext class, with this class you can use to find a JSF Component, change the Locale, get the ELContext, Add a message to your view and get the ExternalContext
// FacesContext
FacesContext facesCtx = FacesContext.getCurrentInstance();
// find UIComponent
UIComponent input = facesCtx.getViewRoot().findComponent("f1");
// change the locale
facesCtx.getViewRoot().setLocale( Locale.ENGLISH);
// el expression
Application app = facesCtx.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesCtx.getELContext();
ValueExpression valueExp = elFactory.createValueExpression(elContext,
"#{xxxx}",
Object.class);
Object result = valueExp.getValue(elContext);
// add a message
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN,
"header",
"detail");
facesCtx.addMessage(input.getClientId(facesCtx), msg);
// ExternalContext
ExternalContext ectx = facesCtx.getExternalContext();
The ExternalContext class, with this you can retrieve all the java init & context (web.xml) parameters, the Request & Session parameters and your web application url.
// ExternalContext
ExternalContext ectx = facesCtx.getExternalContext();
// all the java init parameters
Map<String, Object> initParamsVar = ectx.getInitParameterMap();
Map<String, String> requestParamsVar = ectx.getRequestParameterMap();
Map<String, Object> sessionParamsVar = ectx.getSessionMap();
// web application context root
String contextPath = ectx.getRequestContextPath();
AdfFacesContext class, you can use this class for Partial Page Rendering ( PPR), get the PageFlowScope and ViewScope variables
// AdfFacesContext
AdfFacesContext adfFacesCtx = AdfFacesContext.getCurrentInstance();
// PPR
adfFacesCtx.addPartialTarget(input);
// get the PageFlowScope Params
Map<String, Object> scopePageFlowScopeVar= adfFacesCtx.getPageFlowScope();
// get the viewScope Params
Map<String, Object> scopeViewScopeVar= adfFacesCtx.getViewScope();
ADFContext class, with this you can get all the memory scopes variables even the application scope variables, ELContext and the SecurityContext.
// ADFContext
ADFContext adfCtx = ADFContext.getCurrent();
// Get the scope variables
Map<String, Object> applicationVar2 = adfCtx.getApplicationScope();
Map<String, Object> pageParamsVar2 = adfCtx.getPageFlowScope();
Map<String, String> requestParamsVar2 = adfCtx.getRequestScope();
Map<String, Object> sessionParamsVar2 = adfCtx.getSessionScope();
// el expression
ELContext elContext2 = adfCtx.getELContext();
ExpressionFactory elFactory2 = adfCtx.getExpressionFactory();
ValueExpression valueExp2 = elFactory2.createValueExpression(elContext2,
"#{xxxx}",
Object.class);
Object result2 = valueExp2.getValue(elContext2);
// Security
SecurityContext secCntx = adfCtx.getSecurityContext();
view raw ADFContext.java hosted with ❤ by GitHub
SecurityContext class, retrieve the current user and its roles.
// Security
SecurityContext secCntx = adfCtx.getSecurityContext();
String user = secCntx.getUserName();
String[] roles = secCntx.getUserRoles();
BindingContext, BindingContainer and DCBindingContainer class. These classes are well known when you want to retrieve the ADF pagedef objects.
BindingContext bc = BindingContext.getCurrent();
BindingContainer bcon = bc.getCurrentBindingsEntry();
List<AttributeBinding> attr = bcon.getAttributeBindings();
List<OperationBinding> oper = bcon.getOperationBindings();
List<ControlBinding> ctrl = bcon.getControlBindings();
DCBindingContainer dcbcon = (DCBindingContainer) bc.getCurrentBindingsEntry();
List<AttributeBinding> attr2 = dcbcon.getAttributeBindings();
List<OperationBinding> oper2 = dcbcon.getOperationBindings();
List<ControlBinding> ctrl2 = dcbcon.getControlBindings();
List iters = dcbcon.getIterBindingList();
List exec = dcbcon.getExecutableBindings();
The last class is ControllerContext, which you can use to retrieve the exceptions
ControllerContext cc = ControllerContext.getInstance();
// get the exception
Exception exp = cc.getCurrentViewPort().getExceptionData();

Thursday, January 20, 2011

Get the PageFlowScope of a Region Bounded Task Flow

Sometimes you need to access the PageFlowScope of a Task Flow Region( child Bounded Task Flow ) and get a handle to a pageFlowScope managed Bean. Normally you don't need to do this and Oracle don't want you, to do this. To make this work you need three internal classes so there is no guarantee that it works in 11g R1 PS4 or higher, but it work in PS2 & PS3.

Basically this is what you need to do.

  • Get the Task Flow binding of the region in the page definition, you need to have the full name
  • Get the RootViewPortContext
  • Find the ChildViewPortContext , use the TaskFlow full name
  • Get the pageFlowScope Map of the ChildViewPortContext 

Here some demo code.

package test.adf.global.beans;
import javax.faces.event.ActionEvent;
import java.util.Map;
import oracle.adf.controller.ControllerContext;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.view.rich.context.AdfFacesContext;
import oracle.adf.controller.internal.binding.DCTaskFlowBinding;
import oracle.adfinternal.controller.state.ChildViewPortContextImpl;
import oracle.adfinternal.controller.state.RootViewPortContextImpl;
import test.adf.global.interfaces.BeanInt;
public class MainBean {
public MainBean() {
}
private String name = "main";
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
// dump current Task Flow pageFlowScope
public void dumpPageFlowScope(ActionEvent actionEvent) {
AdfFacesContext facesCtx= null;
facesCtx= AdfFacesContext.getCurrentInstance();
Map<String, Object> scopeVar= facesCtx.getPageFlowScope();
for ( String key : scopeVar.keySet() ) {
System.out.println("key: "+key);
System.out.println("value: "+scopeVar.get(key));
}
}
// dump the child Task Flow pageFlowScope
public void dumpChildPageFlowScope(ActionEvent actionEvent) {
// get the current BindingContainer
BindingContext bctx = BindingContext.getCurrent();
DCBindingContainer mainViewPageBinding = (DCBindingContainer)
bctx.getCurrentBindingsEntry();
// find the task flow pagedef binding, see the pageDef
DCTaskFlowBinding tf = (DCTaskFlowBinding)
mainViewPageBinding.findExecutableBinding("child1");
System.out.println(tf.getFullName());
ControllerContext conn = ControllerContext.getInstance();
RootViewPortContextImpl rootViewPort =
(RootViewPortContextImpl) conn.getCurrentRootViewPort();
ChildViewPortContextImpl childView = (ChildViewPortContextImpl)
rootViewPort.getChildViewPortByClientId(tf.getFullName());
// get pageFlowScope
Map<String, Object> scopeVar= childView.getPageFlowScopeMap();
for ( String key : scopeVar.keySet() ) {
System.out.println("key: "+key);
System.out.println("value: "+scopeVar.get(key));
}
BeanInt bean = (BeanInt)scopeVar.get("childBean");
System.out.println(bean.getName());
}
}
view raw MainBean.java hosted with ❤ by GitHub
Here you can download the demo workspace

Sunday, January 16, 2011

ADF JMX Datacontrol in FMW 11g PS3

One of the new Fusion MiddleWare PS3 ADF features is the JMX Datac Control. Back in late 2007 I already blogged about it and now 3 years later, Oracle finally released this feature in Patch Set 3. With the JMX Data Control you can manage your MBeans, read its attributes or invoke its operations. When an attribute gives back an MBean reference then ADF will look up this reference and returns this object. ( This is not the case in the System MBean browser of the Enterprise Manager application. )
I don't think this feature will be used in enduser applications, but can be very useful in support applications so the administrator can control the FMW servers. In this blogpost I will show you how you can use the JMX Data Control to give you a WebLogic Domain overview with its servers and application deployments.

First you need to create a JMX connection.

 Provide the WebLogic connection information
 After the JMX Connection creation, you can add the JMX Data Control to your application
 Select the JMX Connection
Select the MBeans, ( The EM System MBean browser can be handy to find the right MBean ), I will use the domain MBean located in com.bea.Domain
In the Data Controls window you can see my Domain MBean with its attributes and accessors.

 First I drag my Domain MBean to the page and select the attributes I want to show.
I also want to show you the WebLogic servers as child of the Domain ( in table layout ), the same for application deployments. So drag the Servers accessor on the page and select the attributes you want to show.
 With this as result.
Be aware, the JMX Data Control does not  know every Java Object, so if you want to use this on the SOA Suite MBeans it will give you some errors. It can't find the XML presentation of these SOA Java Object.