Java Studio Creator Portlets: Under the Hood #2 - PortletConfig

This is another in the series of looking under the hood of the portlet development feature for Java Studio Creator 2.

There were many enhancements between Creator 2 Early Access 2 and the final release in regard to portlet development. One such enhancement was to get closer to the goal of fully supporting JSR-168.

Background

In the first article in this series, I discussed how to get portlet preferences. This blog will show the portlet developer how to get “PortletConfig” and ultimately portlet initialization parameters (init-param). Again, let's refer to JSR-168 to see what it says about PortletConfig and portlet init-params. JSR-168 says,

“PLT.6.1 Initialization Parameters - The getInitParameterNames and getInitParameter methods of the PortletConfig interface return the initialization parameter names and values found in the portlet definition in the deployment descriptor. ”

According to JSR-168, the only way to get portlet initialization parameters is to go through the “PortletConfig” interface.

JSR-168 also states that the only way to get PortletConfig is through the “init” life cycle method.

”PLT.5.2.2 Initialization - The portlet container must initialize the portlet object by calling the init method of the Portlet interface with a unique (per portlet definition) object implementing the PortletConfig interface.”

Here's the major problem. Creator uses JavaServer Faces and thus the JavaServer Faces portlet adapter which implements the life cycle methods. This is a shortcoming in JSR-168 because the specification doesn't provide a standard way for a framework, like JavaServer Faces, to provide the PortletConfig to applications leveraging the framework. You'll see below how we decided to expose the PortletConfig through the JavaServer Faces portlet adapter.

I'd like to give a special thanks to Deepak Gothe, the Sun engineer responsible for implementing the PortletConfig exposure in the JavaServer Faces portlet adapter. Deepak accommodated this change just before we pushed out the final release of Creator 2.

One more background issue I should discuss. How does the JavaServer Faces portlet adapter compare with the open source version on java.net? The JavaServer Faces portlet adapter used in Creator 2 has many enhancements to support more of JSR-168 than the version on java.net like supporting modes, PortletConfig, etc. I'm not sure yet whether or not the version used by Creator 2 will be made available on java.net. Hopefully I'll have more info on that later.

Also, keep in mind that JSR-168 mentions two sets of initialization parameters, one set is at the portlet application scope and the other is at the portlet scope. The portlet application level init-params can be obtained through PortletContext. These are the init-params defined in the web.xml file. We will focus on the portlet init-params defined in the portlet.xml file.

Details

First, we must get to the JavaServer Faces ExternalContext. To do this you can either use the following code in the page bean.

        FacesContext.getCurrentInstance().getExternalContext();
Or you can go through the session bean like the following.
        getExternalContext();
Here's an example of how to access the portlet init-param “test” from the page-bean.
         Map applicationMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();
        if(null != applicationMap) {
            PortletConfig config = (PortletConfig)applicationMap.get("javax.portlet.PortletConfig");
            this.txtParam1.setText(config.getInitParameter("test"));
        }

You'll see that the key is the property name “javax.portlet.PortletConfig”. This property is stored in the application map of the external context. To test this in Creator, from the project node, right-click and choose, “Edit Portlet Deployment Descriptor”. You'll see a dialog like the following.

Add an init-param with a name of “test” and a value of “Test Value”. Add a static text field to a portlet page and a button. Double-click on the button and add the source above that goes in the action method of the page bean like this.

    public String button1_action() {
        /\*\*
         \* Get the portlet init-param
         \*/
        Map applicationMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();
        if(null != applicationMap) {
            PortletConfig config = (PortletConfig)applicationMap.get("javax.portlet.PortletConfig");
            this.txtParam1.setText(config.getInitParameter("test"));
        }       
        return null;
    }

When you run the portlet and press the button you should see the static text field with the value “Test Value”.

Cheers!

-David

Comments:

Post a Comment:
Comments are closed for this entry.
About

David Botterill

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today