1 16 17 package org.pentaho.core.audit; 18 19 import java.sql.Connection ; 20 import java.sql.DriverManager ; 21 import java.sql.SQLException ; 22 import javax.naming.Context ; 23 import javax.naming.InitialContext ; 24 import javax.sql.DataSource ; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.pentaho.core.system.PentahoSystem; 28 import org.pentaho.messages.Messages; 29 30 34 public class AuditConnection { 35 36 private DataSource auditDs; 37 38 private Context auditContext; 39 40 private boolean initialized; 41 42 private static final String DRIVER_URL = PentahoSystem.getSystemSetting("auditConnection/driverURL", Messages.getString("AUDCONN.CODE_DEFAULT_CONNECT_URL")); 44 private static final String DRIVER_CLASS = PentahoSystem.getSystemSetting("auditConnection/driverCLASS", Messages.getString("AUDCONN.CODE_DEFAULT_CONNECT_DRIVER")); 46 private static final Log logger = LogFactory.getLog(AuditConnection.class); 47 48 public void initialize() { 49 50 if (!initialized) { 51 try { 52 auditContext = new InitialContext (); 53 Object lkup = null; 54 try { 55 lkup = auditContext.lookup("java:Audit"); } catch (Exception ignored) { 57 } 58 if (lkup == null) { 59 try { 60 lkup = auditContext.lookup("java:comp/env/jdbc/Audit"); } catch (Exception ignored) { 62 } 63 } 64 if (lkup == null) { 65 try { 66 lkup = auditContext.lookup("jdbc/Audit"); } catch (Exception ignored) { 68 } 69 } 70 if (lkup != null) { 71 logger.debug(Messages.getString("AUDCONN.DEBUG_LOOKUP_FOUND_CLASS", lkup.getClass().getName())); auditDs = (DataSource ) lkup; 73 } 74 } catch (Exception dsException) { 75 logger.error(Messages.getErrorString("AUDCONN.ERROR_0001_COULD_NOT_GET_DATASOURCE"), dsException); } 77 if (auditDs != null) { 78 initialized = true; 79 } else { 80 try { 81 logger.warn(Messages.getString("AUDCONN.WARN_FALLING_BACK_TO_DRIVERMGR")); Class.forName(DRIVER_CLASS).newInstance(); 83 initialized = true; 84 } catch (Exception ex) { 85 logger.error(Messages.getErrorString("AUDCONN.ERROR_0002_INSTANCE_DRIVER"), ex); } 87 } 88 } 89 } 90 91 public DataSource getAuditDatasource() { 92 initialize(); 93 return auditDs; 94 } 95 96 protected void waitFor(int millis) { 97 try { 98 Thread.sleep(millis); 99 } catch (Exception ex) { 100 } 102 } 103 104 private Connection getConnection() throws SQLException { 106 return (auditDs != null ? auditDs.getConnection() : DriverManager.getConnection(DRIVER_URL, "sa", "")); } 108 109 public Connection getAuditConnection() throws SQLException { 110 Connection con; 111 try { 112 con = getConnection(); 113 try { 114 con.clearWarnings(); 115 } catch (Exception ex) { 116 } 117 return con; 118 } catch (SQLException ex) { 119 } 120 121 waitFor(200); 122 try { 123 con = getConnection(); 124 try { 125 con.clearWarnings(); 126 } catch (Exception ex) { 127 } 128 return con; 129 } catch (SQLException ex) { 130 } 131 132 waitFor(500); 133 try { 134 con = getConnection(); 135 try { 136 con.clearWarnings(); 137 } catch (Exception ex) { 138 } 139 return con; 140 } catch (SQLException ex) { 141 } 142 143 waitFor(2000); 144 con = getConnection(); 145 try { 146 con.clearWarnings(); 147 } catch (Exception ex) { 148 } 149 return con; 150 } 151 } 152 | Popular Tags |