
com.github.javaclub.cdl.client.common.DataSourceWrapper Maven / Gradle / Ivy
The newest version!
package com.github.javaclub.cdl.client.common;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import javax.sql.DataSource;
import com.github.javaclub.cdl.client.atom.SAtomDataSource;
import com.github.javaclub.cdl.client.group.ReadWeight;
public class DataSourceWrapper implements DataSource {
private String dataSourceKey;
private String weightStr;
private ReadWeight readWeight;
private SAtomDataSource wrappedDataSource;
private int dataSourceIndex;
public void close(){
if(wrappedDataSource != null){
wrappedDataSource.close();
}
}
public boolean isMatchDataSourceIndex(int specifiedIndex) {
/*if (weight.indexes != null && !weight.indexes.isEmpty()) {
return weight.indexes.contains(specifiedIndex);
} else {
return this.dataSourceIndex == specifiedIndex;
}*/
return this.dataSourceIndex == specifiedIndex;
}
/**
*/
public boolean hasReadWeight() {
if(readWeight != null){
if(readWeight.r != 0){
return true;
}
}
return weightStr.contains("r") || weightStr.contains("R");
}
/**
*/
public boolean hasWriteWeight() {
// return weight.w != 0;
return weightStr.contains("w") || weightStr.contains("W");
}
public boolean isNA(){
return weightStr.contains("NA") || weightStr.contains("na");
}
public String toString() {
return new StringBuilder("DataSourceWrapper{dataSourceKey=").append(dataSourceKey).append(", dataSourceIndex=")
.append(dataSourceIndex).append(",weightStr=").append(weightStr).append("}").toString();
}
public String getDataSourceKey() {
return dataSourceKey;
}
public String getWeightStr() {
return weightStr;
}
public DataSource getWrappedDataSource() {
return wrappedDataSource;
}
/*---------------------- jdbc ------------------------------*/
public Connection getConnection() throws SQLException {
return wrappedDataSource.getConnection();
}
public Connection getConnection(String username, String password) throws SQLException {
return wrappedDataSource.getConnection(username, password);
}
public PrintWriter getLogWriter() throws SQLException {
return wrappedDataSource.getLogWriter();
}
public int getLoginTimeout() throws SQLException {
return wrappedDataSource.getLoginTimeout();
}
public void setLogWriter(PrintWriter out) throws SQLException {
wrappedDataSource.setLogWriter(out);
}
public void setLoginTimeout(int seconds) throws SQLException {
wrappedDataSource.setLoginTimeout(seconds);
}
public boolean isWrapperFor(Class> iface) throws SQLException {
return this.getClass().isAssignableFrom(iface);
}
@SuppressWarnings("unchecked")
public T unwrap(Class iface) throws SQLException {
try {
return (T) this;
} catch (Exception e) {
throw new SQLException(e);
}
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return wrappedDataSource.getParentLogger();
}
/*---------------------- jdbc ------------------------------*/
public int getDataSourceIndex() {
return dataSourceIndex;
}
public void setDataSourceIndex(int dataSourceIndex) {
this.dataSourceIndex = dataSourceIndex;
}
public void setDataSourceKey(String dataSourceKey) {
this.dataSourceKey = dataSourceKey;
}
public void setWeightStr(String weightStr) {
this.readWeight = new ReadWeight(weightStr);
this.weightStr = weightStr;
}
public ReadWeight getReadWeight() {
return readWeight;
}
public void setReadWeight(ReadWeight readWeight) {
this.readWeight = readWeight;
}
public void setWrappedDataSource(SAtomDataSource wrappedDataSource) {
this.wrappedDataSource = wrappedDataSource;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + dataSourceIndex;
result = prime * result + ((dataSourceKey == null) ? 0 : dataSourceKey.hashCode());
result = prime * result + ((readWeight == null) ? 0 : readWeight.hashCode());
result = prime * result + ((weightStr == null) ? 0 : weightStr.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DataSourceWrapper other = (DataSourceWrapper) obj;
if (dataSourceIndex != other.dataSourceIndex)
return false;
if (dataSourceKey == null) {
if (other.dataSourceKey != null)
return false;
} else if (!dataSourceKey.equals(other.dataSourceKey))
return false;
if (readWeight == null) {
if (other.readWeight != null)
return false;
} else if (!readWeight.equals(other.readWeight))
return false;
if (weightStr == null) {
if (other.weightStr != null)
return false;
} else if (!weightStr.equals(other.weightStr))
return false;
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy