sascha@4068: package de.intevation.db; sascha@4068: sascha@4068: import java.io.IOException; sascha@4068: import java.io.InputStream; sascha@4068: teichmann@4772: import java.util.Enumeration; teichmann@4772: import java.util.HashMap; teichmann@4772: import java.util.Map; teichmann@4772: import java.util.Properties; sascha@4068: teichmann@4772: import org.apache.log4j.Logger; sascha@4069: sascha@4068: public class Statements sascha@4068: { sascha@4068: private static Logger log = Logger.getLogger(Statements.class); sascha@4068: sascha@4068: public static final String RESOURCE_PATH = "/sql/"; sascha@4068: public static final String COMMON_PROPERTIES = "-common.properties"; sascha@4068: sascha@4068: protected String type; sascha@4068: protected String driver; sascha@4068: sascha@4069: protected Map statements; sascha@4068: sascha@4068: public Statements(String type, String driver) { sascha@4068: this.type = type; sascha@4068: this.driver = driver; sascha@4068: } sascha@4068: sascha@4069: public SymbolicStatement getStatement(String key) { sascha@4074: return getStatements().get(key); sascha@4074: } sascha@4074: sascha@4074: public Map getStatements() { sascha@4069: if (statements == null) { sascha@4069: statements = loadStatements(); sascha@4069: } sascha@4074: return statements; sascha@4069: } sascha@4069: sascha@4069: protected Map loadStatements() { sascha@4069: Map statements = sascha@4069: new HashMap(); sascha@4069: sascha@4069: Properties properties = loadProperties(); sascha@4069: christian@4477: for (Enumeration e = properties.propertyNames(); e.hasMoreElements();) { sascha@4069: String key = (String)e.nextElement(); sascha@4069: String value = properties.getProperty(key); sascha@4069: SymbolicStatement symbolic = new SymbolicStatement(value); sascha@4069: statements.put(key, symbolic); sascha@4069: } sascha@4069: sascha@4069: return statements; sascha@4069: } sascha@4069: sascha@4068: protected String driverToProperties() { sascha@4068: return teichmann@4736: type + "-" + sascha@4068: driver.replace('.', '-').toLowerCase() + ".properties"; sascha@4068: } sascha@4068: sascha@4068: protected Properties loadCommon() { sascha@4068: Properties common = new Properties(); sascha@4068: sascha@4068: String path = RESOURCE_PATH + type + COMMON_PROPERTIES; sascha@4068: sascha@4068: InputStream in = Statements.class.getResourceAsStream(path); sascha@4068: sascha@4068: if (in != null) { sascha@4068: try { sascha@4068: common.load(in); sascha@4068: } sascha@4068: catch (IOException ioe) { sascha@4068: log.error("cannot load defaults: " + path, ioe); sascha@4068: } sascha@4068: finally { sascha@4068: try { sascha@4068: in.close(); sascha@4068: } sascha@4068: catch (IOException ioe) { sascha@4068: } sascha@4068: } sascha@4068: } sascha@4068: else { sascha@4068: log.warn("cannot find: " + path); sascha@4068: } sascha@4068: sascha@4068: return common; sascha@4068: } sascha@4068: sascha@4069: protected Properties loadProperties() { sascha@4068: sascha@4068: Properties common = loadCommon(); sascha@4068: sascha@4069: Properties properties = new Properties(common); sascha@4068: sascha@4068: String path = RESOURCE_PATH + driverToProperties(); sascha@4068: sascha@4068: InputStream in = Statements.class.getResourceAsStream(path); sascha@4068: sascha@4068: if (in != null) { sascha@4068: try { sascha@4068: properties.load(in); sascha@4068: } sascha@4068: catch (IOException ioe) { sascha@4068: log.error("cannot load statements: " + path, ioe); sascha@4068: } sascha@4068: finally { sascha@4068: try { sascha@4068: in.close(); sascha@4068: } sascha@4068: catch (IOException ioe) { sascha@4068: } sascha@4068: } sascha@4068: } sascha@4068: else { sascha@4068: log.warn("cannot find: " + path); sascha@4068: } sascha@4068: sascha@4068: return properties; sascha@4068: } sascha@4068: } sascha@4068: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :