arcgissamples\scenario\map\LANConnectionInfo.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
GIS Client
arcgissamples\scenario\map\LANConnectionInfo.java
/* Copyright 2015 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 at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
package arcgissamples.scenario.map;

import java.io.IOException;

import com.esri.arcgis.carto.IMapDescription;
import com.esri.arcgis.carto.IMapDescriptions;
import com.esri.arcgis.carto.IMapServerInfo;
import com.esri.arcgis.carto.ImageDescription;
import com.esri.arcgis.carto.ImageDisplay;
import com.esri.arcgis.carto.ImageType;
import com.esri.arcgis.carto.MapDescriptions;
import com.esri.arcgis.carto.MapExtent;
import com.esri.arcgis.carto.MapServer;
import com.esri.arcgis.carto.esriImageFormat;
import com.esri.arcgis.carto.esriImageReturnType;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.IPointCollection;
import com.esri.arcgis.gisclient.AGSServerConnection;
import com.esri.arcgis.server.IServerContext;
import com.esri.arcgis.server.IServerObjectConfigurationInfo;
import com.esri.arcgis.server.IServerObjectManager;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.LongArray;

public class LANConnectionInfo extends MapServerConnectionInfo {

  IServerObjectManager serverObjectManager;
  IMapDescriptions mapDescriptions;
  IMapDescription mapDescription = null;
  IServerContext serverObjectContext;
  String mapDescriptionString;
  String imageDescriptionString;
  boolean pooled;

  public LANConnectionInfo() {

  }

  /**Initializes server object manager
   * cleans context if it is already exists
   */

  public void init() {
    try {

      if(serverObjectContext != null) {
        serverObjectContext.removeAll();
        serverObjectContext.releaseContext();
      }

      serverObjectContext = serverObjectManager.createServerContext(mapConfigurationName, "MapServer");
      server = (MapServer)serverObjectContext.getServerObject();
      resetMapInfo();
    }
    catch (Exception e) {
      e.printStackTrace();
      stop();
    }
  }

  /**Method which returns vector of all the configurations currently available on the system
   * @param url webservices catalog url
   * @param connectionType can be either internet or lan
   * @return
   */
  public java.util.Vector getConfigurationList(String host, String connectionType) {
    java.util.Vector mapConfigurationList = super.getConfigurationList(host, connectionType);
    try {
      AGSServerConnection agsConnection = (AGSServerConnection)agsServerConnection;
      serverObjectManager = agsConnection.getServerObjectManager();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }

    return mapConfigurationList;
  }

  /**Method to check for pooled objects
   * @return boolean
   */

  public boolean isPooled() {
    return pooled;
  }

  /**Sets the extent to the map description
   *@param env IEnvelope
   */
  public void setExtent(Envelope env) {
    try {
      MapExtent mapExtent = (MapExtent)serverObjectContext.createObject(MapExtent.getClsid()) ;
      mapExtent.setExtent(env);
      mapDescription.setMapArea(mapExtent);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**Returns the extent of the current activeMap
   *@return env IEnvelope
   */
  public Envelope getExtent() {
    try {
      Envelope env = (Envelope) mapDescription.getMapArea().getExtent();
      return env;
    }
    catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  /**Method to reset the map information.
   */

  public void resetMapInfo() {
    try {
      IServerObjectConfigurationInfo  configInfo = serverObjectManager.getConfigurationInfo(this.mapConfigurationName, "MapServer");
      pooled = configInfo.isPooled();
      activeMap = server.getDefaultMapName();
      mapDescription = server.getServerInfo(activeMap).getDefaultMapDescription();
      mapDescriptions = (MapDescriptions)serverObjectContext.createObject(MapDescriptions.getClsid());
      for(int i = 0; i < server.getMapCount(); i++) {
        String mapName = server.getMapName(i);
        IMapServerInfo mapServerInfo = server.getServerInfo(mapName);
        mapDescriptions.add(mapServerInfo.getDefaultMapDescription());
      }

    }
    catch (Exception e) {
      e.printStackTrace();
      stop();
    }
  }

  /**Method to create envelope
   * @return
   */

  public Envelope createNewEnvelope() {
    try {
      Envelope env = (Envelope) serverObjectContext.createObject(Envelope.getClsid());
      return env;
    }
    catch (IOException ex) {
      return null;
    }
  }

  /**Helper method to create a longarray object
   * @return LongArray
   */
  public LongArray createNewLongArray() {
    try {
      LongArray env = (LongArray)serverObjectContext.createObject(
          LongArray.getClsid());
      return env;
    }
    catch (IOException ex) {
      return null;
    }

  }

  /**Returns mapdescription object for particular map
   * @param activeMap
   * @return IMapDescription
   */

  public IMapDescription getMapDescription(String activeMap) {
    try {
      IMapDescription mapDescription = null;
      for (int i = 0; i < mapDescriptions.getCount(); i++) {
        mapDescription = mapDescriptions.getElement(i);
        if (mapDescription.getName().equalsIgnoreCase(activeMap)) {
          break;
        }
      }
      return mapDescription;
    }
    catch (IOException ex) {
      return null;
    }
  }

  /** In case of pooled objects, creates servercontext, loads the mapDescriptions
   * and imagedescription.For non pooled objects resets the mapdescription
   *
   */


  public void activate() {
    try {
      if (pooled) {
        this.serverObjectContext = serverObjectManager.createServerContext(
            mapConfigurationName, "MapServer");
        server = ((MapServer)serverObjectContext.getServerObject());
        mapDescriptions = (MapDescriptions)serverObjectContext.loadObject(mapDescriptionString);
        for (int i = 0; i < mapDescriptions.getCount(); i++) {
          mapDescription = mapDescriptions.getElement(i);
          if (mapDescription.getName().equalsIgnoreCase(activeMap)) {
            break;
          }
        }
        if(imageDescriptionString != null)
          imageDescription = (ImageDescription)serverObjectContext.loadObject(imageDescriptionString);
      }
      else {
        for (int i = 0; i < mapDescriptions.getCount(); i++) {
          mapDescription = mapDescriptions.getElement(i);
          if (mapDescription.getName().equalsIgnoreCase(activeMap)) {
            break;
          }
        }
      }
    }
    catch (IOException ex) {
    }
  }

  /**Releases context and save mapdescription and imagedescription objects
   */

  public void passivate() {
    try {
      if (pooled) {

        this.mapDescriptionString = this.serverObjectContext.saveObject(
            mapDescriptions);
        this.imageDescriptionString = this.serverObjectContext.saveObject(
            imageDescription);
        serverObjectContext.releaseContext();
      }
    }
    catch (IOException ex) {
    }
  }


  /**Stops the map server. Frees all the resources used by the server.
   */

  public void stop() {
    try {
      if(serverObjectContext != null) {
        serverObjectContext.removeAll();
        serverObjectContext.releaseContext();
      }
      EngineInitializer.trackObjectsInCurrentThread();
      EngineInitializer.releaseAllInCurrentThread();

    }
    catch (Exception e) {
    }
  }

  /**Sets the extent and exports the map image.
   * @param envelope
   * @return String containg the url
   */

  public byte[] exportImage(Envelope envelope) {
    this.setExtent(envelope);
    return exportImage();
  }

  /**Exports the map image.
   * @param envelope
   * @return String containg image location
   */


  public synchronized byte[] exportImage() {
    try {
      if(imageDescription == null)
        imageDescription = (ImageDescription)serverObjectContext.createObject(ImageDescription.getClsid());

      imageType = imageDescription.getType();
      if(imageType == null) {
        imageType = (ImageType)serverObjectContext.createObject(ImageType.getClsid());
      }

      imageDisplay= imageDescription.getDisplay();
      if(imageDisplay == null) {
        imageDisplay = (ImageDisplay)serverObjectContext.createObject(
            ImageDisplay.getClsid());
      }


      imageDisplay.setDeviceResolution(dpi);
      imageDisplay.setHeight(imageHeight);
      imageDisplay.setWidth(imageWidth);
      imageType.setFormat(esriImageFormat.esriImageJPG);
      imageDescription.setDisplay(imageDisplay);
      imageType.setReturnType(esriImageReturnType.esriImageReturnMimeData);
      imageDescription.setType(imageType);

      mapImage = server.exportMapImage(mapDescription, imageDescription);
      return(mapImage.getMimeData());

    }
    catch (IOException ex) {
      ex.printStackTrace();
      return null;
    }
  }

  /**Converts an Envelope screen location to a map coordinate with Envelope as input
   * @param rect Envelope
   * @return IEnvelope with the transformed points
   */

  public Envelope transformScreenToMap(arcgissamples.scenario.map.Envelope1 rect) {
    Envelope env = null;
    try {
      env = createNewEnvelope();
      LongArray longArrayX, longArrayY;
      IPointCollection p1;
      longArrayX = createNewLongArray();
      longArrayY = createNewLongArray();

      longArrayX.add((int)rect.x);
      longArrayX.add((int)(rect.x + rect.width));
      longArrayY.add((int)rect.y);
      longArrayY.add((int)(rect.y  + rect.height));
      p1 = server.toMapPoints(mapDescription, imageDisplay, longArrayX, longArrayY);
      IPoint ip1, ip2;
      ip1 = p1.getPoint(0);
      ip2 = p1.getPoint(1);

      env.setXMin(ip1.getX());
      env.setYMin(ip2.getY());
      env.setXMax(ip2.getX());
      env.setYMax(ip1.getY());

    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
    /*
   finally {
     return env;
   }
     */
    return env;
  }

  /**Converts an Envelope screen location to a map coordinate
   * @param int x
   * @param int y
   * @return IPoint
   */

  public IPoint transformScreenToMap(int x, int y) {
    try {
      LongArray p1, p2;
      p1 = createNewLongArray();
      p2 = createNewLongArray();
      p1.add(x);
      p2.add(y);
      IPointCollection p = server.toMapPoints(mapDescription, imageDisplay, p1, p2);
      return p.getPoint(0);

    }

    catch (IOException ex) {
      return null;
    }

  }
}