1 43 package net.jforum.dao; 44 45 import java.io.File ; 46 import java.io.FileInputStream ; 47 import java.io.FileOutputStream ; 48 import java.io.OutputStream ; 49 import java.sql.Connection ; 50 import java.sql.DatabaseMetaData ; 51 import java.util.Properties ; 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 65 public class DatabaseWorkarounder 66 { 67 private static Logger logger = Logger.getLogger(DatabaseWorkarounder.class); 68 69 public void handleWorkarounds(Connection 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 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 e) { 98 logger.error(e.toString(), e); 99 } 100 } 101 102 private void handleMySql323() throws Exception 103 { 104 this.ensureDaoClassIsCorrect("net.jforum.dao.mysql.MySQL323DataAccessDriver"); 105 106 Properties p = this.loadSqlQueries(); 107 108 if (p != null) { 109 if (p.size() == 0 || p.getProperty("PermissionControl.deleteRoleValuesByRoleId") == null) { 110 String path = this.buildPath("mysql_323.sql"); 111 112 FileInputStream fis = new FileInputStream (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 126 { 127 this.ensureDaoClassIsCorrect("net.jforum.dao.mysql.MysqlDataAccessDriver"); 128 129 Properties p = this.loadSqlQueries(); 130 131 if (p != null) { 132 if (p.size() == 0 || p.getProperty("PermissionControl.deleteAllRoleValues") == null) { 133 String path = this.buildPath("mysql_40.sql"); 134 135 FileInputStream fis = new FileInputStream (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 149 { 150 this.ensureDaoClassIsCorrect("net.jforum.dao.mysql.MysqlDataAccessDriver"); 151 152 Properties p = this.loadSqlQueries(); 153 154 if (p != null && p.size() > 0) { 155 this.saveSqlQueries(new Properties ()); 156 } 157 158 this.fixEncoding(); 159 } 160 161 private void fixEncoding() throws Exception 162 { 163 FileInputStream fis = null; 164 OutputStream os = null; 165 166 try { 167 Properties p = new Properties (); 168 169 File f = new File (SystemGlobals.getValue(ConfigKeys.DATABASE_DRIVER_CONFIG)); 170 171 if (f.canWrite()) { 172 fis = new FileInputStream (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 (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 shouldBe) throws Exception 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 loadSqlQueries() throws Exception 204 { 205 String sqlQueries = SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_DRIVER); 207 208 File f = new File (sqlQueries); 209 210 Properties p = new Properties (); 211 p.load(new FileInputStream (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 p) throws Exception 222 { 223 FileOutputStream fos = new FileOutputStream (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 daoClassName) throws Exception 236 { 237 String driverConfigPath = SystemGlobals.getValue(ConfigKeys.DATABASE_DRIVER_CONFIG); 238 239 File f = new File (driverConfigPath); 240 241 if (f.canWrite()) { 242 Properties p = new Properties (); 244 245 FileInputStream fis = new FileInputStream (driverConfigPath); 246 FileOutputStream fos = null; 247 248 try { 249 p.load(fis); 250 p.setProperty(ConfigKeys.DAO_DRIVER, daoClassName); 251 252 fos = new FileOutputStream (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 buildPath(String concat) 266 { 267 return new StringBuffer (256) 268 .append(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR)) 269 .append('/') 270 .append("database/mysql/") 271 .append(concat) 272 .toString(); 273 } 274 } 275 | Popular Tags |