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 Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,378 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, 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.Logger; 1.49 + 1.50 +/** 1.51 + * Factory for {@link XMLStreamWriter}. 1.52 + * 1.53 + * <p> 1.54 + * This wraps {@link XMLOutputFactory} and allows us to reuse {@link XMLStreamWriter} instances 1.55 + * when appropriate. 1.56 + * 1.57 + * @author Kohsuke Kawaguchi 1.58 + */ 1.59 +public abstract class XMLStreamWriterFactory { 1.60 + 1.61 + private static final Logger LOGGER = Logger.getLogger(XMLStreamWriterFactory.class.getName()); 1.62 + 1.63 + /** 1.64 + * Singleton instance. 1.65 + */ 1.66 + private static volatile @NotNull XMLStreamWriterFactory theInstance; 1.67 + 1.68 + 1.69 + static { 1.70 + XMLOutputFactory xof = null; 1.71 + if (Boolean.getBoolean(XMLStreamWriterFactory.class.getName()+".woodstox")) { 1.72 + try { 1.73 + xof = (XMLOutputFactory)Class.forName("com.ctc.wstx.stax.WstxOutputFactory").newInstance(); 1.74 + } catch (Exception e) { 1.75 + // Ignore and fallback to default XMLOutputFactory 1.76 + } 1.77 + } 1.78 + if (xof == null) { 1.79 + xof = XMLOutputFactory.newInstance(); 1.80 + } 1.81 + 1.82 + XMLStreamWriterFactory f=null; 1.83 + 1.84 + // this system property can be used to disable the pooling altogether, 1.85 + // in case someone hits an issue with pooling in the production system. 1.86 + if(!Boolean.getBoolean(XMLStreamWriterFactory.class.getName()+".noPool")) 1.87 + f = Zephyr.newInstance(xof); 1.88 + if(f==null) { 1.89 + // is this Woodstox? 1.90 + if(xof.getClass().getName().equals("com.ctc.wstx.stax.WstxOutputFactory")) 1.91 + f = new NoLock(xof); 1.92 + } 1.93 + if (f == null) 1.94 + f = new Default(xof); 1.95 + 1.96 + theInstance = f; 1.97 + LOGGER.fine("XMLStreamWriterFactory instance is = "+theInstance); 1.98 + } 1.99 + 1.100 + /** 1.101 + * See {@link #create(OutputStream)} for the contract. 1.102 + * This method may be invoked concurrently. 1.103 + */ 1.104 + public abstract XMLStreamWriter doCreate(OutputStream out); 1.105 + 1.106 + /** 1.107 + * See {@link #create(OutputStream,String)} for the contract. 1.108 + * This method may be invoked concurrently. 1.109 + */ 1.110 + public abstract XMLStreamWriter doCreate(OutputStream out, String encoding); 1.111 + 1.112 + /** 1.113 + * See {@link #recycle(XMLStreamWriter)} for the contract. 1.114 + * This method may be invoked concurrently. 1.115 + */ 1.116 + public abstract void doRecycle(XMLStreamWriter r); 1.117 + 1.118 + /** 1.119 + * Should be invoked when the code finished using an {@link XMLStreamWriter}. 1.120 + * 1.121 + * <p> 1.122 + * If the recycled instance implements {@link RecycleAware}, 1.123 + * {@link RecycleAware#onRecycled()} will be invoked to let the instance 1.124 + * know that it's being recycled. 1.125 + * 1.126 + * <p> 1.127 + * It is not a hard requirement to call this method on every {@link XMLStreamReader} 1.128 + * instance. Not doing so just reduces the performance by throwing away 1.129 + * possibly reusable instances. So the caller should always consider the effort 1.130 + * it takes to recycle vs the possible performance gain by doing so. 1.131 + * 1.132 + * <p> 1.133 + * This method may be invked by multiple threads concurrently. 1.134 + * 1.135 + * @param r 1.136 + * The {@link XMLStreamReader} instance that the caller finished using. 1.137 + * This could be any {@link XMLStreamReader} implementation, not just 1.138 + * the ones that were created from this factory. So the implementation 1.139 + * of this class needs to be aware of that. 1.140 + */ 1.141 + public static void recycle(XMLStreamWriter r) { 1.142 + get().doRecycle(r); 1.143 + } 1.144 + 1.145 + /** 1.146 + * Interface that can be implemented by {@link XMLStreamWriter} to 1.147 + * be notified when it's recycled. 1.148 + * 1.149 + * <p> 1.150 + * This provides a filtering {@link XMLStreamWriter} an opportunity to 1.151 + * recycle its inner {@link XMLStreamWriter}. 1.152 + */ 1.153 + public interface RecycleAware { 1.154 + void onRecycled(); 1.155 + } 1.156 + 1.157 + /** 1.158 + * Gets the singleton instance. 1.159 + */ 1.160 + public static @NotNull XMLStreamWriterFactory get() { 1.161 + return theInstance; 1.162 + } 1.163 + 1.164 + /** 1.165 + * Overrides the singleton {@link XMLStreamWriterFactory} instance that 1.166 + * the JAX-WS RI uses. 1.167 + * 1.168 + * @param f 1.169 + * must not be null. 1.170 + */ 1.171 + public static void set(@NotNull XMLStreamWriterFactory f) { 1.172 + if(f==null) throw new IllegalArgumentException(); 1.173 + theInstance = f; 1.174 + } 1.175 + 1.176 + /** 1.177 + * Short-cut for {@link #create(OutputStream, String)} with UTF-8. 1.178 + */ 1.179 + public static XMLStreamWriter create(OutputStream out) { 1.180 + return get().doCreate(out); 1.181 + } 1.182 + 1.183 + public static XMLStreamWriter create(OutputStream out, String encoding) { 1.184 + return get().doCreate(out, encoding); 1.185 + } 1.186 + 1.187 + /** 1.188 + * @deprecated 1.189 + * Use {@link #create(OutputStream)} 1.190 + */ 1.191 + public static XMLStreamWriter createXMLStreamWriter(OutputStream out) { 1.192 + return create(out); 1.193 + } 1.194 + 1.195 + /** 1.196 + * @deprecated 1.197 + * Use {@link #create(OutputStream, String)} 1.198 + */ 1.199 + public static XMLStreamWriter createXMLStreamWriter(OutputStream out, String encoding) { 1.200 + return create(out, encoding); 1.201 + } 1.202 + 1.203 + /** 1.204 + * @deprecated 1.205 + * Use {@link #create(OutputStream, String)}. The boolean flag was unused anyway. 1.206 + */ 1.207 + public static XMLStreamWriter createXMLStreamWriter(OutputStream out, String encoding, boolean declare) { 1.208 + return create(out,encoding); 1.209 + } 1.210 + 1.211 + /** 1.212 + * Default {@link XMLStreamWriterFactory} implementation 1.213 + * that can work with any {@link XMLOutputFactory}. 1.214 + * 1.215 + * <p> 1.216 + * {@link XMLOutputFactory} is not required to be thread-safe, so the 1.217 + * create method on this implementation is synchronized. 1.218 + */ 1.219 + public static final class Default extends XMLStreamWriterFactory { 1.220 + private final XMLOutputFactory xof; 1.221 + 1.222 + public Default(XMLOutputFactory xof) { 1.223 + this.xof = xof; 1.224 + } 1.225 + 1.226 + public XMLStreamWriter doCreate(OutputStream out) { 1.227 + return doCreate(out,"UTF-8"); 1.228 + } 1.229 + 1.230 + public synchronized XMLStreamWriter doCreate(OutputStream out, String encoding) { 1.231 + try { 1.232 + XMLStreamWriter writer = xof.createXMLStreamWriter(out,encoding); 1.233 + return new HasEncodingWriter(writer, encoding); 1.234 + } catch (XMLStreamException e) { 1.235 + throw new XMLReaderException("stax.cantCreate",e); 1.236 + } 1.237 + } 1.238 + 1.239 + public void doRecycle(XMLStreamWriter r) { 1.240 + // no recycling 1.241 + } 1.242 + } 1.243 + 1.244 + /** 1.245 + * {@link XMLStreamWriterFactory} implementation for Sun's StaX implementation. 1.246 + * 1.247 + * <p> 1.248 + * This implementation supports instance reuse. 1.249 + */ 1.250 + public static final class Zephyr extends XMLStreamWriterFactory { 1.251 + private final XMLOutputFactory xof; 1.252 + private final ThreadLocal<XMLStreamWriter> pool = new ThreadLocal<XMLStreamWriter>(); 1.253 + private final Method resetMethod; 1.254 + private final Method setOutputMethod; 1.255 + private final Class zephyrClass; 1.256 + 1.257 + public static XMLStreamWriterFactory newInstance(XMLOutputFactory xof) { 1.258 + try { 1.259 + Class<?> clazz = xof.createXMLStreamWriter(new StringWriter()).getClass(); 1.260 + 1.261 + if(!clazz.getName().startsWith("com.sun.xml.internal.stream.")) 1.262 + return null; // nope 1.263 + 1.264 + return new Zephyr(xof,clazz); 1.265 + } catch (XMLStreamException e) { 1.266 + return null; // impossible 1.267 + } catch (NoSuchMethodException e) { 1.268 + return null; // this xof wasn't Zephyr 1.269 + } 1.270 + } 1.271 + 1.272 + private Zephyr(XMLOutputFactory xof, Class clazz) throws NoSuchMethodException { 1.273 + this.xof = xof; 1.274 + 1.275 + zephyrClass = clazz; 1.276 + setOutputMethod = clazz.getMethod("setOutput", StreamResult.class, String.class); 1.277 + resetMethod = clazz.getMethod("reset"); 1.278 + } 1.279 + 1.280 + /** 1.281 + * Fetchs an instance from the pool if available, otherwise null. 1.282 + */ 1.283 + private @Nullable XMLStreamWriter fetch() { 1.284 + XMLStreamWriter sr = pool.get(); 1.285 + if(sr==null) return null; 1.286 + pool.set(null); 1.287 + return sr; 1.288 + } 1.289 + 1.290 + public XMLStreamWriter doCreate(OutputStream out) { 1.291 + return doCreate(out,"UTF-8"); 1.292 + } 1.293 + 1.294 + public XMLStreamWriter doCreate(OutputStream out, String encoding) { 1.295 + XMLStreamWriter xsw = fetch(); 1.296 + if(xsw!=null) { 1.297 + // try to reuse 1.298 + try { 1.299 + resetMethod.invoke(xsw); 1.300 + setOutputMethod.invoke(xsw,new StreamResult(out),encoding); 1.301 + } catch (IllegalAccessException e) { 1.302 + throw new XMLReaderException("stax.cantCreate",e); 1.303 + } catch (InvocationTargetException e) { 1.304 + throw new XMLReaderException("stax.cantCreate",e); 1.305 + } 1.306 + } else { 1.307 + // create a new instance 1.308 + try { 1.309 + xsw = xof.createXMLStreamWriter(out,encoding); 1.310 + } catch (XMLStreamException e) { 1.311 + throw new XMLReaderException("stax.cantCreate",e); 1.312 + } 1.313 + } 1.314 + return new HasEncodingWriter(xsw, encoding); 1.315 + } 1.316 + 1.317 + public void doRecycle(XMLStreamWriter r) { 1.318 + if (r instanceof HasEncodingWriter) { 1.319 + r = ((HasEncodingWriter)r).getWriter(); 1.320 + } 1.321 + if(zephyrClass.isInstance(r)) { 1.322 + // this flushes the underlying stream, so it might cause chunking issue 1.323 + try { 1.324 + r.close(); 1.325 + } catch (XMLStreamException e) { 1.326 + throw new WebServiceException(e); 1.327 + } 1.328 + pool.set(r); 1.329 + } 1.330 + if(r instanceof RecycleAware) 1.331 + ((RecycleAware)r).onRecycled(); 1.332 + } 1.333 + } 1.334 + 1.335 + /** 1.336 + * 1.337 + * For {@link javax.xml.stream.XMLOutputFactory} is thread safe. 1.338 + */ 1.339 + public static final class NoLock extends XMLStreamWriterFactory { 1.340 + private final XMLOutputFactory xof; 1.341 + 1.342 + public NoLock(XMLOutputFactory xof) { 1.343 + this.xof = xof; 1.344 + } 1.345 + 1.346 + public XMLStreamWriter doCreate(OutputStream out) { 1.347 + return doCreate(out, SOAPBindingCodec.UTF8_ENCODING); 1.348 + } 1.349 + 1.350 + public XMLStreamWriter doCreate(OutputStream out, String encoding) { 1.351 + try { 1.352 + XMLStreamWriter writer = xof.createXMLStreamWriter(out,encoding); 1.353 + return new HasEncodingWriter(writer, encoding); 1.354 + } catch (XMLStreamException e) { 1.355 + throw new XMLReaderException("stax.cantCreate",e); 1.356 + } 1.357 + } 1.358 + 1.359 + public void doRecycle(XMLStreamWriter r) { 1.360 + // no recycling 1.361 + } 1.362 + 1.363 + } 1.364 + 1.365 + private static class HasEncodingWriter extends XMLStreamWriterFilter implements HasEncoding { 1.366 + private final String encoding; 1.367 + 1.368 + HasEncodingWriter(XMLStreamWriter writer, String encoding) { 1.369 + super(writer); 1.370 + this.encoding = encoding; 1.371 + } 1.372 + 1.373 + public String getEncoding() { 1.374 + return encoding; 1.375 + } 1.376 + 1.377 + XMLStreamWriter getWriter() { 1.378 + return writer; 1.379 + } 1.380 + } 1.381 +}