Fri, 14 Feb 2014 11:13:45 +0100
8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu
ohair@286 | 1 | /* |
alanb@368 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.ws.streaming; |
ohair@286 | 27 | |
ohair@286 | 28 | import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory; |
ohair@286 | 29 | import com.sun.xml.internal.ws.util.FastInfosetUtil; |
ohair@286 | 30 | import com.sun.xml.internal.ws.util.xml.XmlUtil; |
ohair@286 | 31 | |
ohair@286 | 32 | import javax.xml.stream.XMLStreamReader; |
ohair@286 | 33 | import javax.xml.transform.Source; |
ohair@286 | 34 | import javax.xml.transform.Transformer; |
ohair@286 | 35 | import javax.xml.transform.dom.DOMResult; |
ohair@286 | 36 | import javax.xml.transform.dom.DOMSource; |
ohair@286 | 37 | import javax.xml.transform.sax.SAXSource; |
ohair@286 | 38 | import javax.xml.transform.stream.StreamSource; |
ohair@286 | 39 | import java.io.InputStream; |
ohair@286 | 40 | import java.io.InputStreamReader; |
ohair@286 | 41 | import java.io.Reader; |
ohair@286 | 42 | import java.lang.reflect.Method; |
ohair@286 | 43 | import java.net.URL; |
ohair@286 | 44 | |
ohair@286 | 45 | /** |
ohair@286 | 46 | * @author Santiago.PericasGeertsen@sun.com |
ohair@286 | 47 | */ |
ohair@286 | 48 | public class SourceReaderFactory { |
ohair@286 | 49 | |
ohair@286 | 50 | /** |
ohair@286 | 51 | * FI FastInfosetSource class. |
ohair@286 | 52 | */ |
ohair@286 | 53 | static Class fastInfosetSourceClass; |
ohair@286 | 54 | |
ohair@286 | 55 | /** |
ohair@286 | 56 | * FI <code>StAXDocumentSerializer.setEncoding()</code> method via reflection. |
ohair@286 | 57 | */ |
ohair@286 | 58 | static Method fastInfosetSource_getInputStream; |
ohair@286 | 59 | |
ohair@286 | 60 | static { |
ohair@286 | 61 | // Use reflection to avoid static dependency with FI jar |
ohair@286 | 62 | try { |
ohair@286 | 63 | fastInfosetSourceClass = |
ohair@286 | 64 | Class.forName("com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSource"); |
ohair@286 | 65 | fastInfosetSource_getInputStream = |
ohair@286 | 66 | fastInfosetSourceClass.getMethod("getInputStream"); |
ohair@286 | 67 | } |
ohair@286 | 68 | catch (Exception e) { |
ohair@286 | 69 | fastInfosetSourceClass = null; |
ohair@286 | 70 | } |
ohair@286 | 71 | } |
ohair@286 | 72 | |
ohair@286 | 73 | public static XMLStreamReader createSourceReader(Source source, boolean rejectDTDs) { |
ohair@286 | 74 | return createSourceReader(source, rejectDTDs, null); |
ohair@286 | 75 | } |
ohair@286 | 76 | |
ohair@286 | 77 | public static XMLStreamReader createSourceReader(Source source, boolean rejectDTDs, String charsetName) { |
ohair@286 | 78 | try { |
ohair@286 | 79 | if (source instanceof StreamSource) { |
ohair@286 | 80 | StreamSource streamSource = (StreamSource) source; |
ohair@286 | 81 | InputStream is = streamSource.getInputStream(); |
ohair@286 | 82 | |
ohair@286 | 83 | if (is != null) { |
ohair@286 | 84 | // Wrap input stream in Reader if charset is specified |
ohair@286 | 85 | if (charsetName != null) { |
ohair@286 | 86 | return XMLStreamReaderFactory.create( |
ohair@286 | 87 | source.getSystemId(), new InputStreamReader(is, charsetName), rejectDTDs); |
ohair@286 | 88 | } |
ohair@286 | 89 | else { |
ohair@286 | 90 | return XMLStreamReaderFactory.create( |
ohair@286 | 91 | source.getSystemId(), is, rejectDTDs); |
ohair@286 | 92 | } |
ohair@286 | 93 | } |
ohair@286 | 94 | else { |
ohair@286 | 95 | Reader reader = streamSource.getReader(); |
ohair@286 | 96 | if (reader != null) { |
ohair@286 | 97 | return XMLStreamReaderFactory.create( |
ohair@286 | 98 | source.getSystemId(), reader, rejectDTDs); |
ohair@286 | 99 | } |
ohair@286 | 100 | else { |
ohair@286 | 101 | return XMLStreamReaderFactory.create( |
ohair@286 | 102 | source.getSystemId(), new URL(source.getSystemId()).openStream(), rejectDTDs ); |
ohair@286 | 103 | } |
ohair@286 | 104 | } |
ohair@286 | 105 | } |
ohair@286 | 106 | else if (source.getClass() == fastInfosetSourceClass) { |
ohair@286 | 107 | return FastInfosetUtil.createFIStreamReader((InputStream) |
ohair@286 | 108 | fastInfosetSource_getInputStream.invoke(source)); |
ohair@286 | 109 | } |
ohair@286 | 110 | else if (source instanceof DOMSource) { |
ohair@286 | 111 | DOMStreamReader dsr = new DOMStreamReader(); |
ohair@286 | 112 | dsr.setCurrentNode(((DOMSource) source).getNode()); |
ohair@286 | 113 | return dsr; |
ohair@286 | 114 | } |
ohair@286 | 115 | else if (source instanceof SAXSource) { |
ohair@286 | 116 | // TODO: need SAX to StAX adapter here -- Use transformer for now |
ohair@286 | 117 | Transformer tx = XmlUtil.newTransformer(); |
ohair@286 | 118 | DOMResult domResult = new DOMResult(); |
ohair@286 | 119 | tx.transform(source, domResult); |
ohair@286 | 120 | return createSourceReader( |
ohair@286 | 121 | new DOMSource(domResult.getNode()), |
ohair@286 | 122 | rejectDTDs); |
ohair@286 | 123 | } |
ohair@286 | 124 | else { |
ohair@286 | 125 | throw new XMLReaderException("sourceReader.invalidSource", |
ohair@286 | 126 | source.getClass().getName()); |
ohair@286 | 127 | } |
ohair@286 | 128 | } |
ohair@286 | 129 | catch (Exception e) { |
ohair@286 | 130 | throw new XMLReaderException(e); |
ohair@286 | 131 | } |
ohair@286 | 132 | } |
ohair@286 | 133 | |
ohair@286 | 134 | } |