001: package ejb.client.dynamic;
002:
003: import javax.xml.namespace.QName;
004:
005: import org.apache.wsif.WSIFMessage;
006: import org.apache.wsif.WSIFException;
007: import org.apache.wsif.WSIFOperation;
008: import org.apache.wsif.WSIFPort;
009: import org.apache.wsif.WSIFService;
010: import org.apache.wsif.WSIFServiceFactory;
011:
012: import ejb.service.addressbook.wsiftypes.Address;
013: import ejb.service.addressbook.wsiftypes.Phone;
014:
015: public class Run {
016: private static void addFirstAddress(WSIFPort port) {
017: try {
018: // create the operation
019: // note that we have two operations with the same name, so we need to specify the
020: // name of the input and output messages as well
021: WSIFOperation operation = port.createOperation("addEntry",
022: "AddEntryWholeNameRequest", null);
023: // create the input message associated with this operation
024: WSIFMessage input = operation.createInputMessage();
025: // populate the input message
026: input.setObjectPart("name", "John Smith");
027: // create an address object to populate the input
028: Address address = new Address();
029: address.setStreetNum(25);
030: address.setStreetName("Willow Road");
031: address.setCity("MyTown");
032: address.setState("PA");
033: address.setZip(28382);
034: Phone phone = new Phone();
035: phone.setAreaCode(288);
036: phone.setExchange("555");
037: phone.setNumber("9891");
038: address.setPhoneNumber(phone);
039: input.setObjectPart("address", address);
040: // do the invocation
041: System.out.println("Adding address for John Smith...");
042: operation.executeInputOnlyOperation(input);
043: } catch (WSIFException we) {
044: System.out.println("Got exception from WSIF, details:");
045: we.printStackTrace();
046: }
047: }
048:
049: private static void addSecondAddress(WSIFPort port) {
050: try {
051: // create the operation
052: // note that we have two operations with the same name, so we need to specify the
053: // name of the input and output messages as well
054: WSIFOperation operation = port.createOperation("addEntry",
055: "AddEntryFirstAndLastNamesRequest", null);
056: // create the input message associated with this operation
057: WSIFMessage input = operation.createInputMessage();
058: // populate the input message
059: input.setObjectPart("firstName", "Jane");
060: input.setObjectPart("lastName", "White");
061: // create an address object to populate the input
062: Address address = new Address();
063: address.setStreetNum(20);
064: address.setStreetName("Peachtree Avenue");
065: address.setCity("Atlanta");
066: address.setState("GA");
067: address.setZip(39892);
068: Phone phone = new Phone();
069: phone.setAreaCode(701);
070: phone.setExchange("555");
071: phone.setNumber("8721");
072: address.setPhoneNumber(phone);
073: input.setObjectPart("address", address);
074: // do the invocation
075: System.out.println("Adding address for Jane White...");
076: operation.executeInputOnlyOperation(input);
077: } catch (WSIFException we) {
078: System.out.println("Got exception from WSIF, details:");
079: we.printStackTrace();
080: }
081: }
082:
083: private static void queryAddresses(WSIFPort port) {
084: try {
085: // create the operation
086: WSIFOperation operation = port
087: .createOperation("getAddressFromName");
088: // create the input message associated with this operation
089: WSIFMessage input = operation.createInputMessage();
090: WSIFMessage output = operation.createOutputMessage();
091: WSIFMessage fault = operation.createFaultMessage();
092: // populate the input message
093: input.setObjectPart("name", "John Smith");
094: // do the invocation
095: System.out.println("Querying address for John Smith...");
096: if (operation.executeRequestResponseOperation(input,
097: output, fault)) {
098: // invocation succeeded
099: // extract the address from the output message
100: Address address = (Address) output
101: .getObjectPart("address");
102: System.out
103: .println("Service returned the following address:");
104: System.out.println(address.getStreetNum() + " "
105: + address.getStreetName() + ", "
106: + address.getCity() + " " + address.getState()
107: + " " + address.getZip() + "; Phone: ("
108: + address.getPhoneNumber().getAreaCode() + ") "
109: + address.getPhoneNumber().getExchange() + "-"
110: + address.getPhoneNumber().getNumber());
111: } else {
112: // invocation failed, check fault message
113: }
114: // create the operation
115: operation = port.createOperation("getAddressFromName");
116: // create the input message associated with this operation
117: input = operation.createInputMessage();
118: output = operation.createOutputMessage();
119: fault = operation.createFaultMessage();
120: // populate the input message
121: input.setObjectPart("name", "Jane White");
122: // do the invocation
123: System.out.println("Querying address for Jane White...");
124: if (operation.executeRequestResponseOperation(input,
125: output, fault)) {
126: // invocation succeeded
127: // extract the address from the output message
128: Address address = (Address) output
129: .getObjectPart("address");
130: System.out
131: .println("Service returned the following address:");
132: System.out.println(address.getStreetNum() + " "
133: + address.getStreetName() + ", "
134: + address.getCity() + " " + address.getState()
135: + " " + address.getZip() + "; Phone: ("
136: + address.getPhoneNumber().getAreaCode() + ") "
137: + address.getPhoneNumber().getExchange() + "-"
138: + address.getPhoneNumber().getNumber());
139: } else {
140: // invocation failed, check fault message
141: }
142: } catch (WSIFException we) {
143: System.out.println("Got exception from WSIF, details:");
144: we.printStackTrace();
145: }
146: }
147:
148: public static void main(String[] args) throws Exception {
149: if (args.length != 1) {
150: System.out
151: .println("Usage: java ejb.client.dynamic.Run <wsdl location>");
152: System.exit(1);
153: }
154: // create a service factory
155: WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
156:
157: WSIFService service = factory.getService(args[0], null, null,
158: "http://wsifservice.addressbook/", "AddressBook");
159: // map types
160: service.mapType(new QName("http://wsiftypes.addressbook/",
161: "Address"), Class
162: .forName("ejb.service.addressbook.wsiftypes.Address"));
163: service.mapType(new QName("http://wsiftypes.addressbook/",
164: "Phone"), Class
165: .forName("ejb.service.addressbook.wsiftypes.Phone"));
166:
167: // get the port
168: WSIFPort port = service.getPort();
169: // add the first address
170: addFirstAddress(port);
171: // add the second address
172: addSecondAddress(port);
173: // query addresses
174: queryAddresses(port);
175: }
176: }
|