4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/Apackage apple.launcher;
4632N/A
4632N/Aimport java.io.*;
4632N/Aimport java.lang.reflect.*;
4632N/Aimport java.security.PrivilegedAction;
4632N/Aimport java.text.MessageFormat;
4632N/Aimport java.util.*;
4632N/Aimport java.util.jar.*;
4632N/A
4632N/Aimport javax.swing.*;
4632N/A
4632N/Aclass JavaAppLauncher implements Runnable {
4632N/A static {
4632N/A java.security.AccessController.doPrivileged((PrivilegedAction<?>)new sun.security.action.LoadLibraryAction("osx"));
4632N/A }
4632N/A
4632N/A private static native <T> T nativeConvertAndRelease(final long ptr);
4632N/A private static native void nativeInvokeNonPublic(Class<? extends Method> cls, Method m, String[] args);
4632N/A
4632N/A // entry point from native
4632N/A static void launch(final long javaDictionaryPtr, final boolean verbose) {
4632N/A final Map<String, ?> javaDictionary = nativeConvertAndRelease(javaDictionaryPtr);
4632N/A (new JavaAppLauncher(javaDictionary, verbose)).run();
4632N/A }
4632N/A
4632N/A // these are the values for the enumeration JavaFailureMode
4632N/A static final String kJavaFailureMainClassNotSpecified = "MainClassNotSpecified";
4632N/A static final String kJavaFailureMainClassNotFound = "CannotLoadMainClass";
4632N/A static final String kJavaFailureMainClassHasNoMain = "NoMainMethod";
4632N/A static final String kJavaFailureMainClassMainNotStatic = "MainNotStatic";
4632N/A static final String kJavaFailureMainThrewException = "MainThrewException";
4632N/A static final String kJavaFailureMainInitializerException = "MainInitializerException";
4632N/A
4632N/A final boolean verbose; // Normally set by environment variable JAVA_LAUNCHER_VERBOSE.
4632N/A final Map<String, ?> javaDictionary;
4632N/A
4632N/A JavaAppLauncher(final Map<String, ?> javaDictionary, final boolean verbose) {
4632N/A this.verbose = verbose;
4632N/A this.javaDictionary = javaDictionary;
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void run() {
4632N/A final Method m = loadMainMethod(getMainMethod());
4632N/A final String methodName = m.getDeclaringClass().getName() + ".main(String[])";
4632N/A try {
4632N/A log("Calling " + methodName + " method");
4632N/A m.invoke(null, new Object[] { getArguments() });
4632N/A log(methodName + " has returned");
4632N/A } catch (final IllegalAccessException x) {
4632N/A try {
4632N/A nativeInvokeNonPublic(m.getClass(), m, getArguments());
4632N/A } catch (final Throwable excpt) {
4632N/A logError(methodName + " threw an exception:");
4632N/A if ((excpt instanceof UnsatisfiedLinkError) && excpt.getMessage().equals("nativeInvokeNonPublic")) {
4632N/A showFailureAlertAndKill(kJavaFailureMainThrewException, "nativeInvokeNonPublic not registered");
4632N/A } else {
4632N/A excpt.printStackTrace();
4632N/A showFailureAlertAndKill(kJavaFailureMainThrewException, excpt.toString());
4632N/A }
4632N/A }
4632N/A } catch (final InvocationTargetException invokeExcpt) {
4632N/A logError(methodName + " threw an exception:");
4632N/A invokeExcpt.getTargetException().printStackTrace();
4632N/A showFailureAlertAndKill(kJavaFailureMainThrewException, invokeExcpt.getTargetException().toString());
4632N/A }
4632N/A }
4632N/A
4632N/A Method loadMainMethod(final String mainClassName) {
4632N/A try {
4632N/A final Class<?> mainClass = Class.forName(mainClassName, true, sun.misc.Launcher.getLauncher().getClassLoader());
4632N/A final Method mainMethod = mainClass.getDeclaredMethod("main", new Class[] { String[].class });
4632N/A if ((mainMethod.getModifiers() & Modifier.STATIC) == 0) {
4632N/A logError("The main(String[]) method of class " + mainClassName + " is not static!");
4632N/A showFailureAlertAndKill(kJavaFailureMainClassMainNotStatic, mainClassName);
4632N/A }
4632N/A return mainMethod;
4632N/A } catch (final ExceptionInInitializerError x) {
4632N/A logError("The main class \"" + mainClassName + "\" had a static initializer throw an exception.");
4632N/A x.getException().printStackTrace();
4632N/A showFailureAlertAndKill(kJavaFailureMainInitializerException, x.getException().toString());
4632N/A } catch (final ClassNotFoundException x) {
4632N/A logError("The main class \"" + mainClassName + "\" could not be found.");
4632N/A showFailureAlertAndKill(kJavaFailureMainClassNotFound, mainClassName);
4632N/A } catch (final NoSuchMethodException x) {
4632N/A logError("The main class \"" + mainClassName + "\" has no static main(String[]) method.");
4632N/A showFailureAlertAndKill(kJavaFailureMainClassHasNoMain, mainClassName);
4632N/A } catch (final NullPointerException x) {
4632N/A logError("No main class specified");
4632N/A showFailureAlertAndKill(kJavaFailureMainClassNotSpecified, null);
4632N/A }
4632N/A
4632N/A return null;
4632N/A }
4632N/A
4632N/A // get main class name from 'Jar' key, or 'MainClass' key
4632N/A String getMainMethod() {
4632N/A final Object javaJar = javaDictionary.get("Jar");
4632N/A if (javaJar != null) {
4632N/A if (!(javaJar instanceof String)) {
4632N/A logError("'Jar' key in 'Java' sub-dictionary of Info.plist requires a string value");
4632N/A return null;
4632N/A }
4632N/A
4632N/A final String jarPath = (String)javaJar;
4632N/A if (jarPath.length() == 0) {
4632N/A log("'Jar' key of sub-dictionary 'Java' of Info.plist key is empty");
4632N/A } else {
4632N/A // extract main class from manifest of this jar
4632N/A final String main = getMainFromManifest(jarPath);
4632N/A if (main == null) {
4632N/A logError("jar file '" + jarPath + "' does not have Main-Class: attribute in its manifest");
4632N/A return null;
4632N/A }
4632N/A
4632N/A log("Main class " + main + " found in jar manifest");
4632N/A return main;
4632N/A }
4632N/A }
4632N/A
4632N/A final Object javaMain = javaDictionary.get("MainClass");
4632N/A if (!(javaMain instanceof String)) {
4632N/A logError("'MainClass' key in 'Java' sub-dictionary of Info.plist requires a string value");
4632N/A return null;
4632N/A }
4632N/A
4632N/A final String main = (String)javaMain;
4632N/A if (main.length() == 0) {
4632N/A log("'MainClass' key of sub-dictionary 'Java' of Info.plist key is empty");
4632N/A return null;
4632N/A }
4632N/A
4632N/A log("Main class " + (String)javaMain + " found via 'MainClass' key of sub-dictionary 'Java' of Info.plist key");
4632N/A return (String)javaMain;
4632N/A }
4632N/A
4632N/A // get arguments for main(String[]) out of Info.plist and command line
4632N/A String[] getArguments() {
4632N/A // check for 'Arguments' key, which contains the main() args if not defined in Info.plist
4632N/A final Object javaArguments = javaDictionary.get("Arguments");
4632N/A if (javaArguments == null) {
4632N/A // no arguments
4632N/A log("No arguments for main(String[]) specified");
4632N/A return new String[0];
4632N/A }
4632N/A
4632N/A if (javaArguments instanceof List) {
4632N/A final List<?> args = (List<?>)javaArguments;
4632N/A final int count = args.size();
4632N/A log("Arguments to main(String[" + count + "]):");
4632N/A
4632N/A final String[] result = new String[count];
4632N/A for (int i = 0; i < count; ++i) {
4632N/A final Object element = args.get(i);
4632N/A if (element instanceof String) {
4632N/A result[i] = (String)element;
4632N/A } else {
4632N/A logError("Found non-string in array");
4632N/A }
4632N/A log(" arg[" + i + "]=" + result[i]);
4632N/A }
4632N/A return result;
4632N/A }
4632N/A
4632N/A logError("'Arguments' key in 'Java' sub-dictionary of Info.plist requires a string value or an array of strings");
4632N/A return new String[0];
4632N/A }
4632N/A
4632N/A // returns name of main class, or null
4632N/A String getMainFromManifest(final String jarpath) {
4632N/A JarFile jar = null;
4632N/A try {
4632N/A jar = new JarFile(jarpath);
4632N/A final Manifest man = jar.getManifest();
4632N/A final Attributes attr = man.getMainAttributes();
4632N/A return attr.getValue("Main-Class");
4632N/A } catch (final IOException x) {
4632N/A // shrug
4632N/A } finally {
4632N/A if (jar != null) {
4632N/A try {
4632N/A jar.close();
4632N/A } catch (final IOException x) { }
4632N/A }
4632N/A }
4632N/A return null;
4632N/A }
4632N/A
4632N/A void log(final String s) {
4632N/A if (!verbose) return;
4632N/A System.out.println("[LaunchRunner] " + s);
4632N/A }
4632N/A
4632N/A static void logError(final String s) {
4632N/A System.err.println("[LaunchRunner Error] " + s);
4632N/A }
4632N/A
4632N/A // This kills the app and does not return!
4632N/A static void showFailureAlertAndKill(final String msg, String arg) {
4632N/A if (arg == null) arg = "<<null>>";
4632N/A JOptionPane.showMessageDialog(null, getMessage(msg, arg), "", JOptionPane.ERROR_MESSAGE);
4632N/A System.exit(-1);
4632N/A }
4632N/A
4632N/A static String getMessage(final String msgKey, final Object ... args) {
4632N/A final String msg = ResourceBundle.getBundle("appLauncherErrors").getString(msgKey);
4632N/A return MessageFormat.format(msg, args);
4632N/A }
4632N/A}