Sun Java System Portal Server 6 2004Q2 Migration Guide |
Appendix G
Example of Sun ONE Portal Server 3.0 Provider Migrated to Sun ONE Portal Server 6.x
This appendix provides an example of a Sun ONE Portal Server 3.0 provider migrated to run on a Sun ONE Portal Server 6.x implementation.
Code Example G-1 shows a code sample of a quotations provider implemented for the Sun ONE Portal Server 3.0 product.
Code Example G-1 shows a code sample of a quotations provider implemented for the Sun ONE Portal Server 6.x product.
Code Example G-2 Code Sample of a Quotations provider for Sun ONE Portal Server 6.x.
// QuotationProvider.java
// This is a sample desktop provider application used to display
// famous/humorous quotations on the desktop.
//
// It shows how to use the BaseProvider API to:
// - display html content on the desktop
// - allow the user to edit attributes
//
// Where the QuotationProvider.class file can be found.
package custom;
// Use some basic Java classes
import java.lang.*;
import java.io.*;
import java.util.*;
import java.net.*;
// Use some basic Java Servlet classes
import javax.servlet.*;
import javax.servlet.http.*;
// Use some iPS classes
import com.sun.portal.providers.*;
// The QuotationProvider Class
public class QuotationProvider extends ProfileProviderAdapter {
// The constructor-don’t need to do any special initialization
// so just invoke the ProfileProviderAdapter’s constructor
public QuotationProvider() {
super();
}
// Generate the HTML that will be displayed in this content
// provider’s area on the desktop
public StringBuffer getContent(HttpServletRequest req,
HttpServletResponse res) throws ProviderException {
StringBuffer content = new StringBuffer();
// Contains the HTML to be displayed
Hashtable quotesHash = new Hashtable();
// A Hash of quote vectors,
// one vector per category
BufferedReader quotesReader;
// Used to read the quotes
String quote; // A single quotation
Random randomGenerator = new Random();
// To randomly pick quotes
// Read in the quotations into a Hashtable of Vectors,
// each containg the quotations of a specific category.
// If we can’t read the quotations file it’s okay - we
// just won’t display any quotes later.
// The category and quotation are separated by a "|"
// in the quotations file.
try {
String quotationFileName = getStringProperty("fileLocation");
quotesReader = new BufferedReader(new FileReader(new File(quotationFileName)));
while ((quote = quotesReader.readLine()) != null) {
String type = quote.substring(0,quote.indexOf(’|’));
if (!quotesHash.containsKey(type)) {
quotesHash.put(type,new Vector());
}
((Vector)(quotesHash.get(type))).addElement(quote.substring(quote.indexOf(’|’ )+1));
}
} catch (Exception e) {
}
// Determine is user’s preference is to display the category
// along with a quotation.
boolean displayCategories = getBooleanProperty("displayCategories");
// Get the possible quotation categories.
List categoriesSel =
getListProperty("selectedCategories");
// Print a quotation for each category selected by the user.
for (int i = 0; i < categoriesSel.size(); i++) {
String category = (String)categoriesSel.get(i);
// If the user wanted to display the category along with
// the quotation then do so.
if (displayCategories) {
content.append("<B><I>");
content.append(category);
content.append("</B></I>");
}
// If there are any quotations for this category, pick a random
// one and display it, otherwise note that we didn’t find one.
if (quotesHash.containsKey(category)) {
Vector quoteVector = (Vector)quotesHash.get(category);
int whichQuoteIndex = Math.abs(randomGenerator.nextInt())%(quoteVector).size();
content.append("<UL>");
content.append("<LI>");
content.append((String)(quoteVector.elementAt(whichQuoteIndex)));
content.append("</LI>");
content.append("</UL>");
} else {
content.append("<UL>");
content.append("<LI>");
content.append("(no quotes of this type found)");
content.append("</LI>");
content.append("</UL>");
}
}
// Now return the HTML we have constructed.
return content;
}
// Generate the HTML that will be displayed to let the user set
// his/her preferneces.
public StringBuffer getEdit(HttpServletRequest req,
HttpServletResponse res) throws ProviderException {
// Contains the HTML to be displayed
StringBuffer content = new StringBuffer();
// Display things in a single column table - so things line up.
content.append("<P><TABLE>");
// Let the user choose which categories s/he wants.
content.append("<TR>");
content.append("<TD VALIGN='top' HALIGN='left'>");
content.append("Types of Quotes to Display");
content.append("</TD>");
// Get the user’s selected categories
List selectedCategories = getListProperty("selectedCategories");
// List the possible categories, marking those that have already
// been selected by the user.
content.append("<TD VALIGN='top' HALIGN='left'>");
List possibleCategories = getListProperty("possibleCategories")
for (int i = 0; i < possibleCategories.size(); i++) {
String category = (String)possibleCategories.get(i);
content.append("<INPUT TYPE='checkbox' NAME='category-");
content.append(category);
content.append("' VALUE='");
content.append(category);
content.append("'");
if (selectedCategories.contains(category)) {
content.append(" CHECKED");
}
content.append(">");
content.append(category);
content.append("<BR>");
}
content.append("</TD>");
content.append("</TR>");
// Let the user decide if they want to display a category with
// each quotation.
content.append("<TR>");
content.append("<TD VALIGN='top' HALIGN='left'>");
content.append("<INPUT TYPE='checkboX' NAME='display_categories' VALUE='yes'");
if (getBooleanProperty("displayCategories")) {
content.append(" CHECKED");
}
content.append(">Display the Category Along With the Quotation");
content.append("</TD>");
content.append("</TR>");
content.append("</TABLE>");
return content;
}
// Handle the submittal of the edit form.
public URL processEdit(HttpServletRequest req,
HttpServletResponse res) throws ProviderException {
List categorySelections = new ArrayList(); // Categories user picked
boolean displayCategories = false; // Display category too ?
for (Enumeration e = req.getParameterNames(); e.hasMoreElements(); ) {
// Check all the passed parameters and add the selected categories
// as well as check to see if user opted to display categories.
String parameter = (String)e.nextElement();
if (parameter.equals("display_categories")) {
displayCategories = true;
} else if (parameter.startsWith("category-")) {
String []category = (String [])req.getParameterValues(parameter);
categorySelections.add(category[0]);
}
}
// Set the list of selected categories for later storing in the
// profile database.
setListProperty("selectedCategories", categorySelections);
// Set whether the user wants to display categories or not for later
// storing in the profile database.
setBooleanProperty("displayCategories", displayCategories);
return null;
}
}
<Channel name="QuotationChannel" provider="QuotationProvider">
<Properties>
<String name="title" value=" Quotations Provider"/>
<String name="description" value="This channel displays the quotations for selected categories at random."/>
<String name="refreshTime" value="0" advanced="true"/>
<Collection name="possibleCategories">
<String value="Computers"/>
<String value="Freedom"/>
<String value="Science"/>
<String value="Work"/>
</Collection>
<Collection name="selectedCategories">
<String value="Computers"/>
<String value="Freedom"/>
<String value="Science"/>
<String value="Work"/>
</Collection>
<Boolean name="displayCategories" value="true"/>
<String name="fileLocation" value="/var/tmp/quotations"/>
</Properties>
</Channel>
<Provider name="QuotationProvider" class="custom.QuotationProvider">
<Properties>
<String name="title" value=" Quotations Provider"/>
<String name="description" value="This channel displays the quotations for selected categories at random."/>
<Boolean name="isEditable" value="true" advanced="true"/>
<String name="width" value="thick"/>
<String name="editType" value="edit_subset"/>
<String name="refreshTime" value="0" advanced="true"/>
<Collection name="possibleCategories">
<String value="Computers"/>
<String value="Freedom"/>
<String value="Science"/>
<String value="Work"/>
</Collection>
<Collection name="selectedCategories">
<String value="Computers"/>
<String value="Freedom"/>
<String value="Science"/>
<String value="Work"/>
</Collection>
<String name="fontFace1" value="Sans-serif"/>
<String name="productName" value="Sun ONE Portal Server"/>
<Boolean name="displayCategories" value="true"/>
<String name="fileLocation" value="/var/tmp/quotations"/>
</Properties>
</Provider>
Computers|"If you can’t beat your computer at chess, try kickboxing." -Anon.
Computers|"A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and
tequila." -Mitch Ratliffe
Computers|"If the automobile had followed the same development cycle as the computer, a Rolls-Royce would today cost $100, get one million
miles to the gallon, and explode once a year, killing everyone inside." -Robert X Cringely
Computers|"What goes up must come down. Ask any system administrator." -Anon.
Computers|"Perl - The only language that looks the same before and after RSA encryption." -Keith Bostic
Computers|"C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg." -Bjarne
Stroustrup
Computers|"There are two major products that come out of Berkeley: LSD and UNIX. We don’t believe this to be a coincidence." -Jeremy S.
Anderson
Freedom|"Live free or die." -New Hampshire State Motto
Freedom|"Man is free at the moment he wishes to be." -Voltaire
Freedom|"People demand freedom of speech as a compensation for the freedom of thought which they seldom use." -Kierkegaard
Freedom|"The right to be heard does not autmatically include the right to be taken seriously." -Hubert Humphrey
Freedom|"Extremism in the defense of liberty is no vice. And moderation in the pursuit of justice is no virtue." -Barry Goldwater
Freedom|"I disapprove of what you say, but I will defend to the death your right to say it." -Voltaire
Science|"Give me a lever long enough and a fulcrum on which to place it, and I shall move the world." -Archimedes, Pappus of Alexandria
Science|"A scientific truth does not triumph by convincing its opponents and making them see the light, but rather because its opponents
eventually die and a new generation grows up that is familiar with it." -Maxwell Planck
Science|"Every great advance in science has issued from a new audacity of imagination." -John Dewey, The Quest for Certainty
Stupid|"Caution: Cape does not enable user to fly." -Batman Costume warning label
Stupid|"Your food stamps will be stopped effective March 1992 because we received notice that you passed away. May God bless you. You may
reapply if there is a change in your circumstances." -Department of Social Services, Greenville, South Carolina
Stupid|"We’re going to turn this team around 360 degrees." -Jason Kidd
Stupid|"Traditionally, most of Australia’s imports come from overseas." -Keppel Enderbery
Stupid|"If you’re killed, you’ve lost a very important part of your life." -Brooke Shields
Stupid|"We don’t necessarily discriminate. We simply exclude certain types of people." -Colonel Gerald Wellman, ROTC Instructor
Stupid|"Men, I want you just thinking of one word all season. One word and one word only: Super Bowl." -Bill Peterson, football coach
Stupid|"I have opinions of my own -- strong opinions --but I don’t always agree with them." -George Bush, US President