1 /***
2 * License Agreement.
3 *
4 * JSPA (POJO-SP)
5 *
6 * Copyright (C) 2009 HRX Pty Ltd
7 *
8 * This file is part of JSPA.
9 *
10 * JSPA is free software: you can redistribute it and/or modify it under the
11 * terms of the GNU Lesser General Public License version 3 as published by the
12 * Free Software Foundation.
13 *
14 * JSPA is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with JSPA. If not, see <http://www.gnu.org/licenses/>.
20 */
21 package com.hrx.rasp.util.dao.jdbc;
22
23 import java.io.PrintWriter;
24 import java.sql.Connection;
25 import java.sql.SQLException;
26
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import javax.sql.DataSource;
30
31 import org.apache.log4j.Logger;
32
33 /***
34 * @author dan.stoica <dan.stoica@acslink.net.au>
35 *
36 */
37 public class WrapperDataSource implements DataSource
38 {
39
40 private Logger log = Logger.getLogger(getClass());
41
42 private DataSource ds;
43 private String jndiName;
44 private static final int DEFAULT_RETRY = 3;
45
46 private final int retry;
47
48 public WrapperDataSource(String jndiName, DataSource ds)
49 {
50 this(jndiName, ds, DEFAULT_RETRY);
51 }
52
53 public WrapperDataSource(String jndiName, DataSource ds, int retry)
54 {
55 this.ds = ds;
56 this.jndiName = jndiName;
57 this.retry = retry;
58 }
59
60 public Connection getConnection() throws SQLException
61 {
62 Connection conn = getConnection(this.retry);
63 return conn;
64 }
65
66 public int findDataSource(int retry, String name) throws NamingException
67 {
68 try
69 {
70 log.warn("Data Source resource not found; try to lookup a new data source .");
71 ds = (DataSource) InitialContext.doLookup(name);
72 }
73 catch (NamingException e)
74 {
75 if (retry == 0)
76 {
77 log.fatal("Data source Not available.", e);
78 throw e;
79 }
80 log.warn("No Data Source found. Retry.", e);
81 retry = findDataSource(retry - 1, name);
82 }
83 return retry;
84 }
85
86 private Connection getConnection(int retry) throws SQLException
87 {
88 Connection conn = null;
89 try
90 {
91 conn = ds.getConnection();
92 }
93 catch (SQLException e)
94 {
95 if (retry == 0)
96 {
97 throw e;
98 }
99 try
100 {
101 retry = findDataSource(retry, this.jndiName);
102 conn = getConnection(retry);
103 }
104 catch (NamingException e1)
105 {
106 throw new SQLException(e1);
107 }
108 }
109 return conn;
110 }
111
112 public Connection getConnection(String username, String password) throws SQLException
113 {
114 return ds.getConnection(username, password);
115 }
116
117 public int getLoginTimeout() throws SQLException
118 {
119 return ds.getLoginTimeout();
120 }
121
122 public PrintWriter getLogWriter() throws SQLException
123 {
124 return ds.getLogWriter();
125 }
126
127 public void setLoginTimeout(int loginTimeout) throws SQLException
128 {
129 ds.setLoginTimeout(loginTimeout);
130 }
131
132 public void setLogWriter(PrintWriter logger) throws SQLException
133 {
134 ds.setLogWriter(logger);
135 }
136
137 public boolean isWrapperFor(Class<?> iface) throws SQLException
138 {
139 return false;
140 }
141
142 public <T> T unwrap(Class<T> iface) throws SQLException
143 {
144 return null;
145 }
146 }