OpenJDK / amber / amber
changeset 6862:f66eb6b6a6b9
6938813: Swing mutable statics
Reviewed-by: peterz, alexp
author | rupashka |
---|---|
date | Mon, 21 Jun 2010 16:47:05 +0400 |
parents | 0c879c7c2ea2 |
children | e169f270518e |
files | jdk/src/share/classes/javax/swing/text/html/HTMLEditorKit.java jdk/src/share/classes/javax/swing/text/html/parser/DTD.java jdk/src/share/classes/javax/swing/text/html/parser/ParserDelegator.java jdk/test/javax/swing/Security/6938813/bug6938813.java |
diffstat | 4 files changed, 175 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/jdk/src/share/classes/javax/swing/text/html/HTMLEditorKit.java Fri Jun 18 13:18:42 2010 +0400 +++ b/jdk/src/share/classes/javax/swing/text/html/HTMLEditorKit.java Mon Jun 21 16:47:05 2010 +0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2010, 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 @@ -24,6 +24,8 @@ */ package javax.swing.text.html; +import sun.awt.AppContext; + import java.lang.reflect.Method; import java.awt.*; import java.awt.event.*; @@ -369,7 +371,11 @@ * if desired. */ public void setStyleSheet(StyleSheet s) { - defaultStyles = s; + if (s == null) { + AppContext.getAppContext().remove(DEFAULT_STYLES_KEY); + } else { + AppContext.getAppContext().put(DEFAULT_STYLES_KEY, s); + } } /** @@ -379,8 +385,12 @@ * instances. */ public StyleSheet getStyleSheet() { + AppContext appContext = AppContext.getAppContext(); + StyleSheet defaultStyles = (StyleSheet) appContext.get(DEFAULT_STYLES_KEY); + if (defaultStyles == null) { defaultStyles = new StyleSheet(); + appContext.put(DEFAULT_STYLES_KEY, defaultStyles); try { InputStream is = HTMLEditorKit.getResourceAsStream(DEFAULT_CSS); Reader r = new BufferedReader( @@ -620,7 +630,7 @@ private static final ViewFactory defaultFactory = new HTMLFactory(); MutableAttributeSet input; - private static StyleSheet defaultStyles = null; + private static final Object DEFAULT_STYLES_KEY = new Object(); private LinkController linkHandler = new LinkController(); private static Parser defaultParser = null; private Cursor defaultCursor = DefaultCursor;
--- a/jdk/src/share/classes/javax/swing/text/html/parser/DTD.java Fri Jun 18 13:18:42 2010 +0400 +++ b/jdk/src/share/classes/javax/swing/text/html/parser/DTD.java Mon Jun 21 16:47:05 2010 +0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2010, 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 @@ -25,6 +25,8 @@ package javax.swing.text.html.parser; +import sun.awt.AppContext; + import java.io.PrintStream; import java.io.File; import java.io.FileInputStream; @@ -314,13 +316,14 @@ } /** - * The hashtable of DTDs. + * The hashtable key of DTDs in AppContext. */ - static Hashtable<String, DTD> dtdHash = new Hashtable<String, DTD>(); + private static final Object DTD_HASH_KEY = new Object(); - public static void putDTDHash(String name, DTD dtd) { - dtdHash.put(name, dtd); - } + public static void putDTDHash(String name, DTD dtd) { + getDtdHash().put(name, dtd); + } + /** * Returns a DTD with the specified <code>name</code>. If * a DTD with that name doesn't exist, one is created @@ -332,13 +335,27 @@ */ public static DTD getDTD(String name) throws IOException { name = name.toLowerCase(); - DTD dtd = dtdHash.get(name); + DTD dtd = getDtdHash().get(name); if (dtd == null) dtd = new DTD(name); return dtd; } + private static Hashtable<String, DTD> getDtdHash() { + AppContext appContext = AppContext.getAppContext(); + + Hashtable<String, DTD> result = (Hashtable<String, DTD>) appContext.get(DTD_HASH_KEY); + + if (result == null) { + result = new Hashtable<String, DTD>(); + + appContext.put(DTD_HASH_KEY, result); + } + + return result; + } + /** * Recreates a DTD from an archived format. * @param in the <code>DataInputStream</code> to read from
--- a/jdk/src/share/classes/javax/swing/text/html/parser/ParserDelegator.java Fri Jun 18 13:18:42 2010 +0400 +++ b/jdk/src/share/classes/javax/swing/text/html/parser/ParserDelegator.java Mon Jun 21 16:47:05 2010 +0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2002, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2010, 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 @@ -25,6 +25,8 @@ package javax.swing.text.html.parser; +import sun.awt.AppContext; + import javax.swing.text.html.HTMLEditorKit; import java.io.BufferedInputStream; import java.io.IOException; @@ -33,7 +35,6 @@ import java.io.ObjectInputStream; import java.io.Reader; import java.io.Serializable; -import java.lang.reflect.Method; /** * Responsible for starting up a new DocumentParser @@ -45,9 +46,13 @@ public class ParserDelegator extends HTMLEditorKit.Parser implements Serializable { - private static DTD dtd = null; + private static final Object DTD_KEY = new Object(); protected static synchronized void setDefaultDTD() { + AppContext appContext = AppContext.getAppContext(); + + DTD dtd = (DTD) appContext.get(DTD_KEY); + if (dtd == null) { DTD _dtd = null; // (PENDING) Hate having to hard code! @@ -59,6 +64,8 @@ System.out.println("Throw an exception: could not get default dtd: " + nm); } dtd = createDTD(_dtd, nm); + + appContext.put(DTD_KEY, dtd); } } @@ -81,13 +88,11 @@ public ParserDelegator() { - if (dtd == null) { - setDefaultDTD(); - } + setDefaultDTD(); } public void parse(Reader r, HTMLEditorKit.ParserCallback cb, boolean ignoreCharSet) throws IOException { - new DocumentParser(dtd).parse(r, cb, ignoreCharSet); + new DocumentParser((DTD) AppContext.getAppContext().get(DTD_KEY)).parse(r, cb, ignoreCharSet); } /** @@ -113,8 +118,6 @@ private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject(); - if (dtd == null) { - setDefaultDTD(); - } + setDefaultDTD(); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/javax/swing/Security/6938813/bug6938813.java Mon Jun 21 16:47:05 2010 +0400 @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2010, 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 6938813 + * @summary Swing mutable statics + * @author Pavel Porvatov + */ + +import sun.awt.AppContext; +import sun.awt.SunToolkit; + +import javax.swing.text.html.HTMLEditorKit; +import javax.swing.text.html.StyleSheet; +import javax.swing.text.html.parser.DTD; +import javax.swing.text.html.parser.ParserDelegator; +import java.lang.reflect.Field; + +public class bug6938813 { + public static final String DTD_KEY = "dtd_key"; + + private static volatile StyleSheet styleSheet; + + public static void main(String[] args) throws Exception { + // Run validation and init values for this AppContext + validate(); + + Thread thread = new ThreadInAnotherAppContext(); + + thread.start(); + thread.join(); + } + + private static void validate() throws Exception { + AppContext appContext = AppContext.getAppContext(); + + assertTrue(DTD.getDTD(DTD_KEY).getName().equals(DTD_KEY), "DTD.getDTD() mixed AppContexts"); + + // Spoil hash value + DTD invalidDtd = DTD.getDTD("invalid DTD"); + + DTD.putDTDHash(DTD_KEY, invalidDtd); + + assertTrue(DTD.getDTD(DTD_KEY) == invalidDtd, "Something wrong with DTD.getDTD()"); + + Object dtdKey = getParserDelegator_DTD_KEY(); + + assertTrue(appContext.get(dtdKey) == null, "ParserDelegator mixed AppContexts"); + + // Init default DTD + new ParserDelegator(); + + Object dtdValue = appContext.get(dtdKey); + + assertTrue(dtdValue != null, "ParserDelegator.defaultDTD isn't initialized"); + + // Try reinit default DTD + new ParserDelegator(); + + assertTrue(dtdValue == appContext.get(dtdKey), "ParserDelegator.defaultDTD created a duplicate"); + + HTMLEditorKit htmlEditorKit = new HTMLEditorKit(); + + if (styleSheet == null) { + // First AppContext + styleSheet = htmlEditorKit.getStyleSheet(); + + assertTrue(styleSheet != null, "htmlEditorKit.getStyleSheet() returns null"); + assertTrue(htmlEditorKit.getStyleSheet() == styleSheet, "Something wrong with htmlEditorKit.getStyleSheet()"); + } else { + assertTrue(htmlEditorKit.getStyleSheet() != styleSheet, "HtmlEditorKit.getStyleSheet() mixed AppContexts"); + } + } + + private static void assertTrue(boolean b, String msg) { + if (!b) { + throw new RuntimeException("Test failed: " + msg); + } + } + + private static Object getParserDelegator_DTD_KEY() throws Exception { + Field field = ParserDelegator.class.getDeclaredField("DTD_KEY"); + + field.setAccessible(true); + + return field.get(null); + } + + private static class ThreadInAnotherAppContext extends Thread { + public ThreadInAnotherAppContext() { + super(new ThreadGroup("6938813"), "ThreadInAnotherAppContext"); + } + + public void run() { + SunToolkit.createNewAppContext(); + + try { + validate(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } +}