You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2019/05/23 15:54:23 UTC

[sling-whiteboard] 02/08: POC for a Java Agent to set URL connection timeout defaults

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 964966de9ddb1bcf755d37e185cf75952728ae56
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Thu May 23 16:32:19 2019 +0200

    POC for a Java Agent to set URL connection timeout defaults
    
    Start intercepting URL calls
---
 .../main/java/org/apache/sling/uca/impl/Agent.java | 28 ++++++++++++----------
 .../main/java/org/apache/sling/uca/impl/Main.java  | 13 ++++------
 2 files changed, 21 insertions(+), 20 deletions(-)

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
index 909bb9e..1391182 100644
--- 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
@@ -4,9 +4,10 @@ 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 java.util.HashSet;
+import java.util.Set;
 
 import javassist.CannotCompileException;
 import javassist.ClassPool;
@@ -19,12 +20,7 @@ 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);
-        }
+        inst.addTransformer(new URLTimeoutTransformer(), true);
         System.out.println("Loaded agent!");
     }
     
@@ -32,19 +28,27 @@ public class Agent {
         premain(args, inst);
     }
 
-    static class HashMapTransformer implements ClassFileTransformer {
-
+    static class URLTimeoutTransformer implements ClassFileTransformer {
+        
+        private static final Set<String> CLASSES_TO_TRANSFORM = new HashSet<>();
+        
+        static {
+            CLASSES_TO_TRANSFORM.add("sun.net.www.protocol.http.HttpURLConnection".replace('.', '/'));
+            CLASSES_TO_TRANSFORM.add("sun.net.www.protocol.https.HttpsURLConnectionImpl".replace('.', '/'));
+        }
+        
         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) {
+                if ( CLASSES_TO_TRANSFORM.contains(className)) {
                     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 );");
+                    CtMethod connectMethod = cc.getDeclaredMethod("connect");
+                    connectMethod.insertBefore("if ( getConnectTimeout() == 0 ) { setConnectTimeout(60); }");
+                    connectMethod.insertBefore("if ( getReadTimeout() == 0 ) { setReadTimeout(60); }");
                     classfileBuffer = cc.toBytecode();
                     cc.detach();
                     System.err.println("Transformation complete!");
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
index 9cf5802..21bcbb7 100644
--- 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
@@ -1,17 +1,14 @@
 package org.apache.sling.uca.impl;
 
-import java.util.HashMap;
-import java.util.Map;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
 
 public class Main {
     
-    public static void main(String[] args) {
+    public static void main(String[] args) throws MalformedURLException, IOException {
         
-        Map<String, String> map = new HashMap<>();
-        map.put("foo", "bar");
-        map.put("foo", "baz");
-        
-        System.out.println(map.get("foo"));
+        new URL("http://sling.apache.org").openConnection();
     }
 
 }