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.net.URL;
022: import java.util.Collections;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Properties;
026: import java.util.StringTokenizer;
027: import java.util.logging.Level;
028: import java.util.logging.Logger;
029:
030: import javax.annotation.PostConstruct;
031: import javax.annotation.Resource;
032: import javax.wsdl.Definition;
033: import javax.wsdl.Types;
034: import javax.wsdl.WSDLException;
035: import javax.wsdl.extensions.ExtensionRegistry;
036: import javax.wsdl.extensions.mime.MIMEPart;
037: import javax.wsdl.factory.WSDLFactory;
038: import javax.wsdl.xml.WSDLReader;
039: import javax.xml.bind.JAXBException;
040: import javax.xml.namespace.QName;
041:
042: import org.w3c.dom.Element;
043:
044: import com.ibm.wsdl.extensions.soap.SOAPHeaderImpl;
045: import com.ibm.wsdl.extensions.soap.SOAPHeaderSerializer;
046:
047: import org.apache.cxf.Bus;
048: import org.apache.cxf.BusException;
049: import org.apache.cxf.catalog.CatalogWSDLLocator;
050: import org.apache.cxf.catalog.OASISCatalogManager;
051: import org.apache.cxf.common.logging.LogUtils;
052: import org.apache.cxf.common.util.CacheMap;
053: import org.apache.cxf.common.util.PropertiesLoaderUtils;
054: import org.apache.cxf.wsdl.JAXBExtensionHelper;
055: import org.apache.cxf.wsdl.WSDLConstants;
056: import org.apache.cxf.wsdl.WSDLManager;
057:
058: /**
059: * WSDLManagerImpl
060: *
061: * @author dkulp
062: */
063: public class WSDLManagerImpl implements WSDLManager {
064:
065: private static final Logger LOG = LogUtils
066: .getL7dLogger(WSDLManagerImpl.class);
067:
068: private static final String EXTENSIONS_RESOURCE = "META-INF/extensions.xml";
069:
070: final ExtensionRegistry registry;
071: final WSDLFactory factory;
072: final Map<Object, Definition> definitionsMap;
073: private Bus bus;
074:
075: public WSDLManagerImpl() throws BusException {
076: try {
077: factory = WSDLFactory.newInstance();
078: registry = factory.newPopulatedExtensionRegistry();
079: registry.registerSerializer(Types.class,
080: WSDLConstants.SCHEMA_QNAME, new SchemaSerializer());
081: QName header = new QName(
082: "http://schemas.xmlsoap.org/wsdl/soap/", "header");
083: registry.registerDeserializer(MIMEPart.class, header,
084: new SOAPHeaderSerializer());
085: registry.registerSerializer(MIMEPart.class, header,
086: new SOAPHeaderSerializer());
087: registry.mapExtensionTypes(MIMEPart.class, header,
088: SOAPHeaderImpl.class);
089: } catch (WSDLException e) {
090: throw new BusException(e);
091: }
092: definitionsMap = new CacheMap<Object, Definition>();
093:
094: registerInitialExtensions();
095: }
096:
097: @Resource
098: public void setBus(Bus b) {
099: bus = b;
100: }
101:
102: @PostConstruct
103: public void register() {
104: if (null != bus) {
105: bus.setExtension(this , WSDLManager.class);
106: }
107: }
108:
109: public WSDLFactory getWSDLFactory() {
110: return factory;
111: }
112:
113: public Map<Object, Definition> getDefinitions() {
114: synchronized (definitionsMap) {
115: return Collections.unmodifiableMap(definitionsMap);
116: }
117: }
118:
119: /*
120: * (non-Javadoc)
121: *
122: * XXX - getExtensionRegistry()
123: *
124: * @see org.apache.cxf.wsdl.WSDLManager#getExtenstionRegistry()
125: */
126: public ExtensionRegistry getExtenstionRegistry() {
127: return registry;
128: }
129:
130: /*
131: * (non-Javadoc)
132: *
133: * @see org.apache.cxf.wsdl.WSDLManager#getDefinition(java.net.URL)
134: */
135: public Definition getDefinition(URL url) throws WSDLException {
136: synchronized (definitionsMap) {
137: if (definitionsMap.containsKey(url)) {
138: return definitionsMap.get(url);
139: }
140: }
141: Definition def = loadDefinition(url.toString());
142: synchronized (definitionsMap) {
143: definitionsMap.put(url, def);
144: }
145: return def;
146: }
147:
148: /*
149: * (non-Javadoc)
150: *
151: * @see org.apache.cxf.wsdl.WSDLManager#getDefinition(java.lang.String)
152: */
153: public Definition getDefinition(String url) throws WSDLException {
154: synchronized (definitionsMap) {
155: if (definitionsMap.containsKey(url)) {
156: return definitionsMap.get(url);
157: }
158: }
159: return loadDefinition(url);
160: }
161:
162: public Definition getDefinition(Element el) throws WSDLException {
163: synchronized (definitionsMap) {
164: if (definitionsMap.containsKey(el)) {
165: return definitionsMap.get(el);
166: }
167: }
168: WSDLReader reader = factory.newWSDLReader();
169: reader.setFeature("javax.wsdl.verbose", false);
170: reader.setExtensionRegistry(registry);
171: Definition def = reader.readWSDL("", el);
172: synchronized (definitionsMap) {
173: definitionsMap.put(el, def);
174: }
175: return def;
176: }
177:
178: public void addDefinition(Object key, Definition wsdl) {
179: synchronized (definitionsMap) {
180: definitionsMap.put(key, wsdl);
181: }
182: }
183:
184: private Definition loadDefinition(String url) throws WSDLException {
185: WSDLReader reader = factory.newWSDLReader();
186: reader.setFeature("javax.wsdl.verbose", false);
187: reader.setExtensionRegistry(registry);
188: CatalogWSDLLocator catLocator = new CatalogWSDLLocator(url,
189: OASISCatalogManager.getCatalogManager(bus));
190: ResourceManagerWSDLLocator wsdlLocator = new ResourceManagerWSDLLocator(
191: url, catLocator, bus);
192:
193: Definition def = reader.readWSDL(wsdlLocator);
194: synchronized (definitionsMap) {
195: definitionsMap.put(url, def);
196: }
197: return def;
198: }
199:
200: private void registerInitialExtensions() throws BusException {
201: Properties initialExtensions = null;
202: try {
203: initialExtensions = PropertiesLoaderUtils
204: .loadAllProperties(EXTENSIONS_RESOURCE, Thread
205: .currentThread().getContextClassLoader());
206: } catch (IOException ex) {
207: throw new BusException(ex);
208: }
209:
210: for (Iterator it = initialExtensions.keySet().iterator(); it
211: .hasNext();) {
212: StringTokenizer st = new StringTokenizer(initialExtensions
213: .getProperty((String) it.next()), "=");
214: String parentType = st.nextToken();
215: String elementType = st.nextToken();
216: try {
217: if (LOG.isLoggable(Level.FINE)) {
218: LOG.fine("Registering extension: " + elementType
219: + " for parent: " + parentType);
220: }
221: JAXBExtensionHelper.addExtensions(registry, parentType,
222: elementType, getClass().getClassLoader());
223: } catch (ClassNotFoundException ex) {
224: LOG.log(Level.WARNING, "EXTENSION_ADD_FAILED_MSG", ex);
225: } catch (JAXBException ex) {
226: LOG.log(Level.WARNING, "EXTENSION_ADD_FAILED_MSG", ex);
227: }
228: }
229: }
230:
231: }
|