KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > database > DatabaseAbstractionLayer


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.server.database;
20
21 import org.lucane.common.Logging;
22 import org.lucane.server.ServerConfig;
23 import org.lucane.server.database.xml.*;
24
25 import java.lang.reflect.*;
26 import java.sql.*;
27
28 import org.apache.commons.dbcp.BasicDataSource;
29 import javax.sql.DataSource JavaDoc;
30
31 /**
32  * Database abstraction layer.
33  * Allow to use different databases, even if the SQL types aren't completely normalized
34  */

35 public abstract class DatabaseAbstractionLayer
36 {
37     /**
38      * DatabaseLayer Factory
39      * Get the layer corresponding to the driver
40      */

41     public static DatabaseAbstractionLayer createLayer(ServerConfig config)
42     throws Exception JavaDoc
43     {
44         Class.forName(config.getDbDriver());
45         
46         BasicDataSource ds = new BasicDataSource();
47         ds.setDriverClassName(config.getDbDriver());
48         ds.setUsername(config.getDbLogin());
49         ds.setPassword(config.getDbPassword());
50         ds.setUrl(config.getDbUrl());
51                 
52         ds.setPoolPreparedStatements(false);
53         ds.setInitialSize(config.getDbPoolInitialSize());
54         ds.setMaxActive(config.getDbPoolMaxActive());
55         ds.setMaxIdle(config.getDbPoolMaxIdle());
56         ds.setMinIdle(config.getDbPoolMinIdle());
57         ds.setMaxWait(config.getDbPoolMaxWait());
58         
59         Logging.getLogger().info("Pool initialized (" +
60                 "initial size=" + config.getDbPoolInitialSize() +
61                 ", max active=" + config.getDbPoolMaxActive() +
62                 ", max idle=" + config.getDbPoolMaxIdle() +
63                 ", min idle=" + config.getDbPoolMinIdle() +
64                 ", max wait=" + config.getDbPoolMaxWait() +
65                 ")");
66                 
67         
68         //-- dynamic layer loading
69
Class JavaDoc klass = Class.forName(config.getDbLayer());
70         Class JavaDoc[] types = {DataSource.class};
71         Object JavaDoc[] values = {ds};
72         Constructor constr = klass.getConstructor(types);
73         return (DatabaseAbstractionLayer)constr.newInstance(values);
74     }
75         
76     /**
77      * Get an opened connection
78      *
79      * @return the connection
80      */

81     public abstract Connection getConnection() throws SQLException;
82     
83     /**
84      * Check if a table is existing in the system
85      *
86      * @param tableName the name of the table
87      * @return true if the database has this table
88      */

89     public boolean hasTable(String JavaDoc tableName)
90     throws SQLException
91     {
92         boolean has = false;
93         String JavaDoc[] types = {"TABLE"};
94         Connection c = this.getConnection();
95         ResultSet rs = c.getMetaData().getTables(null, null, null, types);
96         
97         while(!has && rs.next())
98             has = rs.getString(3).equalsIgnoreCase(tableName);
99         
100         rs.close();
101         c.close();
102         
103         return has;
104     }
105     
106     /**
107      * Escape a query (replace bad chars with escape codes)
108      *
109      * @param query the query to escape
110      * @return the escaped query
111      */

112     public String JavaDoc escape(String JavaDoc query)
113     {
114         return query.replaceAll("'", "\\'");
115     }
116     
117     /**
118      * Return an instance of xml table creator
119      * @return the TableCreator
120      */

121     public TableCreator getTableCreator()
122     {
123         TableCreator creator = new DefaultTableCreator();
124         creator.setDbLayer(this);
125         return creator;
126     }
127     
128     /**
129      * Resolve a logic type
130      *
131      * @param type the logic type (see DBMSDataTypes on the wiki)
132      * @return the implementation dependent type
133      */

134     public abstract String JavaDoc resolveType(String JavaDoc type);
135 }
136
Popular Tags