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