1 /*
   2  * Copyright (c) 1997, 2014, 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         get().doRecycle(r);
 198         if (r instanceof RecycleAware) {
 199             ((RecycleAware)r).onRecycled();
 200         }
 201     }
 202 
 203     // implementations
 204 
 205     public abstract XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs);
 206 
 207     private XMLStreamReader doCreate(String systemId, InputStream in, @NotNull String encoding, boolean rejectDTDs) {
 208         Reader reader;
 209         try {
 210             reader = new InputStreamReader(in, encoding);
 211         } catch(UnsupportedEncodingException ue) {
 212             throw new XMLReaderException("stax.cantCreate", ue);
 213         }
 214         return doCreate(systemId, reader, rejectDTDs);
 215     }
 216 
 217     public abstract XMLStreamReader doCreate(String systemId, Reader reader, boolean rejectDTDs);
 218 
 219     public abstract void doRecycle(XMLStreamReader r);
 220 
 221     /**
 222      * Interface that can be implemented by {@link XMLStreamReader} to
 223      * be notified when it's recycled.
 224      *
 225      * <p>
 226      * This provides a filtering {@link XMLStreamReader} an opportunity to
 227      * recycle its inner {@link XMLStreamReader}.
 228      */
 229     public interface RecycleAware {
 230         void onRecycled();
 231     }
 232 
 233     /**
 234      * {@link XMLStreamReaderFactory} implementation for SJSXP/JAXP RI.
 235      */
 236     private static final class Zephyr extends XMLStreamReaderFactory {
 237         private final XMLInputFactory xif;
 238 
 239         private final ThreadLocal<XMLStreamReader> pool = new ThreadLocal<XMLStreamReader>();
 240 
 241         /**
 242          * Sun StAX impl <code>XMLReaderImpl.setInputSource()</code> method via reflection.
 243          */
 244         private final Method setInputSourceMethod;
 245 
 246         /**
 247          * Sun StAX impl <code>XMLReaderImpl.reset()</code> method via reflection.
 248          */
 249         private final Method resetMethod;
 250 
 251         /**
 252          * The Sun StAX impl's {@link XMLStreamReader} implementation clas.
 253          */
 254         private final Class zephyrClass;
 255 
 256         /**
 257          * Creates {@link Zephyr} instance if the given {@link XMLInputFactory} is the one
 258          * from Zephyr.
 259          */
 260         public static @Nullable
 261         XMLStreamReaderFactory newInstance(XMLInputFactory xif) {
 262             // check if this is from Zephyr
 263             try {
 264                 Class<?> clazz = xif.createXMLStreamReader(new StringReader("<foo/>")).getClass();
 265                 // JDK has different XMLStreamReader impl class. Even if we check for that,
 266                 // it doesn't have setInputSource(InputSource). Let it use Default
 267                 if(!(clazz.getName().startsWith("com.sun.xml.internal.stream.")) )
 268                     return null;    // nope
 269                 return new Zephyr(xif,clazz);
 270             } catch (NoSuchMethodException e) {
 271                 return null;    // this factory is not for zephyr
 272             } catch (XMLStreamException e) {
 273                 return null;    // impossible to fail to parse <foo/>, but anyway
 274             }
 275         }
 276 
 277         public Zephyr(XMLInputFactory xif, Class clazz) throws NoSuchMethodException {
 278             zephyrClass = clazz;
 279             setInputSourceMethod = clazz.getMethod("setInputSource", InputSource.class);
 280             resetMethod = clazz.getMethod("reset");
 281 
 282             try {
 283                 // Turn OFF internal factory caching in Zephyr.
 284                 // Santiago told me that this makes it thread-safe.
 285                 xif.setProperty("reuse-instance", false);
 286             } catch (IllegalArgumentException e) {
 287                 // falls through
 288             }
 289             this.xif = xif;
 290         }
 291 
 292         /**
 293          * Fetchs an instance from the pool if available, otherwise null.
 294          */
 295         private @Nullable XMLStreamReader fetch() {
 296             XMLStreamReader sr = pool.get();
 297             if(sr==null)    return null;
 298             pool.set(null);
 299             return sr;
 300         }
 301 
 302         @Override
 303         public void doRecycle(XMLStreamReader r) {
 304             if(zephyrClass.isInstance(r))
 305                 pool.set(r);
 306         }
 307 
 308         @Override
 309         public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) {
 310             try {
 311                 XMLStreamReader xsr = fetch();
 312                 if(xsr==null)
 313                     return xif.createXMLStreamReader(systemId,in);
 314 
 315                 // try re-using this instance.
 316                 InputSource is = new InputSource(systemId);
 317                 is.setByteStream(in);
 318                 reuse(xsr,is);
 319                 return xsr;
 320             } catch (IllegalAccessException e) {
 321                 throw new XMLReaderException("stax.cantCreate",e);
 322             } catch (InvocationTargetException e) {
 323                 throw new XMLReaderException("stax.cantCreate",e);
 324             } catch (XMLStreamException e) {
 325                 throw new XMLReaderException("stax.cantCreate",e);
 326             }
 327         }
 328 
 329         @Override
 330         public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) {
 331             try {
 332                 XMLStreamReader xsr = fetch();
 333                 if(xsr==null)
 334                     return xif.createXMLStreamReader(systemId,in);
 335 
 336                 // try re-using this instance.
 337                 InputSource is = new InputSource(systemId);
 338                 is.setCharacterStream(in);
 339                 reuse(xsr,is);
 340                 return xsr;
 341             } catch (IllegalAccessException e) {
 342                 throw new XMLReaderException("stax.cantCreate",e);
 343             } catch (InvocationTargetException e) {
 344                 Throwable cause = e.getCause();
 345                 if (cause == null) {
 346                     cause = e;
 347                 }
 348                 throw new XMLReaderException("stax.cantCreate", cause);
 349             } catch (XMLStreamException e) {
 350                 throw new XMLReaderException("stax.cantCreate",e);
 351             }
 352         }
 353 
 354         private void reuse(XMLStreamReader xsr, InputSource in) throws IllegalAccessException, InvocationTargetException {
 355             resetMethod.invoke(xsr);
 356             setInputSourceMethod.invoke(xsr,in);
 357         }
 358     }
 359 
 360     /**
 361      * Default {@link XMLStreamReaderFactory} implementation
 362      * that can work with any {@link XMLInputFactory}.
 363      *
 364      * <p>
 365      * {@link XMLInputFactory} is not required to be thread-safe, but
 366      * if the create method on this implementation is synchronized,
 367      * it may run into (see <a href="https://jax-ws.dev.java.net/issues/show_bug.cgi?id=555">
 368      * race condition</a>). Hence, using a XMLInputFactory per thread.
 369      */
 370     public static final class Default extends XMLStreamReaderFactory {
 371 
 372         private final ThreadLocal<XMLInputFactory> xif = new ThreadLocal<XMLInputFactory>() {
 373             @Override
 374             public XMLInputFactory initialValue() {
 375                 return getXMLInputFactory();
 376             }
 377         };
 378 
 379         @Override
 380         public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) {
 381             try {
 382                 return xif.get().createXMLStreamReader(systemId,in);
 383             } catch (XMLStreamException e) {
 384                 throw new XMLReaderException("stax.cantCreate",e);
 385             }
 386         }
 387 
 388         @Override
 389         public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) {
 390             try {
 391                 return xif.get().createXMLStreamReader(systemId,in);
 392             } catch (XMLStreamException e) {
 393                 throw new XMLReaderException("stax.cantCreate",e);
 394             }
 395         }
 396 
 397         @Override
 398         public void doRecycle(XMLStreamReader r) {
 399             // there's no way to recycle with the default StAX API.
 400         }
 401 
 402     }
 403 
 404     /**
 405      * Similar to {@link Default} but doesn't do any synchronization.
 406      *
 407      * <p>
 408      * This is useful when you know your {@link XMLInputFactory} is thread-safe by itself.
 409      */
 410     public static class NoLock extends XMLStreamReaderFactory {
 411         private final XMLInputFactory xif;
 412 
 413         public NoLock(XMLInputFactory xif) {
 414             this.xif = xif;
 415         }
 416 
 417         @Override
 418         public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) {
 419             try {
 420                 return xif.createXMLStreamReader(systemId,in);
 421             } catch (XMLStreamException e) {
 422                 throw new XMLReaderException("stax.cantCreate",e);
 423             }
 424         }
 425 
 426         @Override
 427         public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) {
 428             try {
 429                 return xif.createXMLStreamReader(systemId,in);
 430             } catch (XMLStreamException e) {
 431                 throw new XMLReaderException("stax.cantCreate",e);
 432             }
 433         }
 434 
 435         @Override
 436         public void doRecycle(XMLStreamReader r) {
 437             // there's no way to recycle with the default StAX API.
 438         }
 439     }
 440 
 441     /**
 442      * Handles Woodstox's XIF, but sets properties to do the string interning, sets various limits, ...
 443      * Woodstox {@link XMLInputFactory} is thread safe.
 444      */
 445     public static final class Woodstox extends NoLock {
 446 
 447         public final static String PROPERTY_MAX_ATTRIBUTES_PER_ELEMENT = "xml.ws.maximum.AttributesPerElement";
 448         public final static String PROPERTY_MAX_ATTRIBUTE_SIZE = "xml.ws.maximum.AttributeSize";
 449         public final static String PROPERTY_MAX_CHILDREN_PER_ELEMENT = "xml.ws.maximum.ChildrenPerElement";
 450         public final static String PROPERTY_MAX_ELEMENT_COUNT = "xml.ws.maximum.ElementCount";
 451         public final static String PROPERTY_MAX_ELEMENT_DEPTH = "xml.ws.maximum.ElementDepth";
 452         public final static String PROPERTY_MAX_CHARACTERS = "xml.ws.maximum.Characters";
 453 
 454         private static final int DEFAULT_MAX_ATTRIBUTES_PER_ELEMENT = 500;
 455         private static final int DEFAULT_MAX_ATTRIBUTE_SIZE = 65536 * 8;
 456         private static final int DEFAULT_MAX_CHILDREN_PER_ELEMENT = Integer.MAX_VALUE;
 457         private static final int DEFAULT_MAX_ELEMENT_DEPTH = 500;
 458         private static final long DEFAULT_MAX_ELEMENT_COUNT = Integer.MAX_VALUE;
 459         private static final long DEFAULT_MAX_CHARACTERS = Long.MAX_VALUE;
 460 
 461         /* Woodstox default setting:
 462          int mMaxAttributesPerElement = 1000;
 463          int mMaxAttributeSize = 65536 * 8;
 464          int mMaxChildrenPerElement = Integer.MAX_VALUE;
 465          int mMaxElementDepth = 1000;
 466          long mMaxElementCount = Long.MAX_VALUE;
 467          long mMaxCharacters = Long.MAX_VALUE;
 468          */
 469 
 470         private int maxAttributesPerElement = DEFAULT_MAX_ATTRIBUTES_PER_ELEMENT;
 471         private int maxAttributeSize = DEFAULT_MAX_ATTRIBUTE_SIZE;
 472         private int maxChildrenPerElement = DEFAULT_MAX_CHILDREN_PER_ELEMENT;
 473         private int maxElementDepth = DEFAULT_MAX_ELEMENT_DEPTH;
 474         private long maxElementCount = DEFAULT_MAX_ELEMENT_COUNT;
 475         private long maxCharacters = DEFAULT_MAX_CHARACTERS;
 476 
 477         // Note: this is a copy from com.ctc.wstx.api.WstxInputProperties, to be removed in the future
 478         private static final java.lang.String P_MAX_ATTRIBUTES_PER_ELEMENT = "com.ctc.wstx.maxAttributesPerElement";
 479         private static final java.lang.String P_MAX_ATTRIBUTE_SIZE = "com.ctc.wstx.maxAttributeSize";
 480         private static final java.lang.String P_MAX_CHILDREN_PER_ELEMENT = "com.ctc.wstx.maxChildrenPerElement";
 481         private static final java.lang.String P_MAX_ELEMENT_COUNT = "com.ctc.wstx.maxElementCount";
 482         private static final java.lang.String P_MAX_ELEMENT_DEPTH = "com.ctc.wstx.maxElementDepth";
 483         private static final java.lang.String P_MAX_CHARACTERS = "com.ctc.wstx.maxCharacters";
 484         private static final java.lang.String P_INTERN_NSURIS = "org.codehaus.stax2.internNsUris";
 485         private static final java.lang.String P_RETURN_NULL_FOR_DEFAULT_NAMESPACE = "com.ctc.wstx.returnNullForDefaultNamespace";
 486 
 487         public Woodstox(XMLInputFactory xif) {
 488             super(xif);
 489 
 490             if (xif.isPropertySupported(P_INTERN_NSURIS)) {
 491                 xif.setProperty(P_INTERN_NSURIS, true);
 492                 if (LOGGER.isLoggable(Level.FINE)) {
 493                     LOGGER.log(Level.FINE, P_INTERN_NSURIS + " is {0}", true);
 494                 }
 495             }
 496 
 497             if (xif.isPropertySupported(P_MAX_ATTRIBUTES_PER_ELEMENT)) {
 498                 maxAttributesPerElement = Integer.valueOf(buildIntegerValue(
 499                     PROPERTY_MAX_ATTRIBUTES_PER_ELEMENT, DEFAULT_MAX_ATTRIBUTES_PER_ELEMENT)
 500                 );
 501                 xif.setProperty(P_MAX_ATTRIBUTES_PER_ELEMENT, maxAttributesPerElement);
 502                 if (LOGGER.isLoggable(Level.FINE)) {
 503                     LOGGER.log(Level.FINE, P_MAX_ATTRIBUTES_PER_ELEMENT + " is {0}", maxAttributesPerElement);
 504                 }
 505             }
 506 
 507             if (xif.isPropertySupported(P_MAX_ATTRIBUTE_SIZE)) {
 508                 maxAttributeSize = Integer.valueOf(buildIntegerValue(
 509                     PROPERTY_MAX_ATTRIBUTE_SIZE, DEFAULT_MAX_ATTRIBUTE_SIZE)
 510                 );
 511                 xif.setProperty(P_MAX_ATTRIBUTE_SIZE, maxAttributeSize);
 512                 if (LOGGER.isLoggable(Level.FINE)) {
 513                     LOGGER.log(Level.FINE, P_MAX_ATTRIBUTE_SIZE + " is {0}", maxAttributeSize);
 514                 }
 515             }
 516 
 517             if (xif.isPropertySupported(P_MAX_CHILDREN_PER_ELEMENT)) {
 518                 maxChildrenPerElement = Integer.valueOf(buildIntegerValue(
 519                     PROPERTY_MAX_CHILDREN_PER_ELEMENT, DEFAULT_MAX_CHILDREN_PER_ELEMENT)
 520                 );
 521                 xif.setProperty(P_MAX_CHILDREN_PER_ELEMENT, maxChildrenPerElement);
 522                 if (LOGGER.isLoggable(Level.FINE)) {
 523                     LOGGER.log(Level.FINE, P_MAX_CHILDREN_PER_ELEMENT + " is {0}", maxChildrenPerElement);
 524                 }
 525             }
 526 
 527             if (xif.isPropertySupported(P_MAX_ELEMENT_DEPTH)) {
 528                 maxElementDepth = Integer.valueOf(buildIntegerValue(
 529                     PROPERTY_MAX_ELEMENT_DEPTH, DEFAULT_MAX_ELEMENT_DEPTH)
 530                 );
 531                 xif.setProperty(P_MAX_ELEMENT_DEPTH, maxElementDepth);
 532                 if (LOGGER.isLoggable(Level.FINE)) {
 533                     LOGGER.log(Level.FINE, P_MAX_ELEMENT_DEPTH + " is {0}", maxElementDepth);
 534                 }
 535             }
 536 
 537             if (xif.isPropertySupported(P_MAX_ELEMENT_COUNT)) {
 538                 maxElementCount = Long.valueOf(buildLongValue(
 539                     PROPERTY_MAX_ELEMENT_COUNT, DEFAULT_MAX_ELEMENT_COUNT)
 540                 );
 541                 xif.setProperty(P_MAX_ELEMENT_COUNT, maxElementCount);
 542                 if (LOGGER.isLoggable(Level.FINE)) {
 543                     LOGGER.log(Level.FINE, P_MAX_ELEMENT_COUNT + " is {0}", maxElementCount);
 544                 }
 545             }
 546 
 547             if (xif.isPropertySupported(P_MAX_CHARACTERS)) {
 548                 maxCharacters = Long.valueOf(buildLongValue(
 549                     PROPERTY_MAX_CHARACTERS, DEFAULT_MAX_CHARACTERS)
 550                 );
 551                 xif.setProperty(P_MAX_CHARACTERS, maxCharacters);
 552                 if (LOGGER.isLoggable(Level.FINE)) {
 553                     LOGGER.log(Level.FINE, P_MAX_CHARACTERS + " is {0}", maxCharacters);
 554                 }
 555             }
 556             //Using try/catch instead of isPropertySupported because Woodstox
 557             //isPropertySupported is not always reliable
 558             try {
 559                 //this is needed to make sure Woodstox behavior is spec compliant for
 560                 //calls to XMLStreamReader.getNamespacePrefix
 561                 xif.setProperty(P_RETURN_NULL_FOR_DEFAULT_NAMESPACE, Boolean.TRUE);
 562                 if (LOGGER.isLoggable(Level.FINE)) {
 563                     LOGGER.log(Level.FINE, P_RETURN_NULL_FOR_DEFAULT_NAMESPACE + " is {0}", xif.getProperty(P_RETURN_NULL_FOR_DEFAULT_NAMESPACE));
 564                 }
 565             } catch (Throwable t) {
 566                 //ignore - this should not happen Woodstox 4.1.2 or later (maybe older version of Woodstox).
 567                 LOGGER.log(Level.WARNING, "Expected property not found in Woodstox input factory: '{0}'", P_RETURN_NULL_FOR_DEFAULT_NAMESPACE);
 568             }
 569         }
 570 
 571         @Override
 572         public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) {
 573             return super.doCreate(systemId, in, rejectDTDs);
 574         }
 575 
 576         @Override
 577         public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) {
 578             return super.doCreate(systemId, in, rejectDTDs);
 579         }
 580     }
 581 
 582     private static int buildIntegerValue(String propertyName, int defaultValue) {
 583         String propVal = System.getProperty(propertyName);
 584         if (propVal != null && propVal.length() > 0) {
 585             try {
 586                 Integer value = Integer.parseInt(propVal);
 587                 if (value > 0) {
 588                     // return with the value in System property
 589                     return value;
 590                 }
 591             } catch (NumberFormatException nfe) {
 592                 if (LOGGER.isLoggable(Level.WARNING)) {
 593                     LOGGER.log(Level.WARNING, StreamingMessages.INVALID_PROPERTY_VALUE_INTEGER(propertyName, propVal, Integer.toString(defaultValue)), nfe);
 594                 }
 595             }
 596         }
 597         // return with the default value
 598         return defaultValue;
 599     }
 600 
 601     private static long buildLongValue(String propertyName, long defaultValue) {
 602         String propVal = System.getProperty(propertyName);
 603         if (propVal != null && propVal.length() > 0) {
 604             try {
 605                 long value = Long.parseLong(propVal);
 606                 if (value > 0L) {
 607                     // return with the value in System property
 608                     return value;
 609                 }
 610             } catch (NumberFormatException nfe) {
 611                 // defult will be returned
 612                 if (LOGGER.isLoggable(Level.WARNING)) {
 613                     LOGGER.log(Level.WARNING, StreamingMessages.INVALID_PROPERTY_VALUE_LONG(propertyName, propVal, Long.toString(defaultValue)), nfe);
 614                 }
 615             }
 616         }
 617         // return with the default value
 618         return defaultValue;
 619     }
 620 
 621     private static Boolean getProperty(final String prop) {
 622         return AccessController.doPrivileged(
 623             new java.security.PrivilegedAction<Boolean>() {
 624                 @Override
 625                 public Boolean run() {
 626                     String value = System.getProperty(prop);
 627                     return value != null ? Boolean.valueOf(value) : Boolean.FALSE;
 628                 }
 629             }
 630         );
 631     }
 632 
 633 }