KickJava   Java API By Example, From Geeks To Geeks.

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


1 package localjava.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 localjava.client.stub.addressbook.wsiftypes.Address;
13 import localjava.client.stub.addressbook.wsiftypes.Phone;
14
15 public class Run {
16
17     private static void addFirstAddress(WSIFPort port) {
18         try {
19             // create the operation
20
// note that we have two operations with the same name,
21
// so we need to specify the name of the input and output
22
// messages as well
23
WSIFOperation operation =
24                 port.createOperation(
25                     "addEntry",
26                     "AddEntryWholeNameRequest",
27                     null);
28                     
29             // create the input message associated with this operation
30
WSIFMessage input = operation.createInputMessage();
31
32             // populate the input message
33
input.setObjectPart("name", "John Smith");
34
35             // create an address object to populate the input
36
Address address = new Address();
37             address.setStreetNum(25);
38             address.setStreetName("Willow Road");
39             address.setCity("MyTown");
40             address.setState("PA");
41             address.setZip(28382);
42             Phone phone = new Phone();
43             phone.setAreaCode(288);
44             phone.setExchange("555");
45             phone.setNumber("9891");
46             address.setPhoneNumber(phone);
47
48             input.setObjectPart("address", address);
49
50             // do the invocation
51
System.out.println("Adding address for John Smith...");
52             operation.executeInputOnlyOperation(input);
53
54         } catch (WSIFException we) {
55             System.out.println("Got exception from WSIF, details:");
56             we.printStackTrace();
57         }
58     }
59
60     private static void addSecondAddress(WSIFPort port) {
61         try {
62
63             // create the operation
64
// note that we have two operations with the same name, so we need to specify the
65
// name of the input and output messages as well
66
WSIFOperation operation =
67                 port.createOperation(
68                     "addEntry",
69                     "AddEntryFirstAndLastNamesRequest",
70                     null);
71
72             // create the input message associated with this operation
73
WSIFMessage input = operation.createInputMessage();
74
75             // populate the input message
76
input.setObjectPart("firstName", "Jane");
77             input.setObjectPart("lastName", "White");
78
79             // create an address object to populate the input
80
Address address = new Address();
81             address.setStreetNum(20);
82             address.setStreetName("Peachtree Avenue");
83             address.setCity("Atlanta");
84             address.setState("GA");
85             address.setZip(39892);
86             Phone phone = new Phone();
87             phone.setAreaCode(701);
88             phone.setExchange("555");
89             phone.setNumber("8721");
90             address.setPhoneNumber(phone);
91
92             input.setObjectPart("address", address);
93
94             // do the invocation
95
System.out.println("Adding address for Jane White...");
96             operation.executeInputOnlyOperation(input);
97
98         } catch (WSIFException we) {
99             System.out.println("Got exception from WSIF, details:");
100             we.printStackTrace();
101         }
102     }
103
104     private static void queryAddresses(WSIFPort port) {
105         try {
106             // create the operation
107
WSIFOperation operation =
108                 port.createOperation("getAddressFromName");
109
110             // create the input message associated with this operation
111
WSIFMessage input = operation.createInputMessage();
112             WSIFMessage output = operation.createOutputMessage();
113             WSIFMessage fault = operation.createFaultMessage();
114
115             // populate the input message
116
input.setObjectPart("name", "John Smith");
117
118             // do the invocation
119
System.out.println("Querying address for John Smith...");
120             if (operation.executeRequestResponseOperation(input, output, fault)) {
121                 // invocation succeeded
122
// extract the address from the output message
123
Address address = (Address) output.getObjectPart("address");
124                 System.out.println("Service returned the following address:");
125                 System.out.println(
126                     address.getStreetNum()
127                         + " "
128                         + address.getStreetName()
129                         + ", "
130                         + address.getCity()
131                         + " "
132                         + address.getState()
133                         + " "
134                         + address.getZip()
135                         + "; Phone: ("
136                         + address.getPhoneNumber().getAreaCode()
137                         + ") "
138                         + address.getPhoneNumber().getExchange()
139                         + "-"
140                         + address.getPhoneNumber().getNumber());
141             } else {
142                 // invocation failed, check fault message
143
}
144  
145             // create the operation
146
operation = port.createOperation("getAddressFromName");
147  
148             // create the input message associated with this operation
149
input = operation.createInputMessage();
150             output = operation.createOutputMessage();
151             fault = operation.createFaultMessage();
152  
153             // populate the input message
154
input.setObjectPart("name", "Jane White");
155  
156             // do the invocation
157
System.out.println("Querying address for Jane White...");
158             if (operation.executeRequestResponseOperation(input, output, fault)) {
159                 // invocation succeeded
160
// extract the address from the output message
161
Address address = (Address) output.getObjectPart("address");
162                 System.out.println("Service returned the following address:");
163                 System.out.println(
164                     address.getStreetNum()
165                         + " "
166                         + address.getStreetName()
167                         + ", "
168                         + address.getCity()
169                         + " "
170                         + address.getState()
171                         + " "
172                         + address.getZip()
173                         + "; Phone: ("
174                         + address.getPhoneNumber().getAreaCode()
175                         + ") "
176                         + address.getPhoneNumber().getExchange()
177                         + "-"
178                         + address.getPhoneNumber().getNumber());
179             } else {
180                 // invocation failed, check fault message
181
}
182         } catch (WSIFException we) {
183             System.out.println("Got exception from WSIF, details:");
184             we.printStackTrace();
185         }
186     }
187
188     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
189         if (args.length != 1) {
190             System.out.println(
191                 "Usage: java samples.java.client.dynamic.Run <wsdl location>");
192             System.exit(1);
193         }
194
195         // create a service factory
196
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
197         WSIFService service =
198             factory.getService(
199                 args[0],
200                 null,
201                 null,
202                 "http://wsifservice.addressbook/",
203                 "AddressBook");
204
205         // map types
206
service.mapType(
207             new QName JavaDoc("http://wsiftypes.addressbook/", "Address"),
208             Class.forName("localjava.client.stub.addressbook.wsiftypes.Address"));
209         service.mapType(
210             new QName JavaDoc("http://wsiftypes.addressbook/", "Phone"),
211             Class.forName("localjava.client.stub.addressbook.wsiftypes.Phone"));
212
213         // get the port
214
WSIFPort port = service.getPort();
215
216         // add the first address
217
addFirstAddress(port);
218
219         // add the second address
220
addSecondAddress(port);
221
222         // query addresses
223
queryAddresses(port);
224     }
225 }
226
Popular Tags