Main.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.examples;

  18. import java.io.InputStream;
  19. import java.lang.reflect.InvocationTargetException;
  20. import java.lang.reflect.Method;
  21. import java.security.CodeSource;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import java.util.Properties;

  25. /**
  26.  * Helper application for example classes.
  27.  */
  28. public class Main {

  29.     private static boolean fromJar() {
  30.         final CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
  31.         if (codeSource != null) {
  32.             return codeSource.getLocation().getFile().endsWith(".jar");
  33.         }
  34.         return false; // No idea if this can happen
  35.     }

  36.     /**
  37.      * Helper application for example classes. Lists available classes, and provides shorthand invocation. For example:<br>
  38.      * <code>java -jar commons-net-examples-m.n.jar FTPClientExample -l host user password</code>
  39.      *
  40.      * @param args the first argument is used to name the class; remaining arguments are passed to the target class.
  41.      * @throws Throwable if an error occurs
  42.      */
  43.     public static void main(final String[] args) throws Throwable {
  44.         final Properties fp = new Properties();
  45.         final InputStream ras = Main.class.getResourceAsStream("examples.properties");
  46.         if (ras != null) {
  47.             fp.load(ras);
  48.         } else {
  49.             System.err.println("[Cannot find examples.properties file, so aliases cannot be used]");
  50.         }
  51.         if (args.length == 0) {
  52.             if (Thread.currentThread().getStackTrace().length > 2) { // called by Maven
  53.                 System.out.println(
  54.                         "Usage: mvn -q exec:java  -Dexec.arguments=<alias or" + " exampleClass>,<exampleClass parameters> (comma-separated, no spaces)");
  55.                 System.out.println("Or   : mvn -q exec:java  -Dexec.args=\"<alias" + " or exampleClass> <exampleClass parameters>\" (space separated)");
  56.             } else if (fromJar()) {
  57.                 System.out.println("Usage: java -jar commons-net-examples-m.n.jar <alias or exampleClass> <exampleClass parameters>");
  58.             } else {
  59.                 System.out
  60.                         .println("Usage: java -cp target/classes org.apache.commons.net.examples.Main" + " <alias or exampleClass> <exampleClass parameters>");
  61.             }
  62.             @SuppressWarnings("unchecked") // property names are Strings
  63.             final List<String> l = (List<String>) Collections.list(fp.propertyNames());
  64.             if (l.isEmpty()) {
  65.                 return;
  66.             }
  67.             l.sort(null);
  68.             System.out.println("\nAliases and their classes:");
  69.             for (final String s : l) {
  70.                 System.out.printf("%-25s %s%n", s, fp.getProperty(s));
  71.             }
  72.             return;
  73.         }

  74.         final String shortName = args[0];
  75.         String fullName = fp.getProperty(shortName);
  76.         if (fullName == null) {
  77.             fullName = shortName;
  78.         }
  79.         try {
  80.             final Class<?> clazz = Class.forName(fullName);
  81.             final Method m = clazz.getDeclaredMethod("main", args.getClass());
  82.             final String[] args2 = new String[args.length - 1];
  83.             System.arraycopy(args, 1, args2, 0, args2.length);
  84.             try {
  85.                 m.invoke(null, (Object) args2);
  86.             } catch (final InvocationTargetException ite) {
  87.                 final Throwable cause = ite.getCause();
  88.                 if (cause != null) {
  89.                     throw cause;
  90.                 }
  91.                 throw ite;
  92.             }
  93.         } catch (final ClassNotFoundException e) {
  94.             System.out.println(e);
  95.         }
  96.     }
  97. }