This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch feature/url-connection-agent in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
commit e0698ee153f560de36ebba1a47f50e8ca6ef8b5f Author: Robert Munteanu <[email protected]> AuthorDate: Tue May 21 17:57:28 2019 +0200 POC for a Java Agent to set URL connection timeout defaults Initial work towards intercepting HashMap calls, to prove that it can work for core JVM classes. --- url-connection-agent/pom.xml | 51 +++++++++++++++++++ .../main/java/org/apache/sling/uca/impl/Agent.java | 59 ++++++++++++++++++++++ .../main/java/org/apache/sling/uca/impl/Main.java | 17 +++++++ 3 files changed, 127 insertions(+) diff --git a/url-connection-agent/pom.xml b/url-connection-agent/pom.xml new file mode 100644 index 0000000..cdaf3e1 --- /dev/null +++ b/url-connection-agent/pom.xml @@ -0,0 +1,51 @@ +<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>org.apache.sling</groupId> + <artifactId>sling</artifactId> + <version>35</version> + </parent> + <artifactId>url-connection-agent</artifactId> + <version>0.0.1-SNAPSHOT</version> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-assembly-plugin</artifactId> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>single</goal> + </goals> + <configuration> + <archiveBaseDirectory>${project.basedir}</archiveBaseDirectory> + <archive> + <manifestEntries> + <Agent-Class>org.apache.sling.uca.impl.Agent</Agent-Class> + <Premain-Class>org.apache.sling.uca.impl.Agent</Premain-Class> + <Can-Redefine-Classes>true</Can-Redefine-Classes> + <Can-Retransform-Classes>true</Can-Retransform-Classes> + </manifestEntries> + </archive> + <descriptorRefs> + <descriptorRef>jar-with-dependencies</descriptorRef> + </descriptorRefs> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>org.javassist</groupId> + <artifactId>javassist</artifactId> + <version>3.24.0-GA</version> + </dependency> + </dependencies> +</project> \ No newline at end of file diff --git a/url-connection-agent/src/main/java/org/apache/sling/uca/impl/Agent.java b/url-connection-agent/src/main/java/org/apache/sling/uca/impl/Agent.java new file mode 100644 index 0000000..909bb9e --- /dev/null +++ b/url-connection-agent/src/main/java/org/apache/sling/uca/impl/Agent.java @@ -0,0 +1,59 @@ +package org.apache.sling.uca.impl; + +import java.io.IOException; +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.IllegalClassFormatException; +import java.lang.instrument.Instrumentation; +import java.lang.instrument.UnmodifiableClassException; +import java.security.ProtectionDomain; +import java.util.HashMap; + +import javassist.CannotCompileException; +import javassist.ClassPool; +import javassist.CtClass; +import javassist.CtMethod; +import javassist.NotFoundException; + +public class Agent { + + public static void premain(String args, Instrumentation inst) { + + System.out.println("Loading agent..."); + inst.addTransformer(new HashMapTransformer(), true); + try { + inst.retransformClasses(HashMap.class); + } catch (UnmodifiableClassException e) { + throw new RuntimeException(e); + } + System.out.println("Loaded agent!"); + } + + public static void agentmain(String args, Instrumentation inst) { + premain(args, inst); + } + + static class HashMapTransformer implements ClassFileTransformer { + + private final Class<?> klazz = HashMap.class; + + @Override + public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, + ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { + try { + if ( classBeingRedefined == klazz) { + System.out.println("Asked to transform " + className); + CtClass cc = ClassPool.getDefault().get(klazz.getName()); + CtMethod putMethod = cc.getDeclaredMethod("put"); + putMethod.insertAfter("System.out.println(\"[AGENT] Adding key \" + key );"); + classfileBuffer = cc.toBytecode(); + cc.detach(); + System.err.println("Transformation complete!"); + } + return classfileBuffer; + } catch (NotFoundException | CannotCompileException | IOException e) { + throw new RuntimeException("Transformation failed", e); + } + } + } +} + diff --git a/url-connection-agent/src/main/java/org/apache/sling/uca/impl/Main.java b/url-connection-agent/src/main/java/org/apache/sling/uca/impl/Main.java new file mode 100644 index 0000000..9cf5802 --- /dev/null +++ b/url-connection-agent/src/main/java/org/apache/sling/uca/impl/Main.java @@ -0,0 +1,17 @@ +package org.apache.sling.uca.impl; + +import java.util.HashMap; +import java.util.Map; + +public class Main { + + public static void main(String[] args) { + + Map<String, String> map = new HashMap<>(); + map.put("foo", "bar"); + map.put("foo", "baz"); + + System.out.println(map.get("foo")); + } + +}
