Application Techniques



Accessing Data from External Java Clients

How to code an external Java client (outside of the SilverStream development environment) that accesses the SilverStream Server to retrieve and update rows of data.

About this technique

Details

Category

Java Client Techniques> External Java clients

Description

You'll learn about:

Source code

You can get the source code for this technique from:

Related reading

See the chapter on writing external Java clients in the Programmer's Guide

Setting up   Top of page

For this technique, you need:

Coding the client   Top of page

Here's a class that provides a basic implementation of this technique.

  package extjtech; 
   
  import java.io.*; 
  import java.math.*; 
   
  import com.sssw.rt.util.*; 
   
  // The ExtData class shows how you can access a SilverStream 
  // server from an external Java client. It includes examples 
  // of retrieving and updating rows of data from a SilverStream  
  // server object. 
  public class ExtData { 
   
       // Instance variables for the SilverStream objects used  
       // by the ExtData class. 
       AgrServerSession session; 
       AgrData appdata; 
      
    // Constructor for the ExtData class. It does the following: 
    // * Connects to a SilverStream server 
    // * Accesses the appropriate data 
    // * Performs a requested operation on that data (including 
    //   retrieval and/or update)  
    // 
    // It takes 4 arguments: 
    // 1 SilverStream server host 
    //     myserver 
    // 2 SilverStream db & data source object 
    //     mydb:com.myorg.data.dsoMyPassthru 
    // 3 Operation to perform 
    //     costupdate 
    // 4 Value to use in the operation 
    //     2 
    public ExtData(String s3serverhost,   
                   String s3dsolocation, 
                   String operation, 
                   String opvalue) { 
   
      try  { 
        // Initialize the SilverStream runtime environment. Do 
        // this if you need to specify a login handler for the  
        // SilverStream server to call back when it requires user 
        // authentication. (The login handler is a class that you 
        // code and instantiate.) 
        AgRuntime.init(new LoginHandler()); 
   
        // Connect to the appropriate SilverStream server. This 
        // establishes a server session (represented by an instance 
        // of AgrServerSession). 
        // 
        // The following example connects via HTTP. In this case,  
        // you wouldn't use RMI-IIOP (the connectRMI method), 
        // because it doesn't support DSO access. 
        session = AgRuntime.connect(s3serverhost); 
   
   
        // Create an instance of AgrData (to represent the cache of 
        // data you want to work with). 
        appdata = new AgrData(); 
   
        // Initialize the AgrData by passing it the AgrServerSession. 
        appdata.init(session); 
   
        // Specify the data source for the AgrData. In this case,  
        // it is a pass-through DSO. 
        appdata.setDataSource(s3dsolocation); 
   
        // Invoke the DSO to get the data for the AgrData. 
        appdata.invokeQuery(null); 
   
   
        // Perform the requested operation (if supported). 
        if (operation.equals("list")) { 
   
          // Call the listRows() method of the ExtData class to  
          // display the rows that the DSO put in the AgrData.  
          listRows(); 
        } 
        else if (operation.equals("costupdate")) { 
   
          // Call the raiseCost() method of the ExtData class to 
          // get the "Cost per lb" column value in each AgrData  
          // row and update it by a specified percentage.  
          raiseCost(opvalue); 
   
          // For demonstration purposes (to prove that the update  
          // worked), execute a new query against the DSO to  
          // refresh the AgrData with the updated rows from the  
          // data source. (In your own applications, you may want  
          // to keep using the rows you already have cached locally 
          // and avoid extra queries such as this.) 
          appdata.query(null); 
   
          // Call the listRows() method of the ExtData class to  
          // display the updated rows from the AgrData.  
          listRows(); 
        } 
        else { 
          // Notify user if the request can't be performed. 
          System.out.println( 
            "The requested operation:\n   " + 
            operation + "\n" +  
            "is not supported. Please try again."); 
        } 
      } 
      catch (Exception e) { 
        System.out.println("Application error in ExtData"); 
        e.printStackTrace(); 
        System.exit(0); 
      } 
      finally { 
        // When all done, close the SilverStream server session. 
        session.close(); 
      } 
    } 
   
   
    // This method displays all of the rows and columns from 
    // the current cache (AgrData). 
    void listRows() { 
      try { 
        // Go to the first row of the AgrData. 
        boolean found = appdata.gotoFirst(); 
   
        // Loop through the rows of the AgrData. 
        while (found) { 
   
          System.out.println(">>>Table row>>>"); 
   
          // Loop through the columns (properties) of the  
          // current row. For each column, display its name  
          // and value. 
          for (int i = 0; i < appdata.getPropertyCount(); i++) { 
   
            System.out.println( 
              appdata.getPropertyName(i) + ": " + 
              String.valueOf(appdata.getProperty(i))); 
          } 
          // Go to the next row of the AgrData. 
          found = appdata.gotoNext(); 
        } 
        System.out.println("<<<End of table rows<<<"); 
      } 
      catch (AgoApiException e) { 
        e.printStackTrace(); 
      } 
    } 
   
   
    // This method changes the value of a particular column 
    // (Cost per lb) in every row of the current cache (AgrData). 
    // Then it updates the data source with those changes. 
    void raiseCost(String opvalue) { 
   
      BigDecimal costprop;  
      double percent, oldcost, newcost; 
   
      percent = (Double.parseDouble(opvalue)) / 100; 
   
      try { 
        // Loop through the rows of the AgrData. 
        while (appdata.gotoNext()) { 
   
          // Get the desired column value from the current row 
          // and increase it by the specified percentage. 
          costprop = (BigDecimal)appdata.getProperty("Cost per lb"); 
          oldcost = costprop.doubleValue(); 
          newcost = oldcost + (oldcost * percent); 
   
          appdata.setProperty("Cost per lb",new BigDecimal(newcost)); 
        } 
        // Send all of the row updates to the data source. 
        appdata.updateRows(); 
      } 
      catch (AgoApiException e) { 
        e.printStackTrace(); 
      } 
    } 
   
   
    // Main method (used for application startup). 
    public static void main(String[] args) { 
   
      if (args.length < 4) { 
        // Make sure all of the required command-line args 
        // have been provided to the application. If not, 
        // display an error message and terminate. 
        System.out.println( 
          "Missing Command-Line Arguments\n\n" +   
          "Required arguments:\n" + 
            "1  SilverStream server host\n" + 
            "2  SilverStream db & data source object\n" + 
            "3  Operation to perform\n" + 
            "4  Value to use in the operation\n\n" + 
          "Example:\n" + "java extjtech.ExtData " + 
            "myserver " +           
            "mydb:com.myorg.data.dsoMyPassthru " + 
            "costupdate " +  
            "2"); 
        System.exit(0); 
      } 
      else { 
        // Get the command-line args so the application can 
        // pass them to the ExtData constructor. 
        String s3serverhost = args[0]; 
        String s3dsolocation = args[1]; 
        String operation = args[2]; 
        String opvalue = args[3]; 
   
        // Create an instance of ExtData. This executes the  
        // constructor for the class, which then accesses the 
        // SilverStream server and performs the requested 
        // operation. 
        ExtData extdata = new ExtData(s3serverhost, 
                                      s3dsolocation, 
                                      operation, 
                                      opvalue); 
      } 
    } 
  } 

Running the client   Top of page

When this class runs, it retrieves or updates rows of data from the DSO, then displays the result.






Copyright © 2000, SilverStream Software, Inc. All rights reserved.