8079323: Serialization compatibility for Templates: need to exclude Hashtable from serialization

Reviewed-by: dfuchs, lancea, hawtin
This commit is contained in:
Joe Wang 2015-05-12 10:02:21 -07:00
parent 6eafc29cfa
commit 9a0831a18e
2 changed files with 87 additions and 11 deletions
jaxp
src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax
test/javax/xml/jaxp/unittest/transform

@ -31,6 +31,7 @@ import com.sun.org.apache.xalan.internal.xsltc.Translet;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
import java.io.IOException; import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.ObjectStreamField; import java.io.ObjectStreamField;
@ -38,7 +39,6 @@ import java.io.Serializable;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.util.HashMap; import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import javax.xml.XMLConstants; import javax.xml.XMLConstants;
@ -91,7 +91,7 @@ public final class TemplatesImpl implements Templates, Serializable {
/** /**
* Contains the list of auxiliary class definitions. * Contains the list of auxiliary class definitions.
*/ */
private Map<String, Class<?>> _auxClasses = null; private transient Map<String, Class<?>> _auxClasses = null;
/** /**
* Output properties of this translet. * Output properties of this translet.
@ -139,7 +139,6 @@ public final class TemplatesImpl implements Templates, Serializable {
* @serialField _bytecodes byte[][] Class definition * @serialField _bytecodes byte[][] Class definition
* @serialField _class Class[] The translet class definition(s). * @serialField _class Class[] The translet class definition(s).
* @serialField _transletIndex int The index of the main translet class * @serialField _transletIndex int The index of the main translet class
* @serialField _auxClasses Hashtable The list of auxiliary class definitions.
* @serialField _outputProperties Properties Output properties of this translet. * @serialField _outputProperties Properties Output properties of this translet.
* @serialField _indentNumber int Number of spaces to add for output indentation. * @serialField _indentNumber int Number of spaces to add for output indentation.
*/ */
@ -149,7 +148,6 @@ public final class TemplatesImpl implements Templates, Serializable {
new ObjectStreamField("_bytecodes", byte[][].class), new ObjectStreamField("_bytecodes", byte[][].class),
new ObjectStreamField("_class", Class[].class), new ObjectStreamField("_class", Class[].class),
new ObjectStreamField("_transletIndex", int.class), new ObjectStreamField("_transletIndex", int.class),
new ObjectStreamField("_auxClasses", Hashtable.class),
new ObjectStreamField("_outputProperties", Properties.class), new ObjectStreamField("_outputProperties", Properties.class),
new ObjectStreamField("_indentNumber", int.class), new ObjectStreamField("_indentNumber", int.class),
}; };
@ -238,6 +236,7 @@ public final class TemplatesImpl implements Templates, Serializable {
* if yes then we need to deserialize the URIResolver * if yes then we need to deserialize the URIResolver
* Fix for bugzilla bug 22438 * Fix for bugzilla bug 22438
*/ */
@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream is) private void readObject(ObjectInputStream is)
throws IOException, ClassNotFoundException throws IOException, ClassNotFoundException
{ {
@ -257,13 +256,9 @@ public final class TemplatesImpl implements Templates, Serializable {
_class = (Class[])gf.get("_class", null); _class = (Class[])gf.get("_class", null);
_transletIndex = gf.get("_transletIndex", -1); _transletIndex = gf.get("_transletIndex", -1);
Hashtable<String, Class<?>> aux = (Hashtable<String, Class<?>>)gf.get("_auxClasses", null);
_outputProperties = (Properties)gf.get("_outputProperties", null); _outputProperties = (Properties)gf.get("_outputProperties", null);
_indentNumber = gf.get("_indentNumber", 0); _indentNumber = gf.get("_indentNumber", 0);
//convert Hashtable back to HashMap
if (aux != null) _auxClasses = new HashMap<>(aux);
if (is.readBoolean()) { if (is.readBoolean()) {
_uriResolver = (URIResolver) is.readObject(); _uriResolver = (URIResolver) is.readObject();
} }
@ -279,8 +274,11 @@ public final class TemplatesImpl implements Templates, Serializable {
*/ */
private void writeObject(ObjectOutputStream os) private void writeObject(ObjectOutputStream os)
throws IOException, ClassNotFoundException { throws IOException, ClassNotFoundException {
// Convert Maps to Hashtables if (_auxClasses != null) {
Hashtable<String, Class<?>> aux = (_auxClasses == null)? null : new Hashtable<>(_auxClasses); //throw with the same message as when Hashtable was used for compatibility.
throw new NotSerializableException(
"com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable");
}
// Write serialized fields // Write serialized fields
ObjectOutputStream.PutField pf = os.putFields(); ObjectOutputStream.PutField pf = os.putFields();
@ -288,7 +286,6 @@ public final class TemplatesImpl implements Templates, Serializable {
pf.put("_bytecodes", _bytecodes); pf.put("_bytecodes", _bytecodes);
pf.put("_class", _class); pf.put("_class", _class);
pf.put("_transletIndex", _transletIndex); pf.put("_transletIndex", _transletIndex);
pf.put("_auxClasses", aux);
pf.put("_outputProperties", _outputProperties); pf.put("_outputProperties", _outputProperties);
pf.put("_indentNumber", _indentNumber); pf.put("_indentNumber", _indentNumber);
os.writeFields(); os.writeFields();

@ -0,0 +1,79 @@
/*
* Copyright (c) 2015, 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.
*/
package transform;
import java.io.ByteArrayOutputStream;
import java.io.NotSerializableException;
import java.io.ObjectOutputStream;
import java.io.StringReader;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamSource;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/*
* @summary This class contains tests for Templates.
*/
public class TemplatesTest {
/**
* @bug 8079323 Test Templates serialization
* <p>
* Serialization compatibility test: verify that serializing the Templates
* that contain auxiliary classes will result in a NotSerializableException
* due to the use of Xalan's non-serializable Hashtable.
*
* @param templates an instance of Templates
* @throws Exception as expected.
*/
@Test(dataProvider = "templates", expectedExceptions = NotSerializableException.class)
public void testSerialization(Templates templates) throws Exception {
Transformer xformer = templates.newTransformer();
try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);) {
out.writeObject(templates);
out.flush();
}
}
/*
* DataProvider: Templates
*/
@DataProvider(name = "templates")
Object[][] getTemplates() throws Exception {
return new Object[][]{{TransformerFactory.newInstance().
newTemplates(new StreamSource(new StringReader(XSL)))}};
}
static final String XSL = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
+ "<xsl:stylesheet version=\"1.0\""
+ " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
+ "<xsl:variable name=\"validAffectsRelClasses\">"
+ "</xsl:variable>"
+ "<xsl:key name=\"UniqueAffectsRelObjects\""
+ " match=\"/ObjectSetRoot/Object["
+ " contains($validAffectsRelClasses, @Class)]\""
+ " use=\"not(@OBID=preceding-sibling::Object["
+ " contains($validAffectsRelClasses, @Class)]/@OBID)\"/>"
+ "</xsl:stylesheet>";
}