Pages

Thursday, February 27, 2014

Why use @PostConstruct ?

http://docs.oracle.com/javaee/5/api/javax/annotation/PostConstruct.html
http://stackoverflow.com/questions/3406555/why-use-postconstruct

@PostConstruct (or post-construct, if declared in deployment descriptors) was introduced in Java EE 5 to all component types, including all web components (Servlet 2.5).


- The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.This method MUST be invoked before the class is put into service. This annotation MUST be supported on all classes that support dependency injection. The method annotated with PostConstruct MUST be invoked even if the class does not request any resources to be injected.Only one method can be annotated with this annotation.

*** The method on which the PostConstruct annotation is applied MUST fulfill all of the following criteria 

- The method MUST NOT have any parameters except in the case of EJB interceptors in which case it takes an InvocationC ontext object as defined by the EJB specification. 
- The return type of the method MUST be void.
- The method MUST NOT throw a checked exception.
- The method on which PostConstruct is applied MAY be public, protected, package private or private
- The method MUST NOT be static except for the application client
- The method MAY be final. - If the method throws an unchecked exception the class MUST NOT be put into service except in the case of EJBs where the EJB can handle exceptions and even recover from them.

Why use @PostConstruct ?

  • because when the constructor is called, the bean is not yet initialized - i.e. no dependencies are injected. In the @PostConstruct method the bean is fully initialized and you can use the dependencies.
  • because this is the contract that guarantees that this method will be invoked only once in the bean lifecycle. It may happen (though unlikely) that a bean is instantiated multiple times by the container in its internal working, but it guarantees that @PostConstruct will be invoked only once.

in JSF context : use @PostConstruct for @ManagedBean

http://stackoverflow.com/questions/15773350/initialization-of-list-in-a-jsf-managed-bean?lq=1
    There are implications regarding the scope of the managed bean
  1. @RequestScoped: In a managed bean with this annotation, the method will be called per submit of the JSF view concerned. A @RequestScoped bean is destroyed and recreated with every request, The implication of this is that depending on your setup, the list initialized in the @PostConstructmay be reset to empty or default values during each request. Under certain circumstances, conversion errors may occur as a result of the re-initialization of the list mid-JSF request.
  2. @ViewScoped: In a managed bean with this annotation, you're guaranteed to have the @PostConstruct method run once, if and only if you're dealing with the same instance of the @ViewScoped bean. If the viewscoped bean is destroyed and recreated, the @PostConstructmethod will run again.
  3. @SessionScoped: A bean with this annotation is created once and stays alive until the user's HTTP session ends. In this scenario, the @PostConstruct method is guaranteed to run once and only once until the bean is destroyed

Servlet init method vs PostConstruct method

http://javahowto.blogspot.com/2011/07/servlet-init-method-vs-postconstruct.html
invocation order and relationship of the two methods:
servlet class constructor --> PostConstruct --> init and init(ServletConfig)
So inside PostConstruct method at the second step, ServletConfig has not been initialized, and neither is ServletContext. CallinggetServletConfig() in PostConstruct returns null. Calling getServletContext() results in IllegalStateException in most servers (GlassFish 3, Tomcat 7 and JBoss 6), but returns null in Resin 4.
eg :
public class DpartmentBean {
    // service and resource inject
    @EJB
    HrSessionFacadeLocal hrService;
    // ui comp
    private RichTable departmentTable;
    public DpartmentBean() {
        this.departmentTable = new RichTable();
    }
             /**
                load data for table  when hrService injected
            */
    @PostConstruct
    public void initDepartmentTable() {
        if (hrService != null) {
            try {
                List<Departments> departments = hrService.getDepartmentsFindAll();
                this.departmentTable.setValue(departments);
            } catch (Exception e) {
                // TODO: Add catch code
                e.printStackTrace();
            }
        }
    }
}

Saturday, February 15, 2014

Invoke a method from Managed Bean when JSPX/JSFF (JSF fragment) Page Loads in ADF.


link refer
http://www.techartifact.com/blogs/2012/10/invoke-a-method-from-managed-bean-when-jspx-page-loads-in-adf.html
 implements PagePhaseListener
 public void beforePhase(PagePhaseEvent pagePhaseEvent) {
if (pagePhaseEvent.getPhaseId() == Lifecycle.PREPARE_MODEL_ID) {
onPageLoad();
}
}

public void afterPhase(PagePhaseEvent pagePhaseEvent) {
if (pagePhaseEvent.getPhaseId() == Lifecycle.PREPARE_RENDER_ID) {
// onPagePreRender();
}
}

public void onPageLoad() {
if (!AdfFacesContext.getCurrentInstance().isPostback()) {
// add your onPageLoad event here

// to set the View Criteria on the Iterator
doSomeOperation();  ///your custom method.
}

http://www.techartifact.com/blogs/2013/09/call-method-on-page-load-of-jsff-jsf-fragment-in-oracle-adf.html
 implements RegionController
public boolean refreshRegion(RegionContext regionContext)   // you need to override refresh region method.
{
int refreshFlag= regionContext.getRefreshFlag();
FacesContext fctx = FacesContext.getCurrentInstance();
//check internal request parameter
Map requestMap = fctx.getExternalContext().getRequestMap();
PhaseId currentPhase=(PhaseId)requestMap.get("oracle.adfinternal.view.faces.lifecycle.CURRENT_PHASE_ID");
if(currentPhase.getOrdinal()==PhaseId.RENDER_RESPONSE.getOrdinal())   // write custom logic of correct lifecycle phase.
{
Object showPrintableBehavior =
requestMap.get("oracle.adfinternal.view.faces.el.PrintablePage");
if (showPrintableBehavior != null)
{
if (Boolean.TRUE == showPrintableBehavior)
{
ExtendedRenderKitService erks = null;
erks =
Service.getRenderKitService(fctx, ExtendedRenderKitService.class);
//invoke JavaScript from the server
erks.addScript(fctx, "window.print();");
erks.addScript(fctx, "window.close();");

}
  }
regionContext.getRegionBinding().refresh(refreshFlag);
}
return false;
}
     
    public boolean validateRegion(RegionContext regionContext)
    {
        regionContext.getRegionBinding().validate();
        return false;
    }
     
    public boolean isRegionViewable(RegionContext regionContext)
    {
     return regionContext.getRegionBinding().isViewable();
    }
     
public String getName()
{
return null;


Wednesday, February 12, 2014

UCM - checkin without and hiding primary file

Doing check-ins without files in UCM
https://blogs.oracle.com/kyle/entry/check-ins_without_files_ucm

Hiding the Primary File field in UCM
https://blogs.oracle.com/kyle/entry/hiding_the_primary_file_field