view src/java/de/intevation/mxd/ArcGISInitializer.java @ 354:5c5c4ddb20bd

Remove outdated repository URL
author Tom Gottfried <tom@intevation.de>
date Mon, 04 Jan 2021 15:38:14 +0100
parents df4e0946ef02
children
line wrap: on
line source
/*
 * Copyright (c) 2011 by Intevation GmbH, Germany <info@intevation.de>
 *
 * This file is part of MXD2map.
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LICENCE.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 *
 * MXD2map has been developed on behalf of the
 * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg
 * by Intevation GmbH.
 *
 * Authors:
 * Raimund Renkert <raimund.renkert@intevation.de>
 * Bjoern Schilberg <bjoern.schilberg@intevation.de>
 * Stephan Holl <stephan.holl@intevation.de>
 */

package de.intevation.mxd;

import java.io.IOException;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

import org.apache.log4j.Logger;

import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

/**
 * Initializes the ArcGIS Engine and Objects.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class ArcGISInitializer {

    /**
     * The Logger.
     */
    private static final Logger logger = Logger.getLogger(ArcGISInitializer.class);

    /**
     * Private member.
     */
    private AoInitialize    aoInit;
    private String          engineInstallDir = "";


    /**
     * Init ArcGIS Java Objects.
     */
    public boolean initArcGIS ()
    throws IOException {
        logger.debug("initArcGIS()");

        String engineInstallDir    = System.getenv("AGSENGINEJAVA");
        if(engineInstallDir == null) {
            engineInstallDir = System.getenv("AGSDESKTOPJAVA");
            if(engineInstallDir == null) {
                logger.error("Could not find ArcGIS Environment. \n" +
                             "AGSENGINEJAVA or AGSDESKTOPJAVA not set.");
                return false;
            }
        }

        //Obtain the relative path to the arcobjects.jar file
        String jarPath = engineInstallDir + "java" + File.separator + "lib" +
                         File.separator + "arcobjects.jar";

        //Create a new file
        File jarFile = new File(jarPath);

        //Test for file existence
        if(!jarFile.exists()){
                logger.error("The arcobjects.jar was not found in the" +
                             " following location: " + jarFile.getParent());
                logger.error("Verify that arcobjects.jar can" +
                             " be located in the specified folder.");
                logger.error("If not present, try uninstalling your" +
                             " ArcGIS software and reinstalling it.");
                System.exit(0);
        }

        //Helps load classes and resources from a search path of URLs
        URLClassLoader sysloader =
            (URLClassLoader) ClassLoader.getSystemClassLoader();
        Class<URLClassLoader> sysclass = URLClassLoader.class;

        try {
            Method method = sysclass.getDeclaredMethod("addURL",
                                                       new Class[]{URL.class});
            method.setAccessible(true);
            method.invoke(sysloader, new Object[]{jarFile.toURI().toURL()});
        }
        catch (Throwable throwable) {
            throwable.printStackTrace();
            logger.error("Could not add arcobjects.jar to system classloader");
            System.exit(0);
        }

        EngineInitializer.initializeEngine();

        aoInit = new AoInitialize();
        return true;
    }

    /**
     * Init ArcGIS License.
     */
    public boolean initArcGISLicenses()
    throws IOException {
        logger.debug("initArcGISLicenses()");
        if(aoInit.isProductCodeAvailable
           (esriLicenseProductCode.esriLicenseProductCodeEngine) ==
           esriLicenseStatus.esriLicenseAvailable) {
           aoInit.initialize
               (esriLicenseProductCode.esriLicenseProductCodeEngine);
        }
        else if (aoInit.isProductCodeAvailable
                 (esriLicenseProductCode.esriLicenseProductCodeArcView) ==
                 esriLicenseStatus.esriLicenseAvailable) {
                 aoInit.initialize
                     (esriLicenseProductCode.esriLicenseProductCodeArcView);
        }
        else {
            logger.error("Engine Runtime or ArcView" +
                         " license not initialized.\n" +
                         "Please install an ArcGIS product and set the" +
                         " PATH-variable correctly.");
            return false;
        }
        return true;
    }

    /**
     * Shutdown the ArcGIS Objects.
     */
    public boolean shutdownArcGIS()
    throws IOException {
        if(aoInit != null) {
            aoInit.shutdown();
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Get the ArcGIS Engine Directory.
     */
    public String getEngineDirectory()
    throws Exception {
        if(engineInstallDir == "") {
            throw new Exception("Call initArcGIS() first!");
        }
        else {
            return engineInstallDir;
        }
    }
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)