rrenkert@33: package de.intevation.mxd;
rrenkert@25:
rrenkert@25: import java.io.IOException;
rrenkert@103: import java.io.File;
vc11884admin@111: import java.lang.reflect.Method;
vc11884admin@111: import java.net.URL;
vc11884admin@111: import java.net.URLClassLoader;
rrenkert@103:
rrenkert@103: import org.apache.log4j.Logger;
rrenkert@25:
rrenkert@25: import com.esri.arcgis.system.AoInitialize;
rrenkert@25: import com.esri.arcgis.system.EngineInitializer;
rrenkert@25: import com.esri.arcgis.system.esriLicenseProductCode;
rrenkert@25: import com.esri.arcgis.system.esriLicenseStatus;
rrenkert@25:
rrenkert@25: /**
rrenkert@25: * Initializes the ArcGIS Engine and Objects.
rrenkert@25: *
rrenkert@25: * @author Raimund Renkert
rrenkert@25: */
rrenkert@43: public class ArcGISInitializer {
rrenkert@25:
rrenkert@43: /**
rrenkert@103: * The Logger.
rrenkert@103: */
rrenkert@103: private static final Logger logger = Logger.getLogger(ArcGISInitializer.class);
rrenkert@103:
rrenkert@103: /**
rrenkert@43: * Private member.
rrenkert@43: */
rrenkert@25: private AoInitialize aoInit;
rrenkert@25: private String engineInstallDir = "";
rrenkert@25:
rrenkert@25:
rrenkert@25: /**
rrenkert@25: * Init ArcGIS Java Objects.
rrenkert@25: */
rrenkert@43: public boolean initArcGIS ()
rrenkert@43: throws IOException {
rrenkert@103: logger.debug("initArcGIS()");
vc11884admin@111:
vc11884admin@111: String engineInstallDir = System.getenv("AGSENGINEJAVA");
rrenkert@180: if(engineInstallDir == null) {
rrenkert@103: engineInstallDir = System.getenv("AGSDESKTOPJAVA");
rrenkert@180: if(engineInstallDir == null) {
rrenkert@103: logger.error("Could not find ArcGIS Environment. \n" +
rrenkert@103: "AGSENGINEJAVA or AGSDESKTOPJAVA not set.");
rrenkert@180: return false;
rrenkert@180: }
rrenkert@180: }
vc11884admin@111:
rrenkert@180: //Obtain the relative path to the arcobjects.jar file
vc11884admin@111: String jarPath = engineInstallDir + "java" + File.separator + "lib" +
rrenkert@180: File.separator + "arcobjects.jar";
vc11884admin@111:
vc11884admin@111: //Create a new file
vc11884admin@111: File jarFile = new File(jarPath);
rrenkert@180:
vc11884admin@111: //Test for file existence
vc11884admin@111: if(!jarFile.exists()){
rrenkert@180: logger.error("The arcobjects.jar was not found in the" +
rrenkert@180: " following location: " + jarFile.getParent());
rrenkert@180: logger.error("Verify that arcobjects.jar can" +
rrenkert@180: " be located in the specified folder.");
rrenkert@180: logger.error("If not present, try uninstalling your" +
rrenkert@180: " ArcGIS software and reinstalling it.");
vc11884admin@111: System.exit(0);
rrenkert@103: }
vc11884admin@111:
vc11884admin@111: //Helps load classes and resources from a search path of URLs
rrenkert@180: URLClassLoader sysloader =
rrenkert@180: (URLClassLoader) ClassLoader.getSystemClassLoader();
vc11884admin@111: Class sysclass = URLClassLoader.class;
vc11884admin@111:
vc11884admin@111: try {
rrenkert@180: Method method = sysclass.getDeclaredMethod("addURL",
rrenkert@180: new Class[]{URL.class});
vc11884admin@111: method.setAccessible(true);
vc11884admin@111: method.invoke(sysloader, new Object[]{jarFile.toURI().toURL()});
rrenkert@103: }
rrenkert@180: catch (Throwable throwable) {
vc11884admin@111: throwable.printStackTrace();
vc11884admin@111: logger.error("Could not add arcobjects.jar to system classloader");
vc11884admin@111: System.exit(0);
vc11884admin@111: }
rrenkert@180:
rrenkert@180: EngineInitializer.initializeEngine();
rrenkert@180:
rrenkert@180: aoInit = new AoInitialize();
rrenkert@25: return true;
rrenkert@25: }
rrenkert@25:
rrenkert@25: /**
rrenkert@25: * Init ArcGIS License.
rrenkert@25: */
rrenkert@103: public boolean initArcGISLicenses()
rrenkert@43: throws IOException {
rrenkert@103: logger.debug("initArcGISLicenses()");
rrenkert@25: if(aoInit.isProductCodeAvailable
rrenkert@25: (esriLicenseProductCode.esriLicenseProductCodeEngine) ==
rrenkert@43: esriLicenseStatus.esriLicenseAvailable) {
rrenkert@25: aoInit.initialize
rrenkert@25: (esriLicenseProductCode.esriLicenseProductCodeEngine);
rrenkert@25: }
rrenkert@25: else if (aoInit.isProductCodeAvailable
rrenkert@25: (esriLicenseProductCode.esriLicenseProductCodeArcView) ==
rrenkert@43: esriLicenseStatus.esriLicenseAvailable) {
rrenkert@25: aoInit.initialize
rrenkert@25: (esriLicenseProductCode.esriLicenseProductCodeArcView);
rrenkert@25: }
rrenkert@43: else {
rrenkert@103: logger.error("Engine Runtime or ArcView" +
rrenkert@103: " license not initialized.\n" +
rrenkert@103: "Please install an ArcGIS product and set the" +
rrenkert@103: " PATH-variable correctly.");
rrenkert@103: return false;
rrenkert@25: }
rrenkert@103: return true;
rrenkert@25: }
rrenkert@25:
rrenkert@25: /**
rrenkert@25: * Shutdown the ArcGIS Objects.
rrenkert@25: */
rrenkert@43: public boolean shutdownArcGIS()
rrenkert@43: throws IOException {
rrenkert@43: if(aoInit != null) {
rrenkert@25: aoInit.shutdown();
rrenkert@25: return true;
rrenkert@25: }
rrenkert@43: else {
rrenkert@25: return false;
rrenkert@25: }
rrenkert@25: }
rrenkert@25:
rrenkert@25: /**
rrenkert@25: * Get the ArcGIS Engine Directory.
rrenkert@25: */
rrenkert@43: public String getEngineDirectory()
rrenkert@43: throws Exception {
rrenkert@43: if(engineInstallDir == "") {
rrenkert@25: throw new Exception("Call initArcGIS() first!");
rrenkert@43: }
rrenkert@43: else {
rrenkert@25: return engineInstallDir;
rrenkert@43: }
rrenkert@25: }
rrenkert@25: }