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.lang.reflect.Method;
22  import java.security.CodeSource;
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.jar.JarEntry;
27  import java.util.jar.JarFile;
28  
29  public class Main {
30  
31      /**
32       * Helper application for example classes.
33       * Lists available classes, and provides shorthand invocation.
34       * For example:<br/>
35       * <code>java -jar commons-net-examples-m.n.jar FTPClientExample -l host user password</code>
36       *
37       * @param args the first argument is used to name the class; remaining arguments
38       * are passed to the target class.
39       * @throws Exception
40       * @throws Exception
41       */
42      public static void main(String[] args) throws Exception  {
43          if (args.length==0) {
44              System.out.println("Usage: java -jar examples.jar <exampleClass> <exampleClass parameters>");
45          }
46          CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
47          Map<String, String> map = new HashMap<String, String>();
48          if ( codeSource != null) {
49              final String sourceFile = codeSource.getLocation().getFile();
50              if (sourceFile.endsWith(".jar")) {
51                  if (args.length==0) {
52                      System.out.println("\nClasses found in the jar:");
53                  }
54                  JarFile jf = new JarFile(sourceFile);
55                  Enumeration<JarEntry> e = jf.entries();
56                  while (e.hasMoreElements()) {
57                    JarEntry je = e.nextElement();
58                    String name = je.getName();
59                    if (!name.endsWith(".class")
60                            || name.contains("$") // subclasses
61                            || name.equals("examples/nntp/NNTPUtils.class") // no main class
62                            || name.equals("examples/util/IOUtil.class") // no main class
63                            || name.equals("examples/Main.class")) {
64                        continue;
65                    }
66                    name = name.replace(".class", "");
67                    int lastSep = name.lastIndexOf('/');
68                    String alias = name.substring(lastSep+1);
69                    if (args.length==0) {
70                        System.out.printf("%-25s %s%n",alias,name);
71                    }
72                    map.put(alias, name);
73                  }
74                  jf.close();
75              }
76          }
77  
78          if (args.length==0) {
79              return;
80          }
81  
82          String shortName = args[0];
83          String fullName = map.get(shortName);
84          if (fullName == null) {
85              fullName = shortName;
86          }
87          fullName = fullName.replace('/', '.');
88          Class<?> clazz = Class.forName(fullName);
89          Method m = clazz.getDeclaredMethod("main", new Class[]{args.getClass()});
90          String[] args2 = new String[args.length-1];
91          System.arraycopy(args, 1, args2, 0, args2.length);
92          m.invoke(null, (Object)args2);
93      }
94  }