ohair@286: /* alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.api.streaming; ohair@286: ohair@286: import com.sun.istack.internal.NotNull; ohair@286: import com.sun.istack.internal.Nullable; ohair@286: import com.sun.xml.internal.ws.streaming.XMLReaderException; alanb@368: import com.sun.xml.internal.ws.util.xml.XmlUtil; ohair@286: import org.xml.sax.InputSource; ohair@286: ohair@286: import javax.xml.stream.XMLInputFactory; ohair@286: import javax.xml.stream.XMLStreamException; ohair@286: import javax.xml.stream.XMLStreamReader; alanb@368: import java.io.*; ohair@286: import java.lang.reflect.InvocationTargetException; ohair@286: import java.lang.reflect.Method; ohair@286: import java.net.URL; alanb@368: import java.security.AccessController; alanb@368: import java.util.logging.Level; ohair@286: import java.util.logging.Logger; ohair@286: ohair@286: /** ohair@286: * Factory for {@link XMLStreamReader}. ohair@286: * ohair@286: *

ohair@286: * This wraps {@link XMLInputFactory} and allows us to reuse {@link XMLStreamReader} instances ohair@286: * when appropriate. ohair@286: * ohair@286: * @author Kohsuke Kawaguchi ohair@286: */ alanb@368: @SuppressWarnings("StaticNonFinalUsedInInitialization") ohair@286: public abstract class XMLStreamReaderFactory { ohair@286: ohair@286: private static final Logger LOGGER = Logger.getLogger(XMLStreamReaderFactory.class.getName()); ohair@286: ohair@286: /** ohair@286: * Singleton instance. ohair@286: */ ohair@286: private static volatile @NotNull XMLStreamReaderFactory theInstance; ohair@286: ohair@286: static { ohair@286: XMLInputFactory xif = getXMLInputFactory(); ohair@286: XMLStreamReaderFactory f=null; ohair@286: ohair@286: // this system property can be used to disable the pooling altogether, ohair@286: // in case someone hits an issue with pooling in the production system. alanb@368: if(!getProperty(XMLStreamReaderFactory.class.getName()+".noPool")) { ohair@286: f = Zephyr.newInstance(xif); alanb@368: } ohair@286: ohair@286: if(f==null) { ohair@286: // is this Woodstox? alanb@368: if (xif.getClass().getName().equals("com.ctc.wstx.stax.WstxInputFactory")) { ohair@286: f = new Woodstox(xif); alanb@368: } ohair@286: } ohair@286: alanb@368: if (f==null) { ohair@286: f = new Default(); alanb@368: } ohair@286: ohair@286: theInstance = f; alanb@368: LOGGER.log(Level.FINE, "XMLStreamReaderFactory instance is = {0}", theInstance); ohair@286: } ohair@286: ohair@286: private static XMLInputFactory getXMLInputFactory() { ohair@286: XMLInputFactory xif = null; ohair@286: if (getProperty(XMLStreamReaderFactory.class.getName()+".woodstox")) { ohair@286: try { ohair@286: xif = (XMLInputFactory)Class.forName("com.ctc.wstx.stax.WstxInputFactory").newInstance(); ohair@286: } catch (Exception e) { ohair@286: // Ignore and fallback to default XMLInputFactory ohair@286: } ohair@286: } ohair@286: if (xif == null) { alanb@368: xif = XmlUtil.newXMLInputFactory(true); ohair@286: } ohair@286: xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true); ohair@286: xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); ohair@286: xif.setProperty(XMLInputFactory.IS_COALESCING, true); ohair@286: return xif; ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * Overrides the singleton {@link XMLStreamReaderFactory} instance that ohair@286: * the JAX-WS RI uses. ohair@286: */ ohair@286: public static void set(XMLStreamReaderFactory f) { alanb@368: if(f==null) { alanb@368: throw new IllegalArgumentException(); alanb@368: } ohair@286: theInstance = f; ohair@286: } ohair@286: ohair@286: public static XMLStreamReaderFactory get() { ohair@286: return theInstance; ohair@286: } ohair@286: ohair@286: public static XMLStreamReader create(InputSource source, boolean rejectDTDs) { ohair@286: try { ohair@286: // Char stream available? ohair@286: if (source.getCharacterStream() != null) { ohair@286: return get().doCreate(source.getSystemId(), source.getCharacterStream(), rejectDTDs); ohair@286: } ohair@286: ohair@286: // Byte stream available? ohair@286: if (source.getByteStream() != null) { ohair@286: return get().doCreate(source.getSystemId(), source.getByteStream(), rejectDTDs); ohair@286: } ohair@286: ohair@286: // Otherwise, open URI ohair@286: return get().doCreate(source.getSystemId(), new URL(source.getSystemId()).openStream(),rejectDTDs); ohair@286: } catch (IOException e) { ohair@286: throw new XMLReaderException("stax.cantCreate",e); ohair@286: } ohair@286: } ohair@286: ohair@286: public static XMLStreamReader create(@Nullable String systemId, InputStream in, boolean rejectDTDs) { ohair@286: return get().doCreate(systemId,in,rejectDTDs); ohair@286: } ohair@286: ohair@286: public static XMLStreamReader create(@Nullable String systemId, InputStream in, @Nullable String encoding, boolean rejectDTDs) { ohair@286: return (encoding == null) ohair@286: ? create(systemId, in, rejectDTDs) ohair@286: : get().doCreate(systemId,in,encoding,rejectDTDs); ohair@286: } ohair@286: ohair@286: public static XMLStreamReader create(@Nullable String systemId, Reader reader, boolean rejectDTDs) { ohair@286: return get().doCreate(systemId,reader,rejectDTDs); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Should be invoked when the code finished using an {@link XMLStreamReader}. ohair@286: * ohair@286: *

ohair@286: * If the recycled instance implements {@link RecycleAware}, ohair@286: * {@link RecycleAware#onRecycled()} will be invoked to let the instance ohair@286: * know that it's being recycled. ohair@286: * ohair@286: *

ohair@286: * It is not a hard requirement to call this method on every {@link XMLStreamReader} ohair@286: * instance. Not doing so just reduces the performance by throwing away ohair@286: * possibly reusable instances. So the caller should always consider the effort ohair@286: * it takes to recycle vs the possible performance gain by doing so. ohair@286: * ohair@286: *

ohair@286: * This method may be invked by multiple threads concurrently. ohair@286: * ohair@286: * @param r ohair@286: * The {@link XMLStreamReader} instance that the caller finished using. ohair@286: * This could be any {@link XMLStreamReader} implementation, not just ohair@286: * the ones that were created from this factory. So the implementation ohair@286: * of this class needs to be aware of that. ohair@286: */ ohair@286: public static void recycle(XMLStreamReader r) { ohair@286: get().doRecycle(r); ohair@286: if (r instanceof RecycleAware) { ohair@286: ((RecycleAware)r).onRecycled(); ohair@286: } ohair@286: } ohair@286: ohair@286: // implementations ohair@286: ohair@286: public abstract XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs); ohair@286: ohair@286: private XMLStreamReader doCreate(String systemId, InputStream in, @NotNull String encoding, boolean rejectDTDs) { ohair@286: Reader reader; ohair@286: try { ohair@286: reader = new InputStreamReader(in, encoding); ohair@286: } catch(UnsupportedEncodingException ue) { ohair@286: throw new XMLReaderException("stax.cantCreate", ue); ohair@286: } ohair@286: return doCreate(systemId, reader, rejectDTDs); ohair@286: } ohair@286: ohair@286: public abstract XMLStreamReader doCreate(String systemId, Reader reader, boolean rejectDTDs); ohair@286: ohair@286: public abstract void doRecycle(XMLStreamReader r); ohair@286: ohair@286: /** ohair@286: * Interface that can be implemented by {@link XMLStreamReader} to ohair@286: * be notified when it's recycled. ohair@286: * ohair@286: *

ohair@286: * This provides a filtering {@link XMLStreamReader} an opportunity to ohair@286: * recycle its inner {@link XMLStreamReader}. ohair@286: */ ohair@286: public interface RecycleAware { ohair@286: void onRecycled(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * {@link XMLStreamReaderFactory} implementation for SJSXP/JAXP RI. ohair@286: */ alanb@368: private static final class Zephyr extends XMLStreamReaderFactory { ohair@286: private final XMLInputFactory xif; ohair@286: ohair@286: private final ThreadLocal pool = new ThreadLocal(); ohair@286: ohair@286: /** ohair@286: * Sun StAX impl XMLReaderImpl.setInputSource() method via reflection. ohair@286: */ ohair@286: private final Method setInputSourceMethod; ohair@286: ohair@286: /** ohair@286: * Sun StAX impl XMLReaderImpl.reset() method via reflection. ohair@286: */ ohair@286: private final Method resetMethod; ohair@286: ohair@286: /** ohair@286: * The Sun StAX impl's {@link XMLStreamReader} implementation clas. ohair@286: */ ohair@286: private final Class zephyrClass; ohair@286: ohair@286: /** ohair@286: * Creates {@link Zephyr} instance if the given {@link XMLInputFactory} is the one ohair@286: * from Zephyr. ohair@286: */ ohair@286: public static @Nullable ohair@286: XMLStreamReaderFactory newInstance(XMLInputFactory xif) { ohair@286: // check if this is from Zephyr ohair@286: try { ohair@286: Class clazz = xif.createXMLStreamReader(new StringReader("")).getClass(); ohair@286: // JDK has different XMLStreamReader impl class. Even if we check for that, ohair@286: // it doesn't have setInputSource(InputSource). Let it use Default ohair@286: if(!(clazz.getName().startsWith("com.sun.xml.internal.stream.")) ) ohair@286: return null; // nope ohair@286: return new Zephyr(xif,clazz); ohair@286: } catch (NoSuchMethodException e) { ohair@286: return null; // this factory is not for zephyr ohair@286: } catch (XMLStreamException e) { ohair@286: return null; // impossible to fail to parse , but anyway ohair@286: } ohair@286: } ohair@286: ohair@286: public Zephyr(XMLInputFactory xif, Class clazz) throws NoSuchMethodException { ohair@286: zephyrClass = clazz; ohair@286: setInputSourceMethod = clazz.getMethod("setInputSource", InputSource.class); ohair@286: resetMethod = clazz.getMethod("reset"); ohair@286: ohair@286: try { ohair@286: // Turn OFF internal factory caching in Zephyr. ohair@286: // Santiago told me that this makes it thread-safe. ohair@286: xif.setProperty("reuse-instance", false); ohair@286: } catch (IllegalArgumentException e) { ohair@286: // falls through ohair@286: } ohair@286: this.xif = xif; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Fetchs an instance from the pool if available, otherwise null. ohair@286: */ ohair@286: private @Nullable XMLStreamReader fetch() { ohair@286: XMLStreamReader sr = pool.get(); ohair@286: if(sr==null) return null; ohair@286: pool.set(null); ohair@286: return sr; ohair@286: } ohair@286: ohair@286: public void doRecycle(XMLStreamReader r) { ohair@286: if(zephyrClass.isInstance(r)) ohair@286: pool.set(r); ohair@286: } ohair@286: ohair@286: public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) { ohair@286: try { ohair@286: XMLStreamReader xsr = fetch(); ohair@286: if(xsr==null) ohair@286: return xif.createXMLStreamReader(systemId,in); ohair@286: ohair@286: // try re-using this instance. ohair@286: InputSource is = new InputSource(systemId); ohair@286: is.setByteStream(in); ohair@286: reuse(xsr,is); ohair@286: return xsr; ohair@286: } catch (IllegalAccessException e) { ohair@286: throw new XMLReaderException("stax.cantCreate",e); ohair@286: } catch (InvocationTargetException e) { ohair@286: throw new XMLReaderException("stax.cantCreate",e); ohair@286: } catch (XMLStreamException e) { ohair@286: throw new XMLReaderException("stax.cantCreate",e); ohair@286: } ohair@286: } ohair@286: ohair@286: public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) { ohair@286: try { ohair@286: XMLStreamReader xsr = fetch(); ohair@286: if(xsr==null) ohair@286: return xif.createXMLStreamReader(systemId,in); ohair@286: ohair@286: // try re-using this instance. ohair@286: InputSource is = new InputSource(systemId); ohair@286: is.setCharacterStream(in); ohair@286: reuse(xsr,is); ohair@286: return xsr; ohair@286: } catch (IllegalAccessException e) { ohair@286: throw new XMLReaderException("stax.cantCreate",e); ohair@286: } catch (InvocationTargetException e) { ohair@286: Throwable cause = e.getCause(); ohair@286: if (cause == null) { ohair@286: cause = e; ohair@286: } ohair@286: throw new XMLReaderException("stax.cantCreate", cause); ohair@286: } catch (XMLStreamException e) { ohair@286: throw new XMLReaderException("stax.cantCreate",e); ohair@286: } ohair@286: } ohair@286: ohair@286: private void reuse(XMLStreamReader xsr, InputSource in) throws IllegalAccessException, InvocationTargetException { ohair@286: resetMethod.invoke(xsr); ohair@286: setInputSourceMethod.invoke(xsr,in); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Default {@link XMLStreamReaderFactory} implementation ohair@286: * that can work with any {@link XMLInputFactory}. ohair@286: * ohair@286: *

ohair@286: * {@link XMLInputFactory} is not required to be thread-safe, but ohair@286: * if the create method on this implementation is synchronized, ohair@286: * it may run into (see ohair@286: * race condition). Hence, using a XMLInputFactory per theread. ohair@286: */ ohair@286: public static final class Default extends XMLStreamReaderFactory { ohair@286: ohair@286: private final ThreadLocal xif = new ThreadLocal() { ohair@286: @Override ohair@286: public XMLInputFactory initialValue() { ohair@286: return getXMLInputFactory(); ohair@286: } ohair@286: }; ohair@286: ohair@286: public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) { ohair@286: try { ohair@286: return xif.get().createXMLStreamReader(systemId,in); ohair@286: } catch (XMLStreamException e) { ohair@286: throw new XMLReaderException("stax.cantCreate",e); ohair@286: } ohair@286: } ohair@286: ohair@286: public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) { ohair@286: try { ohair@286: return xif.get().createXMLStreamReader(systemId,in); ohair@286: } catch (XMLStreamException e) { ohair@286: throw new XMLReaderException("stax.cantCreate",e); ohair@286: } ohair@286: } ohair@286: ohair@286: public void doRecycle(XMLStreamReader r) { ohair@286: // there's no way to recycle with the default StAX API. ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: /** ohair@286: * Similar to {@link Default} but doesn't do any synchronization. ohair@286: * ohair@286: *

ohair@286: * This is useful when you know your {@link XMLInputFactory} is thread-safe by itself. ohair@286: */ ohair@286: public static class NoLock extends XMLStreamReaderFactory { ohair@286: private final XMLInputFactory xif; ohair@286: ohair@286: public NoLock(XMLInputFactory xif) { ohair@286: this.xif = xif; ohair@286: } ohair@286: ohair@286: public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) { ohair@286: try { ohair@286: return xif.createXMLStreamReader(systemId,in); ohair@286: } catch (XMLStreamException e) { ohair@286: throw new XMLReaderException("stax.cantCreate",e); ohair@286: } ohair@286: } ohair@286: ohair@286: public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) { ohair@286: try { ohair@286: return xif.createXMLStreamReader(systemId,in); ohair@286: } catch (XMLStreamException e) { ohair@286: throw new XMLReaderException("stax.cantCreate",e); ohair@286: } ohair@286: } ohair@286: ohair@286: public void doRecycle(XMLStreamReader r) { ohair@286: // there's no way to recycle with the default StAX API. ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Handles Woodstox's XIF but set properties to do the string interning. ohair@286: * Woodstox {@link XMLInputFactory} is thread safe. ohair@286: */ ohair@286: public static final class Woodstox extends NoLock { ohair@286: public Woodstox(XMLInputFactory xif) { ohair@286: super(xif); ohair@286: xif.setProperty("org.codehaus.stax2.internNsUris",true); ohair@286: } ohair@286: ohair@286: public XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs) { ohair@286: return super.doCreate(systemId, in, rejectDTDs); ohair@286: } ohair@286: ohair@286: public XMLStreamReader doCreate(String systemId, Reader in, boolean rejectDTDs) { ohair@286: return super.doCreate(systemId, in, rejectDTDs); ohair@286: } ohair@286: } ohair@286: ohair@286: private static Boolean getProperty(final String prop) { ohair@286: return AccessController.doPrivileged( ohair@286: new java.security.PrivilegedAction() { ohair@286: public Boolean run() { ohair@286: String value = System.getProperty(prop); ohair@286: return value != null ? Boolean.valueOf(value) : Boolean.FALSE; ohair@286: } ohair@286: } ohair@286: ); ohair@286: } ohair@286: }