Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Adobe ColdFusion

  • Print
  • + Share This
This chapter is from the book

PSML Reader

The next item on Victor's list was the PSML reader component, which would be responsible for reading a user's PSML file. This component would use Jetspeed's Java classes to return this information to ColdFusion.

JAR Files and Setup

The first part of the process is to make the Jetspeed classes available to ColdFusion. To do this, it is necessary to copy all of the JAR, or Java Archive files, from Jetspeed's {Jetspeed-root}/WEB-INF/lib directory to the {ColdFusion_Root}/wwwroot/WEB-INF/ directory. JAR files are essentially Zip files that contain Java classes and other files and directories. The Jetspeed JAR files contain all of the Java classes used by Jetspeed, and placing them in the /WEB-INF/lib directory will make them available to applications in ColdFusion. ColdFusion Server will need to be restarted to finalize this process.

Building the Components Using Java Objects

The primary objective of this component is to read a user's PSML file and return information from it. As such, the Project Omega team decided the following methods should be included:

  • getUserPSML(). Returns a ColdFusion XML document object for debugging purposes.

  • getTabs(). Project Omega had decided to allow users to have a number of pages within the portal; these were called "tabs," and each tab could contain a number of portlets (the user could choose these). This method returns an array including all of the users' tabs.

  • getPortlets(). Returns a two-dimensional array including all of the users' portlets for each tab.

  • getController(). Returns the controller for each tab.

  • getUserSkin(). Returns the user's skin, which could be further looked up in the skin registry to define the portal design.

  • getLog-in(). Project Omega decided to store the user's log-in (encrypted) for different portlets in the user's PSML file. This method returns the user's user name and password (unencrypted).

The first method was straightforward. XmlParse() was used to create the XML document object, and then this was returned to the user. However, while Jim was developing this method he had an idea and emailed Victor about it:

New component


email

Date: June 10

To: Victor

From: Jim

Subject: New component

Hi Victor—

I was working on PSMLReader.cfc—just getting started, and I thought of something. We are going to need to have the location of the user's PSML file. Based on our review of Jetspeed, that could be complex—especially if we add on any localization or WML features in the future.

What we should do is create a method that will return a directory location if the user name, language, and any other relevant information is passed to it. That way, if we ever change how the PSML directory is structured or used, we can just re-write the one method instead of replacing hardcoded directory locations in multiple templates.

What do you think?

Jim

Figure 3-5 Figure I-3.5 Because the development team used a function to return the directory location of PSML files, future changes in directory structure will only require changing this function.

Now it was time to really get down and dirty with the PSMLReader component. The getTab() method included the use of ColdFusion's J2EE integration features. The start of this tag declares the function and arguments and creates Java objects to use:

<CFFUNCTION ACCESS="PUBLIC"
                            NAME="getTabs"
                            RETURNTYPE="ARRAY">
    <CFARGUMENT NAME="User" REQUIRED="TRUE" TYPE="STRING">
    <CFARGUMENT NAME="Language DEFAULT="EN" TYPE="STRING">
    <CFARGUMENT NAME="Media" DEFAULT="HTML" TYPE="STRING">
    <!--- use Jetspeed object for accessing PSML --->
    <CFOBJECT ACTION="CREATE"
                           TYPE="JAVA"

CLASS="org.apache.jetspeed.xml.api.portletmarkup.Portlets"
                       NAME="PortletObject">
    <!--- read the user's PSML file --->
    <CFINVOKE COMPONENT="portal.components.userinfo"
                           METHOD="getUserPSML"
                           USER="#ARGUMENTS.User#"
                           MEDIA="#ARGUMENTS.Media#"
                           LANGUAGE="#ARGUMENTS.Language#"
                           RETURNVARIABLE="PSMLLocation">
    <!--- get file object to retrieve PSML
             file as java.io.Reader, initialize... --->
    <CFOBJECT ACTION="CREATE"
                           TYPE="JAVA"
                           CLASS="java.io.FileReader"
                           NAME="PSMLFile">
          <CFSET PSMLFile.init("#PSMLLocation#")>
    <CFSET ProcessPortlets =
PortletObject.unmarshal(PSMLFile)>
    <CFSET CurrentPortlets = ProcessPortlets.getPortlets()>

After declaring the function, all of the arguments are declared. Note that the arguments enclosed are primarily to be passed to the getUserPSML() method that will return the location of the user's PSML file. <CFINVOKE> invokes this method, and the result is set to the variable, PSMLLocation.

Next, an object of type org.apache.jetspeed.xml.api.portlet-markup.Portlets is created using <CFOBJECT>. When working with Java objects, this tag requires that the TYPE attribute be set to JAVA and the ACTION attribute set to CREATE. The CLASS attribute specifies the class of the object that will be created, and the NAME attribute specifies the name that will be used to access the object within the page. An instance of this object is created because its methods will need to be accessed later in this function. Many objects will require arguments when they are created. This is the case with the java.io.FileReader object in the next <CFOBJECT> tag. It requires that a String object containing the directory location of the file be passed to it. In such cases, the ColdFusion init() method is used. Here, the <CFSET> tag calls the init() method, passing the argument PSMLLocation output in quotes, as is required of string values in Java. Note that this method is called as if it were a method of the PSMLFile object. Assuming the value of PSMLLocation is "c:\default.psml", the Java equivalence of the above object creation would look like this:

