001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package samples.addr;
018:
019: import org.apache.axis.utils.Options;
020:
021: import java.net.URL;
022:
023: /**
024: * This class shows how to use the Call object's ability to
025: * become session aware.
026: *
027: * @author Rob Jellinghaus (robj@unrealities.com)
028: * @author Sanjiva Weerawarana <sanjiva@watson.ibm.com>
029: */
030: public class Main {
031: static String name1;
032: static Address addr1;
033: static Phone phone1;
034:
035: static {
036: name1 = "Purdue Boilermaker";
037: addr1 = new Address();
038: phone1 = new Phone();
039: addr1.setStreetNum(1);
040: addr1.setStreetName("University Drive");
041: addr1.setCity("West Lafayette");
042: addr1.setState(StateType.IN);
043: addr1.setZip(47907);
044: phone1.setAreaCode(765);
045: phone1.setExchange("494");
046: phone1.setNumber("4900");
047: addr1.setPhoneNumber(phone1);
048:
049: }
050:
051: private static void printAddress(Address ad) {
052: if (ad == null) {
053: System.err.println("\t[ADDRESS NOT FOUND!]");
054: return;
055: }
056: System.err.println("\t" + ad.getStreetNum() + " "
057: + ad.getStreetName());
058: System.err.println("\t" + ad.getCity() + ", " + ad.getState()
059: + " " + ad.getZip());
060: Phone ph = ad.getPhoneNumber();
061: System.err.println("\tPhone: (" + ph.getAreaCode() + ") "
062: + ph.getExchange() + "-" + ph.getNumber());
063: }
064:
065: private static Object doit(AddressBook ab) throws Exception {
066: System.err.println(">> Storing address for '" + name1 + "'");
067: ab.addEntry(name1, addr1);
068: System.err.println(">> Querying address for '" + name1 + "'");
069: Address resp = ab.getAddressFromName(name1);
070: System.err.println(">> Response is:");
071: printAddress(resp);
072:
073: // if we are NOT maintaining session, resp must be == null.
074: // If we ARE, resp must be != null.
075:
076: System.err.println(">> Querying address for '" + name1
077: + "' again");
078: resp = ab.getAddressFromName(name1);
079: System.err.println(">> Response is:");
080: printAddress(resp);
081: return resp;
082: }
083:
084: public static void main(String[] args) throws Exception {
085: Options opts = new Options(args);
086:
087: System.err.println("Using proxy without session maintenance.");
088: System.err
089: .println("(queries without session should say: \"ADDRESS NOT FOUND!\")");
090:
091: AddressBookService abs = new AddressBookServiceLocator();
092: opts.setDefaultURL(abs.getAddressBookAddress());
093: URL serviceURL = new URL(opts.getURL());
094:
095: AddressBook ab1 = null;
096: if (serviceURL == null) {
097: ab1 = abs.getAddressBook();
098: } else {
099: ab1 = abs.getAddressBook(serviceURL);
100: }
101: Object ret = doit(ab1);
102: if (ret != null) {
103: throw new Exception(
104: "non-session test expected null response, got "
105: + ret);
106: }
107:
108: System.err.println("\n\nUsing proxy with session maintenance.");
109: AddressBook ab2 = null;
110: if (serviceURL == null) {
111: ab2 = abs.getAddressBook();
112: } else {
113: ab2 = abs.getAddressBook(serviceURL);
114: }
115: ((AddressBookSOAPBindingStub) ab2).setMaintainSession(true);
116: ret = doit(ab2);
117: if (ret == null) {
118: throw new Exception(
119: "session test expected non-null response, got "
120: + ret);
121: }
122: }
123: }
|