ohair@286: /* ohair@286: * Copyright (c) 1997, 2010, 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.model.wsdl; ohair@286: ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLExtensible; ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLExtension; ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLObject; ohair@286: import com.sun.xml.internal.ws.resources.UtilMessages; ohair@286: import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants; ohair@286: import com.sun.istack.internal.NotNull; ohair@286: ohair@286: import javax.xml.stream.XMLStreamReader; ohair@286: import javax.xml.stream.Location; ohair@286: import javax.xml.namespace.QName; ohair@286: import javax.xml.ws.WebServiceException; ohair@286: import java.util.ArrayList; ohair@286: import java.util.HashSet; ohair@286: import java.util.List; ohair@286: import java.util.Set; ohair@286: ohair@286: import org.xml.sax.Locator; ohair@286: import org.xml.sax.helpers.LocatorImpl; ohair@286: ohair@286: /** ohair@286: * All the WSDL 1.1 elements that are extensible should subclass from this abstract implementation of ohair@286: * {@link WSDLExtensible} interface. ohair@286: * ohair@286: * @author Vivek Pandey ohair@286: * @author Kohsuke Kawaguchi ohair@286: */ ohair@286: abstract class AbstractExtensibleImpl extends AbstractObjectImpl implements WSDLExtensible { ohair@286: protected final Set extensions = new HashSet(); ohair@286: // this captures any wsdl extensions that are not understood by WSDLExtensionParsers ohair@286: // and have wsdl:required=true ohair@286: protected List notUnderstoodExtensions = ohair@286: new ArrayList(); ohair@286: ohair@286: protected AbstractExtensibleImpl(XMLStreamReader xsr) { ohair@286: super(xsr); ohair@286: } ohair@286: ohair@286: protected AbstractExtensibleImpl(String systemId, int lineNumber) { ohair@286: super(systemId, lineNumber); ohair@286: } ohair@286: ohair@286: public final Iterable getExtensions() { ohair@286: return extensions; ohair@286: } ohair@286: ohair@286: public final Iterable getExtensions(Class type) { ohair@286: // TODO: this is a rather stupid implementation ohair@286: List r = new ArrayList(extensions.size()); ohair@286: for (WSDLExtension e : extensions) { ohair@286: if(type.isInstance(e)) ohair@286: r.add(type.cast(e)); ohair@286: } ohair@286: return r; ohair@286: } ohair@286: ohair@286: public T getExtension(Class type) { ohair@286: for (WSDLExtension e : extensions) { ohair@286: if(type.isInstance(e)) ohair@286: return type.cast(e); ohair@286: } ohair@286: return null; ohair@286: } ohair@286: ohair@286: public void addExtension(WSDLExtension ex) { ohair@286: if(ex==null) ohair@286: // I don't trust plugins. So let's always check it, instead of making this an assertion ohair@286: throw new IllegalArgumentException(); ohair@286: extensions.add(ex); ohair@286: } ohair@286: ohair@286: /** ohair@286: * This can be used if a WSDL extension element that has wsdl:required=true ohair@286: * is not understood ohair@286: * @param extnEl ohair@286: * @param locator ohair@286: */ ohair@286: public void addNotUnderstoodExtension(QName extnEl, Locator locator) { ohair@286: notUnderstoodExtensions.add(new UnknownWSDLExtension(extnEl, locator)); ohair@286: } ohair@286: ohair@286: protected static class UnknownWSDLExtension implements WSDLExtension, WSDLObject { ohair@286: private final QName extnEl; ohair@286: private final Locator locator; ohair@286: public UnknownWSDLExtension(QName extnEl, Locator locator) { ohair@286: this.extnEl = extnEl; ohair@286: this.locator = locator; ohair@286: } ohair@286: public QName getName() { ohair@286: return extnEl; ohair@286: } ohair@286: @NotNull public Locator getLocation() { ohair@286: return locator; ohair@286: } ohair@286: public String toString(){ ohair@286: return extnEl + " "+ UtilMessages.UTIL_LOCATION( locator.getLineNumber(), locator.getSystemId()); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * This method should be called after freezing the WSDLModel ohair@286: * @return true if all wsdl required extensions on Port and Binding are understood ohair@286: */ ohair@286: public boolean areRequiredExtensionsUnderstood() { ohair@286: if (notUnderstoodExtensions.size() != 0) { ohair@286: StringBuilder buf = new StringBuilder("Unknown WSDL extensibility elements:"); ohair@286: for (UnknownWSDLExtension extn : notUnderstoodExtensions) ohair@286: buf.append('\n').append(extn.toString()); ohair@286: throw new WebServiceException(buf.toString()); ohair@286: } ohair@286: return true; ohair@286: } ohair@286: }