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.binding;
019:
020: import java.util.Collection;
021:
022: import javax.annotation.PostConstruct;
023: import javax.annotation.Resource;
024: import javax.wsdl.Binding;
025: import javax.wsdl.BindingFault;
026: import javax.wsdl.BindingInput;
027: import javax.wsdl.BindingOperation;
028: import javax.wsdl.BindingOutput;
029: import javax.wsdl.extensions.ElementExtensible;
030: import javax.wsdl.extensions.ExtensibilityElement;
031: import javax.xml.namespace.QName;
032:
033: import org.apache.cxf.Bus;
034: import org.apache.cxf.endpoint.Endpoint;
035: import org.apache.cxf.service.Service;
036: import org.apache.cxf.service.model.AbstractPropertiesHolder;
037: import org.apache.cxf.service.model.BindingInfo;
038: import org.apache.cxf.service.model.BindingOperationInfo;
039: import org.apache.cxf.service.model.ServiceInfo;
040: import org.apache.cxf.transport.ChainInitiationObserver;
041: import org.apache.cxf.transport.Destination;
042: import org.apache.cxf.wsdl11.WSDLBindingFactory;
043:
044: import static org.apache.cxf.helpers.CastUtils.cast;
045:
046: public abstract class AbstractBindingFactory implements BindingFactory,
047: WSDLBindingFactory {
048:
049: public static final String DATABINDING_DISABLED = "databinding.disabled";
050:
051: Collection<String> activationNamespaces;
052:
053: Bus bus;
054:
055: @PostConstruct
056: void registerWithBindingManager() {
057: BindingFactoryManager manager = bus
058: .getExtension(BindingFactoryManager.class);
059: for (String ns : activationNamespaces) {
060: manager.registerBindingFactory(ns, this );
061: }
062: }
063:
064: /**
065: * Creates a "default" BindingInfo object for the service. Called by
066: * createBindingInfo(Service service, String binding, Object config) to actually
067: * create the BindingInfo. Can return a subclass which can then process
068: * the extensors within the subclass.
069: * @param service
070: * @return
071: */
072: public BindingInfo createBindingInfo(ServiceInfo service,
073: String namespace, Object config) {
074: return new BindingInfo(service, namespace);
075: }
076:
077: /**
078: * Creates a "default" BindingInfo object for the service. Can return a subclass
079: * which can then process the extensors within the subclass. By default, just
080: * creates it for the first ServiceInfo in the service
081: */
082: public BindingInfo createBindingInfo(Service service,
083: String namespace, Object config) {
084: BindingInfo bi = createBindingInfo(service.getServiceInfos()
085: .get(0), namespace, config);
086: if (bi.getName() == null) {
087: bi.setName(new QName(service.getName().getNamespaceURI(),
088: service.getName().getLocalPart() + "Binding"));
089: }
090: return bi;
091: }
092:
093: /**
094: * Copies extensors from the Binding to BindingInfo.
095: * @param service
096: * @param binding
097: * @return
098: */
099: public BindingInfo createBindingInfo(ServiceInfo service,
100: Binding binding, String ns) {
101:
102: BindingInfo bi = createBindingInfo(service, ns, null);
103: return initializeBindingInfo(service, binding, bi);
104: }
105:
106: protected BindingInfo initializeBindingInfo(ServiceInfo service,
107: Binding binding, BindingInfo bi) {
108: bi.setName(binding.getQName());
109: copyExtensors(bi, binding, null);
110:
111: for (BindingOperation bop : cast(
112: binding.getBindingOperations(), BindingOperation.class)) {
113: String inName = null;
114: String outName = null;
115: if (bop.getBindingInput() != null) {
116: inName = bop.getBindingInput().getName();
117: }
118: if (bop.getBindingOutput() != null) {
119: outName = bop.getBindingOutput().getName();
120: }
121: String portTypeNs = binding.getPortType().getQName()
122: .getNamespaceURI();
123: QName opName = new QName(portTypeNs, bop.getName());
124: BindingOperationInfo bop2 = bi.getOperation(opName);
125: if (bop2 == null) {
126: bop2 = bi.buildOperation(opName, inName, outName);
127: if (bop2 != null) {
128: bi.addOperation(bop2);
129: }
130: }
131: if (bop2 != null) {
132: copyExtensors(bop2, bop, bop2);
133: if (bop.getBindingInput() != null) {
134: copyExtensors(bop2.getInput(), bop
135: .getBindingInput(), bop2);
136: }
137: if (bop.getBindingOutput() != null) {
138: copyExtensors(bop2.getOutput(), bop
139: .getBindingOutput(), bop2);
140: }
141: for (BindingFault f : cast(bop.getBindingFaults()
142: .values(), BindingFault.class)) {
143: copyExtensors(bop2.getFault(new QName(service
144: .getTargetNamespace(), f.getName())), bop
145: .getBindingFault(f.getName()), bop2);
146: }
147: }
148: }
149: return bi;
150: }
151:
152: private void copyExtensors(AbstractPropertiesHolder info,
153: ElementExtensible extElement, BindingOperationInfo bop) {
154: if (info != null) {
155: for (ExtensibilityElement ext : cast(extElement
156: .getExtensibilityElements(),
157: ExtensibilityElement.class)) {
158: info.addExtensor(ext);
159: if (bop != null && extElement instanceof BindingInput) {
160: addMessageFromBinding(ext, bop, true);
161: }
162: if (bop != null && extElement instanceof BindingOutput) {
163: addMessageFromBinding(ext, bop, false);
164: }
165: }
166: }
167: }
168:
169: protected void addMessageFromBinding(ExtensibilityElement ext,
170: BindingOperationInfo bop, boolean isInput) {
171: }
172:
173: public void addListener(Destination d, Endpoint e) {
174: ChainInitiationObserver observer = new ChainInitiationObserver(
175: e, bus);
176:
177: d.setMessageObserver(observer);
178: }
179:
180: public Bus getBus() {
181: return bus;
182: }
183:
184: @Resource(name="bus")
185: public void setBus(Bus bus) {
186: this .bus = bus;
187: }
188:
189: public Collection<String> getActivationNamespaces() {
190: return activationNamespaces;
191: }
192:
193: @Resource(name="activationNamespaces")
194: public void setActivationNamespaces(
195: Collection<String> activationNamespaces) {
196: this.activationNamespaces = activationNamespaces;
197: }
198:
199: }
|