View Javadoc
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  
18  package org.apache.commons.net.examples;
19  
20  import java.io.InputStream;
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  import java.security.CodeSource;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Properties;
27  
28  /**
29   * Helper application for example classes.
30   */
31  public class Main {
32  
33      private static boolean fromJar() {
34          final CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
35          if (codeSource != null) {
36              return codeSource.getLocation().getFile().endsWith(".jar");
37          }
38          return false; // No idea if this can happen
39      }
40  
41      /**
42       * Helper application for example classes. Lists available classes, and provides shorthand invocation. For example:<br>
43       * <code>java -jar commons-net-examples-m.n.jar FTPClientExample -l host user password</code>
44       *
45       * @param args the first argument is used to name the class; remaining arguments are passed to the target class.
46       * @throws Throwable if an error occurs
47       */
48      public static void main(final String[] args) throws Throwable {
49          final Properties fp = new Properties();
50          final InputStream ras = Main.class.getResourceAsStream("examples.properties");
51          if (ras != null) {
52              fp.load(ras);
53          } else {
54              System.err.println("[Cannot find examples.properties file, so aliases cannot be used]");
55          }
56          if (args.length == 0) {
57              if (Thread.currentThread().getStackTrace().length > 2) { // called by Maven
58                  System.out.println(
59                          "Usage: mvn -q exec:java  -Dexec.arguments=<alias or" + " exampleClass>,<exampleClass parameters> (comma-separated, no spaces)");
60                  System.out.println("Or   : mvn -q exec:java  -Dexec.args=\"<alias" + " or exampleClass> <exampleClass parameters>\" (space separated)");
61              } else if (fromJar()) {
62                  System.out.println("Usage: java -jar commons-net-examples-m.n.jar <alias or exampleClass> <exampleClass parameters>");
63              } else {
64                  System.out
65                          .println("Usage: java -cp target/classes org.apache.commons.net.examples.Main" + " <alias or exampleClass> <exampleClass parameters>");
66              }
67              @SuppressWarnings("unchecked") // property names are Strings
68              final List<String> l = (List<String>) Collections.list(fp.propertyNames());
69              if (l.isEmpty()) {
70                  return;
71              }
72              l.sort(null);
73              System.out.println("\nAliases and their classes:");
74              for (final String s : l) {
75                  System.out.printf("%-25s %s%n", s, fp.getProperty(s));
76              }
77              return;
78          }
79  
80          final String shortName = args[0];
81          String fullName = fp.getProperty(shortName);
82          if (fullName == null) {
83              fullName = shortName;
84          }
85          try {
86              final Class<?> clazz = Class.forName(fullName);
87              final Method m = clazz.getDeclaredMethod("main", args.getClass());
88              final String[] args2 = new String[args.length - 1];
89              System.arraycopy(args, 1, args2, 0, args2.length);
90              try {
91                  m.invoke(null, (Object) args2);
92              } catch (final InvocationTargetException ite) {
93                  final Throwable cause = ite.getCause();
94                  if (cause != null) {
95                      throw cause;
96                  }
97                  throw ite;
98              }
99          } catch (final ClassNotFoundException e) {
100             System.out.println(e);
101         }
102     }
103 }