001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *   http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 */
018
019package examples;
020
021import java.lang.reflect.Method;
022import java.security.CodeSource;
023import java.util.Enumeration;
024import java.util.HashMap;
025import java.util.Map;
026import java.util.jar.JarEntry;
027import java.util.jar.JarFile;
028
029public class Main {
030
031    /**
032     * Helper application for example classes.
033     * Lists available classes, and provides shorthand invocation.
034     * For example:<br/>
035     * <code>java -jar commons-net-examples-m.n.jar FTPClientExample -l host user password</code>
036     *
037     * @param args the first argument is used to name the class; remaining arguments
038     * are passed to the target class.
039     * @throws Exception
040     * @throws Exception
041     */
042    public static void main(String[] args) throws Exception  {
043        if (args.length==0) {
044            System.out.println("Usage: java -jar examples.jar <exampleClass> <exampleClass parameters>");
045        }
046        CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
047        Map<String, String> map = new HashMap<String, String>();
048        if ( codeSource != null) {
049            final String sourceFile = codeSource.getLocation().getFile();
050            if (sourceFile.endsWith(".jar")) {
051                if (args.length==0) {
052                    System.out.println("\nClasses found in the jar:");
053                }
054                JarFile jf = new JarFile(sourceFile);
055                Enumeration<JarEntry> e = jf.entries();
056                while (e.hasMoreElements()) {
057                  JarEntry je = e.nextElement();
058                  String name = je.getName();
059                  if (!name.endsWith(".class")
060                          || name.contains("$") // subclasses
061                          || name.equals("examples/nntp/NNTPUtils.class") // no main class
062                          || name.equals("examples/util/IOUtil.class") // no main class
063                          || name.equals("examples/Main.class")) {
064                      continue;
065                  }
066                  name = name.replace(".class", "");
067                  int lastSep = name.lastIndexOf('/');
068                  String alias = name.substring(lastSep+1);
069                  if (args.length==0) {
070                      System.out.printf("%-25s %s%n",alias,name);
071                  }
072                  map.put(alias, name);
073                }
074                jf.close();
075            }
076        }
077
078        if (args.length==0) {
079            return;
080        }
081
082        String shortName = args[0];
083        String fullName = map.get(shortName);
084        if (fullName == null) {
085            fullName = shortName;
086        }
087        fullName = fullName.replace('/', '.');
088        Class<?> clazz = Class.forName(fullName);
089        Method m = clazz.getDeclaredMethod("main", new Class[]{args.getClass()});
090        String[] args2 = new String[args.length-1];
091        System.arraycopy(args, 1, args2, 0, args2.length);
092        m.invoke(null, (Object)args2);
093    }
094}