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.model.wsdl;
27 
28 import com.sun.xml.internal.ws.api.model.wsdl.WSDLExtensible;
29 import com.sun.xml.internal.ws.api.model.wsdl.WSDLExtension;
30 import com.sun.xml.internal.ws.api.model.wsdl.WSDLObject;
31 import com.sun.xml.internal.ws.resources.UtilMessages;
32 import com.sun.istack.internal.NotNull;
33 
34 import javax.xml.stream.XMLStreamReader;
35 import javax.xml.namespace.QName;
36 import javax.xml.ws.WebServiceException;
37 import java.util.ArrayList;
38 import java.util.HashSet;
39 import java.util.List;
40 import java.util.Set;
41 
42 import org.xml.sax.Locator;
43 
44 /**
45  * All the WSDL 1.1 elements that are extensible should subclass from this abstract implementation of
46  * {@link WSDLExtensible} interface.
47  *
48  * @author Vivek Pandey
49  * @author Kohsuke Kawaguchi
50  */
51 abstract class AbstractExtensibleImpl extends AbstractObjectImpl implements WSDLExtensible {
52     protected final Set<WSDLExtension> extensions = new HashSet<WSDLExtension>();
53     // this captures any wsdl extensions that are not understood by WSDLExtensionParsers
54     // and have wsdl:required=true
55     protected List<UnknownWSDLExtension> notUnderstoodExtensions =
56             new ArrayList<UnknownWSDLExtension>();
57 
AbstractExtensibleImpl(XMLStreamReader xsr)58     protected AbstractExtensibleImpl(XMLStreamReader xsr) {
59         super(xsr);
60     }
61 
AbstractExtensibleImpl(String systemId, int lineNumber)62     protected AbstractExtensibleImpl(String systemId, int lineNumber) {
63         super(systemId, lineNumber);
64     }
65 
getExtensions()66     public final Iterable<WSDLExtension> getExtensions() {
67         return extensions;
68     }
69 
getExtensions(Class<T> type)70     public final <T extends WSDLExtension> Iterable<T> getExtensions(Class<T> type) {
71         // TODO: this is a rather stupid implementation
72         List<T> r = new ArrayList<T>(extensions.size());
73         for (WSDLExtension e : extensions) {
74             if(type.isInstance(e))
75                 r.add(type.cast(e));
76         }
77         return r;
78     }
79 
getExtension(Class<T> type)80     public <T extends WSDLExtension> T getExtension(Class<T> type) {
81         for (WSDLExtension e : extensions) {
82             if(type.isInstance(e))
83                 return type.cast(e);
84         }
85         return null;
86     }
87 
addExtension(WSDLExtension ex)88     public void addExtension(WSDLExtension ex) {
89         if(ex==null)
90             // I don't trust plugins. So let's always check it, instead of making this an assertion
91             throw new IllegalArgumentException();
92         extensions.add(ex);
93     }
94 
getNotUnderstoodExtensions()95     public List<? extends UnknownWSDLExtension> getNotUnderstoodExtensions() {
96         return notUnderstoodExtensions;
97     }
98 
99     /**
100      * This can be used if a WSDL extension element that has wsdl:required=true
101      * is not understood
102      * @param extnEl
103      * @param locator
104      */
addNotUnderstoodExtension(QName extnEl, Locator locator)105     public void addNotUnderstoodExtension(QName extnEl, Locator locator) {
106         notUnderstoodExtensions.add(new UnknownWSDLExtension(extnEl, locator));
107     }
108 
109     protected static class UnknownWSDLExtension implements WSDLExtension, WSDLObject {
110         private final QName extnEl;
111         private final Locator locator;
UnknownWSDLExtension(QName extnEl, Locator locator)112         public UnknownWSDLExtension(QName extnEl, Locator locator) {
113             this.extnEl = extnEl;
114             this.locator = locator;
115         }
getName()116         public QName getName() {
117             return extnEl;
118         }
getLocation()119         @NotNull public Locator getLocation() {
120             return locator;
121         }
toString()122         public String toString(){
123            return extnEl + " "+ UtilMessages.UTIL_LOCATION( locator.getLineNumber(), locator.getSystemId());
124        }
125     }
126 
127     /**
128      * This method should be called after freezing the WSDLModel
129      * @return true if all wsdl required extensions on Port and Binding are understood
130      */
areRequiredExtensionsUnderstood()131     public boolean areRequiredExtensionsUnderstood() {
132         if (notUnderstoodExtensions.size() != 0) {
133             StringBuilder buf = new StringBuilder("Unknown WSDL extensibility elements:");
134             for (UnknownWSDLExtension extn : notUnderstoodExtensions)
135                 buf.append('\n').append(extn.toString());
136             throw new WebServiceException(buf.toString());
137         }
138         return true;
139     }
140 }
141