001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.wsdl11;
019:
020: import java.io.IOException;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Properties;
028: import java.util.StringTokenizer;
029: import java.util.logging.Level;
030: import java.util.logging.Logger;
031: import javax.wsdl.Definition;
032: import javax.wsdl.Import;
033: import javax.wsdl.extensions.ExtensionRegistry;
034: import javax.wsdl.extensions.mime.MIMEPart;
035: import javax.wsdl.factory.WSDLFactory;
036: import javax.wsdl.xml.WSDLReader;
037: import javax.xml.bind.JAXBException;
038: import javax.xml.namespace.QName;
039:
040: import com.ibm.wsdl.extensions.soap.SOAPHeaderImpl;
041: import com.ibm.wsdl.extensions.soap.SOAPHeaderSerializer;
042: import org.apache.cxf.Bus;
043: import org.apache.cxf.BusException;
044: import org.apache.cxf.catalog.OASISCatalogManager;
045: import org.apache.cxf.common.i18n.Message;
046: import org.apache.cxf.common.logging.LogUtils;
047: import org.apache.cxf.common.util.PropertiesLoaderUtils;
048: import org.apache.cxf.helpers.CastUtils;
049: import org.apache.cxf.wsdl.JAXBExtensionHelper;
050: import org.apache.cxf.wsdl.WSDLBuilder;
051: import org.apache.cxf.wsdl.WSDLExtensibilityPlugin;
052:
053: public class WSDLDefinitionBuilder implements WSDLBuilder<Definition> {
054:
055: protected static final Logger LOG = LogUtils
056: .getL7dLogger(WSDLDefinitionBuilder.class);
057: private static final String EXTENSIONS_RESOURCE = "META-INF/extensions.xml";
058: private static final String WSDL_PLUGIN_RESOURCE = "META-INF/wsdl.plugin.xml";
059:
060: protected WSDLReader wsdlReader;
061: protected Definition wsdlDefinition;
062: final WSDLFactory wsdlFactory;
063: final ExtensionRegistry registry;
064: private List<Definition> importedDefinitions = new ArrayList<Definition>();
065:
066: private final Map<String, WSDLExtensibilityPlugin> wsdlPlugins = new HashMap<String, WSDLExtensibilityPlugin>();
067:
068: private Bus bus;
069:
070: public WSDLDefinitionBuilder(Bus b) {
071: this ();
072: this .bus = b;
073: }
074:
075: public WSDLDefinitionBuilder() {
076: try {
077: wsdlFactory = WSDLFactory.newInstance();
078: registry = wsdlFactory.newPopulatedExtensionRegistry();
079: QName header = new QName(
080: "http://schemas.xmlsoap.org/wsdl/soap/", "header");
081: registry.registerDeserializer(MIMEPart.class, header,
082: new SOAPHeaderSerializer());
083: registry.registerSerializer(MIMEPart.class, header,
084: new SOAPHeaderSerializer());
085: registry.mapExtensionTypes(MIMEPart.class, header,
086: SOAPHeaderImpl.class);
087:
088: registerInitialExtensions();
089: wsdlReader = wsdlFactory.newWSDLReader();
090: // TODO enable the verbose if in verbose mode.
091: wsdlReader.setFeature("javax.wsdl.verbose", false);
092: wsdlReader.setFeature("javax.wsdl.importDocuments", true);
093: } catch (Exception e) {
094: throw new RuntimeException(e);
095: }
096: }
097:
098: public WSDLDefinitionBuilder(boolean requirePlugins) {
099: this ();
100: if (requirePlugins) {
101: registerWSDLExtensibilityPlugins();
102: }
103: }
104:
105: public void setBus(Bus b) {
106: this .bus = b;
107: }
108:
109: public Definition build(String wsdlURL) {
110: parseWSDL(wsdlURL);
111: return wsdlDefinition;
112: }
113:
114: protected void parseWSDL(String wsdlURL) {
115: try {
116:
117: wsdlReader.setExtensionRegistry(registry);
118:
119: WSDLLocatorImpl wsdlLocator = new WSDLLocatorImpl(wsdlURL);
120: wsdlLocator.setCatalogResolver(OASISCatalogManager
121: .getCatalogManager(bus).getCatalog());
122: wsdlDefinition = wsdlReader.readWSDL(wsdlLocator);
123:
124: parseImports(wsdlDefinition);
125: } catch (Exception we) {
126: Message msg = new Message("FAIL_TO_CREATE_WSDL_DEFINITION",
127: LOG, wsdlURL, we.getMessage());
128: throw new RuntimeException(msg.toString(), we);
129: }
130: }
131:
132: public static Collection<Import> getImports(final Definition wsdlDef) {
133: Collection<Import> importList = new ArrayList<Import>();
134: Map imports = wsdlDef.getImports();
135: for (Iterator iter = imports.keySet().iterator(); iter
136: .hasNext();) {
137: String uri = (String) iter.next();
138: List<Import> lst = CastUtils.cast((List) imports.get(uri));
139: importList.addAll(lst);
140: }
141: return importList;
142: }
143:
144: private void parseImports(Definition def) {
145: for (Import impt : getImports(def)) {
146: if (!importedDefinitions.contains(impt.getDefinition())) {
147: importedDefinitions.add(impt.getDefinition());
148: parseImports(impt.getDefinition());
149: }
150: }
151: }
152:
153: public List<Definition> getImportedDefinitions() {
154: return importedDefinitions;
155: }
156:
157: private void registerInitialExtensions() throws BusException {
158: Properties initialExtensions = null;
159: try {
160: initialExtensions = PropertiesLoaderUtils
161: .loadAllProperties(EXTENSIONS_RESOURCE, Thread
162: .currentThread().getContextClassLoader());
163: } catch (IOException ex) {
164: throw new BusException(ex);
165: }
166:
167: for (Iterator it = initialExtensions.keySet().iterator(); it
168: .hasNext();) {
169: StringTokenizer st = new StringTokenizer(initialExtensions
170: .getProperty((String) it.next()), "=");
171: String parentType = st.nextToken();
172: String elementType = st.nextToken();
173: try {
174: if (LOG.isLoggable(Level.FINE)) {
175: LOG.fine("Registering extension: " + elementType
176: + " for parent: " + parentType);
177: }
178: JAXBExtensionHelper.addExtensions(registry, parentType,
179: elementType, getClass().getClassLoader());
180: } catch (ClassNotFoundException ex) {
181: LOG.log(Level.WARNING, "EXTENSION_ADD_FAILED_MSG", ex);
182: } catch (JAXBException ex) {
183: LOG.log(Level.WARNING, "EXTENSION_ADD_FAILED_MSG", ex);
184: }
185: }
186: }
187:
188: public ExtensionRegistry getExtenstionRegistry() {
189: return registry;
190: }
191:
192: public WSDLFactory getWSDLFactory() {
193: return wsdlFactory;
194: }
195:
196: public WSDLReader getWSDLReader() {
197: return wsdlReader;
198: }
199:
200: public Map<String, WSDLExtensibilityPlugin> getWSDLPlugins() {
201: return wsdlPlugins;
202: }
203:
204: private void registerWSDLExtensibilityPlugins() {
205: Properties initialExtensions = null;
206: try {
207: initialExtensions = PropertiesLoaderUtils
208: .loadAllProperties(WSDL_PLUGIN_RESOURCE, Thread
209: .currentThread().getContextClassLoader());
210: } catch (IOException ex) {
211: throw new RuntimeException(ex);
212: }
213:
214: for (Iterator it = initialExtensions.keySet().iterator(); it
215: .hasNext();) {
216: String key = (String) it.next();
217: String pluginClz = initialExtensions.getProperty(key);
218: try {
219: if (LOG.isLoggable(Level.FINE)) {
220: LOG.fine("Registering : " + pluginClz
221: + " for type: " + key);
222: }
223:
224: WSDLExtensibilityPlugin plugin = (WSDLExtensibilityPlugin) Class
225: .forName(pluginClz).newInstance();
226: plugin.setExtensionRegistry(registry);
227: wsdlPlugins.put(key, plugin);
228: } catch (Exception ex) {
229: LOG.log(Level.WARNING, "EXTENSION_ADD_FAILED_MSG", ex);
230: }
231: }
232: }
233: }
|