1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/api/streaming/XMLStreamWriterFactory.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,406 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.api.streaming; 1.30 + 1.31 +import com.sun.istack.internal.NotNull; 1.32 +import com.sun.istack.internal.Nullable; 1.33 +import com.sun.xml.internal.ws.encoding.HasEncoding; 1.34 +import com.sun.xml.internal.ws.encoding.SOAPBindingCodec; 1.35 +import com.sun.xml.internal.ws.streaming.XMLReaderException; 1.36 +import com.sun.xml.internal.ws.util.xml.XMLStreamWriterFilter; 1.37 + 1.38 +import javax.xml.stream.XMLOutputFactory; 1.39 +import javax.xml.stream.XMLStreamException; 1.40 +import javax.xml.stream.XMLStreamReader; 1.41 +import javax.xml.stream.XMLStreamWriter; 1.42 +import javax.xml.transform.stream.StreamResult; 1.43 +import javax.xml.ws.WebServiceException; 1.44 +import java.io.OutputStream; 1.45 +import java.io.StringWriter; 1.46 +import java.lang.reflect.InvocationTargetException; 1.47 +import java.lang.reflect.Method; 1.48 +import java.util.logging.Level; 1.49 +import java.util.logging.Logger; 1.50 + 1.51 +/** 1.52 + * Factory for {@link XMLStreamWriter}. 1.53 + * 1.54 + * <p> 1.55 + * This wraps {@link XMLOutputFactory} and allows us to reuse {@link XMLStreamWriter} instances 1.56 + * when appropriate. 1.57 + * 1.58 + * @author Kohsuke Kawaguchi 1.59 + */ 1.60 +@SuppressWarnings("StaticNonFinalUsedInInitialization") 1.61 +public abstract class XMLStreamWriterFactory { 1.62 + 1.63 + private static final Logger LOGGER = Logger.getLogger(XMLStreamWriterFactory.class.getName()); 1.64 + 1.65 + /** 1.66 + * Singleton instance. 1.67 + */ 1.68 + private static volatile ContextClassloaderLocal<XMLStreamWriterFactory> writerFactory = 1.69 + new ContextClassloaderLocal<XMLStreamWriterFactory>() { 1.70 + 1.71 + @Override 1.72 + protected XMLStreamWriterFactory initialValue() { 1.73 + XMLOutputFactory xof = null; 1.74 + if (Boolean.getBoolean(XMLStreamWriterFactory.class.getName()+".woodstox")) { 1.75 + try { 1.76 + xof = (XMLOutputFactory)Class.forName("com.ctc.wstx.stax.WstxOutputFactory").newInstance(); 1.77 + } catch (Exception e) { 1.78 + // Ignore and fallback to default XMLOutputFactory 1.79 + } 1.80 + } 1.81 + if (xof == null) { 1.82 + xof = XMLOutputFactory.newInstance(); 1.83 + } 1.84 + 1.85 + XMLStreamWriterFactory f=null; 1.86 + 1.87 + // this system property can be used to disable the pooling altogether, 1.88 + // in case someone hits an issue with pooling in the production system. 1.89 + if (!Boolean.getBoolean(XMLStreamWriterFactory.class.getName()+".noPool")) { 1.90 + try { 1.91 + Class<?> clazz = xof.createXMLStreamWriter(new StringWriter()).getClass(); 1.92 + if (clazz.getName().startsWith("com.sun.xml.internal.stream.")) { 1.93 + f = new Zephyr(xof,clazz); 1.94 + } 1.95 + } catch (XMLStreamException ex) { 1.96 + Logger.getLogger(XMLStreamWriterFactory.class.getName()).log(Level.INFO, null, ex); 1.97 + } catch (NoSuchMethodException ex) { 1.98 + Logger.getLogger(XMLStreamWriterFactory.class.getName()).log(Level.INFO, null, ex); 1.99 + } 1.100 + } 1.101 + 1.102 + if(f==null) { 1.103 + // is this Woodstox? 1.104 + if(xof.getClass().getName().equals("com.ctc.wstx.stax.WstxOutputFactory")) 1.105 + f = new NoLock(xof); 1.106 + } 1.107 + if (f == null) 1.108 + f = new Default(xof); 1.109 + 1.110 + if (LOGGER.isLoggable(Level.FINE)) { 1.111 + LOGGER.log(Level.FINE, "XMLStreamWriterFactory instance is = {0}", f); 1.112 + } 1.113 + return f; 1.114 + } 1.115 + }; 1.116 + 1.117 + /** 1.118 + * See {@link #create(OutputStream)} for the contract. 1.119 + * This method may be invoked concurrently. 1.120 + */ 1.121 + public abstract XMLStreamWriter doCreate(OutputStream out); 1.122 + 1.123 + /** 1.124 + * See {@link #create(OutputStream,String)} for the contract. 1.125 + * This method may be invoked concurrently. 1.126 + */ 1.127 + public abstract XMLStreamWriter doCreate(OutputStream out, String encoding); 1.128 + 1.129 + /** 1.130 + * See {@link #recycle(XMLStreamWriter)} for the contract. 1.131 + * This method may be invoked concurrently. 1.132 + */ 1.133 + public abstract void doRecycle(XMLStreamWriter r); 1.134 + 1.135 + /** 1.136 + * Should be invoked when the code finished using an {@link XMLStreamWriter}. 1.137 + * 1.138 + * <p> 1.139 + * If the recycled instance implements {@link RecycleAware}, 1.140 + * {@link RecycleAware#onRecycled()} will be invoked to let the instance 1.141 + * know that it's being recycled. 1.142 + * 1.143 + * <p> 1.144 + * It is not a hard requirement to call this method on every {@link XMLStreamReader} 1.145 + * instance. Not doing so just reduces the performance by throwing away 1.146 + * possibly reusable instances. So the caller should always consider the effort 1.147 + * it takes to recycle vs the possible performance gain by doing so. 1.148 + * 1.149 + * <p> 1.150 + * This method may be invked by multiple threads concurrently. 1.151 + * 1.152 + * @param r 1.153 + * The {@link XMLStreamReader} instance that the caller finished using. 1.154 + * This could be any {@link XMLStreamReader} implementation, not just 1.155 + * the ones that were created from this factory. So the implementation 1.156 + * of this class needs to be aware of that. 1.157 + */ 1.158 + public static void recycle(XMLStreamWriter r) { 1.159 + get().doRecycle(r); 1.160 + } 1.161 + 1.162 + /** 1.163 + * Interface that can be implemented by {@link XMLStreamWriter} to 1.164 + * be notified when it's recycled. 1.165 + * 1.166 + * <p> 1.167 + * This provides a filtering {@link XMLStreamWriter} an opportunity to 1.168 + * recycle its inner {@link XMLStreamWriter}. 1.169 + */ 1.170 + public interface RecycleAware { 1.171 + void onRecycled(); 1.172 + } 1.173 + 1.174 + /** 1.175 + * Gets the singleton instance. 1.176 + */ 1.177 + public static @NotNull XMLStreamWriterFactory get() { 1.178 + return writerFactory.get(); 1.179 + } 1.180 + 1.181 + /** 1.182 + * Overrides the singleton {@link XMLStreamWriterFactory} instance that 1.183 + * the JAX-WS RI uses. 1.184 + * 1.185 + * @param f 1.186 + * must not be null. 1.187 + */ 1.188 + @SuppressWarnings({"null", "ConstantConditions"}) 1.189 + public static void set(@NotNull XMLStreamWriterFactory f) { 1.190 + if(f==null) throw new IllegalArgumentException(); 1.191 + writerFactory.set(f); 1.192 + } 1.193 + 1.194 + /** 1.195 + * Short-cut for {@link #create(OutputStream, String)} with UTF-8. 1.196 + */ 1.197 + public static XMLStreamWriter create(OutputStream out) { 1.198 + return get().doCreate(out); 1.199 + } 1.200 + 1.201 + public static XMLStreamWriter create(OutputStream out, String encoding) { 1.202 + return get().doCreate(out, encoding); 1.203 + } 1.204 + 1.205 + /** 1.206 + * @deprecated 1.207 + * Use {@link #create(OutputStream)} 1.208 + */ 1.209 + public static XMLStreamWriter createXMLStreamWriter(OutputStream out) { 1.210 + return create(out); 1.211 + } 1.212 + 1.213 + /** 1.214 + * @deprecated 1.215 + * Use {@link #create(OutputStream, String)} 1.216 + */ 1.217 + public static XMLStreamWriter createXMLStreamWriter(OutputStream out, String encoding) { 1.218 + return create(out, encoding); 1.219 + } 1.220 + 1.221 + /** 1.222 + * @deprecated 1.223 + * Use {@link #create(OutputStream, String)}. The boolean flag was unused anyway. 1.224 + */ 1.225 + public static XMLStreamWriter createXMLStreamWriter(OutputStream out, String encoding, boolean declare) { 1.226 + return create(out,encoding); 1.227 + } 1.228 + 1.229 + /** 1.230 + * Default {@link XMLStreamWriterFactory} implementation 1.231 + * that can work with any {@link XMLOutputFactory}. 1.232 + * 1.233 + * <p> 1.234 + * {@link XMLOutputFactory} is not required to be thread-safe, so the 1.235 + * create method on this implementation is synchronized. 1.236 + */ 1.237 + public static final class Default extends XMLStreamWriterFactory { 1.238 + private final XMLOutputFactory xof; 1.239 + 1.240 + public Default(XMLOutputFactory xof) { 1.241 + this.xof = xof; 1.242 + } 1.243 + 1.244 + @Override 1.245 + public XMLStreamWriter doCreate(OutputStream out) { 1.246 + return doCreate(out,"UTF-8"); 1.247 + } 1.248 + 1.249 + @Override 1.250 + public synchronized XMLStreamWriter doCreate(OutputStream out, String encoding) { 1.251 + try { 1.252 + XMLStreamWriter writer = xof.createXMLStreamWriter(out,encoding); 1.253 + return new HasEncodingWriter(writer, encoding); 1.254 + } catch (XMLStreamException e) { 1.255 + throw new XMLReaderException("stax.cantCreate",e); 1.256 + } 1.257 + } 1.258 + 1.259 + @Override 1.260 + public void doRecycle(XMLStreamWriter r) { 1.261 + // no recycling 1.262 + } 1.263 + } 1.264 + 1.265 + /** 1.266 + * {@link XMLStreamWriterFactory} implementation for Sun's StaX implementation. 1.267 + * 1.268 + * <p> 1.269 + * This implementation supports instance reuse. 1.270 + */ 1.271 + public static final class Zephyr extends XMLStreamWriterFactory { 1.272 + private final XMLOutputFactory xof; 1.273 + private final ThreadLocal<XMLStreamWriter> pool = new ThreadLocal<XMLStreamWriter>(); 1.274 + private final Method resetMethod; 1.275 + private final Method setOutputMethod; 1.276 + private final Class zephyrClass; 1.277 + 1.278 + public static XMLStreamWriterFactory newInstance(XMLOutputFactory xof) { 1.279 + try { 1.280 + Class<?> clazz = xof.createXMLStreamWriter(new StringWriter()).getClass(); 1.281 + 1.282 + if(!clazz.getName().startsWith("com.sun.xml.internal.stream.")) 1.283 + return null; // nope 1.284 + 1.285 + return new Zephyr(xof,clazz); 1.286 + } catch (XMLStreamException e) { 1.287 + return null; // impossible 1.288 + } catch (NoSuchMethodException e) { 1.289 + return null; // this xof wasn't Zephyr 1.290 + } 1.291 + } 1.292 + 1.293 + private Zephyr(XMLOutputFactory xof, Class clazz) throws NoSuchMethodException { 1.294 + this.xof = xof; 1.295 + 1.296 + zephyrClass = clazz; 1.297 + setOutputMethod = clazz.getMethod("setOutput", StreamResult.class, String.class); 1.298 + resetMethod = clazz.getMethod("reset"); 1.299 + } 1.300 + 1.301 + /** 1.302 + * Fetchs an instance from the pool if available, otherwise null. 1.303 + */ 1.304 + private @Nullable XMLStreamWriter fetch() { 1.305 + XMLStreamWriter sr = pool.get(); 1.306 + if(sr==null) return null; 1.307 + pool.set(null); 1.308 + return sr; 1.309 + } 1.310 + 1.311 + @Override 1.312 + public XMLStreamWriter doCreate(OutputStream out) { 1.313 + return doCreate(out,"UTF-8"); 1.314 + } 1.315 + 1.316 + @Override 1.317 + public XMLStreamWriter doCreate(OutputStream out, String encoding) { 1.318 + XMLStreamWriter xsw = fetch(); 1.319 + if(xsw!=null) { 1.320 + // try to reuse 1.321 + try { 1.322 + resetMethod.invoke(xsw); 1.323 + setOutputMethod.invoke(xsw,new StreamResult(out),encoding); 1.324 + } catch (IllegalAccessException e) { 1.325 + throw new XMLReaderException("stax.cantCreate",e); 1.326 + } catch (InvocationTargetException e) { 1.327 + throw new XMLReaderException("stax.cantCreate",e); 1.328 + } 1.329 + } else { 1.330 + // create a new instance 1.331 + try { 1.332 + xsw = xof.createXMLStreamWriter(out,encoding); 1.333 + } catch (XMLStreamException e) { 1.334 + throw new XMLReaderException("stax.cantCreate",e); 1.335 + } 1.336 + } 1.337 + return new HasEncodingWriter(xsw, encoding); 1.338 + } 1.339 + 1.340 + @Override 1.341 + public void doRecycle(XMLStreamWriter r) { 1.342 + if (r instanceof HasEncodingWriter) { 1.343 + r = ((HasEncodingWriter)r).getWriter(); 1.344 + } 1.345 + if(zephyrClass.isInstance(r)) { 1.346 + // this flushes the underlying stream, so it might cause chunking issue 1.347 + try { 1.348 + r.close(); 1.349 + } catch (XMLStreamException e) { 1.350 + throw new WebServiceException(e); 1.351 + } 1.352 + pool.set(r); 1.353 + } 1.354 + if(r instanceof RecycleAware) 1.355 + ((RecycleAware)r).onRecycled(); 1.356 + } 1.357 + } 1.358 + 1.359 + /** 1.360 + * 1.361 + * For {@link javax.xml.stream.XMLOutputFactory} is thread safe. 1.362 + */ 1.363 + public static final class NoLock extends XMLStreamWriterFactory { 1.364 + private final XMLOutputFactory xof; 1.365 + 1.366 + public NoLock(XMLOutputFactory xof) { 1.367 + this.xof = xof; 1.368 + } 1.369 + 1.370 + @Override 1.371 + public XMLStreamWriter doCreate(OutputStream out) { 1.372 + return doCreate(out, SOAPBindingCodec.UTF8_ENCODING); 1.373 + } 1.374 + 1.375 + @Override 1.376 + public XMLStreamWriter doCreate(OutputStream out, String encoding) { 1.377 + try { 1.378 + XMLStreamWriter writer = xof.createXMLStreamWriter(out,encoding); 1.379 + return new HasEncodingWriter(writer, encoding); 1.380 + } catch (XMLStreamException e) { 1.381 + throw new XMLReaderException("stax.cantCreate",e); 1.382 + } 1.383 + } 1.384 + 1.385 + @Override 1.386 + public void doRecycle(XMLStreamWriter r) { 1.387 + // no recycling 1.388 + } 1.389 + 1.390 + } 1.391 + 1.392 + private static class HasEncodingWriter extends XMLStreamWriterFilter implements HasEncoding { 1.393 + private final String encoding; 1.394 + 1.395 + HasEncodingWriter(XMLStreamWriter writer, String encoding) { 1.396 + super(writer); 1.397 + this.encoding = encoding; 1.398 + } 1.399 + 1.400 + @Override 1.401 + public String getEncoding() { 1.402 + return encoding; 1.403 + } 1.404 + 1.405 + XMLStreamWriter getWriter() { 1.406 + return writer; 1.407 + } 1.408 + } 1.409 +}