KickJava   Java API By Example, From Geeks To Geeks.

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


1 package ejb.client.dynamic;
2
3 import javax.xml.namespace.QName JavaDoc;
4
5 import org.apache.wsif.WSIFMessage;
6 import org.apache.wsif.WSIFException;
7 import org.apache.wsif.WSIFOperation;
8 import org.apache.wsif.WSIFPort;
9 import org.apache.wsif.WSIFService;
10 import org.apache.wsif.WSIFServiceFactory;
11
12 import ejb.service.addressbook.wsiftypes.Address;
13 import ejb.service.addressbook.wsiftypes.Phone;
14
15 public class Run {
16     private static void addFirstAddress(WSIFPort port) {
17     try {
18         // create the operation
19
// note that we have two operations with the same name, so we need to specify the
20
// name of the input and output messages as well
21
WSIFOperation operation = port.createOperation("addEntry","AddEntryWholeNameRequest",null);
22         // create the input message associated with this operation
23
WSIFMessage input = operation.createInputMessage();
24         // populate the input message
25
input.setObjectPart("name","John Smith");
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         input.setObjectPart("address",address);
39         // do the invocation
40
System.out.println("Adding address for John Smith...");
41         operation.executeInputOnlyOperation(input);
42     } catch (WSIFException we) {
43         System.out.println("Got exception from WSIF, details:");
44         we.printStackTrace();
45     }
46     }
47     
48     private static void addSecondAddress(WSIFPort port) {
49     try {
50         // create the operation
51
// note that we have two operations with the same name, so we need to specify the
52
// name of the input and output messages as well
53
WSIFOperation operation = port.createOperation("addEntry","AddEntryFirstAndLastNamesRequest",null);
54         // create the input message associated with this operation
55
WSIFMessage input = operation.createInputMessage();
56         // populate the input message
57
input.setObjectPart("firstName","Jane");
58         input.setObjectPart("lastName","White");
59         // create an address object to populate the input
60
Address address = new Address();
61         address.setStreetNum(20);
62         address.setStreetName("Peachtree Avenue");
63         address.setCity("Atlanta");
64         address.setState("GA");
65         address.setZip(39892);
66         Phone phone = new Phone();
67         phone.setAreaCode(701);
68         phone.setExchange("555");
69         phone.setNumber("8721");
70         address.setPhoneNumber(phone);
71         input.setObjectPart("address",address);
72         // do the invocation
73
System.out.println("Adding address for Jane White...");
74         operation.executeInputOnlyOperation(input);
75     } catch (WSIFException we) {
76         System.out.println("Got exception from WSIF, details:");
77         we.printStackTrace();
78     }
79     }
80
81     private static void queryAddresses(WSIFPort port) {
82     try {
83         // create the operation
84
WSIFOperation operation = port.createOperation("getAddressFromName");
85         // create the input message associated with this operation
86
WSIFMessage input = operation.createInputMessage();
87         WSIFMessage output = operation.createOutputMessage();
88         WSIFMessage fault = operation.createFaultMessage();
89         // populate the input message
90
input.setObjectPart("name","John Smith");
91         // do the invocation
92
System.out.println("Querying address for John Smith...");
93         if (operation.executeRequestResponseOperation(input,output,fault)) {
94         // invocation succeeded
95
// extract the address from the output message
96
Address address = (Address) output.getObjectPart("address");
97         System.out.println("Service returned the following address:");
98         System.out.println(address.getStreetNum()+" "+address.getStreetName()+
99                    ", "+address.getCity()+" "+address.getState()+" "+
100                    address.getZip()+"; Phone: ("+
101                    address.getPhoneNumber().getAreaCode()+") "+
102                    address.getPhoneNumber().getExchange()+"-"+
103                    address.getPhoneNumber().getNumber());
104         } else {
105         // invocation failed, check fault message
106
}
107         // create the operation
108
operation = port.createOperation("getAddressFromName");
109         // create the input message associated with this operation
110
input = operation.createInputMessage();
111         output = operation.createOutputMessage();
112         fault = operation.createFaultMessage();
113         // populate the input message
114
input.setObjectPart("name","Jane White");
115         // do the invocation
116
System.out.println("Querying address for Jane White...");
117         if (operation.executeRequestResponseOperation(input,output,fault)) {
118         // invocation succeeded
119
// extract the address from the output message
120
Address address = (Address) output.getObjectPart("address");
121         System.out.println("Service returned the following address:");
122         System.out.println(address.getStreetNum()+" "+address.getStreetName()+
123                    ", "+address.getCity()+" "+address.getState()+" "+
124                    address.getZip()+"; Phone: ("+
125                    address.getPhoneNumber().getAreaCode()+") "+
126                    address.getPhoneNumber().getExchange()+"-"+
127                    address.getPhoneNumber().getNumber());
128         } else {
129         // invocation failed, check fault message
130
}
131     } catch (WSIFException we) {
132         System.out.println("Got exception from WSIF, details:");
133         we.printStackTrace();
134     }
135     }
136
137     public static void main(String JavaDoc [] args) throws Exception JavaDoc {
138     if(args.length!=1) {
139         System.out.println("Usage: java ejb.client.dynamic.Run <wsdl location>");
140         System.exit(1);
141     }
142     // create a service factory
143
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
144     
145     WSIFService service = factory.getService(args[0], null,
146                          null, "http://wsifservice.addressbook/",
147                          "AddressBook");
148     // map types
149
service.mapType(new QName JavaDoc("http://wsiftypes.addressbook/","Address"),
150             Class.forName("ejb.service.addressbook.wsiftypes.Address"));
151     service.mapType(new QName JavaDoc("http://wsiftypes.addressbook/","Phone"),
152             Class.forName("ejb.service.addressbook.wsiftypes.Phone"));
153
154     // get the port
155
WSIFPort port = service.getPort();
156     // add the first address
157
addFirstAddress(port);
158     // add the second address
159
addSecondAddress(port);
160     // query addresses
161
queryAddresses(port);
162     }
163 }
164
Popular Tags