1 23 24 package org.dbforms.config; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.dbforms.dom.DOMFactory; 30 31 import org.dbforms.util.IEscaper; 32 import org.dbforms.util.ReflectionUtil; 33 import org.dbforms.util.Util; 34 35 import java.sql.Connection ; 36 import java.sql.SQLException ; 37 38 import java.util.ArrayList ; 39 import java.util.Hashtable ; 40 import java.util.Vector ; 41 42 import javax.servlet.ServletConfig ; 43 import javax.servlet.ServletContext ; 44 45 import javax.sql.DataSource ; 46 47 48 49 61 public class DbFormsConfig { 62 63 public static final String CONFIG = "dbformsConfig"; 64 65 66 private ArrayList dbConnectionsList = new ArrayList (); 67 68 69 private DbConnection defaultDbConnection; 70 private IEscaper escaper = null; 71 private Hashtable dbConnectionsHash = new Hashtable (); 72 73 74 private Hashtable tableNameHash = new Hashtable (); 75 private Log logCat = LogFactory.getLog(this.getClass().getName()); 76 private ServletConfig servletConfig; 77 private String defaultEscaperClass = "org.dbforms.util.DefaultEscaperImpl"; 78 private String defaultFormatterClass = "org.dbforms.util.DefaultFormatEmbeddedDataImpl"; 79 private String realPath; 80 81 82 private Vector interceptors = new Vector (); 83 private Vector tables = new Vector (); 84 85 90 public DbFormsConfig(String realPath) { 91 setRealPath(realPath); 92 } 93 94 101 public Connection getConnection() 102 throws IllegalArgumentException , SQLException { 103 return getConnection(null); 104 } 105 106 107 117 public Connection getConnection(String dbConnectionName) 118 throws IllegalArgumentException , SQLException { 119 DataSource dbConnection = null; 120 Connection con = null; 121 122 if ((dbConnection = getDataSource(dbConnectionName)) == null) { 124 throw new IllegalArgumentException ("No DbConnection object configured with name '" 125 + dbConnectionName + "'"); 126 } 127 128 if ((con = dbConnection.getConnection()) == null) { 130 throw new IllegalArgumentException ("JDBC-Troubles: was not able to create connection from " 131 + dbConnection); 132 } 133 134 return con; 135 } 136 137 138 143 public void setDOMFactoryClass(String string) { 144 DOMFactory.setFactoryClass(string); 145 } 146 147 148 155 public DataSource getDataSource(String dbConnectionName) { 156 DbConnection connection = null; 157 158 if (!Util.isNull(dbConnectionName)) { 159 try { 160 connection = (DbConnection) dbConnectionsList.get(Integer.parseInt(dbConnectionName)); 161 } catch (Exception ex) { 162 connection = null; 164 } 165 166 if (connection == null) { 167 connection = (DbConnection) dbConnectionsHash.get(dbConnectionName); 168 } 169 } 170 171 if (connection == null) { 172 connection = defaultDbConnection; 173 } 174 175 if ((connection != null) 176 && !Util.isNull(connection.getContextDataSource())) { 177 DataSource ds = (DataSource ) getServletContext() 178 .getAttribute(connection 179 .getContextDataSource()); 180 181 if (ds != null) { 182 return ds; 183 } 184 } 185 186 return connection; 187 } 188 189 190 195 public void setDefaultEscaperClass(String string) { 196 defaultEscaperClass = string; 197 } 198 199 200 205 public String getDefaultEscaperClass() { 206 return defaultEscaperClass; 207 } 208 209 210 215 public void setDefaultFormatterClass(String string) { 216 defaultFormatterClass = string; 217 } 218 219 220 225 public String getDefaultFormatterClass() { 226 return defaultFormatterClass; 227 } 228 229 230 235 public IEscaper getEscaper() { 236 if (escaper == null) { 237 String s = getDefaultEscaperClass(); 238 239 if (!Util.isNull(s)) { 240 try { 241 escaper = (IEscaper) ReflectionUtil.newInstance(s); 242 } catch (Exception e) { 243 logCat.error("cannot create the new escaper [" + s + "]", e); 244 } 245 } 246 } 247 248 return escaper; 249 } 250 251 252 257 public Vector getInterceptors() { 258 return interceptors; 259 } 260 261 262 267 public void setRealPath(String realPath) { 268 if (!Util.isNull(realPath)) { 269 realPath = realPath.replace('\\', '/'); 270 } 271 272 this.realPath = realPath; 273 } 274 275 276 281 public String getRealPath() { 282 return realPath; 283 } 284 285 286 291 public void setServletConfig(ServletConfig servletConfig) { 292 this.servletConfig = servletConfig; 293 } 294 295 296 301 public ServletConfig getServletConfig() { 302 return servletConfig; 303 } 304 305 306 312 public ServletContext getServletContext() { 313 return servletConfig.getServletContext(); 314 } 315 316 317 324 public Table getTable(int index) { 325 try { 326 return (Table) tables.elementAt(index); 327 } catch (Exception e) { 328 return null; 329 } 330 } 331 332 333 340 public Table getTableByName(String name) { 341 try { 342 return (Table) tableNameHash.get(name); 343 } catch (Exception e) { 344 return null; 345 } 346 } 347 348 349 354 public void addDbConnection(DbConnection dbConnection) { 355 dbConnection.setName(Util.replaceRealPath(dbConnection.getName(), realPath)); 356 dbConnectionsList.add(dbConnection); 357 358 if (!Util.isNull(dbConnection.getId())) { 359 dbConnectionsHash.put(dbConnection.getId(), dbConnection); 360 } 361 362 if ((dbConnection.isDefaultConnection() 365 && ((defaultDbConnection == null) 366 || !defaultDbConnection.isDefaultConnection())) 367 || (defaultDbConnection == null)) { 368 defaultDbConnection = dbConnection; 369 dbConnection.setDefaultConnection(true); 370 } 371 372 logCat.info("::addDbConnection - added the dbConnection [" + dbConnection 373 + "]"); 374 } 375 376 377 382 public void addInterceptor(Interceptor interceptor) { 383 interceptors.addElement(interceptor); 384 } 385 386 387 392 public void addTable(Table table) { 393 logCat.info("add table called"); 394 table.setId(tables.size()); 395 table.initDefaultOrder(); 396 tables.addElement(table); 397 tableNameHash.put(table.getName(), table); 398 } 399 400 401 406 public boolean hasInterceptors() { 407 return (interceptors != null) && (interceptors.size() > 0); 408 } 409 410 411 416 public String toString() { 417 StringBuffer buf = new StringBuffer (); 418 419 for (int i = 0; i < tables.size(); i++) { 420 Table t = (Table) tables.elementAt(i); 421 buf.append("table:\n"); 422 buf.append(t.toString()); 423 } 424 425 return buf.toString(); 426 } 427 } 428 | Popular Tags |