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