KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > dao > DatabaseWorkarounder


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * Created on 29/11/2005 13:25:55
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.dao;
44
45 import java.io.File JavaDoc;
46 import java.io.FileInputStream JavaDoc;
47 import java.io.FileOutputStream JavaDoc;
48 import java.io.OutputStream JavaDoc;
49 import java.sql.Connection JavaDoc;
50 import java.sql.DatabaseMetaData JavaDoc;
51 import java.util.Properties JavaDoc;
52
53 import net.jforum.ConfigLoader;
54 import net.jforum.util.preferences.ConfigKeys;
55 import net.jforum.util.preferences.SystemGlobals;
56
57 import org.apache.log4j.Logger;
58
59 /**
60  * Try to fix some database configuration problems.
61  * This class will much likely do some checks only for mysql.
62  * @author Rafael Steil
63  * @version $Id: DatabaseWorkarounder.java,v 1.3 2005/12/26 13:04:55 rafaelsteil Exp $
64  */

65 public class DatabaseWorkarounder
66 {
67     private static Logger logger = Logger.getLogger(DatabaseWorkarounder.class);
68     
69     public void handleWorkarounds(Connection JavaDoc c)
70     {
71         if (c == null) {
72             logger.warn("Cannot work with a null connection");
73             return;
74         }
75         
76         if (!"mysql".equals(SystemGlobals.getValue(ConfigKeys.DATABASE_DRIVER_NAME))) {
77             return;
78         }
79         
80         try {
81             DatabaseMetaData JavaDoc meta = c.getMetaData();
82             logger.debug("MySQL Version: " + meta.getDatabaseProductVersion());
83             
84             int major = meta.getDatabaseMajorVersion();
85             int minor = meta.getDatabaseMinorVersion();
86             
87             if (major == 3 && minor == 23) {
88                 this.handleMySql323();
89             }
90             else if (major == 4 && minor == 0) {
91                 this.handleMySql40x();
92             }
93             else if (major > 4 || (major == 4 && minor > 0)) {
94                 this.handleMySql41xPlus();
95             }
96         }
97         catch (Exception JavaDoc e) {
98             logger.error(e.toString(), e);
99         }
100     }
101     
102     private void handleMySql323() throws Exception JavaDoc
103     {
104         this.ensureDaoClassIsCorrect("net.jforum.dao.mysql.MySQL323DataAccessDriver");
105         
106         Properties JavaDoc p = this.loadSqlQueries();
107         
108         if (p != null) {
109             if (p.size() == 0 || p.getProperty("PermissionControl.deleteRoleValuesByRoleId") == null) {
110                 String JavaDoc path = this.buildPath("mysql_323.sql");
111                 
112                 FileInputStream JavaDoc fis = new FileInputStream JavaDoc(path);
113                 
114                 try {
115                     p.load(fis);
116                     this.saveSqlQueries(p);
117                 }
118                 finally {
119                     fis.close();
120                 }
121             }
122         }
123     }
124     
125     private void handleMySql40x() throws Exception JavaDoc
126     {
127         this.ensureDaoClassIsCorrect("net.jforum.dao.mysql.MysqlDataAccessDriver");
128         
129         Properties JavaDoc p = this.loadSqlQueries();
130         
131         if (p != null) {
132             if (p.size() == 0 || p.getProperty("PermissionControl.deleteAllRoleValues") == null) {
133                 String JavaDoc path = this.buildPath("mysql_40.sql");
134                 
135                 FileInputStream JavaDoc fis = new FileInputStream JavaDoc(path);
136                 
137                 try {
138                     p.load(fis);
139                     this.saveSqlQueries(p);
140                 }
141                 finally {
142                     fis.close();
143                 }
144             }
145         }
146     }
147     
148     private void handleMySql41xPlus() throws Exception JavaDoc
149     {
150         this.ensureDaoClassIsCorrect("net.jforum.dao.mysql.MysqlDataAccessDriver");
151         
152         Properties JavaDoc p = this.loadSqlQueries();
153         
154         if (p != null && p.size() > 0) {
155             this.saveSqlQueries(new Properties JavaDoc());
156         }
157         
158         this.fixEncoding();
159     }
160     
161     private void fixEncoding() throws Exception JavaDoc
162     {
163         FileInputStream JavaDoc fis = null;
164         OutputStream JavaDoc os = null;
165         
166         try {
167             Properties JavaDoc p = new Properties JavaDoc();
168             
169             File JavaDoc f = new File JavaDoc(SystemGlobals.getValue(ConfigKeys.DATABASE_DRIVER_CONFIG));
170             
171             if (f.canWrite()) {
172                 fis = new FileInputStream JavaDoc(f);
173                 
174                 p.load(fis);
175                 
176                 p.setProperty(ConfigKeys.DATABASE_MYSQL_ENCODING, "");
177                 p.setProperty(ConfigKeys.DATABASE_MYSQL_UNICODE, "");
178                 
179                 os = new FileOutputStream JavaDoc(f);
180                 p.store(os, null);
181             }
182         }
183         finally {
184             if (fis != null) {
185                 os.close();
186                 fis.close();
187             }
188         }
189     }
190     
191     private void ensureDaoClassIsCorrect(String JavaDoc shouldBe) throws Exception JavaDoc
192     {
193         if (!shouldBe.equals(SystemGlobals.getValue(ConfigKeys.DAO_DRIVER))) {
194             logger.info("MySQL DAO class is incorrect. Setting it to " + shouldBe);
195             
196             this.fixDAODriver(shouldBe);
197             
198             SystemGlobals.setValue(ConfigKeys.DAO_DRIVER, shouldBe);
199             ConfigLoader.loadDaoImplementation();
200         }
201     }
202     
203     private Properties JavaDoc loadSqlQueries() throws Exception JavaDoc
204     {
205         // First, check if we really have a problem
206
String JavaDoc sqlQueries = SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_DRIVER);
207         
208         File JavaDoc f = new File JavaDoc(sqlQueries);
209         
210         Properties JavaDoc p = new Properties JavaDoc();
211         p.load(new FileInputStream JavaDoc(f));
212         
213         if (f.canWrite()) {
214             return p;
215         }
216         
217         logger.warn("Cannot overwrite" + sqlQueries + " file. Insuficient privileges");
218         return null;
219     }
220     
221     private void saveSqlQueries(Properties JavaDoc p) throws Exception JavaDoc
222     {
223         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_DRIVER));
224         
225         try {
226             p.store(fos, null);
227         }
228         finally {
229             fos.close();
230         }
231
232         SystemGlobals.loadQueries(SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_DRIVER));
233     }
234     
235     private void fixDAODriver(String JavaDoc daoClassName) throws Exception JavaDoc
236     {
237         String JavaDoc driverConfigPath = SystemGlobals.getValue(ConfigKeys.DATABASE_DRIVER_CONFIG);
238         
239         File JavaDoc f = new File JavaDoc(driverConfigPath);
240         
241         if (f.canWrite()) {
242             // Fix the DAO class
243
Properties JavaDoc p = new Properties JavaDoc();
244             
245             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(driverConfigPath);
246             FileOutputStream JavaDoc fos = null;
247             
248             try {
249                 p.load(fis);
250                 p.setProperty(ConfigKeys.DAO_DRIVER, daoClassName);
251                 
252                 fos = new FileOutputStream JavaDoc(driverConfigPath);
253                 p.store(fos, null);
254             }
255             finally {
256                 fos.close();
257                 fis.close();
258             }
259         }
260         else {
261             logger.warn("Cannot overwrite" + driverConfigPath + ". Insuficient privileges");
262         }
263     }
264     
265     private String JavaDoc buildPath(String JavaDoc concat)
266     {
267         return new StringBuffer JavaDoc(256)
268             .append(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR))
269             .append('/')
270             .append("database/mysql/")
271             .append(concat)
272             .toString();
273     }
274 }
275
Popular Tags