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

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 558
d950f4a0753b
parent 515
6cd506508147
child 637
9c07ef4934dd
permissions
-rw-r--r--

8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu

ohair@286 1 /*
mkos@515 2 * Copyright (c) 1997, 2014, 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;
mkos@397 37 import java.io.IOException;
mkos@397 38 import java.io.InputStream;
mkos@397 39 import java.io.InputStreamReader;
mkos@397 40 import java.io.Reader;
mkos@397 41 import java.io.StringReader;
mkos@397 42 import java.io.UnsupportedEncodingException;
ohair@286 43 import java.lang.reflect.InvocationTargetException;
ohair@286 44 import java.lang.reflect.Method;
ohair@286 45 import java.net.URL;
alanb@368 46 import java.security.AccessController;
alanb@368 47 import java.util.logging.Level;
ohair@286 48 import java.util.logging.Logger;
ohair@286 49
mkos@397 50 import com.sun.xml.internal.ws.resources.StreamingMessages;
mkos@397 51
ohair@286 52 /**
ohair@286 53 * Factory for {@link XMLStreamReader}.
ohair@286 54 *
ohair@286 55 * <p>
ohair@286 56 * This wraps {@link XMLInputFactory} and allows us to reuse {@link XMLStreamReader} instances
ohair@286 57 * when appropriate.
ohair@286 58 *
ohair@286 59 * @author Kohsuke Kawaguchi
ohair@286 60 */
alanb@368 61 @SuppressWarnings("StaticNonFinalUsedInInitialization")
ohair@286 62 public abstract class XMLStreamReaderFactory {
ohair@286 63
ohair@286 64 private static final Logger LOGGER = Logger.getLogger(XMLStreamReaderFactory.class.getName());
ohair@286 65
mkos@515 66 private static final String CLASS_NAME_OF_WSTXINPUTFACTORY = "com.ctc.wstx.stax.WstxInputFactory";
mkos@515 67
ohair@286 68 /**
ohair@286 69 * Singleton instance.
ohair@286 70 */
mkos@515 71 private static volatile ContextClassloaderLocal<XMLStreamReaderFactory> streamReader =
mkos@515 72 new ContextClassloaderLocal<XMLStreamReaderFactory>() {
ohair@286 73
mkos@515 74 @Override
mkos@515 75 protected XMLStreamReaderFactory initialValue() {
mkos@397 76
mkos@515 77 XMLInputFactory xif = getXMLInputFactory();
mkos@515 78 XMLStreamReaderFactory f=null;
ohair@286 79
mkos@515 80 // this system property can be used to disable the pooling altogether,
mkos@515 81 // in case someone hits an issue with pooling in the production system.
mkos@515 82 if(!getProperty(XMLStreamReaderFactory.class.getName()+".noPool")) {
mkos@515 83 f = Zephyr.newInstance(xif);
mkos@515 84 }
ohair@286 85
mkos@515 86 if(f==null) {
mkos@515 87 // is this Woodstox?
mkos@515 88 if (xif.getClass().getName().equals(CLASS_NAME_OF_WSTXINPUTFACTORY)) {
mkos@515 89 f = new Woodstox(xif);
mkos@515 90 }
mkos@515 91 }
ohair@286 92
mkos@515 93 if (f==null) {
mkos@515 94 f = new Default();
mkos@515 95 }
ohair@286 96
mkos@515 97 if (LOGGER.isLoggable(Level.FINE)) {
mkos@515 98 LOGGER.log(Level.FINE, "XMLStreamReaderFactory instance is = {0}", f);
mkos@515 99 }
mkos@515 100 return f;
mkos@515 101 }
mkos@515 102 };
ohair@286 103
ohair@286 104 private static XMLInputFactory getXMLInputFactory() {
ohair@286 105 XMLInputFactory xif = null;
ohair@286 106 if (getProperty(XMLStreamReaderFactory.class.getName()+".woodstox")) {
ohair@286 107 try {
ohair@286 108 xif = (XMLInputFactory)Class.forName("com.ctc.wstx.stax.WstxInputFactory").newInstance();
ohair@286 109 } catch (Exception e) {
mkos@397 110 if (LOGGER.isLoggable(Level.WARNING)) {
mkos@397 111 LOGGER.log(Level.WARNING, StreamingMessages.WOODSTOX_CANT_LOAD(CLASS_NAME_OF_WSTXINPUTFACTORY), e);
mkos@397 112 }
ohair@286 113 }
ohair@286 114 }
ohair@286 115 if (xif == null) {
alanb@368 116 xif = XmlUtil.newXMLInputFactory(true);
ohair@286 117 }
ohair@286 118 xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
ohair@286 119 xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
ohair@286 120 xif.setProperty(XMLInputFactory.IS_COALESCING, true);
mkos@397 121
ohair@286 122 return xif;
ohair@286 123 }
ohair@286 124
ohair@286 125 /**
ohair@286 126 * Overrides the singleton {@link XMLStreamReaderFactory} instance that
ohair@286 127 * the JAX-WS RI uses.
ohair@286 128 */
ohair@286 129 public static void set(XMLStreamReaderFactory f) {
alanb@368 130 if(f==null) {
alanb@368 131 throw new IllegalArgumentException();
alanb@368 132 }
mkos@515 133 streamReader.set(f);
ohair@286 134 }
ohair@286 135
ohair@286 136 public static XMLStreamReaderFactory get() {
mkos@515 137 return streamReader.get();
ohair@286 138 }
ohair@286 139
ohair@286 140 public static XMLStreamReader create(InputSource source, boolean rejectDTDs) {
ohair@286 141 try {
ohair@286 142 // Char stream available?
ohair@286 143 if (source.getCharacterStream() != null) {
ohair@286 144 return get().doCreate(source.getSystemId(), source.getCharacterStream(), rejectDTDs);
ohair@286 145 }
ohair@286 146
ohair@286 147 // Byte stream available?
ohair@286 148 if (source.getByteStream() != null) {
ohair@286 149 return get().doCreate(source.getSystemId(), source.getByteStream(), rejectDTDs);
ohair@286 150 }
ohair@286 151
ohair@286 152 // Otherwise, open URI
ohair@286 153 return get().doCreate(source.getSystemId(), new URL(source.getSystemId()).openStream(),rejectDTDs);
ohair@286 154 } catch (IOException e) {
ohair@286 155 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 156 }
ohair@286 157 }
ohair@286 158
ohair@286 159 public static XMLStreamReader create(@Nullable String systemId, InputStream in, boolean rejectDTDs) {
ohair@286 160 return get().doCreate(systemId,in,rejectDTDs);
ohair@286 161 }
ohair@286 162
ohair@286 163 public static XMLStreamReader create(@Nullable String systemId, InputStream in, @Nullable String encoding, boolean rejectDTDs) {
ohair@286 164 return (encoding == null)
ohair@286 165 ? create(systemId, in, rejectDTDs)
ohair@286 166 : get().doCreate(systemId,in,encoding,rejectDTDs);
ohair@286 167 }
ohair@286 168
ohair@286 169 public static XMLStreamReader create(@Nullable String systemId, Reader reader, boolean rejectDTDs) {
ohair@286 170 return get().doCreate(systemId,reader,rejectDTDs);
ohair@286 171 }
ohair@286 172
ohair@286 173 /**
ohair@286 174 * Should be invoked when the code finished using an {@link XMLStreamReader}.
ohair@286 175 *
ohair@286 176 * <p>
ohair@286 177 * If the recycled instance implements {@link RecycleAware},
ohair@286 178 * {@link RecycleAware#onRecycled()} will be invoked to let the instance
ohair@286 179 * know that it's being recycled.
ohair@286 180 *
ohair@286 181 * <p>
ohair@286 182 * It is not a hard requirement to call this method on every {@link XMLStreamReader}
ohair@286 183 * instance. Not doing so just reduces the performance by throwing away
ohair@286 184 * possibly reusable instances. So the caller should always consider the effort
ohair@286 185 * it takes to recycle vs the possible performance gain by doing so.
ohair@286 186 *
ohair@286 187 * <p>
mkos@397 188 * This method may be invoked by multiple threads concurrently.
ohair@286 189 *
ohair@286 190 * @param r
ohair@286 191 * The {@link XMLStreamReader} instance that the caller finished using.
ohair@286 192 * This could be any {@link XMLStreamReader} implementation, not just
ohair@286 193 * the ones that were created from this factory. So the implementation
ohair@286 194 * of this class needs to be aware of that.
ohair@286 195 */
ohair@286 196 public static void recycle(XMLStreamReader r) {
ohair@286 197 get().doRecycle(r);
ohair@286 198 if (r instanceof RecycleAware) {
ohair@286 199 ((RecycleAware)r).onRecycled();
ohair@286 200 }
ohair@286 201 }
ohair@286 202
ohair@286 203 // implementations
ohair@286 204
ohair@286 205 public abstract XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs);
ohair@286 206
ohair@286 207 private XMLStreamReader doCreate(String systemId, InputStream in, @NotNull String encoding, boolean rejectDTDs) {
ohair@286 208 Reader reader;
ohair@286 209 try {
ohair@286 210 reader = new InputStreamReader(in, encoding);
ohair@286 211 } catch(UnsupportedEncodingException ue) {
ohair@286 212 throw new XMLReaderException("stax.cantCreate", ue);
ohair@286 213 }
ohair@286 214 return doCreate(systemId, reader, rejectDTDs);
ohair@286 215 }
ohair@286 216
ohair@286 217 public abstract XMLStreamReader doCreate(String systemId, Reader reader, boolean rejectDTDs);
ohair@286 218
ohair@286 219 public abstract void doRecycle(XMLStreamReader r);
ohair@286 220
ohair@286 221 /**
ohair@286 222 * Interface that can be implemented by {@link XMLStreamReader} to
ohair@286 223 * be notified when it's recycled.
ohair@286 224 *
ohair@286 225 * <p>
ohair@286 226 * This provides a filtering {@link XMLStreamReader} an opportunity to
ohair@286 227 * recycle its inner {@link XMLStreamReader}.
ohair@286 228 */
ohair@286 229 public interface RecycleAware {
ohair@286 230 void onRecycled();
ohair@286 231 }
ohair@286 232
ohair@286 233 /**
ohair@286 234 * {@link XMLStreamReaderFactory} implementation for SJSXP/JAXP RI.
ohair@286 235 */
alanb@368 236 private static final class Zephyr extends XMLStreamReaderFactory {
ohair@286 237 private final XMLInputFactory xif;
ohair@286 238
ohair@286 239 private final ThreadLocal<XMLStreamReader> pool = new ThreadLocal<XMLStreamReader>();
ohair@286 240
ohair@286 241 /**
ohair@286 242 * Sun StAX impl <code>XMLReaderImpl.setInputSource()</code> method via reflection.
ohair@286 243 */
ohair@286 244 private final Method setInputSourceMethod;
ohair@286 245
ohair@286 246 /**
ohair@286 247 * Sun StAX impl <code>XMLReaderImpl.reset()</code> method via reflection.
ohair@286 248 */
ohair@286 249 private final Method resetMethod;
ohair@286 250
ohair@286 251 /**
ohair@286 252 * The Sun StAX impl's {@link XMLStreamReader} implementation clas.
ohair@286 253 */
ohair@286 254 private final Class zephyrClass;
ohair@286 255
ohair@286 256 /**
ohair@286 257 * Creates {@link Zephyr} instance if the given {@link XMLInputFactory} is the one
ohair@286 258 * from Zephyr.
ohair@286 259 */
ohair@286 260 public static @Nullable
ohair@286 261 XMLStreamReaderFactory newInstance(XMLInputFactory xif) {
ohair@286 262 // check if this is from Zephyr
ohair@286 263 try {
ohair@286 264 Class<?> clazz = xif.createXMLStreamReader(new StringReader("<foo/>")).getClass();
ohair@286 265 // JDK has different XMLStreamReader impl class. Even if we check for that,
ohair@286 266 // it doesn't have setInputSource(InputSource). Let it use Default
ohair@286 267 if(!(clazz.getName().startsWith("com.sun.xml.internal.stream.")) )
ohair@286 268 return null; // nope
ohair@286 269 return new Zephyr(xif,clazz);
ohair@286 270 } catch (NoSuchMethodException e) {
ohair@286 271 return null; // this factory is not for zephyr
ohair@286 272 } catch (XMLStreamException e) {
ohair@286 273 return null; // impossible to fail to parse <foo/>, but anyway
ohair@286 274 }
ohair@286 275 }
ohair@286 276
ohair@286 277 public Zephyr(XMLInputFactory xif, Class clazz) throws NoSuchMethodException {
ohair@286 278 zephyrClass = clazz;
ohair@286 279 setInputSourceMethod = clazz.getMethod("setInputSource", InputSource.class);
ohair@286 280 resetMethod = clazz.getMethod("reset");
ohair@286 281
ohair@286 282 try {
ohair@286 283 // Turn OFF internal factory caching in Zephyr.
ohair@286 284 // Santiago told me that this makes it thread-safe.
ohair@286 285 xif.setProperty("reuse-instance", false);
ohair@286 286 } catch (IllegalArgumentException e) {
ohair@286 287 // falls through
ohair@286 288 }
ohair@286 289 this.xif = xif;
ohair@286 290 }
ohair@286 291
ohair@286 292 /**
ohair@286 293 * Fetchs an instance from the pool if available, otherwise null.
ohair@286 294 */
ohair@286 295 private @Nullable XMLStreamReader fetch() {
ohair@286 296 XMLStreamReader sr = pool.get();
ohair@286 297 if(sr==null) return null;
ohair@286 298 pool.set(null);
ohair@286 299 return sr;
ohair@286 300 }
ohair@286 301
mkos@397 302 @Override
ohair@286 303 public void doRecycle(XMLStreamReader r) {
ohair@286 304 if(zephyrClass.isInstance(r))
ohair@286 305 pool.set(r);
ohair@286 306 }
ohair@286 307
mkos@397 308 @Override
ohair@286 309 public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) {
ohair@286 310 try {
ohair@286 311 XMLStreamReader xsr = fetch();
ohair@286 312 if(xsr==null)
ohair@286 313 return xif.createXMLStreamReader(systemId,in);
ohair@286 314
ohair@286 315 // try re-using this instance.
ohair@286 316 InputSource is = new InputSource(systemId);
ohair@286 317 is.setByteStream(in);
ohair@286 318 reuse(xsr,is);
ohair@286 319 return xsr;
ohair@286 320 } catch (IllegalAccessException e) {
ohair@286 321 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 322 } catch (InvocationTargetException e) {
ohair@286 323 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 324 } catch (XMLStreamException e) {
ohair@286 325 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 326 }
ohair@286 327 }
ohair@286 328
mkos@397 329 @Override
ohair@286 330 public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) {
ohair@286 331 try {
ohair@286 332 XMLStreamReader xsr = fetch();
ohair@286 333 if(xsr==null)
ohair@286 334 return xif.createXMLStreamReader(systemId,in);
ohair@286 335
ohair@286 336 // try re-using this instance.
ohair@286 337 InputSource is = new InputSource(systemId);
ohair@286 338 is.setCharacterStream(in);
ohair@286 339 reuse(xsr,is);
ohair@286 340 return xsr;
ohair@286 341 } catch (IllegalAccessException e) {
ohair@286 342 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 343 } catch (InvocationTargetException e) {
ohair@286 344 Throwable cause = e.getCause();
ohair@286 345 if (cause == null) {
ohair@286 346 cause = e;
ohair@286 347 }
ohair@286 348 throw new XMLReaderException("stax.cantCreate", cause);
ohair@286 349 } catch (XMLStreamException e) {
ohair@286 350 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 351 }
ohair@286 352 }
ohair@286 353
ohair@286 354 private void reuse(XMLStreamReader xsr, InputSource in) throws IllegalAccessException, InvocationTargetException {
ohair@286 355 resetMethod.invoke(xsr);
ohair@286 356 setInputSourceMethod.invoke(xsr,in);
ohair@286 357 }
ohair@286 358 }
ohair@286 359
ohair@286 360 /**
ohair@286 361 * Default {@link XMLStreamReaderFactory} implementation
ohair@286 362 * that can work with any {@link XMLInputFactory}.
ohair@286 363 *
ohair@286 364 * <p>
ohair@286 365 * {@link XMLInputFactory} is not required to be thread-safe, but
ohair@286 366 * if the create method on this implementation is synchronized,
ohair@286 367 * it may run into (see <a href="https://jax-ws.dev.java.net/issues/show_bug.cgi?id=555">
mkos@397 368 * race condition</a>). Hence, using a XMLInputFactory per thread.
ohair@286 369 */
ohair@286 370 public static final class Default extends XMLStreamReaderFactory {
ohair@286 371
ohair@286 372 private final ThreadLocal<XMLInputFactory> xif = new ThreadLocal<XMLInputFactory>() {
ohair@286 373 @Override
ohair@286 374 public XMLInputFactory initialValue() {
ohair@286 375 return getXMLInputFactory();
ohair@286 376 }
ohair@286 377 };
ohair@286 378
mkos@397 379 @Override
ohair@286 380 public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) {
ohair@286 381 try {
ohair@286 382 return xif.get().createXMLStreamReader(systemId,in);
ohair@286 383 } catch (XMLStreamException e) {
ohair@286 384 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 385 }
ohair@286 386 }
ohair@286 387
mkos@397 388 @Override
ohair@286 389 public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) {
ohair@286 390 try {
ohair@286 391 return xif.get().createXMLStreamReader(systemId,in);
ohair@286 392 } catch (XMLStreamException e) {
ohair@286 393 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 394 }
ohair@286 395 }
ohair@286 396
mkos@397 397 @Override
ohair@286 398 public void doRecycle(XMLStreamReader r) {
ohair@286 399 // there's no way to recycle with the default StAX API.
ohair@286 400 }
ohair@286 401
ohair@286 402 }
ohair@286 403
ohair@286 404 /**
ohair@286 405 * Similar to {@link Default} but doesn't do any synchronization.
ohair@286 406 *
ohair@286 407 * <p>
ohair@286 408 * This is useful when you know your {@link XMLInputFactory} is thread-safe by itself.
ohair@286 409 */
ohair@286 410 public static class NoLock extends XMLStreamReaderFactory {
ohair@286 411 private final XMLInputFactory xif;
ohair@286 412
ohair@286 413 public NoLock(XMLInputFactory xif) {
ohair@286 414 this.xif = xif;
ohair@286 415 }
ohair@286 416
mkos@397 417 @Override
ohair@286 418 public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) {
ohair@286 419 try {
ohair@286 420 return xif.createXMLStreamReader(systemId,in);
ohair@286 421 } catch (XMLStreamException e) {
ohair@286 422 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 423 }
ohair@286 424 }
ohair@286 425
mkos@397 426 @Override
ohair@286 427 public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) {
ohair@286 428 try {
ohair@286 429 return xif.createXMLStreamReader(systemId,in);
ohair@286 430 } catch (XMLStreamException e) {
ohair@286 431 throw new XMLReaderException("stax.cantCreate",e);
ohair@286 432 }
ohair@286 433 }
ohair@286 434
mkos@397 435 @Override
ohair@286 436 public void doRecycle(XMLStreamReader r) {
ohair@286 437 // there's no way to recycle with the default StAX API.
ohair@286 438 }
ohair@286 439 }
ohair@286 440
ohair@286 441 /**
mkos@397 442 * Handles Woodstox's XIF, but sets properties to do the string interning, sets various limits, ...
ohair@286 443 * Woodstox {@link XMLInputFactory} is thread safe.
ohair@286 444 */
ohair@286 445 public static final class Woodstox extends NoLock {
mkos@397 446
mkos@397 447 public final static String PROPERTY_MAX_ATTRIBUTES_PER_ELEMENT = "xml.ws.maximum.AttributesPerElement";
mkos@397 448 public final static String PROPERTY_MAX_ATTRIBUTE_SIZE = "xml.ws.maximum.AttributeSize";
mkos@397 449 public final static String PROPERTY_MAX_CHILDREN_PER_ELEMENT = "xml.ws.maximum.ChildrenPerElement";
mkos@397 450 public final static String PROPERTY_MAX_ELEMENT_COUNT = "xml.ws.maximum.ElementCount";
mkos@397 451 public final static String PROPERTY_MAX_ELEMENT_DEPTH = "xml.ws.maximum.ElementDepth";
mkos@397 452 public final static String PROPERTY_MAX_CHARACTERS = "xml.ws.maximum.Characters";
mkos@397 453
mkos@397 454 private static final int DEFAULT_MAX_ATTRIBUTES_PER_ELEMENT = 500;
mkos@397 455 private static final int DEFAULT_MAX_ATTRIBUTE_SIZE = 65536 * 8;
mkos@397 456 private static final int DEFAULT_MAX_CHILDREN_PER_ELEMENT = Integer.MAX_VALUE;
mkos@397 457 private static final int DEFAULT_MAX_ELEMENT_DEPTH = 500;
mkos@397 458 private static final long DEFAULT_MAX_ELEMENT_COUNT = Integer.MAX_VALUE;
mkos@397 459 private static final long DEFAULT_MAX_CHARACTERS = Long.MAX_VALUE;
mkos@397 460
mkos@397 461 /* Woodstox default setting:
mkos@397 462 int mMaxAttributesPerElement = 1000;
mkos@397 463 int mMaxAttributeSize = 65536 * 8;
mkos@397 464 int mMaxChildrenPerElement = Integer.MAX_VALUE;
mkos@397 465 int mMaxElementDepth = 1000;
mkos@397 466 long mMaxElementCount = Long.MAX_VALUE;
mkos@397 467 long mMaxCharacters = Long.MAX_VALUE;
mkos@397 468 */
mkos@397 469
mkos@397 470 private int maxAttributesPerElement = DEFAULT_MAX_ATTRIBUTES_PER_ELEMENT;
mkos@397 471 private int maxAttributeSize = DEFAULT_MAX_ATTRIBUTE_SIZE;
mkos@397 472 private int maxChildrenPerElement = DEFAULT_MAX_CHILDREN_PER_ELEMENT;
mkos@397 473 private int maxElementDepth = DEFAULT_MAX_ELEMENT_DEPTH;
mkos@397 474 private long maxElementCount = DEFAULT_MAX_ELEMENT_COUNT;
mkos@397 475 private long maxCharacters = DEFAULT_MAX_CHARACTERS;
mkos@397 476
mkos@397 477 // Note: this is a copy from com.ctc.wstx.api.WstxInputProperties, to be removed in the future
mkos@397 478 private static final java.lang.String P_MAX_ATTRIBUTES_PER_ELEMENT = "com.ctc.wstx.maxAttributesPerElement";
mkos@397 479 private static final java.lang.String P_MAX_ATTRIBUTE_SIZE = "com.ctc.wstx.maxAttributeSize";
mkos@397 480 private static final java.lang.String P_MAX_CHILDREN_PER_ELEMENT = "com.ctc.wstx.maxChildrenPerElement";
mkos@397 481 private static final java.lang.String P_MAX_ELEMENT_COUNT = "com.ctc.wstx.maxElementCount";
mkos@397 482 private static final java.lang.String P_MAX_ELEMENT_DEPTH = "com.ctc.wstx.maxElementDepth";
mkos@397 483 private static final java.lang.String P_MAX_CHARACTERS = "com.ctc.wstx.maxCharacters";
mkos@397 484 private static final java.lang.String P_INTERN_NSURIS = "org.codehaus.stax2.internNsUris";
mkos@397 485
ohair@286 486 public Woodstox(XMLInputFactory xif) {
ohair@286 487 super(xif);
mkos@397 488
mkos@397 489 if (xif.isPropertySupported(P_INTERN_NSURIS)) {
mkos@397 490 xif.setProperty(P_INTERN_NSURIS, true);
mkos@397 491 if (LOGGER.isLoggable(Level.FINE)) {
mkos@397 492 LOGGER.log(Level.FINE, P_INTERN_NSURIS + " is {0}", true);
mkos@397 493 }
mkos@397 494 }
mkos@397 495
mkos@397 496 if (xif.isPropertySupported(P_MAX_ATTRIBUTES_PER_ELEMENT)) {
mkos@397 497 maxAttributesPerElement = Integer.valueOf(buildIntegerValue(
mkos@397 498 PROPERTY_MAX_ATTRIBUTES_PER_ELEMENT, DEFAULT_MAX_ATTRIBUTES_PER_ELEMENT)
mkos@397 499 );
mkos@397 500 xif.setProperty(P_MAX_ATTRIBUTES_PER_ELEMENT, maxAttributesPerElement);
mkos@397 501 if (LOGGER.isLoggable(Level.FINE)) {
mkos@397 502 LOGGER.log(Level.FINE, P_MAX_ATTRIBUTES_PER_ELEMENT + " is {0}", maxAttributesPerElement);
mkos@397 503 }
mkos@397 504 }
mkos@397 505
mkos@397 506 if (xif.isPropertySupported(P_MAX_ATTRIBUTE_SIZE)) {
mkos@397 507 maxAttributeSize = Integer.valueOf(buildIntegerValue(
mkos@397 508 PROPERTY_MAX_ATTRIBUTE_SIZE, DEFAULT_MAX_ATTRIBUTE_SIZE)
mkos@397 509 );
mkos@397 510 xif.setProperty(P_MAX_ATTRIBUTE_SIZE, maxAttributeSize);
mkos@397 511 if (LOGGER.isLoggable(Level.FINE)) {
mkos@397 512 LOGGER.log(Level.FINE, P_MAX_ATTRIBUTE_SIZE + " is {0}", maxAttributeSize);
mkos@397 513 }
mkos@397 514 }
mkos@397 515
mkos@397 516 if (xif.isPropertySupported(P_MAX_CHILDREN_PER_ELEMENT)) {
mkos@397 517 maxChildrenPerElement = Integer.valueOf(buildIntegerValue(
mkos@397 518 PROPERTY_MAX_CHILDREN_PER_ELEMENT, DEFAULT_MAX_CHILDREN_PER_ELEMENT)
mkos@397 519 );
mkos@397 520 xif.setProperty(P_MAX_CHILDREN_PER_ELEMENT, maxChildrenPerElement);
mkos@397 521 if (LOGGER.isLoggable(Level.FINE)) {
mkos@397 522 LOGGER.log(Level.FINE, P_MAX_CHILDREN_PER_ELEMENT + " is {0}", maxChildrenPerElement);
mkos@397 523 }
mkos@397 524 }
mkos@397 525
mkos@397 526 if (xif.isPropertySupported(P_MAX_ELEMENT_DEPTH)) {
mkos@397 527 maxElementDepth = Integer.valueOf(buildIntegerValue(
mkos@397 528 PROPERTY_MAX_ELEMENT_DEPTH, DEFAULT_MAX_ELEMENT_DEPTH)
mkos@397 529 );
mkos@397 530 xif.setProperty(P_MAX_ELEMENT_DEPTH, maxElementDepth);
mkos@397 531 if (LOGGER.isLoggable(Level.FINE)) {
mkos@397 532 LOGGER.log(Level.FINE, P_MAX_ELEMENT_DEPTH + " is {0}", maxElementDepth);
mkos@397 533 }
mkos@397 534 }
mkos@397 535
mkos@397 536 if (xif.isPropertySupported(P_MAX_ELEMENT_COUNT)) {
mkos@397 537 maxElementCount = Long.valueOf(buildLongValue(
mkos@397 538 PROPERTY_MAX_ELEMENT_COUNT, DEFAULT_MAX_ELEMENT_COUNT)
mkos@397 539 );
mkos@397 540 xif.setProperty(P_MAX_ELEMENT_COUNT, maxElementCount);
mkos@397 541 if (LOGGER.isLoggable(Level.FINE)) {
mkos@397 542 LOGGER.log(Level.FINE, P_MAX_ELEMENT_COUNT + " is {0}", maxElementCount);
mkos@397 543 }
mkos@397 544 }
mkos@397 545
mkos@397 546 if (xif.isPropertySupported(P_MAX_CHARACTERS)) {
mkos@397 547 maxCharacters = Long.valueOf(buildLongValue(
mkos@397 548 PROPERTY_MAX_CHARACTERS, DEFAULT_MAX_CHARACTERS)
mkos@397 549 );
mkos@397 550 xif.setProperty(P_MAX_CHARACTERS, maxCharacters);
mkos@397 551 if (LOGGER.isLoggable(Level.FINE)) {
mkos@397 552 LOGGER.log(Level.FINE, P_MAX_CHARACTERS + " is {0}", maxCharacters);
mkos@397 553 }
mkos@397 554 }
ohair@286 555 }
ohair@286 556
mkos@397 557 @Override
ohair@286 558 public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) {
ohair@286 559 return super.doCreate(systemId, in, rejectDTDs);
ohair@286 560 }
ohair@286 561
mkos@397 562 @Override
ohair@286 563 public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) {
ohair@286 564 return super.doCreate(systemId, in, rejectDTDs);
ohair@286 565 }
ohair@286 566 }
ohair@286 567
mkos@397 568 private static int buildIntegerValue(String propertyName, int defaultValue) {
mkos@397 569 String propVal = System.getProperty(propertyName);
mkos@397 570 if (propVal != null && propVal.length() > 0) {
mkos@397 571 try {
mkos@397 572 Integer value = Integer.parseInt(propVal);
mkos@397 573 if (value > 0) {
mkos@397 574 // return with the value in System property
mkos@397 575 return value;
mkos@397 576 }
mkos@397 577 } catch (NumberFormatException nfe) {
mkos@397 578 if (LOGGER.isLoggable(Level.WARNING)) {
mkos@397 579 LOGGER.log(Level.WARNING, StreamingMessages.INVALID_PROPERTY_VALUE_INTEGER(propertyName, propVal, Integer.toString(defaultValue)), nfe);
mkos@397 580 }
mkos@397 581 }
mkos@397 582 }
mkos@397 583 // return with the default value
mkos@397 584 return defaultValue;
mkos@397 585 }
mkos@397 586
mkos@397 587 private static long buildLongValue(String propertyName, long defaultValue) {
mkos@397 588 String propVal = System.getProperty(propertyName);
mkos@397 589 if (propVal != null && propVal.length() > 0) {
mkos@397 590 try {
mkos@397 591 long value = Long.parseLong(propVal);
mkos@397 592 if (value > 0L) {
mkos@397 593 // return with the value in System property
mkos@397 594 return value;
mkos@397 595 }
mkos@397 596 } catch (NumberFormatException nfe) {
mkos@397 597 // defult will be returned
mkos@397 598 if (LOGGER.isLoggable(Level.WARNING)) {
mkos@397 599 LOGGER.log(Level.WARNING, StreamingMessages.INVALID_PROPERTY_VALUE_LONG(propertyName, propVal, Long.toString(defaultValue)), nfe);
mkos@397 600 }
mkos@397 601 }
mkos@397 602 }
mkos@397 603 // return with the default value
mkos@397 604 return defaultValue;
mkos@397 605 }
mkos@397 606
ohair@286 607 private static Boolean getProperty(final String prop) {
ohair@286 608 return AccessController.doPrivileged(
ohair@286 609 new java.security.PrivilegedAction<Boolean>() {
mkos@397 610 @Override
ohair@286 611 public Boolean run() {
ohair@286 612 String value = System.getProperty(prop);
ohair@286 613 return value != null ? Boolean.valueOf(value) : Boolean.FALSE;
ohair@286 614 }
ohair@286 615 }
ohair@286 616 );
ohair@286 617 }
mkos@397 618
ohair@286 619 }

mercurial