FileDocCategorySizeDatePackage
AmazonClient.javaAPI DocExample4737Thu Feb 13 22:22:40 GMT 2003ora.jwsnut.chapter1.client

AmazonClient.java

package ora.jwsnut.chapter1.client;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.Stub;
import ora.jwsnut.chapter1.amazon.AmazonSearchPort;
import ora.jwsnut.chapter1.amazon.AmazonSearchService;
import ora.jwsnut.chapter1.amazon.AmazonSearchService_Impl;
import ora.jwsnut.chapter1.amazon.AsinRequest;
import ora.jwsnut.chapter1.amazon.AuthorRequest;
import ora.jwsnut.chapter1.amazon.KeywordRequest;
import ora.jwsnut.chapter1.amazon.ProductInfo;

public class AmazonClient implements PropertyChangeListener {

    // Mode value
    private static final String MODE = "books";

    // Web service tag
    private static final String WEBSERVICE = "webservices-20";

    // Type value
    private static final String TYPE = "heavy";

    // Version code
    private static final String VERSION = "1.0";

    // Access to the Amazon web service
    AmazonSearchPort amazonSearch;

    // Developer's tag
    private String devtag;

    // The GUI
    private AmazonClientGUI gui;

    // The last type used in a search
    private int lastType = -1;

    // The last key used in a search
    private String lastKey = "";

    // The last page number sent
    private int page = 1;

    public void execute(String devtag, String address) throws Throwable {

        // Save the developer's tag
        this.devtag = devtag;

        // Build the user interface
        buildGUI();

        // Get the Service object for Amazon.com
        // and configure a stub to get access to it.
        AmazonSearchService service = new AmazonSearchService_Impl();
        amazonSearch = service.getAmazonSearchPort();
        ((Stub)amazonSearch)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, address);
    }

    // Implements the PropertyChangeListener interface
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(AmazonClientGUI.SEARCH_PROPERTY)) {
            int type = gui.getSearchType();
            String key = gui.getSearchKey();
            if (type >= 0 && key != null) {
                // Need to search....
                doSearch(type, key);
            }
        }
    }

    // Conducts a search at Amazon.com.
    public void doSearch(int type, String key) {
        // If this search is the same as the last, increment
        // the page number.
        String newKey = key.trim();
        if (type == lastType && newKey.equals(lastKey)) {
            page++;
        } else {
            lastType = type;
            lastKey = newKey;
        }

        ProductInfo result = null;
        try {
            switch (type) {
            case AmazonClientGUI.KEYWORD_SEARCH:
                KeywordRequest keywordReq = new KeywordRequest(newKey,
                                                    String.valueOf(page), MODE,
                                                    WEBSERVICE, TYPE,
                                                    devtag, VERSION);
                result = amazonSearch.keywordSearchRequest(keywordReq);
                break;

            case AmazonClientGUI.AUTHOR_SEARCH:
                AuthorRequest authorReq = new AuthorRequest(newKey,
                                                    String.valueOf(page), MODE,
                                                    WEBSERVICE, TYPE,
                                                    devtag, VERSION);
                result = amazonSearch.authorSearchRequest(authorReq);
                break;

            case AmazonClientGUI.ASIN_SEARCH:
                AsinRequest asinReq = new AsinRequest(newKey, WEBSERVICE,
                                                    TYPE, devtag, VERSION);
                result = amazonSearch.asinSearchRequest(asinReq);
                break;
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        } finally {
            gui.setResults(result);
        }
    }

    private void buildGUI() {
        gui = new AmazonClientGUI();

        // Register to receive notification that a search is required.
        gui.addPropertyChangeListener(this);
    }

    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("Must supply your developer tag and service address");
            System.exit(1);
        }
        try {
            new AmazonClient().execute(args[0], args[1]);
        } catch (Throwable t) {
            t.printStackTrace(System.out);
        }
    }
}