diff --git a/src/share/classes/javax/swing/text/html/HTMLEditorKit.java b/src/share/classes/javax/swing/text/html/HTMLEditorKit.java index 88a3661b0bafb88efeed55f80adb192dcc734b0b..f9642c7bdd75488221443683b66305e8edf2b94c 100644 --- a/src/share/classes/javax/swing/text/html/HTMLEditorKit.java +++ b/src/share/classes/javax/swing/text/html/HTMLEditorKit.java @@ -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 @@ public class HTMLEditorKit extends StyledEditorKit implements Accessible { * 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 @@ public class HTMLEditorKit extends StyledEditorKit implements Accessible { * 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 @@ public class HTMLEditorKit extends StyledEditorKit implements Accessible { 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; diff --git a/src/share/classes/javax/swing/text/html/parser/DTD.java b/src/share/classes/javax/swing/text/html/parser/DTD.java index 4639b8379ba7ed679e9aad673ef5096ab0560719..b0787eabbb07f886b1e201b9c72456118b7a8ea7 100644 --- a/src/share/classes/javax/swing/text/html/parser/DTD.java +++ b/src/share/classes/javax/swing/text/html/parser/DTD.java @@ -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 @@ class DTD implements DTDConstants { } /** - * The hashtable of DTDs. + * The hashtable key of DTDs in AppContext. */ - static Hashtable dtdHash = new Hashtable(); + private static final Object DTD_HASH_KEY = new Object(); + + public static void putDTDHash(String name, DTD dtd) { + getDtdHash().put(name, dtd); + } - public static void putDTDHash(String name, DTD dtd) { - dtdHash.put(name, dtd); - } /** * Returns a DTD with the specified name. If * a DTD with that name doesn't exist, one is created @@ -332,13 +335,27 @@ class DTD implements DTDConstants { */ 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 getDtdHash() { + AppContext appContext = AppContext.getAppContext(); + + Hashtable result = (Hashtable) appContext.get(DTD_HASH_KEY); + + if (result == null) { + result = new Hashtable(); + + appContext.put(DTD_HASH_KEY, result); + } + + return result; + } + /** * Recreates a DTD from an archived format. * @param in the DataInputStream to read from diff --git a/src/share/classes/javax/swing/text/html/parser/ParserDelegator.java b/src/share/classes/javax/swing/text/html/parser/ParserDelegator.java index 89289e080970a4bc46641ce81102b374892bbc9f..06b53483dc082b91319b9502eeba04a6c3d0c645 100644 --- a/src/share/classes/javax/swing/text/html/parser/ParserDelegator.java +++ b/src/share/classes/javax/swing/text/html/parser/ParserDelegator.java @@ -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.DataInputStream; 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 @@ import java.lang.reflect.Method; 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 @@ public class ParserDelegator extends HTMLEditorKit.Parser implements Serializabl 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 class ParserDelegator extends HTMLEditorKit.Parser implements Serializabl 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 @@ public class ParserDelegator extends HTMLEditorKit.Parser implements Serializabl private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject(); - if (dtd == null) { - setDefaultDTD(); - } + setDefaultDTD(); } } diff --git a/test/javax/swing/Security/6938813/bug6938813.java b/test/javax/swing/Security/6938813/bug6938813.java new file mode 100644 index 0000000000000000000000000000000000000000..5bedf95e1403036d2040d4c20d342bdefa892e67 --- /dev/null +++ b/test/javax/swing/Security/6938813/bug6938813.java @@ -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); + } + } + } +}