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/06/27 13:45:54 UTC

[sling-org-apache-sling-connection-timeout-agent] 03/05: Adjust class location mechanism for OSGi environments

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch feature/osgi-fixes
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-connection-timeout-agent.git

commit af55e244477b12de061992f531b90ea705864728
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Thu Jun 27 15:36:11 2019 +0200

    Adjust class location mechanism for OSGi environments
---
 .../apache/sling/cta/impl/MBeanAwareTimeoutTransformer.java | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/sling/cta/impl/MBeanAwareTimeoutTransformer.java b/src/main/java/org/apache/sling/cta/impl/MBeanAwareTimeoutTransformer.java
index 490507e..2bd1899 100644
--- a/src/main/java/org/apache/sling/cta/impl/MBeanAwareTimeoutTransformer.java
+++ b/src/main/java/org/apache/sling/cta/impl/MBeanAwareTimeoutTransformer.java
@@ -20,8 +20,10 @@ import java.lang.instrument.ClassFileTransformer;
 import java.security.ProtectionDomain;
 import java.util.Set;
 
+import javassist.ByteArrayClassPath;
 import javassist.ClassPool;
 import javassist.CtClass;
+import javassist.LoaderClassPath;
 import javassist.bytecode.Descriptor;
 
 /**
@@ -43,11 +45,18 @@ public abstract class MBeanAwareTimeoutTransformer implements ClassFileTransform
 
     @Override
     public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
+        
         try {
             if (classesToTransform.contains(className)) {
                 Log.get().log("%s asked to transform %s", getClass().getSimpleName(), className);
-                ClassPool defaultPool = ClassPool.getDefault();
-                CtClass cc = defaultPool.get(Descriptor.toJavaName(className));
+                ClassPool classPool = new ClassPool(true);
+                // in OSGi environments access is automatically permitted to all classes, even for a Java agent
+                // therefore we need to adjust the default class path
+                // 1. append all classes accessible to the specified class loader
+                classPool.appendClassPath(new LoaderClassPath(loader));
+                // 2. insert the current definition of the class
+                classPool.insertClassPath(new ByteArrayClassPath(Descriptor.toJavaName(className), classfileBuffer));
+                CtClass cc = classPool.get(Descriptor.toJavaName(className));
                 if ( cc == null ) {
                     Log.get().log("Could not find a class for %s in the default class pool, skipping transformation", className);
                 } else {