Skip to content

Commit

Permalink
Merge pull request #4 from alipay/master
Browse files Browse the repository at this point in the history
sync code from upstream
  • Loading branch information
glmapper committed Oct 10, 2018
2 parents 72f8df3 + 11b84df commit 9aa096b
Show file tree
Hide file tree
Showing 82 changed files with 5,797 additions and 42 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ SOFATracer 的编译环境的要求为 JDK7 或者 JDK8,需要采用 [Apache M
* [SOFATracer 示例工程(基于 Spring MVC 示例远程上报 Zipkin)](./tracer-samples/tracer-sample-with-zipkin)
* [SOFATracer 示例工程(基于 SOFA RPC 示例落地日志)](./tracer-samples/tracer-sample-with-sofarpc)
* [SOFATracer 示例工程(基于日志编程接口 SLF4J 示例打印 traceId)](./tracer-samples/tracer-sample-with-slf4j)
* [SOFATracer 示例工程(基于 DataSource 示例落地日志)](./tracer-samples/tracer-sample-with-h2)
* [SOFATracer 示例工程(基于 HttpClient 示例落地日志)](./tracer-samples/tracer-sample-with-httpclient)

## 六、文档

Expand Down
15 changes: 9 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>2.4.6</version>
<version>2.4.9</version>
</parent>

<artifactId>tracer-all-parent</artifactId>
<version>2.2.0-SNAPSHOT</version>
<version>2.2.0</version>
<packaging>pom</packaging>

<name>tracer-all-parent</name>
Expand All @@ -20,19 +20,19 @@
<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>
<opentracing.version>0.22.0</opentracing.version>
<sofa.tracer.version>2.2.0-SNAPSHOT</sofa.tracer.version>
<sofa.tracer.version>2.2.0</sofa.tracer.version>
<java.compiler.source.version>1.6</java.compiler.source.version>
<java.compiler.target.version>1.6</java.compiler.target.version>
<jmh.version>1.9.3</jmh.version>
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 Expand Up @@ -139,13 +144,11 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.3</version>
<scope>provided</scope>
</dependency>
<!-- test 依赖 -->
<dependency>
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</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

0 comments on commit 9aa096b

Please sign in to comment.