01: /**
02: * Copyright (C) 2001-2006 France Telecom R&D
03: */package common;
04:
05: import org.objectweb.util.monolog.Monolog;
06: import org.objectweb.util.monolog.api.BasicLevel;
07: import org.objectweb.util.monolog.api.Logger;
08:
09: import java.io.IOException;
10: import java.io.InputStream;
11: import java.util.Map;
12: import java.util.Properties;
13:
14: import javax.jdo.JDOHelper;
15: import javax.jdo.PersistenceManagerFactory;
16:
17: public abstract class MainHelper {
18:
19: public PersistenceManagerFactory pmf = null;
20:
21: public Logger logger;
22:
23: public MainHelper() {
24: }
25:
26: public MainHelper init(String[] args) {
27: Map jdoConfig = getJdoConfig(args);
28: pmf = JDOHelper.getPersistenceManagerFactory(jdoConfig);
29: logger = Monolog.initialize().getLogger(getClass().getName());
30: logger.setIntLevel(BasicLevel.INFO);
31: logger.log(BasicLevel.INFO,
32: "PersistenceManagerFactory fetched: "
33: + pmf.getConnectionURL());
34: return this ;
35: }
36:
37: public abstract MainHelper run();
38:
39: public MainHelper end() {
40: logger.log(BasicLevel.INFO,
41: "Closing the PersistenceManagerFactory: "
42: + pmf.getConnectionURL());
43: pmf.close();
44: return this ;
45: }
46:
47: /**
48: * Loads the jdo configuration file from the classpath
49: * @param args is the arguments of the java application. The first
50: * parameter could the name of the jdo configuration file to use
51: * @return
52: */
53: public Map getJdoConfig(String[] args) {
54: String configFile;
55: InputStream is;
56: if (args != null && args.length > 0) {
57: configFile = args[0];
58: is = getClass().getClassLoader().getResourceAsStream(
59: configFile);
60: if (is == null) {
61: throw new IllegalArgumentException(
62: "No JDO configuration file found from the classpath: "
63: + configFile);
64: }
65: } else {
66: configFile = "speedo-jdo.properties";
67: is = getClass().getClassLoader().getResourceAsStream(
68: configFile);
69: if (is == null) {
70: configFile = "speedo-jdo.properties";
71: is = getClass().getClassLoader().getResourceAsStream(
72: configFile);
73: if (is == null) {
74: throw new IllegalArgumentException(
75: "Impossible to find speedo configuration file from the class path (search speedo-jdo.properties or speedo.properties)");
76: }
77: }
78: }
79: //File has been found from the classpath
80: //now load it into a Properties
81: Properties pmfProp = new Properties();
82: try {
83: pmfProp.load(is);
84: } catch (IOException e) {
85: throw new RuntimeException(e);
86: }
87: return pmfProp;
88: }
89: }
|