|
| 1 | +package org.tarantool.jdbc.ds; |
| 2 | + |
| 3 | +import org.tarantool.jdbc.SQLConnection; |
| 4 | +import org.tarantool.jdbc.SQLConstant; |
| 5 | +import org.tarantool.jdbc.SQLProperty; |
| 6 | + |
| 7 | +import java.io.PrintWriter; |
| 8 | +import java.sql.Connection; |
| 9 | +import java.sql.SQLException; |
| 10 | +import java.sql.SQLFeatureNotSupportedException; |
| 11 | +import java.sql.SQLNonTransientException; |
| 12 | +import java.util.Properties; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | +import java.util.logging.Logger; |
| 15 | +import javax.sql.DataSource; |
| 16 | + |
| 17 | +/** |
| 18 | + * Simple {@code java.sql.DataSource} implementation. |
| 19 | + */ |
| 20 | +public class SQLDataSource implements TarantoolDataSource, DataSource { |
| 21 | + |
| 22 | + private PrintWriter logWriter; |
| 23 | + private String name = "Tarantool basic data source"; |
| 24 | + |
| 25 | + private Properties properties = new Properties(); |
| 26 | + |
| 27 | + @Override |
| 28 | + public Connection getConnection() throws SQLException { |
| 29 | + return new SQLConnection(makeUrl(), new Properties(properties)); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public Connection getConnection(String username, String password) throws SQLException { |
| 34 | + Properties copyProperties = new Properties(properties); |
| 35 | + SQLProperty.USER.setString(copyProperties, username); |
| 36 | + SQLProperty.PASSWORD.setString(copyProperties, password); |
| 37 | + return new SQLConnection(makeUrl(), copyProperties); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public PrintWriter getLogWriter() { |
| 42 | + return logWriter; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void setLogWriter(PrintWriter out) { |
| 47 | + logWriter = out; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void setLoginTimeout(int seconds) { |
| 52 | + SQLProperty.INIT_TIMEOUT.setInt(properties, (int) TimeUnit.SECONDS.toMillis(seconds)); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public int getLoginTimeout() throws SQLException { |
| 57 | + return (int) TimeUnit.MILLISECONDS.toSeconds(SQLProperty.INIT_TIMEOUT.getInt(properties)); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Logger getParentLogger() throws SQLFeatureNotSupportedException { |
| 62 | + throw new SQLFeatureNotSupportedException(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public <T> T unwrap(Class<T> type) throws SQLException { |
| 67 | + if (isWrapperFor(type)) { |
| 68 | + return type.cast(this); |
| 69 | + } |
| 70 | + throw new SQLNonTransientException("Statement does not wrap " + type.getName()); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean isWrapperFor(Class<?> type) { |
| 75 | + return type.isAssignableFrom(this.getClass()); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public String getServerName() { |
| 80 | + return SQLProperty.HOST.getString(properties); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void setServerName(String serverName) { |
| 85 | + SQLProperty.HOST.setString(properties, serverName); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public int getPortNumber() throws SQLException { |
| 90 | + return SQLProperty.PORT.getInt(properties); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void setPortNumber(int port) { |
| 95 | + SQLProperty.PORT.setInt(properties, port); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public String getUser() { |
| 100 | + return SQLProperty.USER.getString(properties); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void setUser(String userName) { |
| 105 | + SQLProperty.USER.setString(properties, userName); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public String getPassword() { |
| 110 | + return SQLProperty.PASSWORD.getString(properties); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void setPassword(String password) { |
| 115 | + SQLProperty.PASSWORD.setString(properties, password); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String getDescription() { |
| 120 | + return "Basic DataSource implementation - produces a standard Connection object. " + |
| 121 | + SQLConstant.DRIVER_NAME + "."; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public String getDataSourceName() { |
| 126 | + return name; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void setDataSourceName(String name) { |
| 131 | + this.name = name; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public String getSocketChannelProvider() { |
| 136 | + return SQLProperty.SOCKET_CHANNEL_PROVIDER.getString(properties); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void setSocketChannelProvider(String classFqdn) { |
| 141 | + SQLProperty.SOCKET_CHANNEL_PROVIDER.setString(properties, classFqdn); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public int getQueryTimeout() throws SQLException { |
| 146 | + return (int) TimeUnit.MILLISECONDS.toSeconds(SQLProperty.QUERY_TIMEOUT.getInt(properties)); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void setQueryTimeout(int seconds) { |
| 151 | + SQLProperty.QUERY_TIMEOUT.setInt(properties, (int) TimeUnit.SECONDS.toMillis(seconds)); |
| 152 | + } |
| 153 | + |
| 154 | + private String makeUrl() { |
| 155 | + return "jdbc:tarantool://" + |
| 156 | + SQLProperty.HOST.getString(properties) + ":" + SQLProperty.PORT.getString(properties); |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments