001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu, jcarsique
018 *
019 * $Id$
020 */
021
022package org.nuxeo.runtime.launcher;
023
024import java.io.File;
025import java.io.IOException;
026import java.lang.reflect.InvocationTargetException;
027import java.lang.reflect.Method;
028import java.net.URL;
029import java.net.URLClassLoader;
030
031/**
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public class Main {
035
036    public static void main(String[] args) {
037        if (args.length == 0) {
038            System.err.println("You must specify a main class to invoke as the first parameter to that program.");
039            System.exit(2);
040        }
041
042        // The class name format is: path/ClassName:methodName
043        // where the path may be a path to a Jar file or a directory
044        // The default method name is 'main'
045        String path = args[0];
046        String method = "main";
047        int p = path.lastIndexOf(':');
048        if (p > -1) {
049            method = path.substring(p + 1);
050            path = path.substring(0, p);
051            if (method == null || method.length() == 0) {
052                method = "main";
053            }
054        }
055        p = path.lastIndexOf('/');
056        String mainClassName = null;
057        if (p > -1) {
058            mainClassName = path.substring(p + 1);
059            path = path.substring(0, p);
060        }
061
062        // construct class loader to use to load application
063        try {
064            File file = new File(path).getCanonicalFile();
065            URL[] urls = new URL[1];
066            if (file.exists()) {
067                urls[0] = file.toURI().toURL();
068            } else {
069                System.err.println("Could not find main class: " + args[0]
070                        + ". Make sure you have this class on the boot class path");
071                System.exit(3);
072            }
073            URLClassLoader classLoader = new URLClassLoader(urls, Main.class.getClassLoader());
074            Thread.currentThread().setContextClassLoader(classLoader);
075
076            // set the property used by Nuxeo OSGi launcher to create the system
077            // bundle
078            Class<?> mainClass = classLoader.loadClass(mainClassName);
079            Method m = mainClass.getMethod(method, String[].class);
080            if (args.length > 1) {
081                String[] tmp = new String[args.length - 1];
082                System.arraycopy(args, 1, tmp, 0, tmp.length);
083                args = tmp;
084            } else {
085                args = new String[0];
086            }
087            m.invoke(null, new Object[] { args });
088        } catch (IOException e) {
089            System.err.println("Could not find main class: " + args[0]
090                    + ". Make sure you have this class on the boot class path");
091            e.printStackTrace();
092            System.exit(3);
093        } catch (ClassNotFoundException e) {
094            System.err.println("Could not load main class: " + args[0]
095                    + ". Make sure you have this class on the boot class path");
096            e.printStackTrace();
097            System.exit(4);
098        } catch (NoSuchMethodException e) {
099            System.err.println("Could not find main class method: " + mainClassName + "." + method + "(String[] args)");
100            e.printStackTrace();
101            System.exit(5);
102        } catch (SecurityException e) {
103            System.err.println("Failed to access the main class method: " + mainClassName + "." + method
104                    + "(String[] args)");
105            e.printStackTrace();
106            System.exit(5);
107        } catch (IllegalAccessException e) {
108            System.err.println("Failed to invoke method: " + mainClassName + "." + method + "(String[] args)");
109            e.printStackTrace();
110            System.exit(6);
111        } catch (IllegalArgumentException e) {
112            System.err.println("Failed to invoke method: " + mainClassName + "." + method + "(String[] args)");
113            e.printStackTrace();
114            System.exit(6);
115        } catch (InvocationTargetException e) {
116            System.err.println("Failed to invoke method: " + mainClassName + "." + method + "(String[] args)");
117            e.printStackTrace();
118            System.exit(6);
119        }
120
121    }
122
123}