Oracle8i Application Developer's Guide - XML
Release 3 (8.1.7)

Part Number A86030-01





Go to previous page Go to beginning of chapter Go to next page

Personalizing Data Display With XML: Portal-to-Go , 11 of 16

Sample Adapter Classes

The following two code examples show how Adapters are implemented in Java. Study them to learn how Adapters work. You can modify them to create your own Adapters for custom content sources.

Portal-to-Go Adapter Example 1: Converts Stock Quotes and Headlines to XML

Consider an Adapter class that must implement several methods. The key method is invoke. Master Service calls invoke every time a client makes a request. This example illustrates the Adapter class invoke method that generates the XML result for the quote and headlines page shown in Table 7-1.

public class StockQuoteAdapter implements Adapter {
  public Element invoke (ServiceRequest sr)
    throws AdapterException {
      Element result = XML.makeElement("SimpleResult");
      result.setAttribute("title", "Stock Data");

      Element quote = XML.makeElement("SimpleText");
      quote.setAttribute("title", "Quote");

      Element tickerSymbol = XML.makeElement("SimpleTextItem");
      tickerSymbol.setAttribute ("title", "Ticker");      
      String t = sr.getArguments().getInputValue("Ticker");
      Text ticker = XML.makeText(t);

      Element stockPrice = XML.makeElement("SimpleTextItem");
      tickerSymbol.setAttribute ("title", "Price");      
      String p = sr.getArguments().getInputValue("Price");
      Text price = XML.makeText(p);

      Element headlines = XML.makeElement("SimpleText");
      headlines.setAttribute ("title", "Headlines");
      Element headline = XML.makeElement("SimpleTextItem");
      int i = 0;
      String argBase = "headline";
      String h = "";
      while (h != null) {
         h = sr.getArguments().getInputValue(argBase + i);
         headline.setAttribute("name", argbase + i);
         Text headText = XML.makeText(h);
      return result;

Portal-to-Go Adapter Example 2: Greets Users by Name

Consider a simple Adapter for a service that greets users by name. It has the following inputs:

Example 2's Adapter uses the invoke method to build a Simple Result document using methods in the following packages:

The invoke method performs the following tasks:

  1. Creates the root result element

  2. Creates a SimpleText element. Sets its title attribute, and appends the element to the root element. As defined in the Simple Result DTD, a SimpleTextItem is a required child element of SimpleText.

  3. Retrieves the input parameter value, appends it to the result document

  4. Returns the result

Here is the Adapter implementation:

import org.w3c.dom.Element;
import org.w3c.dom.Text;
import oracle.panama.Argument;
import oracle.panama.Arguments;
import oracle.panama.ServiceRequest;
import oracle.panama.adapter.Adapter;
import oracle.panama.adapter.AdapterDefinition;
import oracle.panama.adapter.AdapterException;
import oracle.panama.adapter.AdapterHelper;

public class HelloAdapter implements Adapter {
   private boolean initialized = false;
   private String greeting = "Hello";
   public static final String GREETING = "greeting";
   public static final String NAME = "name";

   // Called once, when the adapter is instantiated.
   public void init (Arguments args) throws AdapterException {
      synchronized (this) {
         if(!initialized) {
            initialized = true;
            greeting = args.getInputValue( GREETING );
  public Element invoke (ServiceRequest sr)
                           throws AdapterException {
      Element result = XML.makeElement("SimpleResult");
      Element st = XML.makeElement("SimpleText");
      st.setAttribute ("title", 
                   "Oracle Portal-to-Go Server HelloAdapter Sample");
      result.appendChild (st);
      Element sti = XML.makeElement("SimpleTextItem");
      sti.setAttribute ("name", "message");
      sti.setAttribute ("title", "Portal-to-Go says:");
      st.appendChild (sti);
      // ServiceRequest sr contains input parameters (like NAME, below).
      String name = sr.getArguments().getInputValue(NAME);
      Text txt = XML.makeText( greeting + " " + name + "!");
      sti.appendChild (txt);
      return result;
   // This method enables master services to determine
   // the initialization parameters used by the adapter.
   private AdapterDefinition initDef = null;
   public AdapterDefinition getInitDefinition() {
      if (initDef == null) {
         synchronized (this) {
            if (initDef == null) {
               initDef = AdapterHelper.createAdapterDefinition();
               initDef.createInit( Argument.SINGLE_LINE, 
                                   "Greeting phrase", 
                                   null );
      return initDef;
   // This method defines the adapter’s runtime input parameters.
   private AdapterDefinition adpDef = null;
   public AdapterDefinition getAdapterDefinition() throws AdapterException {
      if (adpDef == null ) {
         synchronized (this) {
            if (adpDef == null) {
                if (initDef == null)
                   throw new AdapterException ("Adapter is 
                                   not properly initialized");
                adpDef = initDef;
                adpDef.createInput( Argument.SINGLE_LINE, 
                                    "Name to greet", 
                                    null );
      return adpDef;

When invoked with an input parameter of "Dolly", the above Adapter returns the following XML result:

   <SimpleText title="Oracle Portal-to-Go Server Hello Sample">
      <SimpleTextItem name="message" title="Portal-to-Go says:">
         Hello Dolly!

Go to previous page Go to beginning of chapter Go to next page
Copyright © 1996-2000, Oracle Corporation.

All Rights Reserved.