Now that the Account object is both IIOP distribution capable and FilepoBC persistence capable, it is time to demonstrate how to actually create instances of the Account object stored persistently in a FilepoBC database.
In addition, this step will demonstrate how the FilepoBC database itself is registered into an activation space. The activation space will provide the mechanism for an ORB executing in a Netscape Servlet to access the persistent Account objects stored in the FilepoBC database. The IIOP protocol will then be able to provide the distribution facility for the Account objects. The discussion of the servlet code itself and distribution is found in the next section of the tutorial.
The Populate application is provided here to demonstrate how to create an activation space, the FilepoBC database registered in the activation space, and two persistent Account objects, a Checking account, and a savings Account. The description of each component of the Populate application can be found following the Populate.java source.
package COM.ibm.jaws.tutorial1; import COM.ibm.jaws.mofw.BaseCollection; import COM.ibm.jaws.mofw.NamedCollection; import COM.ibm.jaws.services.filepobc.FilepoBCImpl; import COM.ibm.jaws.services.bcfw.Syncable; import COM.ibm.jaws.services.sroot.SimpleRootBC; import COM.ibm.jaws.services.sroot.SRootNC; public class Populate { public static void main(String[] args) { // Verify the command line arguments. if (args.length != 3) { System.err.println("Usage: Populate checkingBalance " + "savingsBalance baseDirectory"); System.exit(1); } // Get the arguments from the command line. float initialCheckingBalance = Float.valueOf(args[0]).floatValue(); float initialSavingsBalance = Float.valueOf(args[1]).floatValue(); String baseDirectory = args[2]; // Create the activation space directory name. String activationSpaceDirectory = baseDirectory + "/accountsActivationSpace"; // Create the location where the FilepoBC data will be stored. String filepoDataDirectory = baseDirectory + "/accountsData"; // Create the activation space. SRootNC dependentContext = new SRootNC(); dependentContext.addMOEntries ("COM.ibm.jaws.services.filepobc.FilepoBC", "COM.ibm.jaws.services.sroot", "COM.ibm.jaws.services.filepobc.FilepoBCSRootMO"); BaseCollection activationSpace = new SimpleRootBC(activationSpaceDirectory, dependentContext); // Create the FilepoBC database, and add (register) it to the // activation space. BaseCollection filepoDatabase = new FilepoBCImpl(filepoDataDirectory, "COM.ibm.jaws.tutorial1.AccountFilepoManaged"); BaseCollection filepoDatabaseManaged = (BaseCollection) activationSpace.createFromCopy ("AccountCollection", filepoDatabase, "COM.ibm.jaws.services.filepobc.FilepoBC"); // Create a checking account and manage it in the database. Account checking = new AccountImpl(initialCheckingBalance); checking = (Account) filepoDatabase.createFromCopy ("Checking", checking, "COM.ibm.jaws.tutorial1.Account"); // Create a savings account and manage it in the database. Account savings = new AccountImpl(initialSavingsBalance); savings = (Account) filepoDatabase.createFromCopy ("Savings", savings, "COM.ibm.jaws.tutorial1.Account"); activationSpace.release(); } }
The components of the Populate application that are specific to the client server application based upon the Account business object are highlighted by the red font. The remaining code could easily be reused to populate a FilepoBC database with a different business object.
The following describes each of the components of the Populate application:
Here are the first few line of the application:
// Verify the command line arguments. if (args.length != 3) { System.err.println("Usage: Populate checkingBalance " + "savingsBalance baseDirectory"); System.exit(1); } // Get the arguments from the command line. float initialCheckingBalance = Float.valueOf(args[0]).floatValue(); float initialSavingsBalance = Float.valueOf(args[1]).floatValue(); String baseDirectory = args[2]; // Create the activation space directory name. String activationSpaceDirectory = baseDirectory + "/accountsActivationSpace"; // Create the location where the FilepoBC data will be stored. String filepoDataDirectory = baseDirectory + "/accountsData";These lines verify the command line arguments and saves them into variables. The Populate application expects the following three arguments on the command line: Here's the next couple of lines of code:
// Create the activation space. SRootNC dependentContext = new SRootNC(); dependentContext.addMOEntries ("COM.ibm.jaws.services.filepobc.FilepoBC", "COM.ibm.jaws.services.sroot", "COM.ibm.jaws.services.filepobc.FilepoBCSRootMO"); BaseCollection activationSpace = new SimpleRootBC(activationSpaceDirectory, dependentContext);This code creates an object named SimpleRootBC that manages an activation space for this application. The accounts FilepoBC database will be registered in this activation space making the two account objects in the FilepoBC activatable by the ORB running in the Netscape Servlet. The activation space can be thought of as the glue that binds the server side of the application to the application's managed (distributed and persistent) objects.
Application spaces are identified by a directory name. Our activation space directory will be the Netscape Servlet directory following by the directory named accountsActivationSpace. The activation space directory name will appear in the accounts Netscape Servlet code. This servlet code will provide the mechanism for remote access to the checking and savings objects.
Here are the next 2 lines of code:
// Create the FilepoBC database, and add (register) it to the // activation space. BaseCollection filepoDatabase = new FilepoBCImpl(filepoDataDirectory, "COM.ibm.jaws.tutorial1.AccountFilepoManaged"); BaseCollection filepoDatabaseManaged = (BaseCollection) activationSpace.createFromCopy ("AccountCollection", filepoDatabase, "COM.ibm.jaws.services.filepobc.FilepoBC");These two lines create the FilepoBC database and register it in the activation space.
The FilepoBC constructor takes two parameters, the directory the database should use to store its persistent objects and the Class name of the Manageable version of the object to be stored.
The next next 4 statements create 2 different instances of the Account object and store them persistently into the FilepoBC database.
First, the checking account is created with an initial balance and saved to the database.
Account checking = new AccountImpl(initialCheckingBalance); checking = (Account) filepoDatabase.createFromCopy ("Checking", checking, "COM.ibm.jaws.tutorial1.Account");Next, the savings account is created with an initial balance and saved to the database.
Account savings = new AccountImpl(initialSavingsBalance); savings = (Account) filepoDatabase.createFromCopy ("Savings", savings, "COM.ibm.jaws.tutorial1.Account");The last line of the Populate application releases the SimpleRoot object causing the two persistent account objects to be saved in the database, and the information about the activation space and the FilepoBC database registered in the activation space to be stored in the accountsActivationSpace directory.
activationSpace.release();Executing the Populate application creates the application space, the FilepoBC database, and the persistent Checking and Savings objects. It is important that the activation space directory argument passed to Populate specifies the location of the Netscape applets directory.
java COM.ibm.jaws.tutorial1.Populate 100 200 c:\netscape\server\plugins\java\applets