home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-23 | 6.2 KB | 234 lines |
- package COM.odi.demo.threads;
-
- /**
- * <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
- *
- * main program which starts three threads for three tellers. Each Teller
- * performs some activities like
- * Update Account for a specific SSN
- * Add new Account into the database
- * Remove an account depending on SSN from the database.
- * The database is created in the Load object and the name of the database
- * is a command line parameter. Command line for this program is :
- * java -DtellerDB=g:\testDB\teller.odb COM.odi.demo.threads.Banking
- *
- * The Roots in the database start from Institution/Bank. Institution Object
- * contains a vector of Account holders which can be browsed through the
- * OSVector class and more elements can be added or removed from this vector.
- */
-
- import java.io.*;
-
- import COM.odi.*;
- import COM.odi.util.OSVector;
-
- public class Banking {
- /* Database name. */
-
- public static String dbName;
-
- /* Transaction used by all threads */
-
- public static Transaction t;
-
- public static void main(String args[]) {
- dbName = System.getProperty("tellerDB");
- ObjectStore.initialize(null, null);
-
- if (dbName == null)
- dbName = "teller.odb";
-
- /* Start with a fresh database for this demo. */
-
- try {
- Database.open(dbName, ObjectStore.OPEN_UPDATE).destroy();
- } catch (DatabaseNotFoundException e) {
- }
-
- /* populate Database */
-
- new Load(dbName);
-
- Database db = Database.open(dbName, ObjectStore.OPEN_UPDATE);
- Teller t1 = new Teller("Teller1", "Bank1", Thread.currentThread());
- Teller t2 = new Teller("Teller2", "Bank1", Thread.currentThread());
- Teller t3 = new Teller("Teller3", "Bank1", Thread.currentThread());
- /* start threads */
- t1.start();
- t2.start();
- t3.start();
-
- /* Wait for completion of all threads */
- try {
- t1.join();
- t2.join();
- t3.join();
- } catch (InterruptedException e1) { }
- db.close();
- ObjectStore.shutdown(true);
- System.exit(0);
- }
- };
-
- /* the thread class */
- class Teller extends Thread {
- final int READ = 1;
- final int ADD = 2;
- final int UPDATE = 3;
- final int REMOVE = 4;
-
- /* name of the teller */
-
- private String name;
-
- private Institution inst;
-
- /* Database Root same as name of the bank/Institution */
-
- private String bankName;
-
- /* ObjectStore.initialize will be attached to this thread */
-
- private Thread mainThread;
-
- private Database db;
-
- /**
- * Initialize the Teller before starting the thread with teller name,
- * bank name and mainThread.
- * @param name The name of the Teller.
- * @param bankName Name of the bank/database Root object to access.
- * @mainThread pointer to the parent thread.
- */
- public Teller(String name, String bankName, Thread mainThread) {
- super(name);
- this.name = name;
- this.mainThread = mainThread;
- this.bankName = bankName;
- }
-
- /**
- * the thread starts and ends here
- */
- public void run() {
- /* Thread related initializations */
-
- init();
-
- /*
- * Activities to be perfomed by this thread are read form a flat file
- * called actionData.txt in the current directory.
- * This file looks like this :
- * 1-6(Teller name) 8(Activity) 10-18(ssn)
- * Activity can be 1 = READ, 2= ADD, 3= UPDATE, 4= REMOVE
- */
-
- BufferedReader in = null;
- String actionLine;
- try {
- in = new BufferedReader(new FileReader("actionData.txt"), 60);
- } catch(FileNotFoundException e) {
- System.out.println("actionData.txt file not found");
- return;
- } catch(IOException e) {
- System.out.println("I/O error for actionData.txt file");
- return;
- }
-
- try {
- while((actionLine = in.readLine())!=null) {
- /* Parse the line read from the file */
-
- String forName = actionLine.substring(0, 7);
- int action = new Integer(actionLine.substring(8,9)).intValue();
- String ssn = actionLine.substring(10, 18);
-
- /* if name of the current teller is same as teller in the file then
- * perform activity mentioned in the file
- */
-
- if (name.equals(forName)) {
- switch(action) {
- case READ : /* Read Data */
- performReads(ssn);
- break;
- case ADD : /* Add Data */
- case UPDATE : /* update Data */
- case REMOVE : /* Remove Data */
- performUpdates(action, ssn);
- break;
- default :
- System.out.println("Illegal Operation");
- }
- }
- }
- } catch (IOException e) {
- System.out.println("Error reading ActionData.txt");
- }
- System.out.println("END THREAD :" + name);
- }
-
- /**
- * Thread related initialization.
- */
- private void init() {
- try {
- /* attach ObjectStore initialization to main thread */
-
- ObjectStore.initialize(mainThread);
- db = Database.open(Banking.dbName, ObjectStore.OPEN_UPDATE);
- } catch (ObjectStoreException e) {
- System.out.println(e.getMessage());
- stop();
- }
- }
-
- /**
- * Get the root object and perform update activity for the Bank.
- * @param flg activity could be ADD, UPDATE or REMOVE account.
- * @param ssn for which this activity is performed.
- */
- private void performUpdates(int flg, String ssn) {
- synchronized(db) {
- Banking.t = Transaction.begin(ObjectStore.UPDATE);
- inst = (Institution)db.getRoot(bankName);
- switch(flg) {
- case ADD:
- String actName = "New" + ssn;
- inst.addAccount(name, actName, 36, ssn, "Savings", 1000);
- break;
- case REMOVE:
- inst.removeAccount(name, ssn);
- break;
- case UPDATE:
- inst.updateAccount(name, ssn, "DEPOSIT", 1000);
- break;
- }
- Banking.t.commit();
- }
- }
-
- /**
- * Get the root object and read Account from the Database.
- * @param ssn for which Account object is needed.
- */
-
- private void performReads(String ssn) {
- synchronized(db) {
- Banking.t = Transaction.begin(ObjectStore.READONLY);
- inst = (Institution)db.getRoot(bankName);
-
- /* get the Account to be read from the institution */
-
- Account act = inst.getAccount(ssn);
- if (act == null)
- System.out.println(name + " Account for ssn:" + ssn + " does not exist");
- else
- act.displayAccountInfo(name);
- Banking.t.commit();
- }
- }
-
- }
-
-