src/share/jaxws_classes/com/sun/xml/internal/ws/api/streaming/XMLStreamReaderFactory.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 397
b99d7e355d4b
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, 2013, 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.streaming.XMLReaderException;
alanb@368 31 import com.sun.xml.internal.ws.util.xml.XmlUtil;
ohair@286 32 import org.xml.sax.InputSource;
ohair@286 33
ohair@286 34 import javax.xml.stream.XMLInputFactory;
ohair@286 35 import javax.xml.stream.XMLStreamException;
ohair@286 36 import javax.xml.stream.XMLStreamReader;
alanb@368 37 import java.io.*;
ohair@286 38 import java.lang.reflect.InvocationTargetException;
ohair@286 39 import java.lang.reflect.Method;
ohair@286 40 import java.net.URL;
alanb@368 41 import java.security.AccessController;
alanb@368 42 import java.util.logging.Level;
ohair@286 43 import java.util.logging.Logger;
ohair@286 44
ohair@286 45 /**
ohair@286 46 * Factory for {@link XMLStreamReader}.
ohair@286 47 *
ohair@286 48 * <p>
ohair@286 49 * This wraps {@link XMLInputFactory} and allows us to reuse {@link XMLStreamReader} instances
ohair@286 50 * when appropriate.
ohair@286 51 *
ohair@286 52 * @author Kohsuke Kawaguchi
ohair@286 53 */
alanb@368 54 @SuppressWarnings("StaticNonFinalUsedInInitialization")
ohair@286 55 public abstract class XMLStreamReaderFactory {
ohair@286 56
ohair@286 57 private static final Logger LOGGER = Logger.getLogger(XMLStreamReaderFactory.class.getName());
ohair@286 58
ohair@286 59 /**
ohair@286 60 * Singleton instance.
ohair@286 61 */
ohair@286 62 private static volatile @NotNull XMLStreamReaderFactory theInstance;
ohair@286 63
ohair@286 64 static {
ohair@286 65 XMLInputFactory xif = getXMLInputFactory();
ohair@286 66 XMLStreamReaderFactory f=null;
ohair@286 67
ohair@286 68 // this system property can be used to disable the pooling altogether,
ohair@286 69 // in case someone hits an issue with pooling in the production system.
alanb@368 70 if(!getProperty(XMLStreamReaderFactory.class.getName()+".noPool")) {
ohair@286 71 f = Zephyr.newInstance(xif);
alanb@368 72 }
ohair@286 73
ohair@286 74 if(f==null) {
ohair@286 75 // is this Woodstox?
alanb@368 76 if (xif.getClass().getName().equals("com.ctc.wstx.stax.WstxInputFactory")) {
ohair@286 77 f = new Woodstox(xif);
alanb@368 78 }
ohair@286 79 }
ohair@286 80
alanb@368 81 if (f==null) {
ohair@286 82 f = new Default();
alanb@368 83 }
ohair@286 84
ohair@286 85 theInstance = f;
alanb@368 86 LOGGER.log(Level.FINE, "XMLStreamReaderFactory instance is = {0}", theInstance);
ohair@286 87 }
ohair@286 88
ohair@286 89 private static XMLInputFactory getXMLInputFactory() {
ohair@286 90 XMLInputFactory xif = null;
ohair@286 91 if (getProperty(XMLStreamReaderFactory.class.getName()+".woodstox")) {
ohair@286 92 try {
ohair@286 93 xif = (XMLInputFactory)Class.forName("com.ctc.wstx.stax.WstxInputFactory").newInstance();
ohair@286 94 } catch (Exception e) {
ohair@286 95 // Ignore and fallback to default XMLInputFactory
ohair@286 96 }
ohair@286 97 }
ohair@286 98 if (xif == null) {
alanb@368 99 xif = XmlUtil.newXMLInputFactory(true);
ohair@286 100 }
ohair@286 101 xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
ohair@286 102 xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
ohair@286 103 xif.setProperty(XMLInputFactory.IS_COALESCING, true);
ohair@286 104 return xif;
ohair@286 105 }
ohair@286 106
ohair@286 107
ohair@286 108 /**
ohair@286 109 * Overrides the singleton {@link XMLStreamReaderFactory} instance that
ohair@286 110 * the JAX-WS RI uses.
ohair@286 111 */
ohair@286 112 public static void set(XMLStreamReaderFactory f) {
alanb@368 113 if(f==null) {
alanb@368 114 throw new IllegalArgumentException();
alanb@368 115 }
ohair@286 116 theInstance = f;
ohair@286 117 }
ohair@286 118
ohair@286 119 public static XMLStreamReaderFactory get() {
ohair@286 120 return theInstance;
ohair@286 121 }
ohair@286 122
ohair@286 123 public static XMLStreamReader create(InputSource source, boolean rejectDTDs) {
ohair@286 124 try {
ohair@286 125 // Char stream available?
ohair@286 126 if (source.getCharacterStream() != null) {
ohair@286 127 return get().doCreate(source.getSystemId(), source.getCharacterStream(), rejectDTDs);
ohair@286 128 }
ohair@286 129
ohair@286 130 // Byte stream available?
ohair@286 131 if (source.getByteStream() != null) {
ohair@286 132 return get().doCreate(source.getSystemId(), source.getByteStream(), rejectDTDs);
ohair@286 133 }
ohair@286 134
ohair@286 135 // Otherwise, open URI
ohair@286 136 return get().doCreate(source.getSystemId(), new URL(source.getSystemId()).openStream(),rejectDTDs);
ohair@286 137 } catch (IOException e) {
ohair@286 138 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 139 }
ohair@286 140 }
ohair@286 141
ohair@286 142 public static XMLStreamReader create(@Nullable String systemId, InputStream in, boolean rejectDTDs) {
ohair@286 143 return get().doCreate(systemId,in,rejectDTDs);
ohair@286 144 }
ohair@286 145
ohair@286 146 public static XMLStreamReader create(@Nullable String systemId, InputStream in, @Nullable String encoding, boolean rejectDTDs) {
ohair@286 147 return (encoding == null)
ohair@286 148 ? create(systemId, in, rejectDTDs)
ohair@286 149 : get().doCreate(systemId,in,encoding,rejectDTDs);
ohair@286 150 }
ohair@286 151
ohair@286 152 public static XMLStreamReader create(@Nullable String systemId, Reader reader, boolean rejectDTDs) {
ohair@286 153 return get().doCreate(systemId,reader,rejectDTDs);
ohair@286 154 }
ohair@286 155
ohair@286 156 /**
ohair@286 157 * Should be invoked when the code finished using an {@link XMLStreamReader}.
ohair@286 158 *
ohair@286 159 * <p>
ohair@286 160 * If the recycled instance implements {@link RecycleAware},
ohair@286 161 * {@link RecycleAware#onRecycled()} will be invoked to let the instance
ohair@286 162 * know that it's being recycled.
ohair@286 163 *
ohair@286 164 * <p>
ohair@286 165 * It is not a hard requirement to call this method on every {@link XMLStreamReader}
ohair@286 166 * instance. Not doing so just reduces the performance by throwing away
ohair@286 167 * possibly reusable instances. So the caller should always consider the effort
ohair@286 168 * it takes to recycle vs the possible performance gain by doing so.
ohair@286 169 *
ohair@286 170 * <p>
ohair@286 171 * This method may be invked by multiple threads concurrently.
ohair@286 172 *
ohair@286 173 * @param r
ohair@286 174 * The {@link XMLStreamReader} instance that the caller finished using.
ohair@286 175 * This could be any {@link XMLStreamReader} implementation, not just
ohair@286 176 * the ones that were created from this factory. So the implementation
ohair@286 177 * of this class needs to be aware of that.
ohair@286 178 */
ohair@286 179 public static void recycle(XMLStreamReader r) {
ohair@286 180 get().doRecycle(r);
ohair@286 181 if (r instanceof RecycleAware) {
ohair@286 182 ((RecycleAware)r).onRecycled();
ohair@286 183 }
ohair@286 184 }
ohair@286 185
ohair@286 186 // implementations
ohair@286 187
ohair@286 188 public abstract XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs);
ohair@286 189
ohair@286 190 private XMLStreamReader doCreate(String systemId, InputStream in, @NotNull String encoding, boolean rejectDTDs) {
ohair@286 191 Reader reader;
ohair@286 192 try {
ohair@286 193 reader = new InputStreamReader(in, encoding);
ohair@286 194 } catch(UnsupportedEncodingException ue) {
ohair@286 195 throw new XMLReaderException("stax.cantCreate", ue);
ohair@286 196 }
ohair@286 197 return doCreate(systemId, reader, rejectDTDs);
ohair@286 198 }
ohair@286 199
ohair@286 200 public abstract XMLStreamReader doCreate(String systemId, Reader reader, boolean rejectDTDs);
ohair@286 201
ohair@286 202 public abstract void doRecycle(XMLStreamReader r);
ohair@286 203
ohair@286 204 /**
ohair@286 205 * Interface that can be implemented by {@link XMLStreamReader} to
ohair@286 206 * be notified when it's recycled.
ohair@286 207 *
ohair@286 208 * <p>
ohair@286 209 * This provides a filtering {@link XMLStreamReader} an opportunity to
ohair@286 210 * recycle its inner {@link XMLStreamReader}.
ohair@286 211 */
ohair@286 212 public interface RecycleAware {
ohair@286 213 void onRecycled();
ohair@286 214 }
ohair@286 215
ohair@286 216 /**
ohair@286 217 * {@link XMLStreamReaderFactory} implementation for SJSXP/JAXP RI.
ohair@286 218 */
alanb@368 219 private static final class Zephyr extends XMLStreamReaderFactory {
ohair@286 220 private final XMLInputFactory xif;
ohair@286 221
ohair@286 222 private final ThreadLocal<XMLStreamReader> pool = new ThreadLocal<XMLStreamReader>();
ohair@286 223
ohair@286 224 /**
ohair@286 225 * Sun StAX impl <code>XMLReaderImpl.setInputSource()</code> method via reflection.
ohair@286 226 */
ohair@286 227 private final Method setInputSourceMethod;
ohair@286 228
ohair@286 229 /**
ohair@286 230 * Sun StAX impl <code>XMLReaderImpl.reset()</code> method via reflection.
ohair@286 231 */
ohair@286 232 private final Method resetMethod;
ohair@286 233
ohair@286 234 /**
ohair@286 235 * The Sun StAX impl's {@link XMLStreamReader} implementation clas.
ohair@286 236 */
ohair@286 237 private final Class zephyrClass;
ohair@286 238
ohair@286 239 /**
ohair@286 240 * Creates {@link Zephyr} instance if the given {@link XMLInputFactory} is the one
ohair@286 241 * from Zephyr.
ohair@286 242 */
ohair@286 243 public static @Nullable
ohair@286 244 XMLStreamReaderFactory newInstance(XMLInputFactory xif) {
ohair@286 245 // check if this is from Zephyr
ohair@286 246 try {
ohair@286 247 Class<?> clazz = xif.createXMLStreamReader(new StringReader("<foo/>")).getClass();
ohair@286 248 // JDK has different XMLStreamReader impl class. Even if we check for that,
ohair@286 249 // it doesn't have setInputSource(InputSource). Let it use Default
ohair@286 250 if(!(clazz.getName().startsWith("com.sun.xml.internal.stream.")) )
ohair@286 251 return null; // nope
ohair@286 252 return new Zephyr(xif,clazz);
ohair@286 253 } catch (NoSuchMethodException e) {
ohair@286 254 return null; // this factory is not for zephyr
ohair@286 255 } catch (XMLStreamException e) {
ohair@286 256 return null; // impossible to fail to parse <foo/>, but anyway
ohair@286 257 }
ohair@286 258 }
ohair@286 259
ohair@286 260 public Zephyr(XMLInputFactory xif, Class clazz) throws NoSuchMethodException {
ohair@286 261 zephyrClass = clazz;
ohair@286 262 setInputSourceMethod = clazz.getMethod("setInputSource", InputSource.class);
ohair@286 263 resetMethod = clazz.getMethod("reset");
ohair@286 264
ohair@286 265 try {
ohair@286 266 // Turn OFF internal factory caching in Zephyr.
ohair@286 267 // Santiago told me that this makes it thread-safe.
ohair@286 268 xif.setProperty("reuse-instance", false);
ohair@286 269 } catch (IllegalArgumentException e) {
ohair@286 270 // falls through
ohair@286 271 }
ohair@286 272 this.xif = xif;
ohair@286 273 }
ohair@286 274
ohair@286 275 /**
ohair@286 276 * Fetchs an instance from the pool if available, otherwise null.
ohair@286 277 */
ohair@286 278 private @Nullable XMLStreamReader fetch() {
ohair@286 279 XMLStreamReader sr = pool.get();
ohair@286 280 if(sr==null) return null;
ohair@286 281 pool.set(null);
ohair@286 282 return sr;
ohair@286 283 }
ohair@286 284
ohair@286 285 public void doRecycle(XMLStreamReader r) {
ohair@286 286 if(zephyrClass.isInstance(r))
ohair@286 287 pool.set(r);
ohair@286 288 }
ohair@286 289
ohair@286 290 public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) {
ohair@286 291 try {
ohair@286 292 XMLStreamReader xsr = fetch();
ohair@286 293 if(xsr==null)
ohair@286 294 return xif.createXMLStreamReader(systemId,in);
ohair@286 295
ohair@286 296 // try re-using this instance.
ohair@286 297 InputSource is = new InputSource(systemId);
ohair@286 298 is.setByteStream(in);
ohair@286 299 reuse(xsr,is);
ohair@286 300 return xsr;
ohair@286 301 } catch (IllegalAccessException e) {
ohair@286 302 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 303 } catch (InvocationTargetException e) {
ohair@286 304 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 305 } catch (XMLStreamException e) {
ohair@286 306 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 307 }
ohair@286 308 }
ohair@286 309
ohair@286 310 public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) {
ohair@286 311 try {
ohair@286 312 XMLStreamReader xsr = fetch();
ohair@286 313 if(xsr==null)
ohair@286 314 return xif.createXMLStreamReader(systemId,in);
ohair@286 315
ohair@286 316 // try re-using this instance.
ohair@286 317 InputSource is = new InputSource(systemId);
ohair@286 318 is.setCharacterStream(in);
ohair@286 319 reuse(xsr,is);
ohair@286 320 return xsr;
ohair@286 321 } catch (IllegalAccessException e) {
ohair@286 322 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 323 } catch (InvocationTargetException e) {
ohair@286 324 Throwable cause = e.getCause();
ohair@286 325 if (cause == null) {
ohair@286 326 cause = e;
ohair@286 327 }
ohair@286 328 throw new XMLReaderException("stax.cantCreate", cause);
ohair@286 329 } catch (XMLStreamException e) {
ohair@286 330 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 331 }
ohair@286 332 }
ohair@286 333
ohair@286 334 private void reuse(XMLStreamReader xsr, InputSource in) throws IllegalAccessException, InvocationTargetException {
ohair@286 335 resetMethod.invoke(xsr);
ohair@286 336 setInputSourceMethod.invoke(xsr,in);
ohair@286 337 }
ohair@286 338 }
ohair@286 339
ohair@286 340 /**
ohair@286 341 * Default {@link XMLStreamReaderFactory} implementation
ohair@286 342 * that can work with any {@link XMLInputFactory}.
ohair@286 343 *
ohair@286 344 * <p>
ohair@286 345 * {@link XMLInputFactory} is not required to be thread-safe, but
ohair@286 346 * if the create method on this implementation is synchronized,
ohair@286 347 * it may run into (see <a href="https://jax-ws.dev.java.net/issues/show_bug.cgi?id=555">
ohair@286 348 * race condition</a>). Hence, using a XMLInputFactory per theread.
ohair@286 349 */
ohair@286 350 public static final class Default extends XMLStreamReaderFactory {
ohair@286 351
ohair@286 352 private final ThreadLocal<XMLInputFactory> xif = new ThreadLocal<XMLInputFactory>() {
ohair@286 353 @Override
ohair@286 354 public XMLInputFactory initialValue() {
ohair@286 355 return getXMLInputFactory();
ohair@286 356 }
ohair@286 357 };
ohair@286 358
ohair@286 359 public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) {
ohair@286 360 try {
ohair@286 361 return xif.get().createXMLStreamReader(systemId,in);
ohair@286 362 } catch (XMLStreamException e) {
ohair@286 363 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 364 }
ohair@286 365 }
ohair@286 366
ohair@286 367 public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) {
ohair@286 368 try {
ohair@286 369 return xif.get().createXMLStreamReader(systemId,in);
ohair@286 370 } catch (XMLStreamException e) {
ohair@286 371 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 372 }
ohair@286 373 }
ohair@286 374
ohair@286 375 public void doRecycle(XMLStreamReader r) {
ohair@286 376 // there's no way to recycle with the default StAX API.
ohair@286 377 }
ohair@286 378
ohair@286 379 }
ohair@286 380
ohair@286 381 /**
ohair@286 382 * Similar to {@link Default} but doesn't do any synchronization.
ohair@286 383 *
ohair@286 384 * <p>
ohair@286 385 * This is useful when you know your {@link XMLInputFactory} is thread-safe by itself.
ohair@286 386 */
ohair@286 387 public static class NoLock extends XMLStreamReaderFactory {
ohair@286 388 private final XMLInputFactory xif;
ohair@286 389
ohair@286 390 public NoLock(XMLInputFactory xif) {
ohair@286 391 this.xif = xif;
ohair@286 392 }
ohair@286 393
ohair@286 394 public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) {
ohair@286 395 try {
ohair@286 396 return xif.createXMLStreamReader(systemId,in);
ohair@286 397 } catch (XMLStreamException e) {
ohair@286 398 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 399 }
ohair@286 400 }
ohair@286 401
ohair@286 402 public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) {
ohair@286 403 try {
ohair@286 404 return xif.createXMLStreamReader(systemId,in);
ohair@286 405 } catch (XMLStreamException e) {
ohair@286 406 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 407 }
ohair@286 408 }
ohair@286 409
ohair@286 410 public void doRecycle(XMLStreamReader r) {
ohair@286 411 // there's no way to recycle with the default StAX API.
ohair@286 412 }
ohair@286 413 }
ohair@286 414
ohair@286 415 /**
ohair@286 416 * Handles Woodstox's XIF but set properties to do the string interning.
ohair@286 417 * Woodstox {@link XMLInputFactory} is thread safe.
ohair@286 418 */
ohair@286 419 public static final class Woodstox extends NoLock {
ohair@286 420 public Woodstox(XMLInputFactory xif) {
ohair@286 421 super(xif);
ohair@286 422 xif.setProperty("org.codehaus.stax2.internNsUris",true);
ohair@286 423 }
ohair@286 424
ohair@286 425 public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) {
ohair@286 426 return super.doCreate(systemId, in, rejectDTDs);
ohair@286 427 }
ohair@286 428
ohair@286 429 public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) {
ohair@286 430 return super.doCreate(systemId, in, rejectDTDs);
ohair@286 431 }
ohair@286 432 }
ohair@286 433
ohair@286 434 private static Boolean getProperty(final String prop) {
ohair@286 435 return AccessController.doPrivileged(
ohair@286 436 new java.security.PrivilegedAction<Boolean>() {
ohair@286 437 public Boolean run() {
ohair@286 438 String value = System.getProperty(prop);
ohair@286 439 return value != null ? Boolean.valueOf(value) : Boolean.FALSE;
ohair@286 440 }
ohair@286 441 }
ohair@286 442 );
ohair@286 443 }
ohair@286 444 }

mercurial