KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > audit > AuditConnection


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Mar 21, 2005
14  * @author Marc Batchelor
15  */

16
17 package org.pentaho.core.audit;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.DriverManager JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import javax.naming.Context JavaDoc;
23 import javax.naming.InitialContext JavaDoc;
24 import javax.sql.DataSource JavaDoc;
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 /**
31  * @author mbatchel
32  *
33  */

34 public class AuditConnection {
35
36     private DataSource JavaDoc auditDs;
37
38     private Context JavaDoc auditContext;
39
40     private boolean initialized;
41
42     private static final String JavaDoc DRIVER_URL = PentahoSystem.getSystemSetting("auditConnection/driverURL", Messages.getString("AUDCONN.CODE_DEFAULT_CONNECT_URL")); //$NON-NLS-1$ //$NON-NLS-2$
43

44     private static final String JavaDoc DRIVER_CLASS = PentahoSystem.getSystemSetting("auditConnection/driverCLASS", Messages.getString("AUDCONN.CODE_DEFAULT_CONNECT_DRIVER")); //$NON-NLS-1$ //$NON-NLS-2$
45

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 JavaDoc();
53                 Object JavaDoc lkup = null;
54                 try {
55                     lkup = auditContext.lookup("java:Audit"); //$NON-NLS-1$
56
} catch (Exception JavaDoc ignored) {
57                 }
58                 if (lkup == null) {
59                     try {
60                         lkup = auditContext.lookup("java:comp/env/jdbc/Audit"); //$NON-NLS-1$
61
} catch (Exception JavaDoc ignored) {
62                     }
63                 }
64                 if (lkup == null) {
65                     try {
66                         lkup = auditContext.lookup("jdbc/Audit"); //$NON-NLS-1$
67
} catch (Exception JavaDoc ignored) {
68                     }
69                 }
70                 if (lkup != null) {
71                     logger.debug(Messages.getString("AUDCONN.DEBUG_LOOKUP_FOUND_CLASS", lkup.getClass().getName())); //$NON-NLS-1$
72
auditDs = (DataSource JavaDoc) lkup;
73                 }
74             } catch (Exception JavaDoc dsException) {
75                 logger.error(Messages.getErrorString("AUDCONN.ERROR_0001_COULD_NOT_GET_DATASOURCE"), dsException); //$NON-NLS-1$
76
}
77             if (auditDs != null) {
78                 initialized = true;
79             } else {
80                 try {
81                     logger.warn(Messages.getString("AUDCONN.WARN_FALLING_BACK_TO_DRIVERMGR")); //$NON-NLS-1$
82
Class.forName(DRIVER_CLASS).newInstance();
83                     initialized = true;
84                 } catch (Exception JavaDoc ex) {
85                     logger.error(Messages.getErrorString("AUDCONN.ERROR_0002_INSTANCE_DRIVER"), ex); //$NON-NLS-1$
86
}
87             }
88         }
89     }
90
91     public DataSource JavaDoc getAuditDatasource() {
92         initialize();
93         return auditDs;
94     }
95
96     protected void waitFor(int millis) {
97         try {
98             Thread.sleep(millis);
99         } catch (Exception JavaDoc ex) {
100             // ignore the interrupted exception, if it happens
101
}
102     }
103
104     // Handle JNDI being unavailable
105
private Connection JavaDoc getConnection() throws SQLException JavaDoc {
106         return (auditDs != null ? auditDs.getConnection() : DriverManager.getConnection(DRIVER_URL, "sa", "")); //$NON-NLS-1$ //$NON-NLS-2$
107
}
108
109     public Connection JavaDoc getAuditConnection() throws SQLException JavaDoc {
110         Connection JavaDoc con;
111         try {
112             con = getConnection();
113             try {
114                 con.clearWarnings();
115             } catch (Exception JavaDoc ex) {
116             }
117             return con;
118         } catch (SQLException JavaDoc ex) {
119         }
120
121         waitFor(200);
122         try {
123             con = getConnection();
124             try {
125                 con.clearWarnings();
126             } catch (Exception JavaDoc ex) {
127             }
128             return con;
129         } catch (SQLException JavaDoc ex) {
130         }
131
132         waitFor(500);
133         try {
134             con = getConnection();
135             try {
136                 con.clearWarnings();
137             } catch (Exception JavaDoc ex) {
138             }
139             return con;
140         } catch (SQLException JavaDoc ex) {
141         }
142
143         waitFor(2000);
144         con = getConnection();
145         try {
146             con.clearWarnings();
147         } catch (Exception JavaDoc ex) {
148         }
149         return con;
150     }
151 }
152
Popular Tags