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 018package org.apache.commons.net.examples; 019 020import java.io.InputStream; 021import java.lang.reflect.InvocationTargetException; 022import java.lang.reflect.Method; 023import java.security.CodeSource; 024import java.util.Collections; 025import java.util.List; 026import java.util.Properties; 027 028/** 029 * Helper application for example classes. 030 */ 031public class Main { 032 033 private static boolean fromJar() { 034 final CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource(); 035 if (codeSource != null) { 036 return codeSource.getLocation().getFile().endsWith(".jar"); 037 } 038 return false; // No idea if this can happen 039 } 040 041 /** 042 * Helper application for example classes. Lists available classes, and provides shorthand invocation. For example:<br> 043 * <code>java -jar commons-net-examples-m.n.jar FTPClientExample -l host user password</code> 044 * 045 * @param args the first argument is used to name the class; remaining arguments are passed to the target class. 046 * @throws Throwable if an error occurs 047 */ 048 public static void main(final String[] args) throws Throwable { 049 final Properties fp = new Properties(); 050 final InputStream ras = Main.class.getResourceAsStream("examples.properties"); 051 if (ras != null) { 052 fp.load(ras); 053 } else { 054 System.err.println("[Cannot find examples.properties file, so aliases cannot be used]"); 055 } 056 if (args.length == 0) { 057 if (Thread.currentThread().getStackTrace().length > 2) { // called by Maven 058 System.out.println( 059 "Usage: mvn -q exec:java -Dexec.arguments=<alias or" + " exampleClass>,<exampleClass parameters> (comma-separated, no spaces)"); 060 System.out.println("Or : mvn -q exec:java -Dexec.args=\"<alias" + " or exampleClass> <exampleClass parameters>\" (space separated)"); 061 } else if (fromJar()) { 062 System.out.println("Usage: java -jar commons-net-examples-m.n.jar <alias or exampleClass> <exampleClass parameters>"); 063 } else { 064 System.out 065 .println("Usage: java -cp target/classes org.apache.commons.net.examples.Main" + " <alias or exampleClass> <exampleClass parameters>"); 066 } 067 @SuppressWarnings("unchecked") // property names are Strings 068 final List<String> l = (List<String>) Collections.list(fp.propertyNames()); 069 if (l.isEmpty()) { 070 return; 071 } 072 l.sort(null); 073 System.out.println("\nAliases and their classes:"); 074 for (final String s : l) { 075 System.out.printf("%-25s %s%n", s, fp.getProperty(s)); 076 } 077 return; 078 } 079 080 final String shortName = args[0]; 081 String fullName = fp.getProperty(shortName); 082 if (fullName == null) { 083 fullName = shortName; 084 } 085 try { 086 final Class<?> clazz = Class.forName(fullName); 087 final Method m = clazz.getDeclaredMethod("main", args.getClass()); 088 final String[] args2 = new String[args.length - 1]; 089 System.arraycopy(args, 1, args2, 0, args2.length); 090 try { 091 m.invoke(null, (Object) args2); 092 } catch (final InvocationTargetException ite) { 093 final Throwable cause = ite.getCause(); 094 if (cause != null) { 095 throw cause; 096 } 097 throw ite; 098 } 099 } catch (final ClassNotFoundException e) { 100 System.out.println(e); 101 } 102 } 103}