1 20 21 package freecs.auth.sqlConnectionPool; 22 23 import java.sql.ResultSet ; 24 import java.sql.ResultSetMetaData ; 25 import java.sql.SQLException ; 26 import java.util.Enumeration ; 27 import java.util.Properties ; 28 import java.util.Vector ; 29 30 import freecs.Server; 31 32 37 43 public class DbProperties { 44 public boolean readOnly = false; 45 public String url, table, idField, fc_username, fc_password; 46 public String [] columns, names, updCols, updNames; 47 public Vector colV, nameV; 48 private boolean cachedMetaData=false; 49 public int[] types; 50 public Properties conProps = new Properties (); 51 public int poolsize = 10, maxStmtPerCon = 1000, queryTimeout=0; 52 public long conTTL = 3600000; 53 54 61 public DbProperties (Properties props, Properties mapping) throws Exception { 62 63 int err=0, warn=0; 64 65 colV = new Vector (); 66 nameV = new Vector (); 67 68 Vector updColV = new Vector (); 69 Vector updNamV = new Vector (); 70 71 try { 73 Class.forName (props.getProperty("driver")); 74 } catch (Exception ex) { 75 StringBuffer tsb = new StringBuffer ("Unable to load jdbc-driver '").append (props.getProperty("driver")).append ("'"); 76 throw new Exception (tsb.toString (), ex); 77 } 78 79 StringBuffer warnMsg = new StringBuffer (); 80 81 table = props.getProperty("table"); 83 url = props.getProperty("url"); 84 queryTimeout = parseInt("queryTimeout", props.getProperty("queryTimeout"), 0, warnMsg); 85 poolsize = parseInt("poolsize", props.getProperty("poolsize"), 10, warnMsg); 86 maxStmtPerCon = parseInt("conStatements", props.getProperty("conStatements"), 1000, warnMsg); 87 conTTL = parseInt("conValidityTime", props.getProperty("conValidityTime"), 60, warnMsg); 88 conTTL = conTTL * 60000; 89 if ("true".equalsIgnoreCase(props.getProperty("readOnly")) || 90 "1".equalsIgnoreCase(props.getProperty("readOnly"))) { 91 readOnly = true; 92 } else if ("false".equalsIgnoreCase(props.getProperty("readOnly")) || 93 "0".equalsIgnoreCase(props.getProperty("readOnly"))) { 94 readOnly = false; 95 } else { 96 warnMsg.append (".) Readonly-Flag has unknown value ("); 97 warnMsg.append (props.getProperty("readOnly")); 98 warnMsg.append ("). Defaulting to false."); 99 } 100 conProps.setProperty("user", props.getProperty("username")); 101 conProps.setProperty("password", props.getProperty("password")); 102 103 for (Enumeration e = mapping.keys(); e.hasMoreElements(); ) { 105 String key = (String ) e.nextElement(); 106 String val = mapping.getProperty(key); 107 if ("id".equalsIgnoreCase(key)) { 108 idField = val; 109 nameV.add(key); 110 colV.add(val); 111 } else if ("username".equals(key)) { 112 fc_username = val; 113 } else if ("password".equals(key)) { 114 fc_password = val; 115 } else if (key.startsWith("db.")) { 116 conProps.setProperty(key.substring(3), val); 118 } else if (key.equalsIgnoreCase("userrights") && val.equalsIgnoreCase("serverconfig")) { 119 continue; 120 } else { 121 if ("chattime".equalsIgnoreCase(key) 122 || "userrights".equalsIgnoreCase(key) 123 || "color".equalsIgnoreCase(key) 124 || "cookie".equalsIgnoreCase(key) 125 ) { 130 updNamV.add(key.toLowerCase()); 131 updColV.add(val); 132 } 133 nameV.add(key.toLowerCase()); 134 colV.add(val); 135 } 136 } 137 138 StringBuffer errMsg = new StringBuffer (); 140 if (table == null) { 141 err++; 142 errMsg.append (".) No tablename defined (SQLAuthenticator.table)\r\n"); 143 } 144 if (url == null) { 145 errMsg.append (".) No URL to locate the database (SQLAuthenticator.url)\r\n"); 146 err++; 147 } 148 if (fc_username==null || fc_password==null) { 149 errMsg.append (".) No columns given to check user-credentials for users logging in (username, password)\r\n"); 150 err++; 151 } 152 if (!conProps.containsKey("user") || !conProps.containsKey("password")) { 153 errMsg.append (".) No connection-credentials given (SQLAuthenticator.mapping.username and SQLAuthenticator.mapping.password)\r\n"); 154 err++; 155 } 156 if (warn>0) { 157 warnMsg.insert(0, "Encountered warnings:\r\n"); 158 Server.log (this, warnMsg.toString (), Server.MSG_CONFIG, Server.LVL_MAJOR); 159 } 160 if (err>0) { 161 errMsg.insert(0, " errors:\r\n"); 162 errMsg.insert(0, err); 163 errMsg.insert(0, "Encountered "); 164 throw new Exception (errMsg.toString ()); 165 } 166 columns = (String []) colV.toArray(new String [0]); 167 names = (String []) nameV.toArray(new String [0]); 168 updCols = (String []) updColV.toArray(new String [0]); 169 updNames = (String []) updNamV.toArray(new String [0]); 170 if (Server.TRACE_CREATE_AND_FINALIZE) 171 Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE); 172 } 173 174 public String column4property (String prop) { 175 int idx = nameV.indexOf(prop); 176 if (idx == -1) 177 return null; 178 return columns[idx]; 179 } 180 181 public synchronized void cacheMetaData (ResultSet rs) throws SQLException { 182 if (cachedMetaData) 183 return; 184 Server.log(Thread.currentThread(), this.toString() + " cacheMetaData", Server.MSG_AUTH, Server.LVL_VERBOSE); 185 ResultSetMetaData rsm = rs.getMetaData(); 186 Vector v = new Vector (); 187 types = new int[rsm.getColumnCount()]; 188 for (int i = 1; i < rsm.getColumnCount(); i++) { 189 types[i-1] = rsm.getColumnType(i); 190 } 191 cachedMetaData=true; 192 } 193 194 public String toString() { 195 return "[DbProperties]"; 196 } 197 198 public void finalize() { 199 if (Server.TRACE_CREATE_AND_FINALIZE) 200 Server.log(this, "----------------------------------------FINALIZED", Server.MSG_STATE, Server.LVL_VERY_VERBOSE); 201 } 202 203 210 private int parseInt(String name, String value, int def, StringBuffer logger) { 211 try { 212 int i = Integer.parseInt(value); 213 if (i < 0) { 214 logger.append (".) SQLAuthenticator." + name + " was set to a value below zero. Corrected to default of " + def + ".\r\n"); 215 return def; 216 } else { 217 return i; 218 } 219 } catch (NumberFormatException nfe) { 220 logger.append (".) SQLAuthenticator." + name + " wasn't a number. Corrected to default of " + def + ".\r\n"); 221 return def; 222 } 223 } 224 225 } | Popular Tags |