/*
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * -Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *
 * -Redistribution in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in
 *  the documentation and/or other materials provided with the
 *  distribution.
 *
 * Neither the name of Oracle nor the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
 * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that Software is not designed, licensed or
 * intended for use in the design, construction, operation or
 * maintenance of any nuclear facility.
 */
package examples.activation;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
 * This program looks up a remote object's stub (that implements the
 * Counter remote interface) in the registry on the host
 * supplied as the optional first argument, and then invokes the
 * stub's increment method.  When this client invokes a
 * remote method on the stub acquired from the registry, the remote
 * object will activate if not already active.
 *
 * 
This program should be run as follows: *
* java -Djava.security.policy=client.policy \ * -Dexamples.activation.classes=classes \ * -Dexamples.activation.name=name \ * examples.activation.CounterClient [host] ** *
where:
Note: rmid must be running on its default port,
 * and rmiregistry must be running on its default port
 * (both on the remote host) prior to running this program.
 **/
public class CounterClient {
    public static void main(String args[])  throws Exception {
        /*
         * Obtain hostname supplied (optionally) as the first argument on
         * the command line.
         */
        String hostname = "localhost";
        if (args.length < 1) {
            System.err.println(
                "usage: java [options] examples.activation.CounterClient [host]");
            System.exit(1);
        } else {
            hostname = args[0];
        }
        /*
         * Set a security manager, if one is not already set.
         */
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        /*
         * Obtain the stub for the activatable object from the
         * rmiregistry on port 1099 of the supplied host.  The
         * name to look up in the registry is specified by the
         * system property examples.activation.name.
         */
        String name = System.getProperty("examples.activation.name");
        Registry registry = LocateRegistry.getRegistry(hostname);
        Counter stub = (Counter) registry.lookup(name);
        System.err.println("Obtained stub from the registry.");
        /*
         * Invoke the remote method using the stub.
         */
        System.err.println("Invoking increment method...");
        int count = stub.increment();
        System.err.println("Returned from increment remote call.");
        System.err.println("count = " + count);
    }
}