package bank; import sun.misc.*; import org.omg.CORBA.*; import org.omg.PortableServer.*; import org.omg.CosNaming.*; import java.util.*; import org.omg.PortableServer.ServantLocatorPackage.CookieHolder; //Name :- William Aictison Matriculation Number :- 200515313 /** * Class BankClient represents a client using the bank server. * * @author William Aitchison * @version 1.2 */ public class BankServer { // so in scope for interrupt handler private POA managerPOA; private POA accountPOA; private byte[] oidBytes; private static ORB orb; public BankServer(String args[]) { // handle keyboard interrupt to force etherealize() and exit SignalHandler handler = new SignalHandler() { public void handle(Signal sig) { try { managerPOA.deactivate_object(oidBytes); accountPOA.deactivate_object(oidBytes); String fileName = new String(oidBytes, "Cp1252"); System.err.println("keyboard interrupt handler OK:" + fileName); } catch (Exception e) { System.err.println("keyboard interrupt handler:\n" + e.getMessage()); e.printStackTrace(); } System.exit(0); } }; // handler Signal.handle(new Signal("INT"), handler); //-- Handle CTRL+C try { // Initialise the ORB // to make the IORs persistent you have to start the server on the same // port each time. The port number is arbitrary. // Add -DOAPort=XXXX to jaco command line. orb = ORB.init(args, null); // Find the root POA and activate it org.omg.CORBA.Object tmpobj = orb.resolve_initial_references("RootPOA"); POA rootPOA = POAHelper.narrow(tmpobj); POAManager poaManager = rootPOA.the_POAManager(); poaManager.activate(); System.err.println("\nServer: Obtained from ORB and activated the " + rootPOA.the_name()); // Create a Manager POA with the right policies for a ServantActivator // NB RETAIN!! Policy poaPolicy[] = new Policy[4]; poaPolicy[0] = rootPOA.create_servant_retention_policy(ServantRetentionPolicyValue.RETAIN); poaPolicy[1] = rootPOA.create_request_processing_policy(RequestProcessingPolicyValue.USE_SERVANT_MANAGER); poaPolicy[2] = rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID); poaPolicy[3] = rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT); managerPOA = rootPOA.create_POA("managerActivatorPOA", null, poaPolicy); managerPOA.the_POAManager().activate(); System.err.println("Server: Created and activated manager POA " + managerPOA.the_name()); managerPOA.set_servant_manager(new PoaServantActivator()); // Create a Accounts POA with the right policies for a ServantLocator Policy poaPolicy1[] = new Policy[4]; poaPolicy1[0] = rootPOA.create_servant_retention_policy(ServantRetentionPolicyValue.NON_RETAIN); poaPolicy1[1] = rootPOA.create_request_processing_policy(RequestProcessingPolicyValue.USE_SERVANT_MANAGER); poaPolicy1[2] = rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID); poaPolicy1[3] = rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT); POA accountsPOA = rootPOA.create_POA("accountsLocatorPOA", null, poaPolicy1); accountsPOA.the_POAManager().activate(); System.err.println("Server: Created and activated child POA " + accountsPOA.the_name()); accountsPOA.set_servant_manager(new PoaServantLocator()); // Create object id (object key) manually. Note the cast. String oidString = "BankAccounts"; oidBytes = oidString.getBytes("8859_1"); // yuk!! // The *object reference* is created here but not the servant org.omg.CORBA.Object objRefManager = managerPOA.create_reference_with_id(oidBytes, ManagerHelper.id()); System.err.println("Server: Created a CORBA object reference from id " + ManagerHelper.id()); // The *object reference* is created here but not the servant org.omg.CORBA.Object objRefAccount = accountsPOA.create_reference_with_id(oidBytes, AccountHelper.id()); System.err.println("Server: Created a CORBA object reference from id " + AccountHelper.id()); // Get a reference to the naming service org.omg.CORBA.Object o = orb.resolve_initial_references("NameService"); NamingContextExt rootContext = NamingContextExtHelper.narrow( o ); // Create a context called MyServer.CalcServer NameComponent nc = new NameComponent("MyServer", "BankServer"); NameComponent path[] = {nc}; // Bind the obj ref to that context, overwriting as necessary any previous binding rootContext.rebind(path, objRefManager); System.err.println("Object Reference successfully registered in Naming Service. objRefManager"); // Bind the obj ref to that context, overwriting as necessary any previous binding rootContext.rebind(path, objRefAccount); System.err.println("Object Reference successfully registered in Naming Service. objRefAccount"); // Enter event loop and wait for requests System.err.println("Bank listening ..."); //output detailes System.err.println("Object Reference successfully imported from Naming Service. \n"); System.err.println("Name :- William Aictison Matriculation Number :- 200515313"); orb.run(); System.exit(0); } //try catch (Exception e) { System.err.println("server oops:\n" + e.getMessage()); e.printStackTrace(); } } // BankServer constructor public static void main(String [] args) { BankServer serverServantActivator = new BankServer(args) ; } /** * getOrb - return the orb * * @return ORB orb */ public static ORB getOrb() { return orb; } } //Name :- William Aictison Matriculation Number :- 200515313 /** * Class PoaServantActivator represents a ServantActivator. * * @author William Aitchison * @version 1.8 */ class PoaServantActivator extends LocalObject implements ServantActivator { public Servant incarnate(byte[] oid, POA adapter) throws ForwardRequest { System.out.println("incarnate"); //initialze variables String fileName = ""; FileUtil fileUtil = null; Vector accountsList = null; ManagerImpl servantObj = null; try { //get filename fileName = new String(oid, "Cp1252") + ".bank"; //create fileUtil fileUtil = new FileUtil(fileName); //load accounts list accountsList = (Vector) fileUtil.load(); servantObj = new ManagerImpl(accountsList, BankServer.getOrb()); System.err.println("PoaServantLocator.incarnate(): Created " + servantObj.getClass().getName() + "servant object for " + adapter.the_name()); } catch(Exception e) { System.out.println("There was an error :" + e); } //return servantObj return servantObj; } /** * etherealize - saves the file * * @param byte[] oid, POA adapter, Servant the_servant, boolean cleanup_in_progress, boolean remaining_activations */ public void etherealize(byte[] oid, POA adapter, Servant the_servant, boolean cleanup_in_progress, boolean remaining_activations) { System.out.println("etherealize"); try { //create the file name String fileName = new String(oid, "Cp1252") + ".bank"; //create manger from servent ManagerImpl managerImpl = (ManagerImpl) the_servant; //create fileUtil FileUtil fileUtil = new FileUtil(fileName); //svae the file fileUtil.save(managerImpl); } catch (Exception e) { System.err.println("etherealize: Caught exception - " + e); } } } //Name :- William Aictison Matriculation Number :- 200515313 /** * Class PoaServantLocator represents a PoaServantLocator. * * @author William Aitchison * @version 1.8 */ class PoaServantLocator extends LocalObject implements ServantLocator { public Servant preinvoke(byte[] oid, POA adapter, String operation, CookieHolder the_cookie) throws ForwardRequest { System.out.println("preinvoke"); //initialze variables String fileName = ""; FileUtil fileUtil = null; Vector accountsList = null; ManagerImpl servantObj = null; try { //get filename fileName = new String(oid, "Cp1252") + ".bank"; //create fileUtil fileUtil = new FileUtil(fileName); //load accounts list accountsList = (Vector) fileUtil.load(); servantObj = new ManagerImpl(accountsList, BankServer.getOrb()); System.err.println("PoaServantLocator.incarnate(): Created " + servantObj.getClass().getName() + "servant object for " + adapter.the_name()); } catch(Exception e) { System.out.println("There was an error :" + e); } return servantObj; } public void postinvoke(byte[] oid, POA adapter, String operation, java.lang.Object the_cookie, Servant the_servant) { try { System.out.println("postinvoke"); //create the file name String fileName = new String(oid, "Cp1252") + ".bank"; //create manger from servent ManagerImpl managerImpl = (ManagerImpl) the_servant; //create fileUtil FileUtil fileUtil = new FileUtil(fileName); //svae the file fileUtil.save(managerImpl); } catch (Exception e) { System.err.println("postinvoke: Caught exception - " + e); } } }