1
2
3
4
5
6
7 package genie.core;
8
9
10 import java.io.File;
11 import java.net.URL;
12 import java.util.Collection;
13 import java.util.Enumeration;
14 import java.util.Properties;
15 import java.util.Vector;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import org.apache.commons.digester.Digester;
20 import org.apache.commons.digester.RuleSet;
21 import org.apache.commons.lang.ArrayUtils;
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.log4j.PropertyConfigurator;
26 import org.jdom.Document;
27 import org.jdom.input.SAXBuilder;
28 import org.xml.sax.EntityResolver;
29
30 /***
31 *
32 *
33 * @author T. Kia Ntoni
34 *
35 * 1 janv. 2005
36 * Engine @version
37 */
38 public class Engine extends Manager {
39 private static Log log = LogFactory.getLog(Engine.class);
40 public static final String GENIE_CORE_ENGINE_CONTEXT = "genie.core.engine.context";
41 public static final String GENIE_CORE_ENGINE_CONFIG = "genie.core.engine.config";
42 public static final String GENIE_CORE_ENGINE_CONFIG_PATH = "genie.core.engine.config.path";
43 public static final String ARG_CONFIG_FILE = "-gfile";
44 public static final String ARG_CONFIG_FIND = "-gfind";
45 public static final String ARG_DEFAULT_PROP_FILE = "-dpfile";
46 public static final String ARG_DEFAULT_PROP_FIND = "-dpfind";
47 public static final String ARG_PROP_FIND = "-pfind";
48 public static final String ARG_PROP_FILE = "-pfile";
49 public static final String ARG_ENTITYRESOLVER = "-entityresolver";
50 private static final String ARG_RULESET = "-ruleset";
51 /***
52 *
53 */
54 public Engine() {
55 super();
56
57 }
58
59 public static void main(String[] args) throws Exception {
60 PropertyConfigurator.configure("C:/EclipseBase/workspace/geniesphere/log4j.properties");
61 if (log.isDebugEnabled()) {
62 log.debug("main(String[]) - start");
63 }
64
65 if (StringUtils.isBlank(System.getProperty("genie.home"))) {
66 File dir = new File(".");
67 String home = dir.getCanonicalFile().getAbsolutePath().endsWith("//bin")
68 ? new File("../").getCanonicalPath() : dir.getCanonicalPath();
69 System.setProperty("genie.home", home);
70 if (log.isDebugEnabled()) {
71 log.debug("main(String[]) - setted system property genie.home {" + home + "}");
72 }
73 }
74 try {
75
76 Manager engine = preprocess(args);
77 engine.execute(null);
78
79 } catch (Exception e) {
80 log.error("main(String[])", e);
81
82 e.printStackTrace();
83 throw e;
84 }
85
86 if (log.isDebugEnabled()) {
87 log.debug("main(String[]) - end");
88 }
89 }
90
91 private static Manager preprocess(String[] args) throws Exception {
92 if (log.isDebugEnabled()) {
93 log.debug("preprocess(String[]) - start");
94 log.debug("GENIE_HOME : " + System.getProperty("genie.home"));
95 }
96
97
98 URL defaultPropertiesUrl = getDefaultPropertiesUrl(args);
99 Properties defaults = getDefaultProperties(defaultPropertiesUrl);
100
101 URL customPropertiesUrl = getCustomPropertiesUrl(args, defaults);
102
103 Properties customs = loadCustomProperties(customPropertiesUrl, defaults);
104
105 EntityResolver resolver = getEntityResolver(args, customs);
106
107 URL configUrl = getConfigUrl(args, customs);
108 Document config = getConfig(configUrl, resolver);
109
110 RuleSet ruleSet = getRuleSet(args, customs);
111
112 Manager engine = new Engine();
113 engine.setProperties(customs);
114 engine.setConfig(config);
115
116 Digester digester = getDigester(resolver, ruleSet);
117 digester.push(engine);
118
119 Manager returnManager = (Manager) digester
120 .parse(configUrl.openStream());
121 if (log.isDebugEnabled()) {
122 log.debug("preprocess(String[]) - end");
123 }
124 return returnManager;
125 }
126
127
128
129
130 private static URL findResource(String name, boolean find) throws Exception {
131 if (log.isDebugEnabled()) {
132 log.debug("findGenieConfig(" + name + "," + find + ")");
133 }
134
135 if (StringUtils.isBlank(name)) {
136 if (log.isDebugEnabled()) {
137 log.debug("findGenieConfig(" + name + "," + find + ") - {" + null + "}");
138 }
139 return null;
140 }
141
142 URL url = Engine.class.getResource(name);
143 if (url == null && find) {
144 try {
145
146 File userHomeBased = new File(System.getProperty("user.home") + "/" + name);
147 if (userHomeBased.exists()) {
148 URL returnURL = userHomeBased.toURI().toURL();
149 if (log.isDebugEnabled()) {
150 log.debug("findGenieConfig(" + name + "," + find + ") - {" + returnURL + "}");
151 }
152 return returnURL;
153 }
154
155 String home = StringUtils.isNotBlank(System.getProperty("genie.home"))
156 ? System.getProperty("genie.home") : "";
157 File genieDirBased = new File(home
158 + "/" + name);
159 if (genieDirBased.exists()) {
160 URL returnURL = genieDirBased.toURI().toURL();
161 if (log.isDebugEnabled()) {
162 log.debug("findGenieConfig(" + name + "," + find + ") - {" + returnURL + "}");
163 }
164 return returnURL;
165 }
166
167 File parent = userHomeBased.getAbsoluteFile().getCanonicalFile().getParentFile();
168 while (parent != null) {
169 parent = parent.getAbsoluteFile().getCanonicalFile().getParentFile();
170 File file = new File(parent != null
171 ? parent.getAbsoluteFile().getCanonicalPath() + "/" + name
172 : name);
173 if (file.exists()) {
174 URL returnURL = file.toURI().toURL();
175 if (log.isDebugEnabled()) {
176 log.debug("findGenieConfig(" + name + "," + find + ") - {" + returnURL + "}");
177 }
178 return returnURL;
179 }
180 }
181
182 parent = genieDirBased.getAbsoluteFile().getCanonicalFile().getParentFile();
183 while (parent != null) {
184 parent = parent.getAbsoluteFile().getCanonicalFile().getParentFile();
185 File file = new File(parent != null
186 ? parent.getAbsoluteFile().getCanonicalPath() + "/" + name
187 : name);
188 if (file.exists()) {
189 URL returnURL = file.toURI().toURL();
190 if (log.isDebugEnabled()) {
191 log.debug("findGenieConfig(" + name + "," + find + ") - {" + returnURL + "}");
192 }
193 return returnURL;
194 }
195 }
196 } catch (Exception e) {
197 log.error("findGenieConfig(" + name + "," + find + ")", e);
198 throw e;
199 }
200 }
201 if (log.isDebugEnabled()) {
202 log.debug("findGenieConfig(" + name + "," + find + ") - {" + url + "}");
203 }
204 return url;
205 }
206
207 private static URL getConfigUrl(String[] args, Properties defaults) throws Exception {
208 if (log.isDebugEnabled()) {
209 log.debug("getConfigUrl(String[], Properties) - start");
210 }
211 String find = getKeyValue(args, ARG_CONFIG_FIND).length == 2
212 ? getKeyValue(args, ARG_CONFIG_FIND)[1] : defaults.getProperty("genie.core.engine.config.find", "true");
213 String name = getKeyValue(args, ARG_CONFIG_FILE).length == 2
214 ? getKeyValue(args, ARG_CONFIG_FILE)[1] : defaults.getProperty("genie.core.engine.config.url", "genie.xml");
215 URL returnURL = findResource(name, find.trim().equalsIgnoreCase(
216 "false") ? false : true);
217 if (log.isDebugEnabled()) {
218 log.debug("getConfigUrl(String[], Properties) - name {" + name + "}");
219 log.debug("getConfigUrl(String[], Properties) - url {" + returnURL + "}");
220 log.debug("getConfigUrl(String[], Properties) - end");
221 }
222 return returnURL;
223 }
224
225 private static Document getConfig(URL url, EntityResolver entityResolver) throws Exception {
226 if (log.isDebugEnabled()) {
227 log.debug("getConfig(URL, EntityResolver) - start");
228 }
229
230 try {
231 SAXBuilder builder = new SAXBuilder(true);
232 builder.setEntityResolver(entityResolver);
233 Document genieDoc = builder.build(url.openStream());
234
235 if (log.isDebugEnabled()) {
236 log.debug("getConfig(URL, EntityResolver) - end");
237 }
238 return genieDoc;
239 } catch (Exception e) {
240 log.error("getConfig(URL, EntityResolver)", e);
241
242
243 throw e;
244 }
245 }
246
247 private static void setConfigUrlProperty(String[] args, Properties properties) throws Exception {
248 URL url = getConfigUrl(args, properties);
249 if (url != null && properties != null) {
250 properties.setProperty("genie.core.engine.config.url", url.toURI().toString());
251 }
252 }
253
254 private static URL getCustomPropertiesUrl(String[] args, Properties defaults) throws Exception {
255 String find = getKeyValue(args, ARG_PROP_FIND).length == 2
256 ? getKeyValue(args, ARG_PROP_FIND)[1] : defaults.getProperty("genie.core.engine.user.properties.find", "true");
257 String name = getKeyValue(args, ARG_PROP_FILE).length == 2
258 ? getKeyValue(args, ARG_PROP_FILE)[1] : defaults.getProperty("genie.core.engine.user.properties.url");
259 return findResource(name, find.trim().equalsIgnoreCase("false") ? false : true);
260 }
261
262 private static Properties loadCustomProperties(URL url, Properties defaults) throws Exception {
263 if (log.isDebugEnabled()) {
264 log.debug("loadCustomProperties(URL, Properties) - start");
265 }
266
267 Properties properties = defaults != null ? new Properties(defaults) : new Properties();
268 if ( url != null && url.toURI().toString().endsWith(".xml")) {
269 properties.loadFromXML(url.openStream());
270 }else if ( url != null && ! url.toURI().toString().endsWith(".xml")) {
271 properties.load(url.openStream());
272 }
273
274 if (log.isDebugEnabled()) {
275 log.debug("loadCustomProperties(URL, Properties) - end");
276 }
277 return properties;
278 }
279
280 private static Properties getDefaultProperties(URL url) throws Exception {
281 if (log.isDebugEnabled()) {
282 log.debug("loadDefaultProperties(URL) - url {" + url + "}");
283 }
284 Properties properties = new Properties();
285 if (url != null && url.toURI().toString().endsWith(".properties")) {
286 properties.load(url.openStream());
287 }else if (url != null && url.toURI().toString().endsWith(".xml")) {
288 properties.loadFromXML(url.openStream());
289 }
290 if (log.isDebugEnabled()) {
291 log.debug("loadDefaultProperties(URL) - loaded properties");
292 }
293 return properties;
294 }
295
296 private static URL getDefaultPropertiesUrl(String[] args) throws Exception {
297 if (log.isDebugEnabled()) {
298 log.debug("getDefaultPropertiesUrl(String[], Properties) - start");
299 }
300 String find = getKeyValue(args, ARG_DEFAULT_PROP_FIND).length == 2
301 ? getKeyValue(args, ARG_DEFAULT_PROP_FIND)[1] : "true";
302 String name = getKeyValue(args, ARG_DEFAULT_PROP_FILE).length == 2
303 ? getKeyValue(args, ARG_DEFAULT_PROP_FILE)[1]
304 : "conf/genie_default.properties";
305 URL returnURL = findResource(name, find.trim().equalsIgnoreCase(
306 "false") ? false : true);
307 if (log.isDebugEnabled()) {
308 log
309 .debug("getDefaultPropertiesUrl(String[], Properties) - name {" + name
310 + "}");
311 log.debug("getDefaultPropertiesUrl(String[], Properties) - url {" + returnURL
312 + "}");
313 log.debug("getDefaultPropertiesUrl(String[], Properties) - end");
314 }
315 return returnURL;
316 }
317
318 private static void updateProperties(String key, String value, Properties properties) throws Exception {
319
320
321
322
323 }
324
325 private static void setProperty(String[] args, Properties properties, String regex, String key){
326 if (args != null && properties != null && regex != null && key != null ) {
327 String string = ArrayUtils.toString(args);
328 Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
329 Matcher matcher = pattern.matcher(string);
330 if (matcher.matches()) {
331 String arg = matcher.group();
332 String[] keyValue = arg.split("=");
333 if (keyValue.length == 2) {
334 properties.setProperty(key, keyValue[1]);
335 }
336 }
337 }
338 }
339
340 private static void setDProperties(String[] args, Properties properties){
341 if (args != null && properties != null) {
342 String string = ArrayUtils.toString(args);
343 Pattern pattern = Pattern.compile("-D//w*=//w*\b");
344 Matcher matcher = pattern.matcher(string);
345 if (matcher.matches()) {
346 while (matcher.find()) {
347 String arg = matcher.group();
348 String[] keyValue = arg.split("=");
349 if (keyValue.length == 2) {
350 properties.setProperty(keyValue[0].substring(2), keyValue[1]);
351 System.out.print("Setting Dproperty ["
352 + keyValue[0].substring(2) + "," + keyValue[1] + "]");
353 }
354 }
355 }
356 }
357 }
358
359 private static Digester getDigester(EntityResolver entityResolver, RuleSet ruleSet) throws Exception {
360 Digester digester = new Digester();
361 digester.setEntityResolver(entityResolver);
362 digester.setValidating(true);
363 digester.addRuleSet(ruleSet);
364 return digester;
365 }
366
367 private static RuleSet getRuleSet(String[] args, Properties properties) throws Exception {
368 if (log.isDebugEnabled()) {
369 log.debug("getRuleSet(String[], Properties) - start");
370 }
371
372 String clsName = null;
373 if (args != null && getKeyValue(args, ARG_RULESET).length == 2) {
374 clsName = getKeyValue(args, ARG_RULESET)[1];
375 if (log.isDebugEnabled()) {
376 log.debug("getRuleSet(String[], Properties) - RuleSet implementation from command line {" + clsName + "}");
377 }
378 } else if (properties != null) {
379 clsName = properties.getProperty("genie.core.engine.ruleset", ConfigRuleSet.class.getName());
380 if (log.isDebugEnabled()) {
381 log.debug("getRuleSet(String[], Properties) - RuleSet implementation from properties {" + clsName + "}");
382 }
383 }
384 RuleSet returnRuleSet = (RuleSet) Class.forName(clsName).newInstance();
385 if (log.isDebugEnabled()) {
386 log.debug("getRuleSet(String[], Properties) - end");
387 }
388 return returnRuleSet;
389 }
390
391 private static void setRuleSetProperty(String[] args, Properties properties) {
392 setProperty(args, properties, "-ruleset=//w*\b", "genie.core.engine.ruleset");
393 }
394
395 private static EntityResolver getEntityResolver(String[] args, Properties properties) throws Exception {
396 if (log.isDebugEnabled()) {
397 log.debug("getEntityResolver(String[], Properties) - start");
398 }
399
400 EntityResolver entityResolver = null;
401 String entityResolverClass = null;
402 if (args != null && getKeyValue(args, ARG_ENTITYRESOLVER).length == 2) {
403 entityResolverClass = getKeyValue(args, ARG_ENTITYRESOLVER)[1];
404 if (log.isDebugEnabled()) {
405 log.debug("getEntityResolver(String[], Properties) - EntityResolver implementation from command line argument {" + entityResolverClass + "}");
406 }
407 }
408 if (entityResolver == null && properties != null) {
409 entityResolverClass = properties.getProperty("genie.core.engine.entityResolver", EntityValidator.class.getName());
410 if (log.isDebugEnabled()) {
411 log.debug("getEntityResolver(String[], Properties) - EntityResolver implementation from properties {" + entityResolverClass + "}");
412 }
413 }
414 entityResolver = (EntityResolver) Class
415 .forName(entityResolverClass).newInstance();
416 if (entityResolver instanceof IEntityResolver) {
417 String localSchemaId = System.getProperty("genie.home") + "/conf/genie_1_0.dtd";
418 ((IEntityResolver) entityResolver).register("http://geniesphere.sourceforge.net/dtd/genie_1_0.dtd",
419 localSchemaId);
420 }
421 if (properties != null && entityResolver instanceof IEntityResolver) {
422 Enumeration names = properties.propertyNames();
423 while (names.hasMoreElements()) {
424 String name = (String) names.nextElement();
425 if (name.endsWith(".schema_id") && StringUtils.isNotBlank(properties.getProperty(name + "_mapper"))) {
426 ((IEntityResolver) entityResolver).register(properties.getProperty(name),
427 properties.getProperty(name + "_mapper"));
428 }
429 }
430 }
431
432 if (log.isDebugEnabled()) {
433 log.debug("getEntityResolver(String[], Properties) - end");
434 }
435 return entityResolver;
436 }
437
438 private static void setEntityResolverProperty(String[] args, Properties properties){
439 if (args != null && properties != null) {
440 setProperty(args, properties, "-entityResolver=//w*\b", "genie.core.engine.entityResolver");
441 }
442 }
443
444 private static String[] getKeyValue(String[] args, String regexPrefix) {
445 if (log.isDebugEnabled()) {
446 log.debug("getKeyValue(" + ArrayUtils.toString(args) + "," + regexPrefix + ")");
447 }
448 if (args != null && regexPrefix != null) {
449 for (int i = 0; i < args.length; i++) {
450 String arg = args[i];
451 if (arg.equals(regexPrefix)) {
452 if (log.isDebugEnabled()) {
453 log.debug("getKeyValue(" + ArrayUtils.toString(args) + "," + regexPrefix +
454 ") {" + ArrayUtils.toString(arg.split("=")) + "}");
455 }
456 return arg.split("=");
457 }
458 if (arg.startsWith(regexPrefix+"=")) {
459 if (log.isDebugEnabled()) {
460 log.debug("getKeyValue(" + ArrayUtils.toString(args) + "," + regexPrefix +
461 ") {" + ArrayUtils.toString(arg.split("=")) + "}");
462 }
463 return arg.split("=");
464 }
465 }
466 }
467 String[] returnStringArray = new String[0];
468 if (log.isDebugEnabled()) {
469 log.debug("getKeyValue(String[], String) - end");
470 }
471 return returnStringArray ;
472 }
473
474 private static Collection<String[]> getKeyValuetest(String[] args, String regexPrefix){
475 if (log.isDebugEnabled()) {
476 log.debug("getKeyValuetest(String[], String) - start");
477 }
478
479 Vector<String[]> result = new Vector<String[]>();
480 if (args != null) {
481 String string = ArrayUtils.toString(args);
482 Pattern pattern = Pattern.compile(regexPrefix + "=//w*\b");
483 Matcher matcher = pattern.matcher(string);
484 if (matcher.matches()) {
485 while (matcher.find()) {
486 String arg = matcher.group();
487 result.add(arg.split("="));
488 }
489 }
490 }
491
492 if (log.isDebugEnabled()) {
493 log.debug("getKeyValuetest(String[], String) - end");
494 }
495 return result;
496 }
497
498 private static Collection<String[]> getKeyValues(String[] args, String regexPrefix){
499 Vector<String[]> result = new Vector<String[]>();
500 if (args != null) {
501 String string = ArrayUtils.toString(args);
502 Pattern pattern = Pattern.compile(regexPrefix + "=//w*\b");
503 Matcher matcher = pattern.matcher(string);
504 if (matcher.matches()) {
505 while (matcher.find()) {
506 String arg = matcher.group();
507 result.add(arg.split("="));
508 }
509 }
510 }
511 return result;
512 }
513
514
515 }