KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > db > StatementsBuilder


1 /*
2  * Created on Aug 18, 2004
3  */

4 package org.ashkelon.db;
5
6 import java.util.*;
7 import java.io.*;
8
9 import org.apache.tools.ant.*;
10 import org.apache.tools.ant.types.EnumeratedAttribute;
11
12 /**
13  * @author Eitan Suez
14  */

15 public class StatementsBuilder extends Task
16 {
17    /*
18     * currently, each database supported by ashkelon has its own
19     * statements.properties file. this is fine. the problem is
20     * that the vast majority of these statements are actually the
21     * same. only a few are different, necessary to accomodate a
22     * specific database. so i have duplication. if i want to change
23     * a query, i have to do it in n places, n being the number of
24     * databases i want to support. so to resolve this problem, what
25     * i need is a separation of general statements from db-specific ones
26     * and a build-time merge of the general and db-specific files into
27     * a final statements.properties file customized for
28     *
29     * one way to accomplish this is to simply take advantage of hashtables.
30     * i can load the general statements.properties and then the specific
31     * statements.properties into the same Properties object. the nature of
32     * the hash will ensure that specific statements replace general ones.
33     * the resulting hash is then written out to the final statements.properties
34     * file
35     *
36     * the most ideal way to use this code would be as an ant task. this is it.
37     *
38     * <taskdef name="build-statements"
39     * classname="org.ashkelon.db.StatementsBuilder" classpath="?">
40     * <build-statements
41     * dbtype="${dbtype}"
42     * tofile="${build.classes.dir}/org/ashkelon/db/statements.properties" />
43     */

44    
45    private static final String JavaDoc[] DBTYPES = {"postgres", "mysql", "mckoidb"};
46    private String JavaDoc _dbtype = null;
47    private File _tofile = null;
48    
49    public void setDbtype(DbType dbtype) { _dbtype = dbtype.getValue(); }
50    public void setTofile(File tofile) { _tofile = tofile; }
51    
52    public void execute()
53    {
54       try
55       {
56          File general = new File("etc/db/statements.properties");
57          Properties statements = new Properties();
58          statements.load(new FileInputStream(general));
59          
60          File specific = new File("etc/db/statements-"+_dbtype+".properties");
61          if (!specific.exists())
62          {
63             statements.store(new FileOutputStream(_tofile),
64                   "statements file for "+_dbtype);
65             return;
66          }
67          
68          statements.load(new FileInputStream(specific));
69          statements.store(new FileOutputStream(_tofile),
70                "merged statements file for "+_dbtype);
71       }
72       catch (IOException ex)
73       {
74          throw new BuildException(ex);
75       }
76    }
77    
78    public static class DbType extends EnumeratedAttribute
79    {
80       public String JavaDoc[] getValues() { return DBTYPES; }
81    }
82
83 }
84
Popular Tags