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

Thu, 26 Jul 2018 16:06:47 +0800

author
aoqi
date
Thu, 26 Jul 2018 16:06:47 +0800
changeset 1620
6df7b161ae4a
parent 1609
09b083e0759c
parent 637
9c07ef4934dd
permissions
-rw-r--r--

Merge

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

mercurial