Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datasource support #58

Merged
merged 24 commits into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
<module>tracer-extensions</module>
<module>sofa-tracer-plugins/sofa-tracer-springmvc-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-httpclient-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-datasource-plugin</module>
<module>tracer-all</module>
<module>tracer-sofa-boot-starter</module>
<module>tracer-samples</module>
<module>tracer-test/core-test</module>
<module>tracer-test/logback-test</module>
<module>tracer-test/log4j2-test</module>
<module>tracer-test/log4j-test</module>
<module>tracer-sofa-boot-plugin</module>
</modules>

<properties>
Expand Down Expand Up @@ -95,6 +95,11 @@
<artifactId>sofa-tracer-httpclient-plugin</artifactId>
<version>${sofa.tracer.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-tracer-datasource-plugin</artifactId>
<version>${sofa.tracer.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>tracer-all</artifactId>
Expand Down
82 changes: 82 additions & 0 deletions sofa-tracer-plugins/sofa-tracer-datasource-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>tracer-all-parent</artifactId>
<groupId>com.alipay.sofa</groupId>
<version>2.2.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sofa-tracer-datasource-plugin</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>tracer-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.tracer.plugins.datasource;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;

/**
* @author qilong.zql
* @author shusong.yss
* @since 2.2.0
*/
public abstract class BaseDataSource implements DataSource {

protected DataSource delegate;

protected List<Interceptor> interceptors;

protected List<Interceptor> dataSourceInterceptors;

protected final Prop prop;

protected AtomicBoolean initialized = new AtomicBoolean();

public BaseDataSource(DataSource delegate) {
this.delegate = delegate;
this.prop = new Prop();
}

public DataSource getDelegate() {
return delegate;
}

public void setDelegate(DataSource delegate) {
this.delegate = delegate;
}

public List<Interceptor> getInterceptors() {
return interceptors;
}

public void setInterceptors(List<Interceptor> interceptors) {
this.interceptors = interceptors;
}

public List<Interceptor> getDataSourceInterceptors() {
return dataSourceInterceptors;
}

public void setDataSourceInterceptors(List<Interceptor> dataSourceInterceptors) {
this.dataSourceInterceptors = dataSourceInterceptors;
}

public Prop getProp() {
return prop;
}

/**
* init method must be invoked first
*/
public abstract void init();

private void checkInit() {
if (!initialized.get()) {
throw new IllegalStateException("DataSource has not been initialized");
}
}

@Override
public Connection getConnection() throws SQLException {
checkInit();
try {
DataSourceInterceptorChain dataSourceInterceptorChain = new DataSourceInterceptorChain(
new Invocation(prop.getTargetMethod(MethodRegistry.METHOD_DS_GET_CONNECTION),
delegate));
Connection conn = (Connection) dataSourceInterceptorChain.proceed();
if (conn != null) {
return new ExtendedConnection(this, conn, prop);
}
} catch (Exception e) {
throw new SQLException(e);
}
throw new SQLException("getConnection failed");
}

@Override
public Connection getConnection(String username, String password) throws SQLException {
throw new UnsupportedOperationException(
"please setup user and password in delegate dataSource");
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return delegate.isWrapperFor(iface);
}

@Override
public PrintWriter getLogWriter() throws SQLException {
return delegate.getLogWriter();
}

@Override
public void setLogWriter(PrintWriter out) throws SQLException {
delegate.setLogWriter(out);
}

@Override
public void setLoginTimeout(int seconds) throws SQLException {
delegate.setLoginTimeout(seconds);
}

@Override
public int getLoginTimeout() throws SQLException {
return delegate.getLoginTimeout();
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return delegate.getParentLogger();
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return delegate.unwrap(iface);
}

/**
* DataSource Interceptor Chain, {@link Interceptor.Chain}
*/
class DataSourceInterceptorChain implements Interceptor.Chain {

private int index;

private Invocation invocation;

DataSourceInterceptorChain(Invocation invocation) {
this.index = 0;
this.invocation = invocation;
}

@Override
public Object proceed() throws Exception {
if (hasNext()) {
return next().intercept(this);
} else {
return invocation.invoke();
}
}

@Override
public String getOriginalSql() {
throw new UnsupportedOperationException("this operation not unsupported");
}

@Override
public String getProcessingSql() {
throw new UnsupportedOperationException("this operation unsupported");

}

@Override
public void setProcessingSql(String processingSql) {
throw new UnsupportedOperationException("this operation unsupported");
}

@Override
public BaseDataSource getDataSource() {
return BaseDataSource.this;
}

@Override
public ExtendedConnection getConnection() {
throw new UnsupportedOperationException("this operation unsupported");
}

@Override
public ExtendedStatement getStatement() {
throw new UnsupportedOperationException("this operation unsupported");
}

@Override
public boolean hasNext() {
return dataSourceInterceptors != null && index < dataSourceInterceptors.size();
}

@Override
public Interceptor next() {
return dataSourceInterceptors.get(index++);
}
}
}
Loading