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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 384
8f2986ff0235
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial