Skip to content

Commit

Permalink
init project
Browse files Browse the repository at this point in the history
  • Loading branch information
dragontree101 committed Mar 29, 2016
1 parent aec1531 commit 761de45
Show file tree
Hide file tree
Showing 17 changed files with 713 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.idea
*.iml
*.ipr
*.iws
*/*.iml
*/logs
~
~/*
infer-out
out
.DS_Store
*.classpath
*.project
*/*.classpath
*/*.project
*/*/*.project
*/*/*.classpath
*/*/*.iml
*/.settings
*/*/.settings
logs
csv-log/target
csv-log/src/test
csv-log/dependency-reduced-pom.xml
dependency-reduced-pom.xml
.idea/*
target
src/test
*.csv
/csv-log-save/shell/Untitled 3.py
/csv-log-save/shell/ceate_offline_table.sh
version.yml
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# byte-buddy-agent-example
简单的使用byte-buddy来做成javaagent, 来修改springboot启动的任务
里面修改了httpclient和redis的调用方法
98 changes: 98 additions & 0 deletions agent/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.dragon.study.bytebuddy</groupId>
<artifactId>agent-example</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>agent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<index>true</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Premain-Class>com.dragon.study.bytebuddy.MyAgent</Premain-Class>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source> <!-- 源代码使用的开发版本 -->
<target>1.8</target> <!-- 需要生成的目标class文件的编译版本 -->
<meminitial>128m</meminitial>
<maxmem>512m</maxmem>
<fork>true</fork> <!-- fork is enable,用于明确表示编译版本配置的可用 -->
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.3.5</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.3.0.RELEASE</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.2</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.dragon.study.bytebuddy</groupId>
<artifactId>app</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

</dependencies>

</project>
37 changes: 37 additions & 0 deletions agent/src/main/java/com/dragon/study/bytebuddy/MyAgent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.dragon.study.bytebuddy;

import com.dragon.study.bytebuddy.httpclient.HttpClientTransformer;
import com.dragon.study.bytebuddy.redis.RedisTransformer;

import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.pool.TypePool;

import java.lang.instrument.Instrumentation;

import static net.bytebuddy.matcher.ElementMatchers.named;

/**
* Created by dragon on 16/3/29.
*/
public class MyAgent {

public static void premain(String arg, Instrumentation instrumentation) {

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ClassFileLocator.Compound compound = new ClassFileLocator.Compound(ClassFileLocator.ForClassLoader.of(classLoader), ClassFileLocator.ForClassLoader.ofClassPath());

String httpInterceptor = "com.dragon.study.bytebuddy.httpclient.HttpClientInterceptor";
String redisInterceptor = "com.dragon.study.bytebuddy.redis.RedisInterceptor";

new AgentBuilder.Default()
.type(named("org.apache.http.impl.client.CloseableHttpClient"))
.transform(new HttpClientTransformer(TypePool.Default.of(compound).describe(httpInterceptor).resolve()))
.type(named("redis.clients.jedis.Client"))
.transform(new RedisTransformer(TypePool.Default.of(compound).describe(redisInterceptor).resolve()))
.installOn(instrumentation);


}
}
67 changes: 67 additions & 0 deletions agent/src/main/java/com/dragon/study/bytebuddy/Trace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.dragon.study.bytebuddy;

/**
* Created by dragon on 16/3/28.
*/
public class Trace {
private String url;
private int statusCode;
private long cost;
private Exception e;

public Trace() {
}

public Trace(String url, int statusCode, long cost, Exception e) {
this.url = url;
this.statusCode = statusCode;
this.cost = cost;
this.e = e;
}

public Trace(String url, int statusCode, long cost) {
this(url, statusCode, cost, null);
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public int getStatusCode() {
return statusCode;
}

public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}

public long getCost() {
return cost;
}

public void setCost(long cost) {
this.cost = cost;
}

public Exception getE() {
return e;
}

public void setE(Exception e) {
this.e = e;
}

@Override
public String toString() {
return "Trace{" +
"url='" + url + '\'' +
", statusCode=" + statusCode +
", cost=" + cost +
", e=" + e +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.dragon.study.bytebuddy.context;


import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by Reilost on 14-3-4.
*/
public class ApplicationContextHolder implements ApplicationContextAware {
private static ApplicationContext _context;

private static Map<Class, Object> mockBeans;

public static ApplicationContext getApplicatioinContext() {
return _context;
}

@Override
public void setApplicationContext(ApplicationContext context) {
_context = context;
}

/**
* 将该对象中的带有Autowired annotation的属性自动注入
*/
public static void autowireBean(Object obj) {
if (_context != null) {
AutowireCapableBeanFactory factory = _context.getAutowireCapableBeanFactory();
factory.autowireBean(obj);
}
}

@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
return (T) _context.getBean(name);
}

@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
T bean = null;
if (mockBeans != null) {
bean = (T) mockBeans.get(clazz);
}
if (bean != null) {
return bean;
}


String[] names = _context.getBeanNamesForType(clazz);
if (names == null || names.length == 0) {
return null;
}
return (T) _context.getBean(names[0]);
}

public static <T> List<T> getBeans(Class<T> clazz) {
List<T> ret = new ArrayList<T>();
if (_context == null) {
return ret;
}
String[] names = _context.getBeanNamesForType(clazz);
if (names == null || names.length == 0) {
return ret;
}
for (String name : names) {
ret.add((T) _context.getBean(name));
}
return ret;
}

public static void setMockBean(Class clazz, Object object) {
if (mockBeans == null) {
mockBeans = new HashMap<Class, Object>();
}
mockBeans.put(clazz, object);
}


}
Loading

0 comments on commit 761de45

Please sign in to comment.