1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.dao.shard;
16  
17  import com.liferay.portal.kernel.dao.shard.ShardUtil;
18  
19  import java.io.PrintWriter;
20  
21  import java.sql.Connection;
22  import java.sql.SQLException;
23  
24  import java.util.logging.Logger;
25  
26  import javax.sql.DataSource;
27  
28  /**
29   * <a href="ShardDataSource.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Alexander Chow
32   */
33  public class ShardDataSource implements DataSource {
34  
35      public static DataSource getInstance() {
36          return _instance;
37      }
38  
39      public Connection getConnection() throws SQLException {
40          return getDataSource().getConnection();
41      }
42  
43      public Connection getConnection(String username, String password)
44          throws SQLException {
45  
46          return getDataSource().getConnection(username, password);
47      }
48  
49      public int getLoginTimeout() throws SQLException {
50          return getDataSource().getLoginTimeout();
51      }
52  
53      public PrintWriter getLogWriter() throws SQLException {
54          return getDataSource().getLogWriter();
55      }
56  
57      public Logger getParentLogger() {
58  
59          // JDK 7
60  
61          throw new UnsupportedOperationException();
62      }
63  
64      public boolean isWrapperFor(Class<?> clazz) {
65  
66          // Directly implement this method for JDK 5 compatibility. Logic is
67          // copied from org.springframework.jdbc.datasource.AbstractDataSource.
68  
69          return DataSource.class.equals(clazz);
70      }
71  
72      public void setLoginTimeout(int seconds) throws SQLException {
73          getDataSource().setLoginTimeout(seconds);
74      }
75  
76      public void setLogWriter(PrintWriter printWriter) throws SQLException {
77          getDataSource().setLogWriter(printWriter);
78      }
79  
80      public <T> T unwrap(Class<T> clazz) throws SQLException {
81  
82          // Directly implement this method for JDK 5 compatibility. Logic is
83          // copied from org.springframework.jdbc.datasource.AbstractDataSource.
84  
85          if (!DataSource.class.equals(clazz)) {
86              throw new SQLException("Invalid class " + clazz);
87          }
88  
89          return (T)this;
90      }
91  
92      protected DataSource getDataSource() {
93          return ShardUtil.getDataSource();
94      }
95  
96      private static ShardDataSource _instance = new ShardDataSource();
97  
98  }