FileDocCategorySizeDatePackage
PeopleFinderRPCClient.javaAPI DocExample5630Thu Dec 15 21:39:42 GMT 2005com.oreilly.jent.people.soap

PeopleFinderRPCClient.java

package com.oreilly.jent.people.soap;

/**
 * In general, you may use the code in this book in your programs and 
 * documentation. You do not need to contact us for permission unless 
 * you're reproducing a significant portion of the code. For example, 
 * writing a program that uses several chunks of code from this book does 
 * not require permission. Selling or distributing a CD-ROM of examples 
 * from O'Reilly books does require permission. Answering a question by 
 * citing this book and quoting example code does not require permission. 
 * Incorporating a significant amount of example code from this book into 
 * your product's documentation does require permission.
 * 
 * We appreciate, but do not require, attribution. An attribution usually 
 * includes the title, author, publisher, and ISBN. For example: 
 * 
 *   "Java Enterprise in a Nutshell, Third Edition, 
 *    by Jim Farley and William Crawford 
 *    with Prakash Malani, John G. Norman, and Justin Gehtland. 
 *    Copyright 2006 O'Reilly Media, Inc., 0-596-10142-2."
 *  
 *  If you feel your use of code examples falls outside fair use or the 
 *  permission given above, feel free to contact us at 
 *  permissions@oreilly.com.
 */

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Logger;

import javax.xml.rpc.ServiceException;

import com.oreilly.jent.people.PersonDAO;

import com.oreilly.jent.people.soap.pfclient.PeopleFinder;
import com.oreilly.jent.people.soap.pfclient.PeopleFinderWSLocator;
import com.oreilly.jent.people.soap.pfclient.SearchArg;

/**
 * Simple command-line client for the PeopleFinder SOAP service.
 * 
 */
public class PeopleFinderRPCClient {
    static private Logger sLog = 
        Logger.getLogger(PeopleFinderRPCClient.class.getName());
    static final private String USAGE = 
        "PeopleFinderClient <service URL> " +
        "[-first pattern] " +
        "[-last pattern] " +
        "[-email pattern]";
    
    public static void main(String[] args) {
        HashMap searchParams = new HashMap();
        // Parse the command-line arguments, construct the search
        // parameters name/value map along the way
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-first")) {
                if (i >= args.length) {
                    sLog.info(USAGE);
                    return;
                }
                else {
                    i++;
                    searchParams.put(PersonDAO.FIRST_NAME, args[i]);
                }
            }
            else if (args[i].equals("-last")) {
                if (i >= args.length) {
                    sLog.info(USAGE);
                    return;
                }
                else {
                    i++;
                    searchParams.put(PersonDAO.LAST_NAME, args[i]);
                }
            }
            else if (args[i].equals("-email")) {
                if (i >= args.length) {
                    sLog.info(USAGE);
                    return;
                }
                else {
                    i++;
                    searchParams.put(PersonDAO.EMAIL, args[i]);
                }
            }
        }
        
        try {
            // Get the remote service reference
            com.oreilly.jent.people.soap.pfclient.PeopleFinder finder = 
                new PeopleFinderWSLocator().getPeopleFinderPort(new URL("http://localhost:9090/JEnt-examples/services/peopleFinder"));
            // Convert the search args into an array of SearchArg objects
            SearchArg[] sargs = new SearchArg[searchParams.size()];
            if (searchParams.size() > 0) {
                Iterator kIter = searchParams.keySet().iterator();
                int i = 0;
                while (kIter.hasNext()) {
                    String name = (String)kIter.next();
                    SearchArg arg = new SearchArg();
                    arg.setName(name);
                    arg.setValue((String)searchParams.get(name));
                    sargs[i] = arg;
                    i++;
                }
            }
            
            // Perform the search
            com.oreilly.jent.people.soap.pfclient.Person[] people = 
                finder.findPeople(sargs);
            // Print the results
            for (int i = 0; i < people.length; i++) {
                com.oreilly.jent.people.soap.pfclient.Person p = people[i];
                sLog.info(p.getFirstName() + " " + p.getLastName());
                for (int j = 0; j < p.getEmailAddresses().length; j++) {
                    String email = p.getEmailAddresses()[j];
                    sLog.info("\temail: " + email);
                }
                sLog.info("\n");
            }
        }
        catch (com.oreilly.jent.people.soap.pfclient.InvalidSearchException ise) {
            sLog.severe("Search failed: " + ise.getMessage());
        }
        catch (com.oreilly.jent.people.soap.pfclient.PersistenceException pe) {
            sLog.severe("Persistence problem during search: " + pe.getMessage());
        }
        catch (ServiceException se) {
            sLog.severe("Service failure while talking to people finder service: "
                        + se.getMessage());
        }
        catch (RemoteException re) {
            sLog.severe("Failed to make remote method call: " + re.getMessage());
            re.printStackTrace();
        }
        catch (MalformedURLException mue) {
            sLog.severe("Bad service URL: " + mue.getMessage());
        }
    }
}