KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > cli > Main


1 package org.openejb.cli;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.lang.reflect.InvocationTargetException JavaDoc;
6 import java.lang.reflect.Method JavaDoc;
7 import java.net.JarURLConnection JavaDoc;
8 import java.net.URL JavaDoc;
9 import java.net.URI JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Locale JavaDoc;
13 import java.util.Properties JavaDoc;
14 import java.util.jar.JarEntry JavaDoc;
15 import java.util.jar.JarFile JavaDoc;
16
17 import org.openejb.loader.SystemClassPath;
18
19 /**
20  * Entry point for ALL things OpenEJB. This will use the new service
21  * architecture explained here:
22  *
23  * @link http://docs.codehaus.org/display/OPENEJB/Executables
24  *
25  */

26 public class Main {
27     private static CommandFinder finder = null;
28     private static String JavaDoc basePath = "META-INF/org.openejb.cli/";
29     private static String JavaDoc locale = "";
30     private static String JavaDoc descriptionBase = "description";
31
32     private static void setupHome() {
33         try {
34             URL JavaDoc classURL = Thread.currentThread().getContextClassLoader().getResource(basePath + "start");
35
36             if (classURL != null) {
37                 String JavaDoc propsString = classURL.getFile();
38
39                 propsString = propsString.substring(0, propsString.indexOf("!"));
40
41                 URI JavaDoc uri = new URI JavaDoc(propsString);
42
43                 File JavaDoc jarFile = new File JavaDoc(uri);
44
45                 if (jarFile.getName().indexOf("openejb-core") > -1) {
46                     File JavaDoc lib = jarFile.getParentFile();
47                     File JavaDoc home = lib.getParentFile();
48
49                     System.setProperty("openejb.home", home.getAbsolutePath());
50                 }
51             }
52         } catch (Exception JavaDoc 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 JavaDoc lib = new File JavaDoc(System.getProperty("openejb.home") + File.separator + "lib");
60             SystemClassPath systemCP = new SystemClassPath();
61             systemCP.addJarsToPath(lib);
62         } catch (Exception JavaDoc e) {
63             System.err.println("Error setting up the classpath: "+e.getClass() +": "+e.getMessage());
64         }
65     }
66
67     public static void main(String JavaDoc[] args) {
68         ArrayList JavaDoc argsList = new ArrayList JavaDoc();
69
70         for (int i = 0; i < args.length; i++) {
71             String JavaDoc arg = args[i];
72             if (arg.indexOf("-D") == -1) {
73                 argsList.add(arg);
74             } else {
75                 String JavaDoc prop = arg.substring(arg.indexOf("-D") + 2, arg.indexOf("="));
76                 String JavaDoc val = arg.substring(arg.indexOf("=") + 1);
77
78                 System.setProperty(prop, val);
79             }
80         }
81
82         args = (String JavaDoc[])argsList.toArray(new String JavaDoc[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 JavaDoc 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 JavaDoc commandName = args[argIndex];
115
116         try {
117             props = finder.doFindCommandProperies(commandName);
118         } catch (IOException JavaDoc 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 JavaDoc mainClass = props.getProperty("main.class");
133         if (mainClass == null) {
134             throw new NullPointerException JavaDoc("Command "+commandName+" did not specify a main.class property");
135         }
136
137         Class JavaDoc clazz = null;
138         try {
139             clazz = Thread.currentThread().getContextClassLoader().loadClass(mainClass);
140         } catch (ClassNotFoundException JavaDoc e) {
141             throw new IllegalStateException JavaDoc("Command "+commandName+" main.class does not exist: "+mainClass);
142         }
143
144         Method JavaDoc mainMethod = null;
145         try {
146             mainMethod = clazz.getMethod("main", new Class JavaDoc[]{String JavaDoc[].class});
147         } catch (Exception JavaDoc e) {
148             throw new IllegalStateException JavaDoc("Main class of command "+commandName+" does not have a static main method: "+mainClass);
149         }
150
151         argsList = new ArrayList JavaDoc();
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 JavaDoc[])argsList.toArray(new String JavaDoc[argsList.size()]);
165
166         try {
167             mainMethod.invoke(clazz, new Object JavaDoc[] { args });
168         } catch (IllegalArgumentException JavaDoc e) {
169             e.printStackTrace();
170         } catch (IllegalAccessException JavaDoc e) {
171             e.printStackTrace();
172         } catch (InvocationTargetException JavaDoc e) {
173             e.printStackTrace();
174         }
175     }
176
177     private static void printAvailableCommands() {
178         System.out.println("COMMANDS:");
179         
180         try {
181             Enumeration JavaDoc commandHomes = finder.doFindCommands();
182             
183             if (commandHomes != null) {
184                 for (; commandHomes.hasMoreElements(); ) {
185                     URL JavaDoc cHomeURL = (URL JavaDoc)commandHomes.nextElement();
186                     JarURLConnection JavaDoc conn = (JarURLConnection JavaDoc)cHomeURL.openConnection();
187                     JarFile JavaDoc jarfile = conn.getJarFile();
188                     Enumeration JavaDoc commands = jarfile.entries();
189                     
190                     if (commands != null) {
191                         while (commands.hasMoreElements()) {
192                             JarEntry JavaDoc je = (JarEntry JavaDoc)commands.nextElement();
193                             
194                             if (je.getName().indexOf(basePath) > -1 && !je.getName().equals(basePath) && !je.getName().endsWith(".help") && !je.getName().endsWith(".examples")) {
195                                 Properties JavaDoc props = finder.doFindCommandProperies(je.getName().substring(je.getName().lastIndexOf("/") + 1));
196                                  
197                                 String JavaDoc 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 JavaDoc 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