You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2016/09/06 12:02:33 UTC

karaf git commit: [KARAF-4687] Prevent classloader leaks with static Throwables

Repository: karaf
Updated Branches:
  refs/heads/karaf-4.0.x 6e004c1f4 -> aa9a16445


[KARAF-4687] Prevent classloader leaks with static Throwables


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/aa9a1644
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/aa9a1644
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/aa9a1644

Branch: refs/heads/karaf-4.0.x
Commit: aa9a16445d28215cd906f85e02179df780691ccf
Parents: 6e004c1
Author: Grzegorz Grzybek <gg...@redhat.com>
Authored: Wed Aug 31 18:15:08 2016 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Tue Sep 6 14:02:14 2016 +0200

----------------------------------------------------------------------
 .../src/main/java/java/lang/Exception.java      | 33 +++++++++++++++++---
 1 file changed, 29 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/aa9a1644/exception/src/main/java/java/lang/Exception.java
----------------------------------------------------------------------
diff --git a/exception/src/main/java/java/lang/Exception.java b/exception/src/main/java/java/lang/Exception.java
index 76eee71..d07c280 100644
--- a/exception/src/main/java/java/lang/Exception.java
+++ b/exception/src/main/java/java/lang/Exception.java
@@ -18,6 +18,8 @@
 package java.lang;
 
 import java.lang.reflect.Field;
+import java.lang.ref.Reference;
+import java.lang.ref.WeakReference;
 
 import javax.xml.bind.annotation.XmlTransient;
 
@@ -34,13 +36,14 @@ import javax.xml.bind.annotation.XmlTransient;
 public class Exception extends Throwable {
     private static final long serialVersionUID = -3387516993124229948L;
 
-    private transient Class[] classContext = SecurityManagerEx.getInstance().getThrowableContext(this);
+    private transient Reference<Class<?>>[] classContext;
 
     /**
      * Constructs a new {@code Exception} that includes the current stack trace.
      */
     public Exception() {
         super();
+        initClassContext();
     }
 
     /**
@@ -52,6 +55,7 @@ public class Exception extends Throwable {
      */
     public Exception(String detailMessage) {
         super(detailMessage);
+        initClassContext();
     }
 
     /**
@@ -65,6 +69,7 @@ public class Exception extends Throwable {
      */
     public Exception(String detailMessage, Throwable throwable) {
         super(detailMessage, throwable);
+        initClassContext();
     }
 
     /**
@@ -76,6 +81,7 @@ public class Exception extends Throwable {
      */
     public Exception(Throwable throwable) {
         super(throwable);
+        initClassContext();
     }
 
     /**
@@ -115,17 +121,36 @@ public class Exception extends Throwable {
         } catch (Exception e) {
             e.printStackTrace();
         }
-
+        initClassContext();
     }
  
     @XmlTransient
     @Deprecated
     public Class[] getClassContext() {
-        return classContext;
+        Class<?>[] context = new Class<?>[classContext.length];
+        for (int i = 0; i < classContext.length; i++) {
+            Class<?> c = classContext[i].get();
+            context[i] = c == null ? Object.class : c;
+        }
+        return context;
+    }
+
+    @SuppressWarnings("unchecked")
+    private void initClassContext() {
+        Class[] context = SecurityManagerEx.getInstance().getThrowableContext(this);
+        classContext = new Reference[context.length];
+        for (int i = 0; i < context.length; i++) {
+            classContext[i] = new WeakReference<Class<?>>(context[i]);
+        }
     }
     
     protected Class[] classContext() {
-      return classContext;
+        Class<?>[] context = new Class<?>[classContext.length];
+        for (int i = 0; i < classContext.length; i++) {
+            Class<?> c = classContext[i].get();
+            context[i] = c == null ? Object.class : c;
+        }
+        return context;
     }
 
     private static class SecurityManagerEx extends SecurityManager