FileDocCategorySizeDatePackage
MovieClient.javaAPI DocExample3520Sun Jul 07 09:56:38 BST 2002None

MovieClient

public class MovieClient extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        if (args.length < 2) {
            System.out.println("Usage:\n java javajaxb.MovieClient \n" +
                "    config=[XML configuration file] \n" +
                "    action=[list | addMovie | addActor] \n" +
                "    title=<movie title> \n" +
                "    director=<movie director> \n" +
                "    actor=<actor name> \n" +
                "    headliner=[true | false]");
            return;
        }
        
        Arguments arguments = new Arguments(args);

        try {
            File configFile = new File(arguments.getValue("config"));
            FileInputStream inputStream = 
                new FileInputStream(configFile);

            // Unmarshal the connection information
            Connection connection = Connection.unmarshal(inputStream);

            // Determine the data needed
            Host host = connection.getHost();
            Url configURL = connection.getUrl();
            String filename = new StringBuffer("/")
                .append(configURL.getContext())
                .append("/")
                .append(configURL.getServletPrefix())
                .append("/")
                .append(configURL.getServletName())
                .toString();

            // Connect to the servlet
            URL url = new URL("http", 
                              host.getHostname(),
                              Integer.parseInt(host.getPort()),
                              filename);
            HttpMessage msg = new HttpMessage(url);

            // Indicate the action desired
            Properties props = new Properties();
            String action = arguments.getValue("action");
            props.put("action", action);
            
            // Add any other required parameters
            if (action.equalsIgnoreCase("addMovie")) {
                String title = arguments.getValue("title");
                String director = arguments.getValue("director");
                
                props.put("title", title);
                props.put("director", director);
            } else if (action.equalsIgnoreCase("addActor")) {
                String title = arguments.getValue("title");
                String actor = arguments.getValue("actor");
                String headliner = arguments.getValue("headliner");
                
                props.put("title", title);
                props.put("actor", actor);
                props.put("headliner", headliner);
            }

            // Get response
            InputStream in = msg.sendPostMessage(props);
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(in));

            // Output response to screen
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }