1 package dslprovider; 2 3 28 29 import ejb.service.addressbook.wsifservice.AddressBook; 31 import ejb.service.addressbook.wsiftypes.Address; 32 33 import complexsoap.client.stub.com.cdyne.ws.LatLongReturn; 35 import complexsoap.client.stub.com.cdyne.ws.Zip2GeoSoap; 36 37 import jms.client.stub.org.apache.xml.CheckAvailabilityPortType; 39 40 import org.apache.wsif.WSIFException; 42 import org.apache.wsif.WSIFService; 43 import org.apache.wsif.WSIFServiceFactory; 44 import javax.xml.namespace.QName ; 45 46 import java.io.LineNumberReader ; 48 import java.io.InputStreamReader ; 49 50 public class ServiceChecker { 51 static AddressBook addressBook; 52 static Zip2GeoSoap zip2geo; 53 static CheckAvailabilityPortType serviceAvailability; 54 55 public static void printError(String 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 zip2geoWSDL, 66 String addressbookWSDL, 67 String serviceAvailabilityWSDL) throws Exception { 68 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 70 WSIFService service = 73 factory.getService(addressbookWSDL,null,null, 74 "http://wsifservice.addressbook/", 75 "AddressBook"); 76 addressBook = (AddressBook) service.getStub(AddressBook.class); 78 service = factory.getService(zip2geoWSDL,null,null,"http://ws.cdyne.com", 81 "Zip2GeoSoap"); 82 service.mapType(new QName ("http://ws.cdyne.com", "LatLongReturn"), 84 Class.forName("complexsoap.client.stub.com.cdyne.ws.LatLongReturn")); 85 zip2geo = (Zip2GeoSoap) service.getStub(Zip2GeoSoap.class); 87 service = factory.getService(serviceAvailabilityWSDL,null,null, 90 "http://xml.apache.org/axis/wsif/samples/jms/ServiceAvailability", 91 "CheckAvailabilityPortType"); 92 serviceAvailability = 94 (CheckAvailabilityPortType) service.getStub(CheckAvailabilityPortType.class); 95 } 96 97 private static Address lookupAddress(String name) throws Exception { 98 return addressBook.getAddressFromName(name); 100 } 101 102 private static Address createAndAddAddress(String name,String streetNum, String streetName, 103 String city, String state, String zip) throws Exception { 104 Address address = new Address(); 106 address.setStreetNum(new Integer (streetNum).intValue()); 107 address.setStreetName(streetName); 108 address.setCity(city); 109 address.setState(state); 110 address.setZip(new Integer (zip).intValue()); 111 address.setPhoneNumber(null); 112 addressBook.addEntry(name,address); 114 return address; 115 } 116 117 private static void verifyAddress(Address address) throws Exception { 118 String zipCode = ""+address.getZip(); 120 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 serviceIsAvailable(int zipCode) throws Exception { 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 reader = new LineNumberReader (new InputStreamReader (System.in)); 150 System.out.print("Name: "); 151 String name = reader.readLine(); 152 System.out.print("Street Number: "); 153 String streetNum = reader.readLine(); 154 System.out.print("Street Name: "); 155 String streetName = reader.readLine(); 156 System.out.print("City: "); 157 String city = reader.readLine(); 158 System.out.print("State: "); 159 String state = reader.readLine(); 160 System.out.print("Zip: "); 161 String zip = reader.readLine(); 162 System.out.println(); 163 System.out.println(); 164 Address address = null; 165 if (streetName==null || streetName.equals("")) { 168 System.out.println("Looking up address..."); 169 address = lookupAddress(name); 170 if (address==null) { 171 printError("Address for "+name+" wasn't found"); 173 } 174 } else { 175 System.out.println("Adding address to records..."); 177 address = createAndAddAddress(name,streetNum,streetName,city,state,zip); 178 } 179 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 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 choice = reader.readLine(); 209 if (choice.equals("q")) 210 System.exit(0); 211 } catch (Exception 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 [] args) { 219 try { 220 if (args.length!=3) { 225 printUsage(); 226 System.exit(1); 227 } 228 init(args[0],args[1],args[2]); 229 } catch (Exception 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 |