EJB and JDO
Session EJB Facade to JDO Objects
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\Client.java |
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: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: |
* 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.Properties; import java.util.ArrayList; import java.util.Iterator; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.RemoveException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; import com.hywy.pej.persistence.oidserver.Oid; /** * This class illustrates calling a stateless Session Bean and performing * the following exercises: * <ul> * <li> Create a Teller * <li> Create accounts using the Teller * <li> Transfer Money using the Teller * <li> Remove the Teller * </ul> * * @author Gopalan Suresh Raj */ public class Client { private static final String JNDI_NAME_ = "TellerHome"; private String url_; private TellerHome home_; public Client(String url) throws NamingException { this.url_ = url; home_ = lookupHome(); } /** * Runs this example from the command linamingException. Example: * <p> * <tt>java com.hywy.samples.ejb20.session.stateless.Client "t3://localhost:7001"</tt> * <p> * The parameters are optional, but if any are supplied, * they are interpreted in this order: * <p> * @param url URL such as "t3://localhost:7001" of Server */ public static void main(String[] args) throws Exception { log("\nBeginning stateless.Client...\n"); String url = "t3://localhost:7001"; // Parse the argument list if (args.length != 1) { System.out.println("Usage: java com.hywy.samples.ejb20.session.stateless.Client t3://hostname:port"); return; } else { url = args[0]; } Client client = null; try { client = new Client(url); } catch (NamingException namingException) { System.exit(1); } try { client.example(); } catch (Exception exception) { log("There was an exception while creating and using the Teller."); log("This indicates that there was a problem communicating with the server: "+exception); } log("\nEnd stateless.Client...\n"); } /** * Runs this example. */ public void example() throws CreateException, RemoteException, RemoveException { // create a Teller log("Creating a Teller"); Teller teller = (Teller) narrow(home_.create(), Teller.class); ///////////////////////////// // Invoke the Servant ///////////////////////////// // try creating new accounts try { System.out.println("Create Checking Account for Gopalan Suresh Raj"); teller.createAccount("Gopalan Suresh Raj", 10000000, true); } catch (Exception exception) { exception.printStackTrace(); } try { System.out.println("Create Checking Account for Athul Suresh Raj"); teller.createAccount("Athul Suresh Raj", 20000000, true); } catch (Exception exception) { exception.printStackTrace(); } try { System.out.println("Create Savings Account for Gopalan Suresh Raj"); teller.createAccount("Gopalan Suresh Raj", 910000000, false); } catch (Exception exception) { exception.printStackTrace(); } try { System.out.println("Create Savings Account for Athul Suresh Raj"); teller.createAccount("Athul Suresh Raj", 2000000000, false); } catch (Exception exception) { exception.printStackTrace(); } Iterator iterator = null; Object oid = null; // List of Checking accounts ArrayList checkingAccountList = teller.listAllCheckingAccounts(); System.out.println("The size of checkingAccountList is :"+checkingAccountList.size()); iterator = checkingAccountList.iterator(); System.out.println("_______________________________________"); System.out.println(" List of Checking Accounts"); System.out.println("_______________________________________"); while(iterator.hasNext()) { oid = iterator.next(); if(oid != null) { System.out.println("Checking Account Number :"+oid.toString()); } } System.out.println("_______________________________________"); // List of Savings accounts ArrayList savingsAccountList = teller.listAllSavingsAccounts(); System.out.println("The size of savingsAccountList is :"+savingsAccountList.size()); iterator = savingsAccountList.iterator(); System.out.println("_______________________________________"); System.out.println(" List of Savings Accounts"); System.out.println("_______________________________________"); while(iterator.hasNext()) { oid = iterator.next(); if(oid != null) { System.out.println("Savings Account Number :"+oid.toString()); } } System.out.println("_______________________________________"); Long longValue = null; // Try deleting the first 2 accounts if((checkingAccountList.size() > 0)&&(savingsAccountList.size() > 0)) { try { Oid checkingOid = (Oid)checkingAccountList.get(0); if(checkingOid != null) { System.out.println("Delete Checking Account Number :"+checkingOid.toString()); teller.deleteAccount(checkingOid.toLong()); checkingAccountList.remove(checkingOid); System.out.println("The size of checkingAccountList is now :"+checkingAccountList.size()); } Oid savingsOid = (Oid)savingsAccountList.get(0); if(savingsOid != null) { System.out.println("Delete Savings Account Number :"+savingsOid.toString()); teller.deleteAccount(savingsOid.toLong()); savingsAccountList.remove(savingsOid); System.out.println("The size of savingsAccountList is now :"+savingsAccountList.size()); } } catch (Exception exception) { exception.printStackTrace(); } } // Try transfering money from one account to the other if((checkingAccountList.size() > 0)&&(savingsAccountList.size() > 0)) { try { Oid fromOid = (Oid)checkingAccountList.get(0); Oid toOid = (Oid)savingsAccountList.get(0); if((fromOid != null) && (toOid != null)) { System.out.println("Transfer Money - 100 from Checking Account :"+ fromOid.toString()+ " to Savings Account :"+ toOid.toString()); boolean result = teller.transferMoney(fromOid.toLong(), toOid.toLong(), 100); if(result == true) { System.out.println("The Money Transfer was a SUCCESS"); } else { System.out.println("The Money Transfer was a FAILURE"); } } } catch (Exception exception) { exception.printStackTrace(); } } // remove the Teller log("Removing the teller"); teller.remove(); } /** * RMI/IIOP clients should use this narrow function */ private Object narrow(Object reference, Class classReference) { return PortableRemoteObject.narrow(reference, classReference); } /** * Lookup the EJBs home_ in the JNDI tree */ private TellerHome lookupHome() throws NamingException { // Lookup the beans home_ using JNDI Context context = getInitialContext(); try { Object home = context.lookup(JNDI_NAME_); return (TellerHome) narrow(home, TellerHome.class); } catch (NamingException namingException) { log("The client was unable to lookup the EJBHome. Please make sure "); log("that you have deployed the ejb with the JNDI name "+JNDI_NAME_+" on the WebLogic server at "+url_); throw namingException; } } /** * Using a Properties object will work on JDK 1.1.x and Java2 * clients */ private Context getInitialContext() throws NamingException { try { // Get an InitialContext Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, url_); return new InitialContext(properties); } catch (NamingException namingException) { log("We were unable to get a connection to the WebLogic server at "+url_); log("Please make sure that the server is running."); log(namingException.toString()); throw namingException; } } /** * This is the Java2 version to get an InitialContext. * This version relies on the existence of a jndi.properties file in * the application's classpath. * */ // private static Context getInitialContext() // throws NamingException // { // return new InitialContext(); // } private static void log(String string) { System.out.println(string); } } |
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. |
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 |
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. |