|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| package org.objectledge.container; |
|
30 |
| |
|
31 |
| import java.lang.reflect.InvocationTargetException; |
|
32 |
| import java.lang.reflect.Method; |
|
33 |
| |
|
34 |
| import org.apache.commons.cli.CommandLine; |
|
35 |
| import org.apache.commons.cli.CommandLineParser; |
|
36 |
| import org.apache.commons.cli.HelpFormatter; |
|
37 |
| import org.apache.commons.cli.Options; |
|
38 |
| import org.apache.commons.cli.ParseException; |
|
39 |
| import org.apache.commons.cli.PosixParser; |
|
40 |
| import org.apache.log4j.BasicConfigurator; |
|
41 |
| import org.apache.log4j.Logger; |
|
42 |
| import org.objectledge.filesystem.FileSystem; |
|
43 |
| import org.picocontainer.PicoContainer; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| public class Main |
|
112 |
| { |
|
113 |
| |
|
114 |
| protected static final String VERSION = "0.1-dev"; |
|
115 |
| |
|
116 |
| private static Options options = new Options(); |
|
117 |
| |
|
118 |
| static |
|
119 |
| { |
|
120 |
86
| options.addOption("r", "root", true, "set local file system root directory");
|
|
121 |
86
| options.addOption("c", "config", true, "set config directory FS path");
|
|
122 |
86
| options.addOption("v", "version", false, "print version information");
|
|
123 |
86
| options.addOption("h", "help", false, "print usage information and exit");
|
|
124 |
| } |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
0
| private Main()
|
|
130 |
| { |
|
131 |
| |
|
132 |
| } |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
86
| public static void main(String[] args)
|
|
140 |
| { |
|
141 |
86
| CommandLineParser parser = new PosixParser();
|
|
142 |
86
| try
|
|
143 |
| { |
|
144 |
86
| CommandLine line = parser.parse(options, args);
|
|
145 |
86
| if(line.hasOption("v"))
|
|
146 |
| { |
|
147 |
0
| printVersion();
|
|
148 |
| } |
|
149 |
86
| if(line.hasOption("h"))
|
|
150 |
| { |
|
151 |
0
| printUsage();
|
|
152 |
| } |
|
153 |
| else |
|
154 |
| { |
|
155 |
86
| String root = line.getOptionValue("r", System.getProperty("user.dir"));
|
|
156 |
86
| String config = line.getOptionValue("c", "/config");
|
|
157 |
86
| int componentArgCount = line.getArgList().size()-1;
|
|
158 |
86
| if(componentArgCount < 0)
|
|
159 |
| { |
|
160 |
0
| System.err.println("component class name required");
|
|
161 |
| } |
|
162 |
| else |
|
163 |
| { |
|
164 |
86
| String componentClassName = (String)line.getArgList().get(0);
|
|
165 |
86
| String[] componentArgs = new String[componentArgCount];
|
|
166 |
86
| line.getArgList().subList(1, componentArgCount).toArray(componentArgs);
|
|
167 |
86
| run(root, config, componentClassName, componentArgs);
|
|
168 |
| } |
|
169 |
| } |
|
170 |
| } |
|
171 |
| catch(ParseException exp) |
|
172 |
| { |
|
173 |
0
| System.err.println("Command line parsing failed: " + exp.getMessage());
|
|
174 |
| } |
|
175 |
| } |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
0
| protected static void printVersion()
|
|
181 |
| { |
|
182 |
0
| System.out.println("ObjectLedge "+VERSION);
|
|
183 |
| } |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
0
| protected static void printUsage()
|
|
189 |
| { |
|
190 |
0
| HelpFormatter formatter = new HelpFormatter();
|
|
191 |
0
| formatter.printHelp("ledge", options);
|
|
192 |
| } |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
86
| public static void run(String root, String config, String componentClassName,
|
|
203 |
| String[] componentArgs) |
|
204 |
| { |
|
205 |
86
| BasicConfigurator.configure();
|
|
206 |
86
| Logger log = Logger.getLogger(Main.class);
|
|
207 |
86
| PicoContainer container = null;
|
|
208 |
86
| try
|
|
209 |
| { |
|
210 |
86
| FileSystem fs = FileSystem.getStandardFileSystem(root);
|
|
211 |
86
| LedgeContainer ledgeContainer = new LedgeContainer(fs, config,
|
|
212 |
| Main.class.getClassLoader()); |
|
213 |
86
| container = ledgeContainer.getContainer();
|
|
214 |
86
| addShutdownHook(ledgeContainer);
|
|
215 |
| } |
|
216 |
| catch(Exception e) |
|
217 |
| { |
|
218 |
0
| log.error("Container composition failed", e);
|
|
219 |
| } |
|
220 |
86
| Class componentClass = null;
|
|
221 |
86
| Object component = null;
|
|
222 |
86
| if(container != null)
|
|
223 |
| { |
|
224 |
86
| try
|
|
225 |
| { |
|
226 |
86
| componentClass = Class.forName(componentClassName);
|
|
227 |
86
| component = container.getComponentInstance(componentClass);
|
|
228 |
86
| if(component == null)
|
|
229 |
| { |
|
230 |
0
| log.error("Component "+componentClassName+" is missing from the assembly");
|
|
231 |
| } |
|
232 |
| } |
|
233 |
| catch(ClassNotFoundException e) |
|
234 |
| { |
|
235 |
0
| log.error("Component class "+componentClassName+" cannot be loaded", e);
|
|
236 |
| } |
|
237 |
| } |
|
238 |
86
| Method method = null;
|
|
239 |
86
| if(componentClass != null)
|
|
240 |
| { |
|
241 |
86
| try
|
|
242 |
| { |
|
243 |
86
| method = componentClass.getMethod("main",
|
|
244 |
| new Class[] { (new String[0]).getClass() }); |
|
245 |
| } |
|
246 |
| catch(NoSuchMethodException e) |
|
247 |
| { |
|
248 |
0
| log.error("Component class "+componentClassName+
|
|
249 |
| " does not declare main(String[]) method", e); |
|
250 |
| } |
|
251 |
| } |
|
252 |
86
| if(method != null)
|
|
253 |
| { |
|
254 |
86
| try
|
|
255 |
| { |
|
256 |
86
| method.invoke(component, new Object[] { componentArgs });
|
|
257 |
| } |
|
258 |
| catch(InvocationTargetException e) |
|
259 |
| { |
|
260 |
0
| log.error("Invocation of "+componentClassName+".main(String[]) threw exception",
|
|
261 |
| e.getTargetException()); |
|
262 |
| } |
|
263 |
| catch(Exception e) |
|
264 |
| { |
|
265 |
0
| log.error("Failed to invoke "+componentClassName+".main(String[])", e);
|
|
266 |
| } |
|
267 |
| } |
|
268 |
| } |
|
269 |
| |
|
270 |
86
| private static void addShutdownHook(final LedgeContainer ledgeContainer)
|
|
271 |
| { |
|
272 |
| |
|
273 |
86
| Runnable shutdownHook = new Runnable() {
|
|
274 |
84
| public void run() {
|
|
275 |
84
| System.out.println("Shutting Down NanoContainer");
|
|
276 |
83
| try {
|
|
277 |
83
| ledgeContainer.killContainer();
|
|
278 |
| } catch (Exception e) { |
|
279 |
0
| e.printStackTrace();
|
|
280 |
| } finally { |
|
281 |
78
| System.out.println("Exiting VM");
|
|
282 |
| } |
|
283 |
| } |
|
284 |
| }; |
|
285 |
86
| Runtime.getRuntime().addShutdownHook(new Thread(shutdownHook));
|
|
286 |
| } |
|
287 |
| } |