001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ClientCustomer.java 4745 2004-05-12 09:18:14Z camillej $
023: * --------------------------------------------------------------------------
024: */package xdoclet;
025:
026: import java.io.BufferedReader;
027: import java.io.FileInputStream;
028: import java.io.InputStreamReader;
029: import java.io.IOException;
030: import java.rmi.RemoteException;
031: import java.util.Collection;
032: import java.util.Iterator;
033: import java.util.Hashtable;
034: import java.util.Properties;
035: import javax.ejb.FinderException;
036: import javax.ejb.RemoveException;
037: import javax.transaction.UserTransaction;
038: import javax.naming.Context;
039: import javax.naming.InitialContext;
040: import javax.rmi.PortableRemoteObject;
041: import xdoclet.CustomerHomeRemote;
042: import xdoclet.CustomerRemote;
043:
044: /**
045: * Sample for entity beans CMP2 xdoclet
046: */
047:
048: public class ClientCustomer {
049:
050: private static UserTransaction utx = null;
051:
052: private static void PrintAllCustomerAndPhones(CustomerHomeRemote h) {
053: Iterator alist;
054: CustomerRemote customer;
055: String phone;
056: try {
057: utx.begin(); // faster if made inside a Tx
058: alist = h.findAllCustomers().iterator();
059: while (alist.hasNext()) {
060: customer = (CustomerRemote) alist.next();
061: System.out.println("Customer name is :"
062: + customer.getLastName() + " "
063: + customer.getFirstName());
064: Iterator phonelist = customer.getPhoneList().iterator();
065: while (phonelist.hasNext()) {
066: phone = (String) phonelist.next();
067: System.out.println("Phone number is: " + phone);
068: }
069: }
070: utx.commit();
071: } catch (Exception e) {
072: System.err
073: .println("Exception getting all Customer phone list: "
074: + e);
075: System.exit(2);
076: }
077: }
078:
079: public static void main(String[] args) {
080:
081: String beanName = "CustomerBeanHome";
082:
083: // get JNDI initial context
084: Context initialContext = null;
085: try {
086: initialContext = new InitialContext();
087: } catch (Exception e) {
088: System.err.println("Cannot get initial context for JNDI: "
089: + e);
090: System.exit(2);
091: }
092:
093: // We want to start transactions from client: get UserTransaction
094: System.out
095: .println("Getting a UserTransaction object from JNDI");
096: try {
097:
098: // Comment the following lines if you want to use a David Client:
099: utx = (UserTransaction) initialContext
100: .lookup("javax.transaction.UserTransaction");
101:
102: } catch (Exception e) {
103: System.err.println("Cannot lookup UserTransaction: " + e);
104: System.exit(2);
105: }
106:
107: // Connecting to Home thru JNDI
108: System.out.println("Connecting to the CustomerHome");
109: CustomerHomeRemote home = null;
110: try {
111: home = (CustomerHomeRemote) PortableRemoteObject.narrow(
112: initialContext.lookup(beanName),
113: CustomerHomeRemote.class);
114: } catch (Exception e) {
115: System.err.println("Cannot lookup " + beanName + ": " + e);
116: System.exit(2);
117: }
118:
119: // List existing customer
120: System.out
121: .println("Getting the list of existing Customer in database");
122: PrintAllCustomerAndPhones(home);
123:
124: // Create a first customer
125: System.out.println("Creating a new customer in database");
126: CustomerRemote c1 = null;
127: try {
128: c1 = home.create(new Integer(1), new Name("jerome",
129: "camilleri"));
130: } catch (Exception e) {
131: System.err.println("Cannot create Customer: " + e);
132: System.exit(2);
133: }
134:
135: // Add phone number
136: try {
137: c1.addPhoneNumber("0476254717", (byte) 0);
138: c1.addPhoneNumber("0625785511", (byte) 0);
139: } catch (Exception e) {
140: System.out.println("Problem during add phone number");
141: e.printStackTrace(System.out);
142: }
143: // List existing customer
144: System.out
145: .println("Getting the list of customer with Phone number in database");
146: PrintAllCustomerAndPhones(home);
147:
148: // Delete Account2
149: System.out
150: .println("Removing Customer and phone previously created in database");
151: try {
152: c1.removePhoneNumber((byte) 0);
153: c1.remove();
154: } catch (Exception e) {
155: System.err.println("exception during remove: " + e);
156: System.exit(2);
157: }
158:
159: // Exit program
160: System.out.println("ClientCustomer terminated");
161: }
162:
163: }
|