forked from sofastack/sofa-tracer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from alipay/master
sync code from upstream
- Loading branch information
Showing
82 changed files
with
5,797 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
211 changes: 211 additions & 0 deletions
211
...source-plugin/src/main/java/com/alipay/sofa/tracer/plugins/datasource/BaseDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++); | ||
} | ||
} | ||
} |
Oops, something went wrong.