001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.model.wsdl;
038:
039: import com.sun.xml.ws.api.model.wsdl.WSDLExtensible;
040: import com.sun.xml.ws.api.model.wsdl.WSDLExtension;
041: import com.sun.xml.ws.api.model.wsdl.WSDLObject;
042: import com.sun.xml.ws.resources.UtilMessages;
043: import com.sun.xml.ws.wsdl.parser.WSDLConstants;
044: import com.sun.istack.NotNull;
045:
046: import javax.xml.stream.XMLStreamReader;
047: import javax.xml.stream.Location;
048: import javax.xml.namespace.QName;
049: import javax.xml.ws.WebServiceException;
050: import java.util.ArrayList;
051: import java.util.HashSet;
052: import java.util.List;
053: import java.util.Set;
054:
055: import org.xml.sax.Locator;
056: import org.xml.sax.helpers.LocatorImpl;
057:
058: /**
059: * All the WSDL 1.1 elements that are extensible should subclass from this abstract implementation of
060: * {@link WSDLExtensible} interface.
061: *
062: * @author Vivek Pandey
063: * @author Kohsuke Kawaguchi
064: */
065: abstract class AbstractExtensibleImpl extends AbstractObjectImpl
066: implements WSDLExtensible {
067: protected final Set<WSDLExtension> extensions = new HashSet<WSDLExtension>();
068: // this captures any wsdl extensions that are not understood by WSDLExtensionParsers
069: // and have wsdl:required=true
070: protected List<UnknownWSDLExtension> notUnderstoodExtensions = new ArrayList<UnknownWSDLExtension>();
071:
072: protected AbstractExtensibleImpl(XMLStreamReader xsr) {
073: super (xsr);
074: }
075:
076: protected AbstractExtensibleImpl(String systemId, int lineNumber) {
077: super (systemId, lineNumber);
078: }
079:
080: public final Iterable<WSDLExtension> getExtensions() {
081: return extensions;
082: }
083:
084: public final <T extends WSDLExtension> Iterable<T> getExtensions(
085: Class<T> type) {
086: // TODO: this is a rather stupid implementation
087: List<T> r = new ArrayList<T>(extensions.size());
088: for (WSDLExtension e : extensions) {
089: if (type.isInstance(e))
090: r.add(type.cast(e));
091: }
092: return r;
093: }
094:
095: public <T extends WSDLExtension> T getExtension(Class<T> type) {
096: for (WSDLExtension e : extensions) {
097: if (type.isInstance(e))
098: return type.cast(e);
099: }
100: return null;
101: }
102:
103: public void addExtension(WSDLExtension ex) {
104: if (ex == null)
105: // I don't trust plugins. So let's always check it, instead of making this an assertion
106: throw new IllegalArgumentException();
107: extensions.add(ex);
108: }
109:
110: /**
111: * This can be used if a WSDL extension element that has wsdl:required=true
112: * is not understood
113: * @param extnEl
114: * @param locator
115: */
116: public void addNotUnderstoodExtension(QName extnEl, Locator locator) {
117: notUnderstoodExtensions.add(new UnknownWSDLExtension(extnEl,
118: locator));
119: }
120:
121: protected class UnknownWSDLExtension implements WSDLExtension,
122: WSDLObject {
123: private final QName extnEl;
124: private final Locator locator;
125:
126: public UnknownWSDLExtension(QName extnEl, Locator locator) {
127: this .extnEl = extnEl;
128: this .locator = locator;
129: }
130:
131: public QName getName() {
132: return extnEl;
133: }
134:
135: @NotNull
136: public Locator getLocation() {
137: return locator;
138: }
139:
140: public String toString() {
141: return extnEl
142: + " "
143: + UtilMessages.UTIL_LOCATION(locator
144: .getLineNumber(), locator.getSystemId());
145: }
146: }
147:
148: /**
149: * This method should be called after freezing the WSDLModel
150: * @return true if all wsdl required extensions on Port and Binding are understood
151: */
152: public boolean areRequiredExtensionsUnderstood() {
153: if (notUnderstoodExtensions.size() != 0) {
154: StringBuilder buf = new StringBuilder(
155: "Unknown WSDL extensibility elements:");
156: for (UnknownWSDLExtension extn : notUnderstoodExtensions)
157: buf.append('\n').append(extn.toString());
158: throw new WebServiceException(buf.toString());
159: }
160: return true;
161: }
162: }
|