JNIBridge.java :  » IDE-Eclipse » equinox » org » eclipse » equinox » launcher » Java Open Source

Java Open Source » IDE Eclipse » equinox 
equinox » org » eclipse » equinox » launcher » JNIBridge.java
/*******************************************************************************
 * Copyright (c) 2006, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.launcher;

/**
 * <b>Note:</b> This class should not be referenced programmatically by
 * other Java code. This class exists only for the purpose of interacting with
 * a native launcher. To launch Eclipse programmatically, use 
 * org.eclipse.core.runtime.adaptor.EclipseStarter. This class is not API.
 *
 * @noextend This class is not intended to be subclassed by clients.
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public class JNIBridge {
  //TODO: This class should not be public
  private native void _set_exit_data(String sharedId, String data);

  private native void _update_splash();

  private native long _get_splash_handle();

  private native void _show_splash(String bitmap);

  private native void _takedown_splash();

  private native int OleInitialize(int reserved);

  private native void OleUninitialize();

  private String library;
  private boolean libraryLoaded = false;

  /**
   * @noreference This constructor is not intended to be referenced by clients.
   * 
   * @param library the given library
   */
  public JNIBridge(String library) {
    this.library = library;
  }

  private void loadLibrary() {
    if (library != null) {
      try {
        if (library.indexOf("wpf") != -1) { //$NON-NLS-1$
          int idx = library.indexOf("eclipse_"); //$NON-NLS-1$
          if (idx != -1) {
            String comLibrary = library.substring(0, idx) + "com_"; //$NON-NLS-1$
            comLibrary += library.substring(idx + 8, library.length());
            Runtime.getRuntime().load(comLibrary);
            OleInitialize(0);
          }
        }
        Runtime.getRuntime().load(library);
      } catch (UnsatisfiedLinkError e) {
        //failed
      }
    }
    libraryLoaded = true;
  }

  /**
   * @noreference This method is not intended to be referenced by clients.
   */
  public boolean setExitData(String sharedId, String data) {
    try {
      _set_exit_data(sharedId, data);
      return true;
    } catch (UnsatisfiedLinkError e) {
      if (!libraryLoaded) {
        loadLibrary();
        return setExitData(sharedId, data);
      }
      return false;
    }
  }

  /**
   * @noreference This method is not intended to be referenced by clients.
   */
  public boolean showSplash(String bitmap) {
    try {
      _show_splash(bitmap);
      return true;
    } catch (UnsatisfiedLinkError e) {
      if (!libraryLoaded) {
        loadLibrary();
        return showSplash(bitmap);
      }
      return false;
    }
  }

  /**
   * @noreference This method is not intended to be referenced by clients.
   */
  public boolean updateSplash() {
    try {
      _update_splash();
      return true;
    } catch (UnsatisfiedLinkError e) {
      if (!libraryLoaded) {
        loadLibrary();
        return updateSplash();
      }
      return false;
    }
  }

  /**
   * @noreference This method is not intended to be referenced by clients.
   */
  public long getSplashHandle() {
    try {
      return _get_splash_handle();
    } catch (UnsatisfiedLinkError e) {
      if (!libraryLoaded) {
        loadLibrary();
        return getSplashHandle();
      }
      return -1;
    }
  }

  /**
   * @noreference This method is not intended to be referenced by clients.
   */
  public boolean takeDownSplash() {
    try {
      _takedown_splash();
      return true;
    } catch (UnsatisfiedLinkError e) {
      if (!libraryLoaded) {
        loadLibrary();
        return takeDownSplash();
      }
      return false;
    }
  }

  /**
   * @noreference This method is not intended to be referenced by clients.
   */
  public boolean uninitialize() {
    if (libraryLoaded && library != null) {
      if (library.indexOf("wpf") != -1) { //$NON-NLS-1$
        try {
          OleUninitialize();
        } catch (UnsatisfiedLinkError e) {
          // library not loaded
          return false;
        }
      }
    }
    return true;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.