Length Calculator Server Object Extension
arcgissamples\soe\client\LengthCalculatorClient.java
/* Copyright 2010 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions.
* 
*/
package arcgissamples.soe.client;


import arcgissamples.soe.ICalculateLength;

import com.esri.arcgis.carto.MapServer;
import com.esri.arcgis.server.IServerObjectExtension;
import com.esri.arcgis.server.IServerObjectExtensionManager;
import com.esri.arcgis.server.ServerConnection;
import com.esri.arcgis.server.ServerContext;
import com.esri.arcgis.server.ServerObjectManager;
import com.esri.arcgis.system.ServerInitializer;

public class LengthCalculatorClient {
  private static String serverName, domainName, userName, password, serviceName;
  private static ServerConnection connection;

  public static void main(String[] args) {
    try {
      if (args.length != 5) {
        System.out.println("Wrong Usage. For correct usage, see following: \n"
            + "\nUsage: LengthCalculatorClient [server name] [domain name] [user name] [password] [map service name]\n\n"
          + "[server name]\tSpecifies name of ArcGIS Server\n" + "[domain name]\tSpecifies domain name of user\n"
          + "[user name]\tSpecifies user name\n" + "[password]\tSpecifies user password\n" 
          + "[map service name]\tSpecifies name of map service\n");
        
        System.exit(1);
      } else {
        // connect to Server
        serverName = args[0];
        domainName = args[1];
        userName = args[2];
        password = args[3];
        serviceName = args[4];

        ServerInitializer serverInit = new ServerInitializer();
        serverInit.initializeServer(domainName, userName, password);

        System.out.print("Connecting to ArcGIS Server " + serverName + "...");
        connection = new ServerConnection();
        connection.connect(serverName);
        System.out.println("Done.");

        // Retrieve the SOM to get ServerContext and the Map Server Object
        System.out.print("\nRetrieving Server Object Manager.");
        ServerObjectManager som = new ServerObjectManager(connection.getServerObjectManager());
        System.out.println("Done.");

        System.out.print("\nCreating Server Context...");
        String soeName = "LengthCalculatorSOE";
        ServerContext serverContext = new ServerContext(som.createServerContext(serviceName, "MapServer"));
        MapServer mapServer = (MapServer) serverContext.getServerObject();
        System.out.println("Done.");

        // Access the soe through the IServerObjectExtensionManager
        System.out.println("\nCreating instance of SOE..\n");
        IServerObjectExtensionManager extnMgr = (IServerObjectExtensionManager) mapServer;
        IServerObjectExtension calculatorSOE = extnMgr.findExtensionByTypeName(soeName);
        System.out.println("Done.");

        // Consume the SOE
        System.out.println("\nInvoking SOE's calculateLength() method...");
        ICalculateLength mySOE = (ICalculateLength) calculatorSOE;
        System.out.println("Total Length returned by SOE: " + mySOE.calculateLength());

        // releasing connection
        System.out.print("\nDisconnecting " + serverName + "...");
        connection.release();
        System.out.println("Done.");
      }
    } catch (Exception e) {
      try {
        connection.release();
      } catch (Exception e1) {
        e1.printStackTrace();
      }

      e.printStackTrace();
    }
  }
}