![]() |
|||
![]() ![]() |
![]() |
|
![]() |
Car.java This is the interface for cars. It extends java.rmi.Remote and is implemented by the CarImpl class. All methods invoked on Car instances trigger respective methods implemented by CarImpl instances. Listing 10-23 shows the source code for Car.java. Listing 10-23: Car.java. // // Car.java // // (C) 1996 Wim De Munck mailto: wimdm@dm-mediaware.be // // Interface describing stub Car functionality // the actual implementation will be the responibility of // CarImpl; Car's will be created by a remote factory object: // CarFactory and CarFactoryImplementation. // import java.rmi.Remote; import java.rmi.RemoteException; public interface Car extends Remote { public int getPrice () throws RemoteException; public int getTopSpeed() throws RemoteException; public long getSerialNr() throws RemoteException; public void sellTo ( String owner ) throws RemoteException; public String getOwner ( ) throws RemoteException; public String getColor ( ) throws RemoteException; public String getModel ( ) throws RemoteException; public String getObjectString ( ) throws RemoteException; } CarImpl.java CarImpl.java class implements Car. It contains various methods to perform miscellaneous actions on Cars. Listing 10-24 is the source code for CarImpl.java. Listing 10-24: CarImpl.java. // // CarImpl.java // // (C) 1996 Wim De Munck mailto: wimdm@dm-mediaware.be // import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class CarImpl extends UnicastRemoteObject implements Car { private int price; private long serialNr; private String brand; private String model; private String color; private String owner = null; public CarImpl ( String brand, String model, String color, long serialNr ) throws RemoteException { this.brand = brand; this.model = model; this.color = color; this.serialNr = serialNr; } public CarImpl ( String brand, String model, String color, long serialNr, String owner ) throws RemoteException { this.brand = brand; this.model = model; this.color = color; this.serialNr = serialNr; this.owner = owner; } public int getPrice () throws RemoteException { return 0; // Catalog.getPrice( brand, model ); } public int getTopSpeed() throws RemoteException { return 0; // Catalog.getTopSpeed( brand, model ); } public long getSerialNr() throws RemoteException { return serialNr; } oid sellTo ( String owner ) throws RemoteException { this.owner = owner; } public String getOwner ( ) throws RemoteException { return owner; } public String getColor ( ) throws RemoteException { return color; } public String getBrand ( ) throws RemoteException { return brand; } public String getModel ( ) throws RemoteException { return model; } public String toString ( ) { return "Car: model=" + brand + " model=" + model + " color=" + color + " serial=" + serialNr + " owner=" + ((owner == null)? "None" : owner); } public String getObjectString ( ) throws RemoteException { return this.toString(); } } CarFactory.java The CarFactory class is an interface implemented by CarFactoryImpl. Like the Car interface, CarFactory inherits from java.rmi.Remote. Listing 10-25 is the source code for CarFactory.java. Listing 10-25: CarFactory.java. // // CarFactory.java // // (C) 1996 Wim De Munck mailto: wimdm@dm-mediaware.be // // interface describing stub CarFactory functionality // the actual implementation will be the responibility of // CarFactoryImpl. // import java.rmi.Remote; import java.rmi.RemoteException; import java.sql.*; public interface CarFactory extends Remote { public Car createCar ( String model, String color ) throws RemoteException, SQLException; public Car getCar ( long serialNr ) throws RemoteException; public Car[] getAll () throws RemoteException; public boolean deleteCar ( Car car ) throws RemoteException, SQLException; public String getObjectString () throws RemoteException; } CarFactoryImpl.java CarFactoryImpl implements CarFactory and provides connectivity to the database through JDBC. It manipulates objects whose lifetime is longer than the applications lifetime. These objects are persistified, stored in a database table, and loaded by the CarFactoryImpl constructor upon initialization. The methods that perform updates of data call JDBC and execute SQL statements to synchronize the data in the database. Listing 10-26 shows the source code for CarFactoryImpl.java. Listing 10-26: CarFactoryImpl.java. // // CarFactoryImpl.java // // (C) 1996 Wim De Munck mailto: wimdm@dm-mediaware.be // // interface describing stub CarFactory functionality // the actual implementation will be the responibility of // CarFactoryImpl. // import java.util.Hashtable; import java.util.Enumeration; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.sql.*; public class CarFactoryImpl extends UnicastRemoteObject implements CarFactory { private String brand; private long lastSerialNr = 1000000; private Hashtable cars = new Hashtable(); // local variables for connection state private Connection conn; private String uid = "guest"; private String pwd = "sybase"; private String table = "cars"; // url for sybase's driver private String url = "jdbc:sybase:Tds:guadalajara:8192"; private String driver = "com.sybase.jdbc.SybDriver"; public CarFactoryImpl ( String brand ) throws RemoteException, SQLException, Exception { Class.forName(driver); conn = DriverManager.getConnection(url, uid, pwd); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM " + table + " WHERE brand = '" + brand + "'"); while (rs.next()) { wakeupCar(rs.getInt("serialno"), rs.getString("brand"), rs.getString("model"), rs.getString("color"), rs.getString("owner")); } rs = stmt.executeQuery("SELECT MAX(serialno) FROM " + table + " WHERE brand = '" + brand + "'"); rs.next(); lastSerialNr = rs.getLong(1); rs.close(); stmt.close(); this.brand = brand; } protected void finalize() throws SQLException { conn.close(); } public void wakeupCar ( int sn, String brand, String model, String color, String owner ) throws RemoteException { CarImpl car = new CarImpl ( brand, model, color, sn, owner ); cars.put("SN"+car.getSerialNr(), car); System.out.println("CarFactory: Loaded new Car:" + car); } public Car createCar ( String model, String color ) throws RemoteException, SQLException { CarImpl car = new CarImpl ( brand, model, color, ++lastSerialNr ); Statement stmt = conn.createStatement(); int res = stmt.executeUpdate("INSERT INTO " + table + "(serialno, brand, model, color, owner) VALUES (" + car.getSerialNr() + ", '" + brand + "', '" + model + "', '" + color + "', '')"); stmt.close(); cars.put ( "SN" + car.getSerialNr(), car ); System.out.println("CarFactory: Created new Car:" + car); return car; } public Car getCar ( long serialNr ) throws RemoteException { return (Car)cars.get("SN"+serialNr); } /** * get all cars without worrying about synchronization */ public Car[] getAll () throws RemoteException { Car [] allcars = new Car[cars.size()]; int i = 0; Enumeration e = cars.elements(); while (e.hasMoreElements()) { allcars[i++] = (Car)e.nextElement(); } System.out.println("CarFactory " + brand + ": request for all cars. " + i + " returned."); return allcars; } public boolean deleteCar ( Car car ) throws RemoteException, SQLException { long sn = car.getSerialNr(); if ( cars.remove("SN"+ sn) != null ) { Statement stmt = conn.createStatement(); int res = stmt.executeUpdate("DELETE " + table + " WHERE brand = '" + brand + "' AND serialno = " + sn + ""); stmt.close(); System.out.println("CarFactory: Deleted Car:" + car); return true; } else { return false; } } public String toString () { return "CarFactory: " + brand + " lastNr=" + lastSerialNr; } public String getObjectString () throws RemoteException { return this.toString(); } }
|
![]() |
|