    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    private static boolean fromJar() {
    035        final CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
    036        if (codeSource != null) {
    037            return codeSource.getLocation().getFile().endsWith(".jar");
    038        }
    039        return false; // No idea if this can happen
    040    }
    041
    042    /**
    043     * Helper application for example classes. Lists available classes, and provides shorthand invocation. For example:<br>
    044     * <code>java -jar commons-net-examples-m.n.jar FTPClientExample -l host user password</code>
    045     *
    046     * @param args the first argument is used to name the class; remaining arguments are passed to the target class.
    047     * @throws Throwable if an error occurs
    048     */
    049    public static void main(final String[] args) throws Throwable {
    050        final Properties fp = new Properties();
    051        final InputStream ras = Main.class.getResourceAsStream("examples.properties");
    052        if (ras != null) {
    053            fp.load(ras);
    054        } else {
    055            System.err.println("[Cannot find examples.properties file, so aliases cannot be used]");
    056        }
    057        if (args.length == 0) {
    058            if (Thread.currentThread().getStackTrace().length > 2) { // called by Maven
    059                System.out.println(
    060                        "Usage: mvn -q exec:java  -Dexec.arguments=<alias or" + " exampleClass>,<exampleClass parameters> (comma-separated, no spaces)");
    061                System.out.println("Or   : mvn -q exec:java  -Dexec.args=\"<alias" + " or exampleClass> <exampleClass parameters>\" (space separated)");
    062            } else if (fromJar()) {
    063                System.out.println("Usage: java -jar commons-net-examples-m.n.jar <alias or exampleClass> <exampleClass parameters>");
    064            } else {
    065                System.out
    066                        .println("Usage: java -cp target/classes org.apache.commons.net.examples.Main" + " <alias or exampleClass> <exampleClass parameters>");
    067            }
    068            @SuppressWarnings("unchecked") // property names are Strings
    069            final List<String> l = (List<String>) Collections.list(fp.propertyNames());
    070            if (l.isEmpty()) {
    071                return;
    072            }
    073            l.sort(null);
    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        try {
    087            final Class<?> clazz = Class.forName(fullName);
    088            final Method m = clazz.getDeclaredMethod("main", args.getClass());
    089            final 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 (final InvocationTargetException ite) {
    094                final Throwable cause = ite.getCause();
    095                if (cause != null) {
    096                    throw cause;
    097                }
    098                throw ite;
    099            }
    100        } catch (final ClassNotFoundException e) {
    101            System.out.println(e);
    102        }
    103    }
    104}