001/* 002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bstefanescu, jcarsique 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.runtime.launcher; 021 022import java.io.File; 023import java.io.IOException; 024import java.lang.reflect.InvocationTargetException; 025import java.lang.reflect.Method; 026import java.net.URL; 027import java.net.URLClassLoader; 028 029/** 030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 031 */ 032public class Main { 033 034 public static void main(String[] args) { 035 if (args.length == 0) { 036 System.err.println("You must specify a main class to invoke as the first parameter to that program."); 037 System.exit(2); 038 } 039 040 // The class name format is: path/ClassName:methodName 041 // where the path may be a path to a Jar file or a directory 042 // The default method name is 'main' 043 String path = args[0]; 044 String method = "main"; 045 int p = path.lastIndexOf(':'); 046 if (p > -1) { 047 method = path.substring(p + 1); 048 path = path.substring(0, p); 049 if (method == null || method.length() == 0) { 050 method = "main"; 051 } 052 } 053 p = path.lastIndexOf('/'); 054 String mainClassName = null; 055 if (p > -1) { 056 mainClassName = path.substring(p + 1); 057 path = path.substring(0, p); 058 } 059 060 // construct class loader to use to load application 061 try { 062 File file = new File(path).getCanonicalFile(); 063 URL[] urls = new URL[1]; 064 if (file.exists()) { 065 urls[0] = file.toURI().toURL(); 066 } else { 067 System.err.println("Could not find main class: " + args[0] 068 + ". Make sure you have this class on the boot class path"); 069 System.exit(3); 070 } 071 URLClassLoader classLoader = new URLClassLoader(urls, Main.class.getClassLoader()); 072 Thread.currentThread().setContextClassLoader(classLoader); 073 074 // set the property used by Nuxeo OSGi launcher to create the system 075 // bundle 076 Class<?> mainClass = classLoader.loadClass(mainClassName); 077 Method m = mainClass.getMethod(method, String[].class); 078 if (args.length > 1) { 079 String[] tmp = new String[args.length - 1]; 080 System.arraycopy(args, 1, tmp, 0, tmp.length); 081 args = tmp; 082 } else { 083 args = new String[0]; 084 } 085 m.invoke(null, new Object[] { args }); 086 } catch (IOException e) { 087 System.err.println("Could not find main class: " + args[0] 088 + ". Make sure you have this class on the boot class path"); 089 e.printStackTrace(); 090 System.exit(3); 091 } catch (ClassNotFoundException e) { 092 System.err.println("Could not load main class: " + args[0] 093 + ". Make sure you have this class on the boot class path"); 094 e.printStackTrace(); 095 System.exit(4); 096 } catch (NoSuchMethodException e) { 097 System.err.println("Could not find main class method: " + mainClassName + "." + method + "(String[] args)"); 098 e.printStackTrace(); 099 System.exit(5); 100 } catch (SecurityException e) { 101 System.err.println("Failed to access the main class method: " + mainClassName + "." + method 102 + "(String[] args)"); 103 e.printStackTrace(); 104 System.exit(5); 105 } catch (IllegalAccessException e) { 106 System.err.println("Failed to invoke method: " + mainClassName + "." + method + "(String[] args)"); 107 e.printStackTrace(); 108 System.exit(6); 109 } catch (IllegalArgumentException e) { 110 System.err.println("Failed to invoke method: " + mainClassName + "." + method + "(String[] args)"); 111 e.printStackTrace(); 112 System.exit(6); 113 } catch (InvocationTargetException e) { 114 System.err.println("Failed to invoke method: " + mainClassName + "." + method + "(String[] args)"); 115 e.printStackTrace(); 116 System.exit(6); 117 } 118 119 } 120 121}