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