KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ejb > client > stub > Run


1 package ejb.client.stub;
2
3 import java.rmi.RemoteException JavaDoc;
4
5 import ejb.service.addressbook.wsifservice.AddressBook;
6 import ejb.service.addressbook.wsiftypes.Address;
7 import ejb.service.addressbook.wsiftypes.Phone;
8
9 import org.apache.wsif.WSIFException;
10 import org.apache.wsif.WSIFService;
11 import org.apache.wsif.WSIFServiceFactory;
12
13 /**
14  * Class that runs the ejb sample using a pregenerated stub interface
15  * To use this class provide the location of the address book service's WSDL
16  * location on the command line. WSIF
17  * should then invoke the EJB service for populating and then
18  * querying an addressbook.
19  * @author Nirmal K. Mukhi (nmukhi@us.ibm.com)
20  */

21
22 public class Run {
23     private static void addFirstAddress(AddressBook addressBook) {
24         try {
25             
26             // create an address object to populate the input
27
Address address = new Address();
28             address.setStreetNum(25);
29             address.setStreetName("Willow Road");
30             address.setCity("MyTown");
31             address.setState("PA");
32             address.setZip(28382);
33             Phone phone = new Phone();
34             phone.setAreaCode(288);
35             phone.setExchange("555");
36             phone.setNumber("9891");
37             address.setPhoneNumber(phone);
38
39             // do the invocation
40
System.out.println("Adding address for John Smith...");
41             addressBook.addEntry("John Smith", address);
42
43         } catch (WSIFException we) {
44             System.out.println("Got exception from WSIF, details:");
45             we.printStackTrace();
46         } catch (RemoteException JavaDoc re) {
47             System.out.println("Got exception while invoking stub, details:");
48             re.printStackTrace();
49         }
50     }
51
52     private static void addSecondAddress(AddressBook addressBook) {
53         try {
54             
55             // create an address object to populate the input
56
Address address = new Address();
57             address.setStreetNum(20);
58             address.setStreetName("Peachtree Avenue");
59             address.setCity("Atlanta");
60             address.setState("GA");
61             address.setZip(39892);
62             Phone phone = new Phone();
63             phone.setAreaCode(701);
64             phone.setExchange("555");
65             phone.setNumber("8721");
66             address.setPhoneNumber(phone);
67
68             // do the invocation
69
System.out.println("Adding address for Jane White...");
70             addressBook.addEntry("Jane", "White", address);
71
72         } catch (WSIFException we) {
73             System.out.println("Got exception from WSIF, details:");
74             we.printStackTrace();
75         } catch (RemoteException JavaDoc re) {
76             System.out.println("Got exception while invoking stub, details:");
77             re.printStackTrace();
78         }
79     }
80
81     private static void queryAddresses(AddressBook addressBook) {
82         try {
83
84             // do the invocation
85
System.out.println("Querying address for John Smith...");
86             Address address = addressBook.getAddressFromName("John Smith");
87
88             System.out.println("Service returned the following address:");
89             System.out.println(
90                 address.getStreetNum()
91                     + " "
92                     + address.getStreetName()
93                     + ", "
94                     + address.getCity()
95                     + " "
96                     + address.getState()
97                     + " "
98                     + address.getZip()
99                     + "; Phone: ("
100                     + address.getPhoneNumber().getAreaCode()
101                     + ") "
102                     + address.getPhoneNumber().getExchange()
103                     + "-"
104                     + address.getPhoneNumber().getNumber());
105
106             System.out.println("Querying address for Jane White...");
107             address = addressBook.getAddressFromName("Jane White");
108
109             System.out.println("Service returned the following address:");
110             System.out.println(
111                 address.getStreetNum()
112                     + " "
113                     + address.getStreetName()
114                     + ", "
115                     + address.getCity()
116                     + " "
117                     + address.getState()
118                     + " "
119                     + address.getZip()
120                     + "; Phone: ("
121                     + address.getPhoneNumber().getAreaCode()
122                     + ") "
123                     + address.getPhoneNumber().getExchange()
124                     + "-"
125                     + address.getPhoneNumber().getNumber());
126
127         } catch (WSIFException we) {
128             System.out.println("Got exception from WSIF, details:");
129             we.printStackTrace();
130         } catch (RemoteException JavaDoc re) {
131             System.out.println("Got exception while invoking stub, details:");
132             re.printStackTrace();
133         }
134     }
135
136     public static void main(String JavaDoc[] args) {
137         try {
138             if (args.length != 1) {
139                 System.out.println(
140                     "Usage: java ejb.client.stub.Run <wsdl location>");
141                 System.exit(1);
142             }
143
144             // create a service factory
145
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
146
147             // parse WSDL
148
WSIFService service =
149                 factory.getService(
150                     args[0],
151                     null,
152                     null,
153                     "http://wsifservice.addressbook/",
154                     "AddressBook");
155
156             // create the stub
157
AddressBook stub =
158                 (AddressBook) service.getStub(AddressBook.class);
159
160             // do the invocations
161
addFirstAddress(stub);
162             addSecondAddress(stub);
163             queryAddresses(stub);
164
165         } catch (WSIFException we) {
166             System.out.println("Got exception from WSIF, details:");
167             we.printStackTrace();
168         }
169     }
170 }
171
Popular Tags