001: package dslprovider;
002:
003: /**
004: * This is a toy application that demonstrates how WSIF can be used to create flexible
005: * applications.
006: * This ServiceChecker application allows users to enter their names and addresses
007: * through a command line interface and then checks if DSL service is available in
008: * the user's area. If the user has previously registered their addresses, they need not
009: * enter them again. The application also verifies that addresses are correct.
010: *
011: * The application makes use of three WSDL-described services:
012: * 1. Zip2Geo: this service provides information about a US zip code, such as the
013: * corresponding city and state
014: * 2. AddressBook: this service allows names and addresses to be stored and
015: * looked up
016: * 3. ServiceAvailability: this service checks if DSL service is available at a
017: * particular zip code
018: * The locations of the WSDL documents corresponding to these services are provided at
019: * runtime as command line arguments.
020: *
021: * The key feature of this application is that since it is completely WSDL driven,
022: * we can swap the service protocols, change their location, add new protocols and
023: * make them available dynamically, etc. without having to touch the application code
024: * here.
025: *
026: * @author Nirmal Mukhi (nmukhi@us.ibm.com)
027: */
028:
029: // types for address book service
030: import ejb.service.addressbook.wsifservice.AddressBook;
031: import ejb.service.addressbook.wsiftypes.Address;
032:
033: // types for Zip2Geo service
034: import complexsoap.client.stub.com.cdyne.ws.LatLongReturn;
035: import complexsoap.client.stub.com.cdyne.ws.Zip2GeoSoap;
036:
037: // types for service availability service
038: import jms.client.stub.org.apache.xml.CheckAvailabilityPortType;
039:
040: // wsif classes
041: import org.apache.wsif.WSIFException;
042: import org.apache.wsif.WSIFService;
043: import org.apache.wsif.WSIFServiceFactory;
044: import javax.xml.namespace.QName;
045:
046: // java IO classes
047: import java.io.LineNumberReader;
048: import java.io.InputStreamReader;
049:
050: public class ServiceChecker {
051: static AddressBook addressBook;
052: static Zip2GeoSoap zip2geo;
053: static CheckAvailabilityPortType serviceAvailability;
054:
055: public static void printError(String message) {
056: System.err.println(message);
057: System.exit(1);
058: }
059:
060: public static void printUsage() {
061: System.out
062: .println("java dslprovider.ServiceChecker <zip2geo service WSDL>"
063: + "<addressbook service WSDL> <serviceAvailability service WSDL>");
064: }
065:
066: private static void init(String zip2geoWSDL,
067: String addressbookWSDL, String serviceAvailabilityWSDL)
068: throws Exception {
069: // create a service factory
070: WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
071: // initialize the address book stub
072: // parse the WSDL
073: WSIFService service = factory.getService(addressbookWSDL, null,
074: null, "http://wsifservice.addressbook/", "AddressBook");
075: // create the stub
076: addressBook = (AddressBook) service.getStub(AddressBook.class);
077: // initialize the zip2geo stub
078: // parse the WSDL
079: service = factory.getService(zip2geoWSDL, null, null,
080: "http://ws.cdyne.com", "Zip2GeoSoap");
081: // map types
082: service
083: .mapType(
084: new QName("http://ws.cdyne.com",
085: "LatLongReturn"),
086: Class
087: .forName("complexsoap.client.stub.com.cdyne.ws.LatLongReturn"));
088: // create the stub
089: zip2geo = (Zip2GeoSoap) service.getStub(Zip2GeoSoap.class);
090: // initialize the service availability stub
091: // parse the WSDL
092: service = factory
093: .getService(
094: serviceAvailabilityWSDL,
095: null,
096: null,
097: "http://xml.apache.org/axis/wsif/samples/jms/ServiceAvailability",
098: "CheckAvailabilityPortType");
099: // create the stub
100: serviceAvailability = (CheckAvailabilityPortType) service
101: .getStub(CheckAvailabilityPortType.class);
102: }
103:
104: private static Address lookupAddress(String name) throws Exception {
105: // lookup and return the address for that name
106: return addressBook.getAddressFromName(name);
107: }
108:
109: private static Address createAndAddAddress(String name,
110: String streetNum, String streetName, String city,
111: String state, String zip) throws Exception {
112: // create an address
113: Address address = new Address();
114: address.setStreetNum(new Integer(streetNum).intValue());
115: address.setStreetName(streetName);
116: address.setCity(city);
117: address.setState(state);
118: address.setZip(new Integer(zip).intValue());
119: address.setPhoneNumber(null);
120: // add an entry to the addressbook
121: addressBook.addEntry(name, address);
122: return address;
123: }
124:
125: private static void verifyAddress(Address address) throws Exception {
126: // extract the zip code from the address
127: String zipCode = "" + address.getZip();
128: // look up information for that zip
129: LatLongReturn zipInfo = zip2geo.GetLatLong(zipCode, "");
130: if (!zipInfo.getCity().equals(address.getCity())) {
131: printError("Zip " + zipCode + " is in city "
132: + zipInfo.getCity() + ", not city "
133: + address.getCity() + " as you specified");
134: }
135: if (!zipInfo.getStateAbbrev().equals(address.getState())) {
136: printError("Zip " + zipCode + " is in state "
137: + zipInfo.getStateAbbrev() + ", not state "
138: + address.getState() + " as you specified");
139: }
140: }
141:
142: private static String serviceIsAvailable(int zipCode)
143: throws Exception {
144: return serviceAvailability.checkAvailability("" + zipCode);
145: }
146:
147: private static void loopInput() {
148: try {
149: System.out.println("************************");
150: System.out.println("WELCOME TO FAST DSL INC.");
151: System.out.println("************************");
152: System.out
153: .println("\n\nInterested in DSL service? Enter your address "
154: + "in the form below and we will check whether we have "
155: + "service available in your area.");
156: System.out.println();
157: System.out
158: .println("If you have previously expressed interest, just enter "
159: + "your name (leave other fields blank) and we will look "
160: + "up the rest of the information "
161: + "in our records");
162: LineNumberReader reader = new LineNumberReader(
163: new InputStreamReader(System.in));
164: System.out.print("Name: ");
165: String name = reader.readLine();
166: System.out.print("Street Number: ");
167: String streetNum = reader.readLine();
168: System.out.print("Street Name: ");
169: String streetName = reader.readLine();
170: System.out.print("City: ");
171: String city = reader.readLine();
172: System.out.print("State: ");
173: String state = reader.readLine();
174: System.out.print("Zip: ");
175: String zip = reader.readLine();
176: System.out.println();
177: System.out.println();
178: Address address = null;
179: // if street is blank, look for name in addressbook service
180: // otherwise assume this is a new user and add information to addressbook
181: if (streetName == null || streetName.equals("")) {
182: System.out.println("Looking up address...");
183: address = lookupAddress(name);
184: if (address == null) {
185: // if address wasn't found, we have a problem
186: printError("Address for " + name + " wasn't found");
187: }
188: } else {
189: // create address from provided information and add to address book
190: System.out.println("Adding address to records...");
191: address = createAndAddAddress(name, streetNum,
192: streetName, city, state, zip);
193: }
194: // verify that address is correct
195: System.out.println();
196: System.out.println();
197: System.out.println("Verifying validity of address...");
198: verifyAddress(address);
199: System.out.println();
200: System.out.println();
201: // check if we offer DSL service in that zip code
202: System.out.println("Customer: " + name);
203: System.out.println("Address: " + address.getStreetNum()
204: + " " + address.getStreetName() + ", "
205: + address.getCity() + " " + address.getState()
206: + " " + address.getZip());
207: System.out.println("Checking service availability...");
208: if (serviceIsAvailable(address.getZip()).equals("true")) {
209: System.out
210: .println("Yes, we offer service in your area,"
211: + "please call 800 555 FST-DSL to order");
212: } else {
213: System.out
214: .println("No, we do not offer service in your area");
215: }
216: System.out.println();
217: System.out.println();
218: System.out
219: .println("Enter 'q' to quit, any other key continue...");
220: String choice = reader.readLine();
221: if (choice.equals("q"))
222: System.exit(0);
223: } catch (Exception e) {
224: System.out
225: .println("ServiceChecker application got exception "
226: + e);
227: System.out.println("Details:");
228: e.printStackTrace();
229: }
230: }
231:
232: public static void main(String[] args) {
233: try {
234: // we must have three args
235: // args[0] is the location of the Zip2Geo service WSDL
236: // args[1] is the location of the AddressBook service WSDL
237: // args[2] is the location of the ServiceAvailability service WSDL
238: if (args.length != 3) {
239: printUsage();
240: System.exit(1);
241: }
242: init(args[0], args[1], args[2]);
243: } catch (Exception e) {
244: System.out
245: .println("ServiceChecker application got exception "
246: + e);
247: System.out.println("Details:");
248: e.printStackTrace();
249: }
250: while (true)
251: loopInput();
252: }
253: }
|