1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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;
39 }
40
41
42
43
44
45
46
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) {
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")
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 }