001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *   http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 */
018
019package examples;
020
021import java.io.InputStream;
022import java.lang.reflect.InvocationTargetException;
023import java.lang.reflect.Method;
024import java.security.CodeSource;
025import java.util.Collections;
026import java.util.List;
027import java.util.Properties;
028
029/**
030 * Helper application for example classes.
031 */
032public class Main {
033
034    /**
035     * Helper application for example classes.
036     * Lists available classes, and provides shorthand invocation.
037     * For example:<br>
038     * <code>java -jar commons-net-examples-m.n.jar FTPClientExample -l host user password</code>
039     *
040     * @param args the first argument is used to name the class; remaining arguments
041     * are passed to the target class.
042     * @throws Throwable if an error occurs
043     */
044    public static void main(String[] args) throws Throwable  {
045        final Properties fp = new Properties();
046        final InputStream ras = Main.class.getResourceAsStream("examples.properties");
047        if (ras != null) {
048            fp.load(ras);
049        } else {
050            System.err.println("[Cannot find examples.properties file, so aliases cannot be used]");
051        }
052        if (args.length == 0) {
053            if (Thread.currentThread().getStackTrace().length > 2) { // called by Maven
054                System.out.println("Usage: mvn -q exec:java  -Dexec.arguments=<alias or" +
055                                    " exampleClass>,<exampleClass parameters> (comma-separated, no spaces)");
056                System.out.println("Or   : mvn -q exec:java  -Dexec.args=\"<alias" +
057                                    " or exampleClass> <exampleClass parameters>\" (space separated)");
058            } else {
059                if (fromJar()) {
060                    System.out.println(
061                        "Usage: java -jar commons-net-examples-m.n.jar <alias or exampleClass> <exampleClass parameters>");
062                } else {
063                    System.out.println(
064                        "Usage: java -cp target/classes examples/Main <alias or exampleClass> <exampleClass parameters>");
065                }
066            }
067            @SuppressWarnings("unchecked") // property names are Strings
068            List<String> l = (List<String>) Collections.list(fp.propertyNames());
069            if (l.isEmpty()) {
070                return;
071            }
072            Collections.sort(l);
073            System.out.println("\nAliases and their classes:");
074            for(String s : l) {
075                System.out.printf("%-25s %s%n",s,fp.getProperty(s));
076            }
077            return;
078        }
079
080        String shortName = args[0];
081        String fullName = fp.getProperty(shortName);
082        if (fullName == null) {
083            fullName = shortName;
084        }
085        fullName = fullName.replace('/', '.');
086        try {
087            Class<?> clazz = Class.forName(fullName);
088            Method m = clazz.getDeclaredMethod("main", new Class[]{args.getClass()});
089            String[] args2 = new String[args.length-1];
090            System.arraycopy(args, 1, args2, 0, args2.length);
091            try {
092                m.invoke(null, (Object)args2);
093            } catch (InvocationTargetException ite) {
094                Throwable cause = ite.getCause();
095                if (cause != null) {
096                    throw cause;
097                } else {
098                    throw ite;
099                }
100            }
101        } catch (ClassNotFoundException e) {
102            System.out.println(e);
103        }
104    }
105
106    private static boolean fromJar() {
107        final CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
108        if ( codeSource != null) {
109            return codeSource.getLocation().getFile().endsWith(".jar");
110        }
111        return false; // No idea if this can happen
112    }
113}