KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dslprovider > ServiceChecker


1 package dslprovider;
2
3 /**
4  * This is a toy application that demonstrates how WSIF can be used to create flexible
5  * applications.
6  * This ServiceChecker application allows users to enter their names and addresses
7  * through a command line interface and then checks if DSL service is available in
8  * the user's area. If the user has previously registered their addresses, they need not
9  * enter them again. The application also verifies that addresses are correct.
10  *
11  * The application makes use of three WSDL-described services:
12  * 1. Zip2Geo: this service provides information about a US zip code, such as the
13  * corresponding city and state
14  * 2. AddressBook: this service allows names and addresses to be stored and
15  * looked up
16  * 3. ServiceAvailability: this service checks if DSL service is available at a
17  * particular zip code
18  * The locations of the WSDL documents corresponding to these services are provided at
19  * runtime as command line arguments.
20  *
21  * The key feature of this application is that since it is completely WSDL driven,
22  * we can swap the service protocols, change their location, add new protocols and
23  * make them available dynamically, etc. without having to touch the application code
24  * here.
25  *
26  * @author Nirmal Mukhi (nmukhi@us.ibm.com)
27  */

28
29 // types for address book service
30
import ejb.service.addressbook.wsifservice.AddressBook;
31 import ejb.service.addressbook.wsiftypes.Address;
32
33 // types for Zip2Geo service
34
import complexsoap.client.stub.com.cdyne.ws.LatLongReturn;
35 import complexsoap.client.stub.com.cdyne.ws.Zip2GeoSoap;
36
37 // types for service availability service
38
import jms.client.stub.org.apache.xml.CheckAvailabilityPortType;
39
40 // wsif classes
41
import org.apache.wsif.WSIFException;
42 import org.apache.wsif.WSIFService;
43 import org.apache.wsif.WSIFServiceFactory;
44 import javax.xml.namespace.QName JavaDoc;
45
46 // java IO classes
47
import java.io.LineNumberReader JavaDoc;
48 import java.io.InputStreamReader JavaDoc;
49
50 public class ServiceChecker {
51     static AddressBook addressBook;
52     static Zip2GeoSoap zip2geo;
53     static CheckAvailabilityPortType serviceAvailability;
54
55     public static void printError(String JavaDoc message) {
56     System.err.println(message);
57     System.exit(1);
58     }
59
60     public static void printUsage() {
61     System.out.println("java dslprovider.ServiceChecker <zip2geo service WSDL>"+
62                "<addressbook service WSDL> <serviceAvailability service WSDL>");
63     }
64
65     private static void init(String JavaDoc zip2geoWSDL,
66                  String JavaDoc addressbookWSDL,
67                  String JavaDoc serviceAvailabilityWSDL) throws Exception JavaDoc {
68     // create a service factory
69
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
70     // initialize the address book stub
71
// parse the WSDL
72
WSIFService service =
73         factory.getService(addressbookWSDL,null,null,
74                    "http://wsifservice.addressbook/",
75                    "AddressBook");
76     // create the stub
77
addressBook = (AddressBook) service.getStub(AddressBook.class);
78     // initialize the zip2geo stub
79
// parse the WSDL
80
service = factory.getService(zip2geoWSDL,null,null,"http://ws.cdyne.com",
81                      "Zip2GeoSoap");
82     // map types
83
service.mapType(new QName JavaDoc("http://ws.cdyne.com", "LatLongReturn"),
84             Class.forName("complexsoap.client.stub.com.cdyne.ws.LatLongReturn"));
85     // create the stub
86
zip2geo = (Zip2GeoSoap) service.getStub(Zip2GeoSoap.class);
87     // initialize the service availability stub
88
// parse the WSDL
89
service = factory.getService(serviceAvailabilityWSDL,null,null,
90                      "http://xml.apache.org/axis/wsif/samples/jms/ServiceAvailability",
91                      "CheckAvailabilityPortType");
92     // create the stub
93
serviceAvailability =
94         (CheckAvailabilityPortType) service.getStub(CheckAvailabilityPortType.class);
95     }
96
97     private static Address lookupAddress(String JavaDoc name) throws Exception JavaDoc {
98     // lookup and return the address for that name
99
return addressBook.getAddressFromName(name);
100     }
101
102     private static Address createAndAddAddress(String JavaDoc name,String JavaDoc streetNum, String JavaDoc streetName,
103                 String JavaDoc city, String JavaDoc state, String JavaDoc zip) throws Exception JavaDoc {
104     // create an address
105
Address address = new Address();
106     address.setStreetNum(new Integer JavaDoc(streetNum).intValue());
107     address.setStreetName(streetName);
108     address.setCity(city);
109     address.setState(state);
110     address.setZip(new Integer JavaDoc(zip).intValue());
111     address.setPhoneNumber(null);
112     // add an entry to the addressbook
113
addressBook.addEntry(name,address);
114     return address;
115     }
116
117     private static void verifyAddress(Address address) throws Exception JavaDoc {
118     // extract the zip code from the address
119
String JavaDoc zipCode = ""+address.getZip();
120     // look up information for that zip
121
LatLongReturn zipInfo = zip2geo.GetLatLong(zipCode,"");
122     if (!zipInfo.getCity().equals(address.getCity())) {
123         printError("Zip "+zipCode+" is in city "+zipInfo.getCity()+
124                ", not city "+address.getCity()+" as you specified");
125     }
126     if (!zipInfo.getStateAbbrev().equals(address.getState())) {
127         printError("Zip "+zipCode+" is in state "+zipInfo.getStateAbbrev()+
128                ", not state "+address.getState()+" as you specified");
129     }
130     }
131
132     private static String JavaDoc serviceIsAvailable(int zipCode) throws Exception JavaDoc {
133     return serviceAvailability.checkAvailability(""+zipCode);
134     }
135
136     private static void loopInput() {
137     try {
138         System.out.println("************************");
139         System.out.println("WELCOME TO FAST DSL INC.");
140         System.out.println("************************");
141         System.out.println("\n\nInterested in DSL service? Enter your address "+
142                    "in the form below and we will check whether we have "+
143                    "service available in your area.");
144         System.out.println();
145         System.out.println("If you have previously expressed interest, just enter "+
146                    "your name (leave other fields blank) and we will look "+
147                    "up the rest of the information "+
148                    "in our records");
149         LineNumberReader JavaDoc reader = new LineNumberReader JavaDoc(new InputStreamReader JavaDoc(System.in));
150         System.out.print("Name: ");
151         String JavaDoc name = reader.readLine();
152         System.out.print("Street Number: ");
153         String JavaDoc streetNum = reader.readLine();
154         System.out.print("Street Name: ");
155         String JavaDoc streetName = reader.readLine();
156         System.out.print("City: ");
157         String JavaDoc city = reader.readLine();
158         System.out.print("State: ");
159         String JavaDoc state = reader.readLine();
160         System.out.print("Zip: ");
161         String JavaDoc zip = reader.readLine();
162         System.out.println();
163         System.out.println();
164         Address address = null;
165         // if street is blank, look for name in addressbook service
166
// otherwise assume this is a new user and add information to addressbook
167
if (streetName==null || streetName.equals("")) {
168         System.out.println("Looking up address...");
169         address = lookupAddress(name);
170         if (address==null) {
171             // if address wasn't found, we have a problem
172
printError("Address for "+name+" wasn't found");
173         }
174         } else {
175         // create address from provided information and add to address book
176
System.out.println("Adding address to records...");
177         address = createAndAddAddress(name,streetNum,streetName,city,state,zip);
178         }
179         // verify that address is correct
180
System.out.println();
181         System.out.println();
182         System.out.println("Verifying validity of address...");
183         verifyAddress(address);
184         System.out.println();
185         System.out.println();
186         // check if we offer DSL service in that zip code
187
System.out.println("Customer: "+name);
188         System.out.println("Address: "+
189                    address.getStreetNum()
190                    + " "
191                    + address.getStreetName()
192                    + ", "
193                    + address.getCity()
194                    + " "
195                    + address.getState()
196                    + " "
197                    + address.getZip());
198         System.out.println("Checking service availability...");
199         if (serviceIsAvailable(address.getZip()).equals("true")) {
200         System.out.println("Yes, we offer service in your area,"+
201                    "please call 800 555 FST-DSL to order");
202         } else {
203         System.out.println("No, we do not offer service in your area");
204         }
205         System.out.println();
206         System.out.println();
207         System.out.println("Enter 'q' to quit, any other key continue...");
208         String JavaDoc choice = reader.readLine();
209         if (choice.equals("q"))
210         System.exit(0);
211     } catch (Exception JavaDoc e) {
212         System.out.println("ServiceChecker application got exception "+e);
213         System.out.println("Details:");
214         e.printStackTrace();
215     }
216     }
217
218     public static void main(String JavaDoc [] args) {
219     try {
220         // we must have three args
221
// args[0] is the location of the Zip2Geo service WSDL
222
// args[1] is the location of the AddressBook service WSDL
223
// args[2] is the location of the ServiceAvailability service WSDL
224
if (args.length!=3) {
225         printUsage();
226         System.exit(1);
227         }
228         init(args[0],args[1],args[2]);
229     } catch (Exception JavaDoc e) {
230         System.out.println("ServiceChecker application got exception "+e);
231         System.out.println("Details:");
232         e.printStackTrace();
233     }
234     while(true)
235         loopInput();
236     }
237 }
238
Popular Tags