    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 org.apache.commons.net.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(final 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            final
    069            List<String> l = (List<String>) Collections.list(fp.propertyNames());
    070            if (l.isEmpty()) {
    071                return;
    072            }
    073            Collections.sort(l);
    074            System.out.println("\nAliases and their classes:");
    075            for(final String s : l) {
    076                System.out.printf("%-25s %s%n",s,fp.getProperty(s));
    077            }
    078            return;
    079        }
    080
    081        final String shortName = args[0];
    082        String fullName = fp.getProperty(shortName);
    083        if (fullName == null) {
    084            fullName = shortName;
    085        }
    086        fullName = fullName.replace('/', '.');
    087        try {
    088            final Class<?> clazz = Class.forName(fullName);
    089            final Method m = clazz.getDeclaredMethod("main", args.getClass());
    090            final String[] args2 = new String[args.length-1];
    091            System.arraycopy(args, 1, args2, 0, args2.length);
    092            try {
    093                m.invoke(null, (Object)args2);
    094            } catch (final InvocationTargetException ite) {
    095                final Throwable cause = ite.getCause();
    096                if (cause != null) {
    097                    throw cause;
    098                }
    099                throw ite;
    100            }
    101        } catch (final 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}