1 package org.openejb.cli; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.lang.reflect.InvocationTargetException ; 6 import java.lang.reflect.Method ; 7 import java.net.JarURLConnection ; 8 import java.net.URL ; 9 import java.net.URI ; 10 import java.util.ArrayList ; 11 import java.util.Enumeration ; 12 import java.util.Locale ; 13 import java.util.Properties ; 14 import java.util.jar.JarEntry ; 15 import java.util.jar.JarFile ; 16 17 import org.openejb.loader.SystemClassPath; 18 19 26 public class Main { 27 private static CommandFinder finder = null; 28 private static String basePath = "META-INF/org.openejb.cli/"; 29 private static String locale = ""; 30 private static String descriptionBase = "description"; 31 32 private static void setupHome() { 33 try { 34 URL classURL = Thread.currentThread().getContextClassLoader().getResource(basePath + "start"); 35 36 if (classURL != null) { 37 String propsString = classURL.getFile(); 38 39 propsString = propsString.substring(0, propsString.indexOf("!")); 40 41 URI uri = new URI (propsString); 42 43 File jarFile = new File (uri); 44 45 if (jarFile.getName().indexOf("openejb-core") > -1) { 46 File lib = jarFile.getParentFile(); 47 File home = lib.getParentFile(); 48 49 System.setProperty("openejb.home", home.getAbsolutePath()); 50 } 51 } 52 } catch (Exception e) { 53 System.err.println("Error setting openejb.home :"+e.getClass() +": "+e.getMessage()); 54 } 55 } 56 57 private static void setupClasspath() { 58 try { 59 File lib = new File (System.getProperty("openejb.home") + File.separator + "lib"); 60 SystemClassPath systemCP = new SystemClassPath(); 61 systemCP.addJarsToPath(lib); 62 } catch (Exception e) { 63 System.err.println("Error setting up the classpath: "+e.getClass() +": "+e.getMessage()); 64 } 65 } 66 67 public static void main(String [] args) { 68 ArrayList argsList = new ArrayList (); 69 70 for (int i = 0; i < args.length; i++) { 71 String arg = args[i]; 72 if (arg.indexOf("-D") == -1) { 73 argsList.add(arg); 74 } else { 75 String prop = arg.substring(arg.indexOf("-D") + 2, arg.indexOf("=")); 76 String val = arg.substring(arg.indexOf("=") + 1); 77 78 System.setProperty(prop, val); 79 } 80 } 81 82 args = (String [])argsList.toArray(new String [argsList.size()]); 83 84 finder = new CommandFinder(basePath); 85 locale = Locale.getDefault().getLanguage(); 86 87 setupHome(); 88 setupClasspath(); 89 90 if (args.length == 0 || args[0].equals("--help")) { 91 System.out.println("Usage: openejb help [command]"); 92 93 printAvailableCommands(); 94 95 return; 96 } 97 98 Properties props = null; 99 boolean help = false; 100 int argIndex = 0; 101 102 if (args[0].equals("help")) { 103 if (args.length < 2) { 104 printAvailableCommands(); 105 106 return; 107 } 108 109 help = true; 110 111 argIndex = 1; 112 } 113 114 String commandName = args[argIndex]; 115 116 try { 117 props = finder.doFindCommandProperies(commandName); 118 } catch (IOException e1) { 119 System.out.println("Unavailable command: " + commandName); 120 121 printAvailableCommands(); 122 123 return; 124 } 125 126 if (props == null) { 127 System.out.println("Unavailable command: " + commandName); 128 printAvailableCommands(); 129 return; 130 } 131 132 String mainClass = props.getProperty("main.class"); 133 if (mainClass == null) { 134 throw new NullPointerException ("Command "+commandName+" did not specify a main.class property"); 135 } 136 137 Class clazz = null; 138 try { 139 clazz = Thread.currentThread().getContextClassLoader().loadClass(mainClass); 140 } catch (ClassNotFoundException e) { 141 throw new IllegalStateException ("Command "+commandName+" main.class does not exist: "+mainClass); 142 } 143 144 Method mainMethod = null; 145 try { 146 mainMethod = clazz.getMethod("main", new Class []{String [].class}); 147 } catch (Exception e) { 148 throw new IllegalStateException ("Main class of command "+commandName+" does not have a static main method: "+mainClass); 149 } 150 151 argsList = new ArrayList (); 152 int startPoint = 1; 153 154 if (help) { 155 startPoint = 2; 156 157 argsList.add("--help"); 158 } 159 160 for (int i = startPoint; i < args.length; i++) { 161 argsList.add(args[i]); 162 } 163 164 args = (String [])argsList.toArray(new String [argsList.size()]); 165 166 try { 167 mainMethod.invoke(clazz, new Object [] { args }); 168 } catch (IllegalArgumentException e) { 169 e.printStackTrace(); 170 } catch (IllegalAccessException e) { 171 e.printStackTrace(); 172 } catch (InvocationTargetException e) { 173 e.printStackTrace(); 174 } 175 } 176 177 private static void printAvailableCommands() { 178 System.out.println("COMMANDS:"); 179 180 try { 181 Enumeration commandHomes = finder.doFindCommands(); 182 183 if (commandHomes != null) { 184 for (; commandHomes.hasMoreElements(); ) { 185 URL cHomeURL = (URL )commandHomes.nextElement(); 186 JarURLConnection conn = (JarURLConnection )cHomeURL.openConnection(); 187 JarFile jarfile = conn.getJarFile(); 188 Enumeration commands = jarfile.entries(); 189 190 if (commands != null) { 191 while (commands.hasMoreElements()) { 192 JarEntry je = (JarEntry )commands.nextElement(); 193 194 if (je.getName().indexOf(basePath) > -1 && !je.getName().equals(basePath) && !je.getName().endsWith(".help") && !je.getName().endsWith(".examples")) { 195 Properties props = finder.doFindCommandProperies(je.getName().substring(je.getName().lastIndexOf("/") + 1)); 196 197 String key = locale.equals("en") ? descriptionBase : descriptionBase + "." + locale; 198 199 System.out.println("\n " + props.getProperty("name") + " - " + props.getProperty(key)); 200 } 201 } 202 } 203 } 204 } else { 205 System.out.println("No available commands!"); 206 } 207 } catch (IOException e) { 208 e.printStackTrace(); 209 } 210 211 System.out.println("\nTry 'openejb help <command>' for more information about the command.\n"); 212 System.out.println("OpenEJB -- EJB Container System and EJB Server."); 213 System.out.println("For updates and additional information, visit"); 214 System.out.println("http://www.openejb.org\n"); 215 System.out.println("Bug Reports to <user@openejb.org>"); 216 } 217 } 218 | Popular Tags |