summaryrefslogtreecommitdiff
path: root/java/openjdk6/files/icedtea/openjdk/7064279-resource_release.patch
blob: 22d0e2caecd42a9f852a0da7b290e7a19c5c3590 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# HG changeset patch
# User andrew
# Date 1365712268 -3600
# Node ID 4d66f7ebcf99c1b322f47ff0aa6adadcd995f8f4
# Parent  df591e0dfd349dc5986cc17949939c588d5a9690
7064279: Introspector.getBeanInfo() should release some resources in timely manner
Reviewed-by: art, alexp

diff --git a/src/share/classes/java/beans/Beans.java b/src/share/classes/java/beans/Beans.java
--- jdk/src/share/classes/java/beans/Beans.java
+++ jdk/src/share/classes/java/beans/Beans.java
@@ -32,7 +32,6 @@
 import java.applet.AppletStub;
 import java.applet.AudioClip;
 
-import java.awt.GraphicsEnvironment;
 import java.awt.Image;
 
 import java.beans.beancontext.BeanContext;
@@ -53,15 +52,11 @@
 import java.util.Iterator;
 import java.util.Vector;
 
-import sun.awt.AppContext;
-
 /**
  * This class provides some general purpose beans control methods.
  */
 
 public class Beans {
-    private static final Object DESIGN_TIME = new Object();
-    private static final Object GUI_AVAILABLE = new Object();
 
     /**
      * <p>
@@ -395,8 +390,7 @@
      * @see DesignMode
      */
     public static boolean isDesignTime() {
-        Object value = AppContext.getAppContext().get(DESIGN_TIME);
-        return (value instanceof Boolean) && (Boolean) value;
+        return ThreadGroupContext.getContext().isDesignTime();
     }
 
     /**
@@ -413,8 +407,7 @@
      *
      */
     public static boolean isGuiAvailable() {
-        Object value = AppContext.getAppContext().get(GUI_AVAILABLE);
-        return (value instanceof Boolean) ? (Boolean) value : !GraphicsEnvironment.isHeadless();
+        return ThreadGroupContext.getContext().isGuiAvailable();
     }
 
     /**
@@ -440,7 +433,7 @@
         if (sm != null) {
             sm.checkPropertiesAccess();
         }
-        AppContext.getAppContext().put(DESIGN_TIME, Boolean.valueOf(isDesignTime));
+        ThreadGroupContext.getContext().setDesignTime(isDesignTime);
     }
 
     /**
@@ -466,7 +459,7 @@
         if (sm != null) {
             sm.checkPropertiesAccess();
         }
-        AppContext.getAppContext().put(GUI_AVAILABLE, Boolean.valueOf(isGuiAvailable));
+        ThreadGroupContext.getContext().setGuiAvailable(isGuiAvailable);
     }
 }
 
diff --git a/src/share/classes/java/beans/Introspector.java b/src/share/classes/java/beans/Introspector.java
--- jdk/src/share/classes/java/beans/Introspector.java
+++ jdk/src/share/classes/java/beans/Introspector.java
@@ -38,7 +38,6 @@
 import java.util.EventListener;
 import java.util.List;
 import java.util.TreeMap;
-import sun.awt.AppContext;
 import sun.reflect.misc.ReflectUtil;
 
 /**
@@ -103,10 +102,7 @@
     public final static int IGNORE_ALL_BEANINFO        = 3;
 
     // Static Caches to speed up introspection.
-    private static WeakCache<Class<?>, Method[]> declaredMethodCache =
-            new WeakCache<Class<?>, Method[]>();
-
-    private static final Object BEANINFO_CACHE = new Object();
+    private static final WeakCache<Class<?>, Method[]> declaredMethodCache = new WeakCache<>();
 
     private Class beanClass;
     private BeanInfo explicitBeanInfo;
@@ -170,21 +166,15 @@
         if (!ReflectUtil.isPackageAccessible(beanClass)) {
             return (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
         }
-        WeakCache<Class<?>, BeanInfo> beanInfoCache;
+        ThreadGroupContext context = ThreadGroupContext.getContext();
         BeanInfo beanInfo;
-        synchronized (BEANINFO_CACHE) {
-            beanInfoCache = (WeakCache<Class<?>, BeanInfo>) AppContext.getAppContext().get(BEANINFO_CACHE);
-
-            if (beanInfoCache == null) {
-                beanInfoCache = new WeakCache<Class<?>, BeanInfo>();
-                AppContext.getAppContext().put(BEANINFO_CACHE, beanInfoCache);
-            }
-            beanInfo = beanInfoCache.get(beanClass);
+        synchronized (declaredMethodCache) {
+            beanInfo = context.getBeanInfo(beanClass);
 	}
 	if (beanInfo == null) {
 	    beanInfo = (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
-            synchronized (BEANINFO_CACHE) {
-                beanInfoCache.put(beanClass, beanInfo);
+            synchronized (declaredMethodCache) {
+                context.putBeanInfo(beanClass, beanInfo);
             }
         }
 	return beanInfo;
@@ -334,11 +324,8 @@
      */
 
     public static void flushCaches() {
-        synchronized (BEANINFO_CACHE) {
-            WeakCache beanInfoCache = (WeakCache) AppContext.getAppContext().get(BEANINFO_CACHE);
-            if (beanInfoCache != null) {
-                beanInfoCache.clear();
-            }
+        synchronized (declaredMethodCache) {
+            ThreadGroupContext.getContext().clearBeanInfoCache();
             declaredMethodCache.clear();
         }
     }
@@ -362,11 +349,8 @@
         if (clz == null) {
             throw new NullPointerException();
         }
-        synchronized (BEANINFO_CACHE) {
-            WeakCache beanInfoCache = (WeakCache) AppContext.getAppContext().get(BEANINFO_CACHE);
-            if (beanInfoCache != null) {
-                beanInfoCache.put(clz, null);
-            }
+        synchronized (declaredMethodCache) {
+            ThreadGroupContext.getContext().removeBeanInfo(clz);
             declaredMethodCache.put(clz, null);
         }
     }
@@ -1313,7 +1297,7 @@
         if (!ReflectUtil.isPackageAccessible(clz)) {
             return new Method[0];
         }
-        synchronized (BEANINFO_CACHE) {
+        synchronized (declaredMethodCache) {
             Method[] result = declaredMethodCache.get(clz);
             if (result == null) {
                 result = clz.getMethods();
diff --git a/src/share/classes/java/beans/ThreadGroupContext.java b/src/share/classes/java/beans/ThreadGroupContext.java
new file mode 100644
--- /dev/null
+++ jdk/src/share/classes/java/beans/ThreadGroupContext.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package java.beans;
+
+import com.sun.beans.finder.BeanInfoFinder;
+import com.sun.beans.finder.PropertyEditorFinder;
+
+import java.awt.GraphicsEnvironment;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+/**
+ * The {@code ThreadGroupContext} is an application-dependent
+ * context referenced by the specific {@link ThreadGroup}.
+ * This is a replacement for the {@link sun.awt.AppContext}.
+ *
+ * @author  Sergey Malenkov
+ */
+final class ThreadGroupContext {
+
+    private static final Map<ThreadGroup, ThreadGroupContext> contexts = new WeakHashMap<>();
+
+    /**
+     * Returns the appropriate {@code AppContext} for the caller,
+     * as determined by its {@code ThreadGroup}.
+     *
+     * @return  the application-dependent context
+     */
+    static ThreadGroupContext getContext() {
+        ThreadGroup group = Thread.currentThread().getThreadGroup();
+        synchronized (contexts) {
+            ThreadGroupContext context = contexts.get(group);
+            if (context == null) {
+                context = new ThreadGroupContext();
+                contexts.put(group, context);
+            }
+            return context;
+        }
+    }
+
+    private volatile boolean isDesignTime;
+    private volatile Boolean isGuiAvailable;
+
+    private Map<Class<?>, BeanInfo> beanInfoCache;
+    private BeanInfoFinder beanInfoFinder;
+    private PropertyEditorFinder propertyEditorFinder;
+
+
+    boolean isDesignTime() {
+        return this.isDesignTime;
+    }
+
+    void setDesignTime(boolean isDesignTime) {
+        this.isDesignTime = isDesignTime;
+    }
+
+
+    boolean isGuiAvailable() {
+        Boolean isGuiAvailable = this.isGuiAvailable;
+        return (isGuiAvailable != null)
+                ? isGuiAvailable.booleanValue()
+                : !GraphicsEnvironment.isHeadless();
+    }
+
+    void setGuiAvailable(boolean isGuiAvailable) {
+        this.isGuiAvailable = Boolean.valueOf(isGuiAvailable);
+    }
+
+
+    BeanInfo getBeanInfo(Class<?> type) {
+        return (this.beanInfoCache != null)
+                ? this.beanInfoCache.get(type)
+                : null;
+    }
+
+    BeanInfo putBeanInfo(Class<?> type, BeanInfo info) {
+        if (this.beanInfoCache == null) {
+            this.beanInfoCache = new WeakHashMap<>();
+        }
+        return this.beanInfoCache.put(type, info);
+    }
+
+    void removeBeanInfo(Class<?> type) {
+        if (this.beanInfoCache != null) {
+            this.beanInfoCache.remove(type);
+        }
+    }
+
+    void clearBeanInfoCache() {
+        if (this.beanInfoCache != null) {
+            this.beanInfoCache.clear();
+        }
+    }
+
+
+    synchronized BeanInfoFinder getBeanInfoFinder() {
+        if (this.beanInfoFinder == null) {
+            this.beanInfoFinder = new BeanInfoFinder();
+        }
+        return this.beanInfoFinder;
+    }
+
+    synchronized PropertyEditorFinder getPropertyEditorFinder() {
+        if (this.propertyEditorFinder == null) {
+            this.propertyEditorFinder = new PropertyEditorFinder();
+        }
+        return this.propertyEditorFinder;
+    }
+}
diff --git a/test/java/beans/Beans/6669869/TestDesignTime.java b/test/java/beans/Beans/6669869/TestDesignTime.java
--- jdk/test/java/beans/Beans/6669869/TestDesignTime.java
+++ jdk/test/java/beans/Beans/6669869/TestDesignTime.java
@@ -29,7 +29,6 @@
  */
 
 import java.beans.Beans;
-import sun.awt.SunToolkit;
 
 public class TestDesignTime implements Runnable {
     public static void main(String[] args) throws InterruptedException {
@@ -44,7 +43,6 @@
     }
 
     public void run() {
-        SunToolkit.createNewAppContext();
         if (Beans.isDesignTime()) {
             throw new Error("shared DesignTime property");
         }
diff --git a/test/java/beans/Beans/6669869/TestGuiAvailable.java b/test/java/beans/Beans/6669869/TestGuiAvailable.java
--- jdk/test/java/beans/Beans/6669869/TestGuiAvailable.java
+++ jdk/test/java/beans/Beans/6669869/TestGuiAvailable.java
@@ -30,7 +30,6 @@
 
 import java.awt.GraphicsEnvironment;
 import java.beans.Beans;
-import sun.awt.SunToolkit;
 
 public class TestGuiAvailable implements Runnable {
     public static void main(String[] args) throws InterruptedException {
@@ -45,7 +44,6 @@
     }
 
     public void run() {
-        SunToolkit.createNewAppContext();
         if (Beans.isGuiAvailable() == GraphicsEnvironment.isHeadless()) {
             throw new Error("shared GuiAvailable property");
         }
diff --git a/test/java/beans/Introspector/7064279/Test7064279.java b/test/java/beans/Introspector/7064279/Test7064279.java
new file mode 100644
--- /dev/null
+++ jdk/test/java/beans/Introspector/7064279/Test7064279.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 7064279
+ * @summary Tests that Introspector does not have strong references to context class loader
+ * @author Sergey Malenkov
+ */
+
+import java.beans.Introspector;
+import java.io.File;
+import java.lang.ref.WeakReference;
+import java.net.URL;
+import java.net.URLClassLoader;
+
+public class Test7064279 {
+
+    public static void main(String[] args) throws Exception {
+        WeakReference ref = new WeakReference(test("test.jar", "test.Test"));
+        try {
+            int[] array = new int[1024];
+            while (true) {
+                array = new int[array.length << 1];
+            }
+        }
+        catch (OutOfMemoryError error) {
+            System.gc();
+        }
+        if (null != ref.get()) {
+            throw new Error("ClassLoader is not released");
+        }
+    }
+
+    private static Object test(String jarName, String className) throws Exception {
+        StringBuilder sb = new StringBuilder(256);
+        sb.append("file:");
+        sb.append(System.getProperty("test.src", "."));
+        sb.append(File.separatorChar);
+        sb.append(jarName);
+
+        ClassLoader newLoader = new URLClassLoader(new URL[] { new URL(sb.toString()) });
+        ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
+
+        Thread.currentThread().setContextClassLoader(newLoader);
+        test(newLoader.loadClass(className));
+        Thread.currentThread().setContextClassLoader(oldLoader);
+
+        return newLoader;
+    }
+
+    private static void test(Class type) throws Exception {
+        Introspector.getBeanInfo(type);
+    }
+}
diff --git a/test/java/beans/Introspector/Test6660539.java b/test/java/beans/Introspector/Test6660539.java
--- jdk/test/java/beans/Introspector/Test6660539.java
+++ jdk/test/java/beans/Introspector/Test6660539.java
@@ -28,8 +28,6 @@
  * @author Sergey Malenkov
  */
 
-import sun.awt.SunToolkit;
-
 import java.beans.BeanInfo;
 import java.beans.IntrospectionException;
 import java.beans.Introspector;
@@ -49,7 +47,6 @@
     }
 
     public void run() {
-        SunToolkit.createNewAppContext();
         for (PropertyDescriptor pd : getPropertyDescriptors()) {
             if (pd.getDisplayName().equals(NAME))
                 throw new Error("shared BeanInfo cache");