java.io.FileReader PSMLFile = new java.io.FileReader("c:\default.psml"); 

The java.io package has a number of classes within it that are used for inputting and outputting to and from Java programs (io stands for input/output). The FileReader class, specifically, creates a character stream (actually a java.io.Reader object) which contains the contents of the file whose location is passed as an argument. This works much like <CFFILE> does when reading files. So now, the PSML file is imported into the application as this Java object.

Java object methods can be called on directly in the <CFSET> tag. The first of the next two <CFSET> tags creates a variable called ProcessPortlets and sets it to the value returned by PortletObject.unmarshal(PSML-File), which executes the unmarshal() method of the PortletObject object that was created by the first <CFOBJECT> tag. Note that the PSML-File variable is passed to this method. Project Omega knew this was required by looking at the Jetspeed API. Figure I-3.6 shows the Javadoc for this method from the Jetspeed API.

instant message

When Java objects require arguments to be created, the arguments are passed using init() immediately after the <CFOBJECT> tag is used to create the object.

Figure 3-6 Figure I-3.6 The Javadoc from the Jetspeed API shows what is required to call a method and what it will return.

In all Javadoc API documents, a list of methods for any given object will be illustrated in this manner. On the left side of the table is what the object will return, in this case, an array of Portlets objects. This method returns an object of type org.apache.jetspeed.xml.api.portlet-markup.Portlets, also written as Portlets (if referred to from within the same package: org.apache.jetspeed.xml.api.portletmarkup) This item can be clicked on, and the API document for the object type will be brought up. On the right side of this table, the method is listed and within the parenthesis any arguments that must be passed to the method, and the name of that argument. Here, a java.io.Reader object called reader needs to be passed to it.

So, the unmarshal() method uses the java.io.Reader object returned by the FileReader call and returns an object of type Portlets. Because the results are returned to the ProcessPortlets variable, this variable becomes a Java object of this type—hence another way to create Java objects, using Java objects. The Portlets object is used by Jetspeed as an abstraction of the PSML file—much as ColdFusion's XML document object creates an abstraction of XML files. A number of additional methods are available to this object to obtain additional information about all of the items within the <portlets> element of the PSML file.

This can be seen in the next <CFSET> where a new variable, Current-Portlets, is set to the value returned by ProcessPortlets. getPortlets(). The getPortlets() method returns an array of Portlets objects—actually this array will represent the <portlets> objects within the root <portlets> tag, the children. In this application, these represent a tab. So if there are three <portlets> child elements, this method will return an array containing three Portlets objects. In turn, each of these Portlets objects will represent one of the three tabs that will be displayed for a user of the portal.

The rest of the function creates the array of tabs that will ultimately be returned to the calling template:

<CFSET TabNames = ArrayNew(1)>
<CFLOOP FROM="1" TO="#ArrayLen(CurrentPortlets)#"
                   INDEX="i">
   <!---
                   build the array of title values in the metainfo
                   section of XML template. CurrentPortlets[i]
                   returns a Portlets object, which then calls on
                   the getTitle() method which returns a String
                   value to add to the list
   --->
  <CFIF CurrentPortlets[i].getMetaInfo() NEQ "">
       <CFSET ArrayAppend(TabNames,

CurrentPortlets[i].getMetaInfo().getTitle())>
   </CFIF>
      </CFLOOP>
      <CFRETURN TabNames>
</CFFUNCTION>

A new array is created, TabNames, using the ArrayNew() function. Next, a <CFLOOP> tag loops through the CurrentPortlets array of Portlets objects. ColdFusion recognizes that CurrentPortlets is an array, nicely allowing Jim to use array functions on it, specifically ArrayLen() to determine the length of the array.

Within the <CFLOOP>, a <CFIF> checks to see if the current Portlets object returns any value for getMetaInfo(). Because the item at Cur-rentPortlets[i] is an object, methods of that object can be called on directly. The getMetaInfo() returns a new object (called a MetaInfo object), which has methods to return information from within the <meta-info> element of a <portlets> element. If this method returns an empty string, that would signify that there is no <meta-info> element, and this <CFIF> block would be ignored.

If it isn't an empty string, the TabNames array is appended with the results of an interesting method call. Basically, the CurrentPortlets[i] array returns a Portlets object; the getMetaInfo() method of that object is called on which returns a MetaInfo object. Finally, the getTitle() method of that object is called on. This will return a string value containing the title of the current <portlets> element, as represented by an item in the CurrentPortlets array of Portlets.

Jim was a bit overwhelmed at first, but as he developed the other functions for this component—all of which used the same basic principles—it actually turned out to be rather easy. Every element of the PSML file had a corresponding object that represented it. And methods within each of these objects could return information that was embedded within them. For example, when he needed to obtain the name for a portlet entry, he would obtain the appropriate Portlets object, use the getEntry() method to return an Entry object, then use the getName() method of that object to return the portlet name.

  • + Share This
  • 🔖 Save To Your Account