src/share/jaxws_classes/com/sun/xml/internal/ws/api/streaming/XMLStreamWriterFactory.java

Mon, 23 Apr 2018 13:24:44 +0100

author
aefimov
date
Mon, 23 Apr 2018 13:24:44 +0100
changeset 1667
e716ed4b0efb
parent 1609
09b083e0759c
child 1620
6df7b161ae4a
permissions
-rw-r--r--

8196491: Newlines in JAXB string values of SOAP-requests are escaped to "
"
Reviewed-by: lancea, rgrigoriadi

ohair@286 1 /*
aefimov@1609 2 * Copyright (c) 1997, 2018, 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.api.streaming;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.istack.internal.Nullable;
ohair@286 30 import com.sun.xml.internal.ws.encoding.HasEncoding;
ohair@286 31 import com.sun.xml.internal.ws.encoding.SOAPBindingCodec;
ohair@286 32 import com.sun.xml.internal.ws.streaming.XMLReaderException;
ohair@286 33 import com.sun.xml.internal.ws.util.xml.XMLStreamWriterFilter;
ohair@286 34
ohair@286 35 import javax.xml.stream.XMLOutputFactory;
ohair@286 36 import javax.xml.stream.XMLStreamException;
ohair@286 37 import javax.xml.stream.XMLStreamReader;
ohair@286 38 import javax.xml.stream.XMLStreamWriter;
ohair@286 39 import javax.xml.transform.stream.StreamResult;
ohair@286 40 import javax.xml.ws.WebServiceException;
ohair@286 41 import java.io.OutputStream;
ohair@286 42 import java.io.StringWriter;
ohair@286 43 import java.lang.reflect.InvocationTargetException;
ohair@286 44 import java.lang.reflect.Method;
mkos@384 45 import java.util.logging.Level;
ohair@286 46 import java.util.logging.Logger;
ohair@286 47
ohair@286 48 /**
ohair@286 49 * Factory for {@link XMLStreamWriter}.
ohair@286 50 *
ohair@286 51 * <p>
ohair@286 52 * This wraps {@link XMLOutputFactory} and allows us to reuse {@link XMLStreamWriter} instances
ohair@286 53 * when appropriate.
ohair@286 54 *
ohair@286 55 * @author Kohsuke Kawaguchi
ohair@286 56 */
mkos@384 57 @SuppressWarnings("StaticNonFinalUsedInInitialization")
ohair@286 58 public abstract class XMLStreamWriterFactory {
ohair@286 59
ohair@286 60 private static final Logger LOGGER = Logger.getLogger(XMLStreamWriterFactory.class.getName());
ohair@286 61
ohair@286 62 /**
ohair@286 63 * Singleton instance.
ohair@286 64 */
mkos@515 65 private static volatile ContextClassloaderLocal<XMLStreamWriterFactory> writerFactory =
mkos@515 66 new ContextClassloaderLocal<XMLStreamWriterFactory>() {
ohair@286 67
mkos@515 68 @Override
mkos@515 69 protected XMLStreamWriterFactory initialValue() {
mkos@515 70 XMLOutputFactory xof = null;
mkos@515 71 if (Boolean.getBoolean(XMLStreamWriterFactory.class.getName()+".woodstox")) {
mkos@515 72 try {
mkos@515 73 xof = (XMLOutputFactory)Class.forName("com.ctc.wstx.stax.WstxOutputFactory").newInstance();
mkos@515 74 } catch (Exception e) {
mkos@515 75 // Ignore and fallback to default XMLOutputFactory
mkos@515 76 }
mkos@515 77 }
mkos@515 78 if (xof == null) {
mkos@515 79 xof = XMLOutputFactory.newInstance();
mkos@515 80 }
ohair@286 81
mkos@515 82 XMLStreamWriterFactory f=null;
mkos@515 83
mkos@515 84 // this system property can be used to disable the pooling altogether,
mkos@515 85 // in case someone hits an issue with pooling in the production system.
mkos@515 86 if (!Boolean.getBoolean(XMLStreamWriterFactory.class.getName()+".noPool")) {
mkos@515 87 try {
mkos@515 88 Class<?> clazz = xof.createXMLStreamWriter(new StringWriter()).getClass();
mkos@515 89 if (clazz.getName().startsWith("com.sun.xml.internal.stream.")) {
mkos@515 90 f = new Zephyr(xof,clazz);
mkos@515 91 }
mkos@515 92 } catch (XMLStreamException ex) {
mkos@515 93 Logger.getLogger(XMLStreamWriterFactory.class.getName()).log(Level.INFO, null, ex);
mkos@515 94 } catch (NoSuchMethodException ex) {
mkos@515 95 Logger.getLogger(XMLStreamWriterFactory.class.getName()).log(Level.INFO, null, ex);
mkos@515 96 }
ohair@286 97 }
mkos@515 98
mkos@515 99 if(f==null) {
mkos@515 100 // is this Woodstox?
mkos@515 101 if(xof.getClass().getName().equals("com.ctc.wstx.stax.WstxOutputFactory"))
mkos@515 102 f = new NoLock(xof);
mkos@515 103 }
mkos@515 104 if (f == null)
mkos@515 105 f = new Default(xof);
mkos@515 106
mkos@515 107 if (LOGGER.isLoggable(Level.FINE)) {
mkos@515 108 LOGGER.log(Level.FINE, "XMLStreamWriterFactory instance is = {0}", f);
mkos@515 109 }
mkos@515 110 return f;
ohair@286 111 }
mkos@515 112 };
ohair@286 113
ohair@286 114 /**
ohair@286 115 * See {@link #create(OutputStream)} for the contract.
ohair@286 116 * This method may be invoked concurrently.
ohair@286 117 */
ohair@286 118 public abstract XMLStreamWriter doCreate(OutputStream out);
ohair@286 119
ohair@286 120 /**
ohair@286 121 * See {@link #create(OutputStream,String)} for the contract.
ohair@286 122 * This method may be invoked concurrently.
ohair@286 123 */
ohair@286 124 public abstract XMLStreamWriter doCreate(OutputStream out, String encoding);
ohair@286 125
ohair@286 126 /**
ohair@286 127 * See {@link #recycle(XMLStreamWriter)} for the contract.
ohair@286 128 * This method may be invoked concurrently.
ohair@286 129 */
ohair@286 130 public abstract void doRecycle(XMLStreamWriter r);
ohair@286 131
ohair@286 132 /**
ohair@286 133 * Should be invoked when the code finished using an {@link XMLStreamWriter}.
ohair@286 134 *
ohair@286 135 * <p>
ohair@286 136 * If the recycled instance implements {@link RecycleAware},
ohair@286 137 * {@link RecycleAware#onRecycled()} will be invoked to let the instance
ohair@286 138 * know that it's being recycled.
ohair@286 139 *
ohair@286 140 * <p>
ohair@286 141 * It is not a hard requirement to call this method on every {@link XMLStreamReader}
ohair@286 142 * instance. Not doing so just reduces the performance by throwing away
ohair@286 143 * possibly reusable instances. So the caller should always consider the effort
ohair@286 144 * it takes to recycle vs the possible performance gain by doing so.
ohair@286 145 *
ohair@286 146 * <p>
ohair@286 147 * This method may be invked by multiple threads concurrently.
ohair@286 148 *
ohair@286 149 * @param r
ohair@286 150 * The {@link XMLStreamReader} instance that the caller finished using.
ohair@286 151 * This could be any {@link XMLStreamReader} implementation, not just
ohair@286 152 * the ones that were created from this factory. So the implementation
ohair@286 153 * of this class needs to be aware of that.
ohair@286 154 */
ohair@286 155 public static void recycle(XMLStreamWriter r) {
ohair@286 156 get().doRecycle(r);
ohair@286 157 }
ohair@286 158
ohair@286 159 /**
ohair@286 160 * Interface that can be implemented by {@link XMLStreamWriter} to
ohair@286 161 * be notified when it's recycled.
ohair@286 162 *
ohair@286 163 * <p>
ohair@286 164 * This provides a filtering {@link XMLStreamWriter} an opportunity to
ohair@286 165 * recycle its inner {@link XMLStreamWriter}.
ohair@286 166 */
ohair@286 167 public interface RecycleAware {
ohair@286 168 void onRecycled();
ohair@286 169 }
ohair@286 170
ohair@286 171 /**
ohair@286 172 * Gets the singleton instance.
ohair@286 173 */
ohair@286 174 public static @NotNull XMLStreamWriterFactory get() {
mkos@515 175 return writerFactory.get();
ohair@286 176 }
ohair@286 177
ohair@286 178 /**
ohair@286 179 * Overrides the singleton {@link XMLStreamWriterFactory} instance that
ohair@286 180 * the JAX-WS RI uses.
ohair@286 181 *
ohair@286 182 * @param f
ohair@286 183 * must not be null.
ohair@286 184 */
mkos@384 185 @SuppressWarnings({"null", "ConstantConditions"})
ohair@286 186 public static void set(@NotNull XMLStreamWriterFactory f) {
ohair@286 187 if(f==null) throw new IllegalArgumentException();
mkos@515 188 writerFactory.set(f);
ohair@286 189 }
ohair@286 190
ohair@286 191 /**
ohair@286 192 * Short-cut for {@link #create(OutputStream, String)} with UTF-8.
ohair@286 193 */
ohair@286 194 public static XMLStreamWriter create(OutputStream out) {
ohair@286 195 return get().doCreate(out);
ohair@286 196 }
ohair@286 197
ohair@286 198 public static XMLStreamWriter create(OutputStream out, String encoding) {
ohair@286 199 return get().doCreate(out, encoding);
ohair@286 200 }
ohair@286 201
ohair@286 202 /**
ohair@286 203 * @deprecated
ohair@286 204 * Use {@link #create(OutputStream)}
ohair@286 205 */
ohair@286 206 public static XMLStreamWriter createXMLStreamWriter(OutputStream out) {
ohair@286 207 return create(out);
ohair@286 208 }
ohair@286 209
ohair@286 210 /**
ohair@286 211 * @deprecated
ohair@286 212 * Use {@link #create(OutputStream, String)}
ohair@286 213 */
ohair@286 214 public static XMLStreamWriter createXMLStreamWriter(OutputStream out, String encoding) {
ohair@286 215 return create(out, encoding);
ohair@286 216 }
ohair@286 217
ohair@286 218 /**
ohair@286 219 * @deprecated
ohair@286 220 * Use {@link #create(OutputStream, String)}. The boolean flag was unused anyway.
ohair@286 221 */
ohair@286 222 public static XMLStreamWriter createXMLStreamWriter(OutputStream out, String encoding, boolean declare) {
ohair@286 223 return create(out,encoding);
ohair@286 224 }
ohair@286 225
ohair@286 226 /**
ohair@286 227 * Default {@link XMLStreamWriterFactory} implementation
ohair@286 228 * that can work with any {@link XMLOutputFactory}.
ohair@286 229 *
ohair@286 230 * <p>
ohair@286 231 * {@link XMLOutputFactory} is not required to be thread-safe, so the
ohair@286 232 * create method on this implementation is synchronized.
ohair@286 233 */
ohair@286 234 public static final class Default extends XMLStreamWriterFactory {
ohair@286 235 private final XMLOutputFactory xof;
ohair@286 236
ohair@286 237 public Default(XMLOutputFactory xof) {
ohair@286 238 this.xof = xof;
ohair@286 239 }
ohair@286 240
mkos@384 241 @Override
ohair@286 242 public XMLStreamWriter doCreate(OutputStream out) {
ohair@286 243 return doCreate(out,"UTF-8");
ohair@286 244 }
ohair@286 245
mkos@384 246 @Override
ohair@286 247 public synchronized XMLStreamWriter doCreate(OutputStream out, String encoding) {
ohair@286 248 try {
ohair@286 249 XMLStreamWriter writer = xof.createXMLStreamWriter(out,encoding);
ohair@286 250 return new HasEncodingWriter(writer, encoding);
ohair@286 251 } catch (XMLStreamException e) {
ohair@286 252 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 253 }
ohair@286 254 }
ohair@286 255
mkos@384 256 @Override
ohair@286 257 public void doRecycle(XMLStreamWriter r) {
ohair@286 258 // no recycling
ohair@286 259 }
ohair@286 260 }
ohair@286 261
ohair@286 262 /**
ohair@286 263 * {@link XMLStreamWriterFactory} implementation for Sun's StaX implementation.
ohair@286 264 *
ohair@286 265 * <p>
ohair@286 266 * This implementation supports instance reuse.
ohair@286 267 */
ohair@286 268 public static final class Zephyr extends XMLStreamWriterFactory {
ohair@286 269 private final XMLOutputFactory xof;
ohair@286 270 private final ThreadLocal<XMLStreamWriter> pool = new ThreadLocal<XMLStreamWriter>();
ohair@286 271 private final Method resetMethod;
ohair@286 272 private final Method setOutputMethod;
ohair@286 273 private final Class zephyrClass;
ohair@286 274
ohair@286 275 public static XMLStreamWriterFactory newInstance(XMLOutputFactory xof) {
ohair@286 276 try {
ohair@286 277 Class<?> clazz = xof.createXMLStreamWriter(new StringWriter()).getClass();
ohair@286 278
ohair@286 279 if(!clazz.getName().startsWith("com.sun.xml.internal.stream."))
ohair@286 280 return null; // nope
ohair@286 281
ohair@286 282 return new Zephyr(xof,clazz);
ohair@286 283 } catch (XMLStreamException e) {
ohair@286 284 return null; // impossible
ohair@286 285 } catch (NoSuchMethodException e) {
ohair@286 286 return null; // this xof wasn't Zephyr
ohair@286 287 }
ohair@286 288 }
ohair@286 289
ohair@286 290 private Zephyr(XMLOutputFactory xof, Class clazz) throws NoSuchMethodException {
ohair@286 291 this.xof = xof;
ohair@286 292
ohair@286 293 zephyrClass = clazz;
ohair@286 294 setOutputMethod = clazz.getMethod("setOutput", StreamResult.class, String.class);
ohair@286 295 resetMethod = clazz.getMethod("reset");
ohair@286 296 }
ohair@286 297
ohair@286 298 /**
ohair@286 299 * Fetchs an instance from the pool if available, otherwise null.
ohair@286 300 */
ohair@286 301 private @Nullable XMLStreamWriter fetch() {
ohair@286 302 XMLStreamWriter sr = pool.get();
ohair@286 303 if(sr==null) return null;
ohair@286 304 pool.set(null);
ohair@286 305 return sr;
ohair@286 306 }
ohair@286 307
mkos@384 308 @Override
ohair@286 309 public XMLStreamWriter doCreate(OutputStream out) {
ohair@286 310 return doCreate(out,"UTF-8");
ohair@286 311 }
ohair@286 312
mkos@384 313 @Override
ohair@286 314 public XMLStreamWriter doCreate(OutputStream out, String encoding) {
ohair@286 315 XMLStreamWriter xsw = fetch();
ohair@286 316 if(xsw!=null) {
ohair@286 317 // try to reuse
ohair@286 318 try {
ohair@286 319 resetMethod.invoke(xsw);
ohair@286 320 setOutputMethod.invoke(xsw,new StreamResult(out),encoding);
ohair@286 321 } catch (IllegalAccessException e) {
ohair@286 322 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 323 } catch (InvocationTargetException e) {
ohair@286 324 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 325 }
ohair@286 326 } else {
ohair@286 327 // create a new instance
ohair@286 328 try {
ohair@286 329 xsw = xof.createXMLStreamWriter(out,encoding);
ohair@286 330 } catch (XMLStreamException e) {
ohair@286 331 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 332 }
ohair@286 333 }
ohair@286 334 return new HasEncodingWriter(xsw, encoding);
ohair@286 335 }
ohair@286 336
mkos@384 337 @Override
ohair@286 338 public void doRecycle(XMLStreamWriter r) {
ohair@286 339 if (r instanceof HasEncodingWriter) {
ohair@286 340 r = ((HasEncodingWriter)r).getWriter();
ohair@286 341 }
ohair@286 342 if(zephyrClass.isInstance(r)) {
ohair@286 343 // this flushes the underlying stream, so it might cause chunking issue
ohair@286 344 try {
ohair@286 345 r.close();
ohair@286 346 } catch (XMLStreamException e) {
ohair@286 347 throw new WebServiceException(e);
ohair@286 348 }
ohair@286 349 pool.set(r);
ohair@286 350 }
ohair@286 351 if(r instanceof RecycleAware)
ohair@286 352 ((RecycleAware)r).onRecycled();
ohair@286 353 }
ohair@286 354 }
ohair@286 355
ohair@286 356 /**
ohair@286 357 *
ohair@286 358 * For {@link javax.xml.stream.XMLOutputFactory} is thread safe.
ohair@286 359 */
ohair@286 360 public static final class NoLock extends XMLStreamWriterFactory {
ohair@286 361 private final XMLOutputFactory xof;
ohair@286 362
ohair@286 363 public NoLock(XMLOutputFactory xof) {
ohair@286 364 this.xof = xof;
ohair@286 365 }
ohair@286 366
mkos@384 367 @Override
ohair@286 368 public XMLStreamWriter doCreate(OutputStream out) {
ohair@286 369 return doCreate(out, SOAPBindingCodec.UTF8_ENCODING);
ohair@286 370 }
ohair@286 371
mkos@384 372 @Override
ohair@286 373 public XMLStreamWriter doCreate(OutputStream out, String encoding) {
ohair@286 374 try {
ohair@286 375 XMLStreamWriter writer = xof.createXMLStreamWriter(out,encoding);
ohair@286 376 return new HasEncodingWriter(writer, encoding);
ohair@286 377 } catch (XMLStreamException e) {
ohair@286 378 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 379 }
ohair@286 380 }
ohair@286 381
mkos@384 382 @Override
ohair@286 383 public void doRecycle(XMLStreamWriter r) {
ohair@286 384 // no recycling
ohair@286 385 }
ohair@286 386
ohair@286 387 }
ohair@286 388
aefimov@1609 389 public static class HasEncodingWriter extends XMLStreamWriterFilter implements HasEncoding {
ohair@286 390 private final String encoding;
ohair@286 391
ohair@286 392 HasEncodingWriter(XMLStreamWriter writer, String encoding) {
ohair@286 393 super(writer);
ohair@286 394 this.encoding = encoding;
ohair@286 395 }
ohair@286 396
mkos@384 397 @Override
ohair@286 398 public String getEncoding() {
ohair@286 399 return encoding;
ohair@286 400 }
ohair@286 401
aefimov@1609 402 public XMLStreamWriter getWriter() {
ohair@286 403 return writer;
ohair@286 404 }
ohair@286 405 }
ohair@286 406 }

mercurial