Java Data Objects (JDO)

EJB and JDO

Session EJB Facade to JDO Objects

Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................JDK 1.2 or higher (I use JDK 1.3.1)
.........................................PE:JTM - The Productivity Environment for JavaTM (from HYWY Software Corporation)

 

Note: This article assumes that the reader has already created simple bank project using PE:J and JDO.

 

com\hywy\samples\ejb20\session\stateless\TellerBean.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
/**
* The following example illustrates
* a Session Facade to a JDO object
*
* author: Gopalan Suresh Raj
* Copyright (c), 2002. All Rights Reserved.
* URL: https://gsraj.tripod.com/
* email: gopalan@gmx.net
*/


package com.hywy.samples.ejb20.session.stateless;

import java.util.ArrayList;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.hywy.samples.bank.Teller;

/**
* TellerBean is a stateless Session Bean. This bean illustrates:
* <ul>
* <li> No persistence of state between calls to the Session Bean
* <li> Looking up values from the Environment
* </ul>
*
* @author Gopalan Suresh Raj
*/

public class TellerBean implements SessionBean {

  private static final boolean VERBOSE_ = true;
  private SessionContext context_;
  private int transactionLimit_;
  /** The Teller reference */
  private Teller teller_;

  // You might also consider using WebLogic's log service
  private void log(String string) {
    if (VERBOSE_) System.out.println(string);
  }

  /**
   * This method is required by the EJB Specification,
   * but is not used by this example.
   *
   */

  public void ejbActivate() {
    log("ejbActivate called");
  }

  /**
   * This method is required by the EJB Specification,
   * but is not used by this example.
   *
   */

  public void ejbRemove() {
    log("ejbRemove called");
  }

  /**
   * This method is required by the EJB Specification,
   * but is not used by this example.
   *
   */

  public void ejbPassivate() {
    log("ejbPassivate called");
  }

  /**
   * Sets the session context_.
   *
   * @param context_ SessionContext Context for session
   */

  public void setSessionContext(SessionContext context_) {
    log("setSessionContext called");
    this.context_ = context_;
  }

  /**
   * This method corresponds to the create method in the home interface
   * "TellerHome.java".
   * The parameter sets of the two methods are identical. When the client calls
   * <code>TellerHome.create()</code>, the containamingExceptionr allocates an instance of
   * the EJBean and calls <code>ejbCreate()</code>.
   *
   * @exception               javax.ejb.CreateException if there is
   *                          a communications or systems failure
   * @see                     com.hywy.samples.ejb20.session.stateless.Teller
   */

  public void ejbCreate () throws CreateException {
    log("ejbCreate called");

    try {
      InitialContext initialContext = new InitialContext();

      Integer limit = (Integer) initialContext.lookup("java:/comp/env/transactionLimit_");

      transactionLimit_ = limit.intValue();
    } catch (NamingException namingException) {
      throw new CreateException("Failed to find environment value "+namingException);
    }
    this.teller_ = new com.hywy.samples.bank.Teller();
  }

  /**
   * Create Checking or Savings Account
   */

  public void createAccount(String customerNames,
                            int startingBalance,
                            boolean isCheckingAccount) {
    final String THIS = "TellerBean::createAccount(customerNames, startingBalance, isCheckingAccount) :";
    try {
      this.teller_.createAccount(customerNames, startingBalance, isCheckingAccount);
    }
    catch(Exception exception) {
      log(THIS+exception.toString());
    }
  }

  /**
   * Delete and close the account
   */

  public void deleteAccount(long accountNumber) {
    final String THIS = "TellerBean::deleteAccount(accountNumber) :";
    try {
      this.teller_.deleteAccount(accountNumber);
    }
    catch(Exception exception) {
      log(THIS+exception.toString());
    }
  }

  /**
   * Transfer Money
   */

  public boolean transferMoney(long fromAccountNumber,
                               long toAccountNumber,
                               int amount) {
    final String THIS = "TellerBean::transferMoney(fromAccountNumber,toAccountNumber,amount) :";
    boolean result = false;
    if (amount > transactionLimit_) {
      log(THIS+"Attempt to transer "+amount+" is greater than the limit of "+transactionLimit_);
      amount = transactionLimit_;
    }
    log(THIS+"Attempting to Transfer "+amount+" from Account No: "+fromAccountNumber+ " to Account No: "+toAccountNumber);
    try {
      result = this.teller_.transferMoney(fromAccountNumber, toAccountNumber, amount);
    }
    catch(Exception exception) {
      log(THIS+exception.toString());
    }
    return result;
  }

  /**
   * List out all Checking accounts found
   */

  public ArrayList listAllCheckingAccounts() {
    final String THIS = "TellerBean::listAllCheckingAccounts() :";
    ArrayList list = null;
    Iterator iterator = null;
    try {
      list = this.teller_.listAllCheckingAccounts();
    }
    catch(Exception exception) {
      log(THIS+exception.toString());
    }
    if(list != null) {
      iterator = list.iterator();
      while(iterator.hasNext()) {
        log(THIS+iterator.next().toString());
      }
    }
    return list;
  }

  /**
   * List out all Savings accounts found
   */

  public ArrayList listAllSavingsAccounts() {
    final String THIS = "TellerBean::listAllSavingsAccounts() :";
    ArrayList list = null;
    Iterator iterator = null;
    try {
      list = this.teller_.listAllSavingsAccounts();
    }
    catch(Exception exception) {
      log(THIS+exception.toString());
    }
    if(list != null) {
      iterator = list.iterator();
      while(iterator.hasNext()) {
        log(THIS+iterator.next().toString());
      }
    }
    return list;
  }

}

 

Session EJB Facade to JDO Objects
 Teller.java
 TellerHome.java
 TellerBean.java
 Client.java
 build.xml
 application.xml
 ejb-jar.xml
 weblogic-ejb-jar.xml

click here to go to
My JDO HomePage...

click here to go to
My Advanced Java Tutorial Page...

About the Author...
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He has co-authored a number of books including "Professional JMS", "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia© site (https://gsraj.tripod.com/) or mail him at gopalan@gmx.net.

Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since February 26,2002.

Last Updated : Feb 26, 2002

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-2002, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.