home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / TRIAL / JBUILDER / JSAMPLES.Z / ClientResolver.java < prev    next >
Encoding:
Java Source  |  1998-05-08  |  4.9 KB  |  126 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20.  
  21. //Title:        JBuilder Tutorial
  22. //Version:      2.0
  23. //Copyright:    Copyright (c) 1998
  24. //Author:       Jens Ole Lauridsen
  25. //Company:      Borland Int'l
  26. //Description:  Tutorial of Distributed Computing using RMI and DataSetData.
  27.  
  28. package borland.samples.tutorial.dataset.datasetdata;
  29.  
  30. import borland.jbcl.dataset.*;
  31. import java.rmi.*;
  32. import java.util.*;
  33.  
  34. // The ClientResolver class is an implementation of a Resolver.
  35. //
  36. class ClientResolver extends Resolver {
  37.   ResourceBundle res = Res.getBundle("borland.samples.tutorial.dataset.datasetdata.Res");
  38.            
  39.   // resolveData
  40.   // Implementation of method in borland.jbcl.dataset.Resolver
  41.   // First, We lookup the "DataServerApp" service on the host specified by the 
  42.   //   HostName property. 
  43.   // Then, we extract the changes into a DataSetData instance.
  44.   // Next, we make the remote method call: resolveEmployeeChanges.
  45.   // Next, if there are resolution errors, handle these accordingly.
  46.   // Last, we call resetPendingStatus(true), which will change the
  47.   //   status bits for all our changed rows in our dataSet to be resolved.
  48.   //
  49.   public void resolveData(DataSet dataSet) throws DataSetException {
  50.     try {
  51.       String         serverName = "//" + hostName + "/DataServerApp";
  52.       EmployeeApi    server  = (EmployeeApi)Naming.lookup(serverName);
  53.       ProviderHelp.startResolution(dataSet.getStorageDataSet(), true);    // This blocks the dataset for invalid operations while resolving.
  54.       DataSetData changes = DataSetData.extractDataSetChanges(dataSet);
  55.       DataSetData errors  = server.resolveEmployeeChanges(changes);
  56.       if (errors != null)
  57.         handleErrors(dataSet,errors);
  58.       dataSet.resetPendingStatus(true);
  59.     }
  60.     catch (DataSetException ex) {
  61.       dataSet.resetPendingStatus(false); // reset status bits to: nothing is resolved !
  62.       throw ex;
  63.     }     
  64.     catch (NotBoundException ex) {
  65.       dataSet.resetPendingStatus(false); // reset status bits to: nothing is resolved !
  66.       throw new DataSetException(res.getString("RS_NoServer"));
  67.     }
  68.     catch (Exception ex) {
  69.       dataSet.resetPendingStatus(false); // reset status bits to: nothing is resolved !
  70.       String error = ex.getMessage();
  71.       if (error == null)
  72.         error = res.getString("RS_NoConnect")+hostName;
  73.       throw new DataSetException(error);
  74.     }
  75.     finally {
  76.       ProviderHelp.endResolution(dataSet.getStorageDataSet());  // This unblocks the dataSet after resolving.
  77.     }
  78.   }
  79.  
  80.   // handleErrors.
  81.   // Errors are handled in the following way in this tutorial. Note that this is
  82.   // not the only nor the best way to handle errors, but it might give some good
  83.   // ideas for the 'ultimate' implementation.
  84.   //
  85.   // We just want to show 1 error in the UI. Ignore all other possible errors.
  86.   //
  87.   // First we have to loop through all the errors reported, and reset the
  88.   // status bits to not resolved for each row that caused an error.
  89.   // Then we go to the first reported error, and let the provider fail with
  90.   // that error.
  91.   //
  92.   private void handleErrors(DataSet dataSet, DataSetData errors) 
  93.     throws DataSetException
  94.   {
  95.     TableDataSet errorDataSet = new TableDataSet();
  96.     errors.loadDataSet(errorDataSet);
  97.     long internalRow;
  98.     errorDataSet.first();
  99.     do {            
  100.       internalRow = errorDataSet.getLong(0);
  101.       dataSet.resetPendingStatus(internalRow,false);
  102.     } while (errorDataSet.next());
  103.     
  104.     dataSet.resetPendingStatus(true);
  105.     
  106.     errorDataSet.first();
  107.     internalRow = errorDataSet.getLong(0);
  108.     dataSet.goToInternalRow(internalRow);
  109.     DataSetException ex = (DataSetException)errorDataSet.getObject(1);
  110.     throw ex;
  111.   }
  112.   
  113.   // The HostName property is used to locate the "DataServerApp" service
  114.   // through RMI. By default we just look at our local machine.
  115.   //
  116.   public String getHostName() {
  117.     return hostName;
  118.   }
  119.  
  120.   public void setHostName(String hostName) {
  121.     this.hostName = hostName;
  122.   }
  123.   
  124.   private String hostName = "localhost";
  125. }
  126.