/**
* The application server for the banking application.
* This makes itself available to clients which can use it to get
* references to client objects.
*/
package bank.server;
import imaginary.persist.DatabaseTransaction;
import imaginary.persist.PersistenceException;
import imaginary.persist.Persistent;
import imaginary.persist.RemoteLockHolder;
import imaginary.persist.Transaction;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Properties;
import java.rmi.RMISecurityManager;
public class AppServer extends UnicastRemoteObject implements RemoteAppServer {
/**
* The entry point to the application server.
* It takes three arguments:<BR>
* <OL>
* <LI> The data store URL
* <LI> The user ID used to connect to the data store
* <LI> The password used to connect to the data store
* </OL>
* It then makes an instance of the AppServer class available to
* all connecting clients via the RMI URL:<BR>
* rmi://host/AppServer
* @param args the args to the AppServer process
*/
static public void main(String[] args) {
if( args.length != 3 ) {
System.out.println("Syntax: [java AppServer URL UID PASSWORD]");
return;
}
System.out.println("Installing the security manager...");
System.setSecurityManager(new RMISecurityManager());
try {
AppServer server;
Properties props = new Properties();
String url = args[0];
props.put("user", args[1]);
props.put("password", args[2]);
System.out.println("Starting the application server...");
Naming.rebind("AppServer", new AppServer(url, props));
System.out.println("AppServer bound with url: " + url + "...");
}
catch( Exception e ) {
e.printStackTrace();
}
}
/**
* Constructs a new AppServer object. There should be only one
* per application server process.
* @param u the data store URL
* @param props the properties containing the user ID and password
* @exception java.rmi.RemoteException thrown if the object cannot be
* exported
*/
public AppServer(String u, Properties props) throws RemoteException {
super();
Transaction.url = u;
Transaction.properties = props;
}
/**
* Allows a remote client to get a reference to a customer object
* located on the application server.
* @param h the lock holder on the client
* @param id the customer ID for the desired customer object
* @exception imaginary.persist.PersistenceException an error occurred
* restopring the desired customer
* @exception java.rmi.RemoteException an error occurred exporting the
* customer
*/
public RemoteCustomer getCustomer(RemoteLockHolder h, int id)
throws PersistenceException, RemoteException {
Transaction t = Transaction.getTransaction();
RemoteCustomer c;
try {
c = (RemoteCustomer)Persistent.getPersistent(t, id,
"bank.server.Customer");
}
catch( Exception e ) {
e.printStackTrace();
return null;
}
return c;
}
}
|