Common Utility Classes

This is a set classes that are provide shared functionality to the examples. The files are:

ResourceMapper  // XAResourceHandleMapper implementation for recovery/logging
TestDataSource  // DB Connectivity to Cloudscape Databases
Utils           // Utility functions such as printing out Exceptions.

Compiling

Run the build script to generate Java form IDL, and compile all sources.

   ant

ResourceMapper Implementation

This class provides XAResource to byte[] mapping for the transaction manager. The transaction manager logs the handles and uses them in the recovery process to locate the XAResource.

package common;
                                                                           
import java.util.Hashtable;
                                                                           
import javax.transaction.xa.Xid;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.XAException;
                                                                           
import com.sssw.jts.api.XAResourceHandleMapper;
                                                                           
//
// XAResourceHandleMapper implementation
// Required by JTS to log Resource for recovery.
//
                                                                           
public class ResourceMapper implements XAResourceHandleMapper
{
    byte        _id = 1;
    Hashtable   _resources;
                                                                           
    public ResourceMapper()
    {
    |   _resources = new Hashtable();
    }
                                                                           
    public void addResource(XAResource resource)
    {
    |   if (_resources.get(resource) == null) {
    |   |   byte [] handle = new byte[1];
    |   |   handle[0] = _id++;
    |   |   _resources.put(resource, handle);
    |   }
    }
                                                                           
    public byte[] toHandle(XAResource resource)
    {
    |   return (byte[])_resources.get(resource);
    }
                                                                           
    public XAResource toXAResource(byte[] handle)
    {
    |   return null;
    }
                                                                           
    public void release(XAResource res)
    {
    |   // nothing to do
    }
}

TestDataSource Implementation

This class provides generalized connection to Cloudscape database used in the example programs. This code can be modified to use other database drivers.

package common;
                                                                           
import java.sql.*;
                                                                           
import javax.sql.*;
import javax.transaction.*;
import javax.transaction.xa.*;
                                                                           
import COM.cloudscape.core.XaDataSource;
                                                                           
public class TestDataSource implements XADataSource
{
    public static final String _testBaseDir = "../../";
    public static final String _testDB1 = "dbs/testDB1";
    public static final String _testDB2 = "dbs/testDB2";
    public static final String _testDB3 = "dbs/testDB3";
    public static final String _testDBR = "dbs/testRecRes";
    public static final String _testDBkA= "dbs/testBankA";
    public static final String _testDBkB= "dbs/testBankB";
                                                                           
    public static final String _testTB1 = "testTab1";
    public static final String _testTB2 = "testTab2";
    public static final String _testTB3 = "testTab3";
    public static final String _testTBR = "tabRecRes";
                                                                           
    private static boolean _driverLoaded = false;
                                                                           
    private XADataSource    _xads;
    private String          _connURLBase;
    private String          _connURL;
                                                                           
    public TestDataSource(String dbname) throws Exception
    {
    |   if (!_driverLoaded) {
    |   |   Driver jdbcDriver = (java.sql.Driver)
    |   |       Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();
    |   |   DriverManager.registerDriver(jdbcDriver);
    |   }
    |                                                                      
    |   COM.cloudscape.core.XaDataSource clxads = new COM.cloudscape.core.XaDataSource();
    |   clxads.setDatabaseName(_testBaseDir+dbname);
    |                                                                      
    |   _xads = clxads;
    |   _connURLBase = "jdbc:cloudscape:"+_testBaseDir+dbname;
    |   _connURL     = "jdbc:cloudscape:"+_testBaseDir+dbname+";create=true";
    }
                                                                           
    public void shutdown() throws Exception
    {
    |   try {
    |   |   DriverManager.getConnection(_connURLBase+";shutdown=true");
    |   } catch (SQLException ex) {
    |   }
    }
                                                                           
    //
    // XADataSource methods
    //
    public int getLoginTimeout() throws SQLException
    {
    |   return _xads.getLoginTimeout();
    }
                                                                           
    public java.io.PrintWriter getLogWriter() throws SQLException
    {
    |   return _xads.getLogWriter();
    }
                                                                           
    public void setLoginTimeout(int seconds) throws SQLException
    {
    |   _xads.setLoginTimeout(seconds);
    }
                                                                           
    public void setLogWriter(java.io.PrintWriter out) throws SQLException
    {
    |   _xads.setLogWriter(out);
    }
                                                                           
    public XAConnection getXAConnection() throws SQLException
    {
    |   return _xads.getXAConnection();
    }
                                                                           
    public XAConnection getXAConnection(String user, String pass) throws SQLException
    {
    |   return _xads.getXAConnection(user, pass);
    }
                                                                           
    public Connection getConnection() throws SQLException
    {
    |   return DriverManager.getConnection(_connURL);
    }
}

Utils Implementation

This class contains set of utility methods.

package common;
                                                                           
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
                                                                           
import java.sql.SQLException;
                                                                           
public class Utils
{
    public static boolean _debug = System.getProperty("DEBUG") != null;
                                                                           
    public static void displayError(String msg, Throwable T)
    {
    |   System.out.println();
    |   System.out.println("--------------------[ ERROR ]--------------------");
    |   System.out.println(msg);
    |   System.out.println("--------------------[ TRACE ]--------------------");
    |   if (T instanceof SQLException) {
    |   |   SQLException e =(SQLException)T;
    |   |   for (int i=1; e!=null; e = e.getNextException()) {
    |   |   |   System.out.println("--- SQLException "+i+" "+e.getMessage());
    |   |   |   e.printStackTrace();
    |   |   }
    |   } else {
    |   |   System.out.println("--- Exception "+T.getMessage());
    |   |   T.printStackTrace();
    |   }
    |   System.out.println("-------------------------------------------------");
    }
                                                                           
    public static void writeIOR(String ior, String fileName) throws Exception
    {
    |   FileOutputStream fs = new FileOutputStream(fileName);
    |   fs.write(ior.getBytes());
    |   fs.close();
    }
                                                                           
    public static String readIOR(String fileName) throws Exception
    {
    |   File fi = new File(fileName);
    |   byte[] iorBytes = new byte[(int)fi.length()]; 
    |   FileInputStream fs = new FileInputStream(fi);
    |   fs.read(iorBytes);
    |   fs.close();
    |                                                                      
    |   return new String(iorBytes);
    }
                                                                           
    public static void ifdp(String s)
    {
    |   if (_debug) System.out.println("+ "+s);
    }
                                                                           
    public static void ifdp(String s, Throwable T)
    {
    |   if (_debug) dp(s,T);
    }
                                                                           
    public static void dp(String s)
    {
    |   System.out.println("+ "+s);
    }
                                                                           
    public static void dp(String s, Throwable t)
    {
    |   dp(s);
    |   if (_debug) t.printStackTrace();
    }
}


Copyright © 2003, 2004 Novell, Inc. All rights reserved. Copyright © 2001, 2002, 2003 SilverStream Software, LLC. All rights reserved.