MySQLVersionWorkarounder.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 8k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  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. import java.io.File;
  45. import java.io.FileInputStream;
  46. import java.io.FileOutputStream;
  47. import java.io.OutputStream;
  48. import java.sql.Connection;
  49. import java.sql.DatabaseMetaData;
  50. import java.util.Properties;
  51. import net.jforum.ConfigLoader;
  52. import net.jforum.util.preferences.ConfigKeys;
  53. import net.jforum.util.preferences.SystemGlobals;
  54. import org.apache.log4j.Logger;
  55. /**
  56.  * Try to fix some database configuration problems.
  57.  * This class will much likely do some checks only for mysql.
  58.  * @author Rafael Steil
  59.  * @version $Id: MySQLVersionWorkarounder.java,v 1.1 2007/09/12 14:43:13 rafaelsteil Exp $
  60.  */
  61. public class MySQLVersionWorkarounder
  62. {
  63. private static Logger logger = Logger.getLogger(MySQLVersionWorkarounder.class);
  64.     private static final String MYSQL_323_DATA_ACCESS_DRIVER = net.jforum.dao.mysql.MySQL323DataAccessDriver.class.getName();
  65.     private static final String MYSQL_DATA_ACCESS_DRIVER = net.jforum.dao.mysql.MysqlDataAccessDriver.class.getName();
  66.     public void handleWorkarounds(Connection c)
  67. {
  68. if (c == null) {
  69. logger.warn("Cannot work with a null connection");
  70. return;
  71.      }
  72.     
  73.      if (!"mysql".equals(SystemGlobals.getValue(ConfigKeys.DATABASE_DRIVER_NAME))) {
  74.      return;
  75.      }
  76.     
  77.      try {
  78.      DatabaseMetaData meta = c.getMetaData();
  79.      logger.debug("MySQL Version: " + meta.getDatabaseProductVersion());
  80.     
  81.      int major = meta.getDatabaseMajorVersion();
  82.      int minor = meta.getDatabaseMinorVersion();
  83.     
  84.      if (major == 3 && minor == 23) {
  85.      this.handleMySql323();
  86.      }
  87.      else if (major == 4 && minor == 0) {
  88.      this.handleMySql40x();
  89.      }
  90.      else if (major > 4 || (major == 4 && minor > 0)) {
  91.      this.handleMySql41xPlus();
  92.      }
  93.      }
  94.      catch (Exception e) {
  95.      logger.error(e.toString(), e);
  96.      }
  97. }
  98. private void handleMySql323() throws Exception
  99. {
  100. this.ensureDaoClassIsCorrect(MYSQL_323_DATA_ACCESS_DRIVER);
  101. Properties p = this.loadSqlQueries();
  102. if (p != null) {
  103. String[] necessaryKeys = { 
  104. "PermissionControl.deleteRoleValuesByRoleId",
  105. "PermissionControl.getRoleIdsByGroup",
  106. "PermissionControl.getRoles",
  107. "PermissionControl.getRoleValues"
  108. };
  109. boolean shouldUpdate = false;
  110. if (p.size() == 0) {
  111. shouldUpdate = true;
  112. }
  113. else {
  114. for (int i = 0; i < necessaryKeys.length; i++) {
  115. String key = necessaryKeys[i];
  116. if (p.getProperty(key) == null) {
  117. shouldUpdate = true;
  118. break;
  119. }
  120. }
  121. }
  122. if (shouldUpdate) {
  123. String path = this.buildPath("mysql_323.sql");
  124. FileInputStream fis = new FileInputStream(path);
  125. try {
  126. p.load(fis);
  127. this.saveSqlQueries(p);
  128. }
  129. finally {
  130. fis.close();
  131. }
  132. }
  133. }
  134. }
  135. private void handleMySql40x() throws Exception
  136. {
  137. this.ensureDaoClassIsCorrect(MYSQL_DATA_ACCESS_DRIVER);
  138. Properties p = this.loadSqlQueries();
  139. if (p != null) {
  140. if (p.size() == 0 || p.getProperty("PermissionControl.deleteAllRoleValues") == null) {
  141. String path = this.buildPath("mysql_40.sql");
  142. FileInputStream fis = new FileInputStream(path);
  143. try {
  144. p.load(fis);
  145. this.saveSqlQueries(p);
  146. }
  147. finally {
  148. fis.close();
  149. }
  150. }
  151. }
  152. }
  153. private void handleMySql41xPlus() throws Exception
  154. {
  155. this.ensureDaoClassIsCorrect(MYSQL_DATA_ACCESS_DRIVER);
  156. Properties p = this.loadSqlQueries();
  157. if (p != null && p.size() > 0) {
  158. this.saveSqlQueries(new Properties());
  159. }
  160. this.fixEncoding();
  161. }
  162. private void fixEncoding() throws Exception
  163. {
  164. FileInputStream fis = null;
  165. OutputStream os = null;
  166. try {
  167. Properties p = new Properties();
  168. File f = new File(SystemGlobals.getValue(ConfigKeys.DATABASE_DRIVER_CONFIG));
  169. if (f.canWrite()) {
  170. fis = new FileInputStream(f);
  171. p.load(fis);
  172. p.setProperty(ConfigKeys.DATABASE_MYSQL_ENCODING, "");
  173. p.setProperty(ConfigKeys.DATABASE_MYSQL_UNICODE, "");
  174. os = new FileOutputStream(f);
  175. p.store(os, null);
  176. }
  177. }
  178. finally {
  179. if (fis != null) {
  180. fis.close();
  181. }
  182. if (os != null) {
  183. os.close();
  184. }
  185. }
  186. }
  187. private void ensureDaoClassIsCorrect(String shouldBe) throws Exception
  188. {
  189. if (!shouldBe.equals(SystemGlobals.getValue(ConfigKeys.DAO_DRIVER))) {
  190. logger.info("MySQL DAO class is incorrect. Setting it to " + shouldBe);
  191. this.fixDAODriver(shouldBe);
  192. SystemGlobals.setValue(ConfigKeys.DAO_DRIVER, shouldBe);
  193. ConfigLoader.loadDaoImplementation();
  194. }
  195. }
  196. private Properties loadSqlQueries() throws Exception
  197. {
  198. // First, check if we really have a problem
  199. String sqlQueries = SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_DRIVER);
  200. File f = new File(sqlQueries);
  201. Properties p = new Properties();
  202. FileInputStream fis = new FileInputStream(f);
  203. try {
  204. p.load(fis);
  205. if (f.canWrite()) {
  206. return p;
  207. }
  208. }
  209. finally {
  210. try { fis.close(); } catch (Exception e) {}
  211. }
  212. logger.warn("Cannot overwrite" + sqlQueries + " file. Insuficient privileges");
  213. return null;
  214. }
  215. private void saveSqlQueries(Properties p) throws Exception
  216. {
  217. FileOutputStream fos = new FileOutputStream(SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_DRIVER));
  218. try {
  219. p.store(fos, null);
  220. }
  221. finally {
  222. fos.close();
  223. }
  224. SystemGlobals.loadQueries(SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_DRIVER));
  225. }
  226. private void fixDAODriver(String daoClassName) throws Exception
  227. {
  228. String driverConfigPath = SystemGlobals.getValue(ConfigKeys.DATABASE_DRIVER_CONFIG);
  229. File f = new File(driverConfigPath);
  230. if (f.canWrite()) {
  231. // Fix the DAO class
  232. Properties p = new Properties();
  233. FileInputStream fis = new FileInputStream(driverConfigPath);
  234. FileOutputStream fos = null;
  235. try {
  236. p.load(fis);
  237. p.setProperty(ConfigKeys.DAO_DRIVER, daoClassName);
  238. fos = new FileOutputStream(driverConfigPath);
  239. p.store(fos, null);
  240. }
  241. finally {
  242. if (fos != null) {
  243. fos.close();
  244. }
  245.                 
  246. fis.close();
  247. }
  248. }
  249. else {
  250. logger.warn("Cannot overwrite" + driverConfigPath + ". Insuficient privileges");
  251. }
  252. }
  253. private String buildPath(String concat)
  254. {
  255. return new StringBuffer(256)
  256. .append(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR))
  257. .append('/')
  258. .append("database/mysql/")
  259. .append(concat)
  260. .toString();
  261. }
  262. }