HW2: Certificate Repository Homework (DB Version)

Requirement

Build

 

NsDBBridge

import java.sql.*;
import java.util.*;
import java.io.*;

public class NsDBBridge
{
	private String _driver = null;
	private String _dburl = null;
	private String _username = null;
	private String _password = null;
	private Connection _c = null;
	private Statement _s = null;

	public void disconnect()
	{
		try
		{
			_s.close();
			_c.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return; 
	}

	public NsBridgeDataRes search(String statement, String resultID)
	{
		NsBridgeDataRes dataRes = new NsBridgeDataRes(resultID);
		NsNVPairs curNV = null;

		try
		{
			ResultSet rs = _s.executeQuery(statement);
			ResultSetMetaData rsmd = rs.getMetaData();

			//
			// We can print the col types as well
			//
			// PrintColumnTypes.printColTypes(rsmd);
			// System.out.println("");
			//
			int numberOfColumns = rsmd.getColumnCount();
			Vector colInfo = new Vector();
	
			for (int i = 1; i <= numberOfColumns; i++) 
			{
				String columnName = rsmd.getColumnName(i);
				colInfo.add(columnName);
			}

			//
			// Print the result content
			//
			while (rs.next()) 
			{
				curNV = new NsNVPairs(); 
				for (int i = 1; i <= numberOfColumns; i++) 
				{
					String columnValue = rs.getString(i);
					// FIXME 
					// get primary key here in the first para later
					if (i == 1) 
						curNV.setID(columnValue); 
					curNV.addStrNsPair((String)colInfo.elementAt(i-1), columnValue); 
				}
				dataRes.add(curNV);
			}

		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}

		return dataRes; 
	} // End of search

	public NsDBBridge(String driver, String dburl, String username, String password)
	{
		_driver = driver;
		_dburl = dburl;
		_username = username;
		_password = password;	

		//
		// Load the driver
		//
		try
		{
			Class.forName(_driver);
			_c = DriverManager.getConnection(_dburl, _username, _password);
			_s = _c.createStatement();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

} // End of Class File

NsBridgeManager.java

import java.util.*;

// FIXME: Implement by all Bridge later on
interface Bridge
{
	/**
	 * Get the name of the bridge
	 */
	public String getBridgeName(); 

	/**
	 * Get the type of the bridge
	 */
	public String getBridgeType();

	/**
	 * Get all user data
	 */
	public String getAllObjects();
}

/**
 * Bridge Manager
 */
class NsBridgeManager
{
	private Vector	_bridgeList;

	/**
	 * Constructor
	 */
	public NsBridgeManager()
	{
		_bridgeList = new Vector();
	}

	//
	// Todo (Bridge)
	// - Read the configuration info (NsBridgeConfInfo.xml)
	// - Determine 
	//    - How many bridges need to construct
	//    - For LDAP bridges, what is the host, port, filter, searchbase,
	//      username, password (if need to bind)  
	//    - For DB bridges, what is the driver, db url, username, password 
	// - Construct LDAP bridge
	// - Do
	//    - create ld connection object (LDAPConnection) 
	//    - keep the object around 
	//    - All detail of LDAP communication is in here 
	// - Construct DB bridge
	// - Do
	//    - create db connection (Connection) 
	//    - All detail of DB communication is in here 
	//
	// Todo (Data Model) (Next homework)
	// - Read the configuration info (NsDataConfInfo.xml)
	// - Determine
	//    - Mapping of user data 
	//    - Store mapping in internal data structure 
	// - Provides function for obtain all user data 
	//   (Loop the bridges and get all user data) 

}

 

NsBridgeConfInfo.xml

 

NsTestDriver2

A test driver program that uses/tests all the above classes by:

Copyright 1996-2003 OpenLoop Computing. All rights reserved.