001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.ws.security.opt.impl.util;
024:
025: import javax.xml.stream.StreamFilter;
026: import org.xml.sax.InputSource;
027: import javax.xml.ws.WebServiceException;
028: import javax.xml.stream.XMLInputFactory;
029: import javax.xml.stream.XMLStreamReader;
030: import java.io.InputStream;
031: import java.io.Reader;
032: import java.net.URL;
033:
034: /**
035: * <p>A factory to create XML and FI parsers.</p>
036: *
037: * @author Santiago.PericasGeertsen@sun.com
038: */
039: public class XMLStreamReaderFactory {
040:
041: /**
042: * StAX input factory shared by all threads.
043: */
044: static final XMLInputFactory xmlInputFactory;
045:
046: /**
047: * FI stream reader for each thread.
048: */
049: static final ThreadLocal fiStreamReader = new ThreadLocal();
050:
051: /**
052: * Zephyr's stream reader for each thread.
053: */
054: static final ThreadLocal<XMLStreamReader> xmlStreamReader = new ThreadLocal<XMLStreamReader>();
055:
056: static {
057: // Use StAX pluggability layer to get factory instance
058: xmlInputFactory = XMLInputFactory.newInstance();
059: xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE,
060: Boolean.TRUE);
061:
062: try {
063: // Turn OFF internal factory caching in Zephyr -- not thread safe
064: xmlInputFactory
065: .setProperty("reuse-instance", Boolean.FALSE);
066: } catch (IllegalArgumentException e) {
067: // falls through
068: }
069: }
070:
071: // -- XML ------------------------------------------------------------
072:
073: /**
074: * Returns a fresh StAX parser created from an InputSource. Use this
075: * method when concurrent instances are needed within a single thread.
076: *
077: * TODO: Reject DTDs?
078: */
079: public static XMLStreamReader createFreshXMLStreamReader(
080: InputSource source, boolean rejectDTDs) {
081: try {
082: synchronized (xmlInputFactory) {
083: // Char stream available?
084: if (source.getCharacterStream() != null) {
085: return xmlInputFactory
086: .createXMLStreamReader(
087: source.getSystemId(), source
088: .getCharacterStream());
089: }
090:
091: // Byte stream available?
092: if (source.getByteStream() != null) {
093: return xmlInputFactory.createXMLStreamReader(source
094: .getSystemId(), source.getByteStream());
095: }
096:
097: // Otherwise, open URI
098: return xmlInputFactory.createXMLStreamReader(source
099: .getSystemId(), new URL(source.getSystemId())
100: .openStream());
101: }
102: } catch (Exception e) {
103: throw new WebServiceException("stax.cantCreate", e);
104: }
105: }
106:
107: /**
108: * This factory method would be used for example when caller wants to close the stream.
109: */
110: public static XMLStreamReader createFreshXMLStreamReader(
111: String systemId, InputStream stream) {
112: try {
113: synchronized (xmlInputFactory) {
114: // Otherwise, open URI
115: return xmlInputFactory.createXMLStreamReader(systemId,
116: stream);
117: }
118: } catch (Exception e) {
119: throw new WebServiceException("stax.cantCreate", e);
120: }
121: }
122:
123: /**
124: * This factory method would be used for example when caller wants to close the stream.
125: */
126: public static XMLStreamReader createFreshXMLStreamReader(
127: String systemId, Reader reader) {
128: try {
129: synchronized (xmlInputFactory) {
130: // Otherwise, open URI
131: return xmlInputFactory.createXMLStreamReader(systemId,
132: reader);
133: }
134: } catch (Exception e) {
135: throw new WebServiceException("stax.cantCreate", e);
136: }
137: }
138:
139: public static XMLStreamReader createFilteredXMLStreamReader(
140: XMLStreamReader reader, StreamFilter filter) {
141: try {
142: synchronized (xmlInputFactory) {
143: // Otherwise, open URI
144: return xmlInputFactory.createFilteredReader(reader,
145: filter);
146: }
147: } catch (Exception e) {
148: throw new WebServiceException("stax.cantCreate", e);
149: }
150: }
151:
152: }
|