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.service.model;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.List;
023:
024: import javax.xml.namespace.QName;
025:
026: import org.apache.cxf.endpoint.Endpoint;
027: import org.apache.cxf.message.Exchange;
028: import org.apache.cxf.service.Service;
029: import org.apache.ws.commons.schema.XmlSchemaAnnotated;
030: import org.apache.ws.commons.schema.XmlSchemaComplexType;
031: import org.apache.ws.commons.schema.XmlSchemaElement;
032: import org.apache.ws.commons.schema.XmlSchemaSequence;
033:
034: public final class ServiceModelUtil {
035:
036: private ServiceModelUtil() {
037: }
038:
039: public static Service getService(Exchange exchange) {
040: return exchange.get(Service.class);
041: }
042:
043: public static String getTargetNamespace(Exchange exchange) {
044: //all ServiceInfo's will have the same target namespace
045: return getService(exchange).getServiceInfos().get(0)
046: .getTargetNamespace();
047: }
048:
049: public static BindingOperationInfo getOperation(Exchange exchange,
050: String opName) {
051: Endpoint ep = exchange.get(Endpoint.class);
052: BindingInfo service = ep.getEndpointInfo().getBinding();
053:
054: for (BindingOperationInfo b : service.getOperations()) {
055: if (b.getName().getLocalPart().equals(opName)) {
056: return b;
057: }
058: }
059: return null;
060: }
061:
062: public static BindingOperationInfo getOperation(Exchange exchange,
063: QName opName) {
064: Endpoint ep = exchange.get(Endpoint.class);
065: BindingInfo service = ep.getEndpointInfo().getBinding();
066: return service.getOperation(opName);
067: }
068:
069: public static SchemaInfo getSchema(ServiceInfo serviceInfo,
070: MessagePartInfo messagePartInfo) {
071: SchemaInfo schemaInfo = null;
072: String tns = null;
073: if (messagePartInfo.isElement()) {
074: tns = messagePartInfo.getElementQName().getNamespaceURI();
075: } else {
076: tns = messagePartInfo.getTypeQName().getNamespaceURI();
077: }
078: for (SchemaInfo schema : serviceInfo.getSchemas()) {
079: if (tns.equals(schema.getNamespaceURI())) {
080: schemaInfo = schema;
081: }
082: }
083: return schemaInfo;
084: }
085:
086: public static List<String> getOperationInputPartNames(
087: OperationInfo operation) {
088: List<String> names = new ArrayList<String>();
089: List<MessagePartInfo> parts = operation.getInput()
090: .getMessageParts();
091: if (parts == null && parts.size() == 0) {
092: return names;
093: }
094:
095: for (MessagePartInfo part : parts) {
096: XmlSchemaAnnotated schema = part.getXmlSchema();
097:
098: if (schema instanceof XmlSchemaElement
099: && ((XmlSchemaElement) schema).getSchemaType() instanceof XmlSchemaComplexType) {
100: XmlSchemaElement element = (XmlSchemaElement) schema;
101: XmlSchemaComplexType cplxType = (XmlSchemaComplexType) element
102: .getSchemaType();
103: XmlSchemaSequence seq = (XmlSchemaSequence) cplxType
104: .getParticle();
105: if (seq == null || seq.getItems() == null) {
106: return names;
107: }
108: for (int i = 0; i < seq.getItems().getCount(); i++) {
109: XmlSchemaElement elChild = (XmlSchemaElement) seq
110: .getItems().getItem(i);
111: names.add(elChild.getName());
112: }
113: } else {
114: names.add(part.getConcreteName().getLocalPart());
115: }
116: }
117: return names;
118: }
119:
120: public static EndpointInfo findBestEndpointInfo(QName qn,
121: List<ServiceInfo> serviceInfos) {
122: for (ServiceInfo serviceInfo : serviceInfos) {
123: Collection<EndpointInfo> eps = serviceInfo.getEndpoints();
124: for (EndpointInfo ep : eps) {
125: if (ep.getInterface().getName().equals(qn)) {
126: return ep;
127: }
128: }
129: }
130:
131: EndpointInfo best = null;
132: for (ServiceInfo serviceInfo : serviceInfos) {
133: Collection<EndpointInfo> eps = serviceInfo.getEndpoints();
134: for (EndpointInfo ep : eps) {
135: if (best == null) {
136: best = ep;
137: }
138: if (ep.getTransportId().equals(
139: "http://schemas.xmlsoap.org/wsdl/soap/")) {
140: return ep;
141: }
142: }
143: }
144:
145: return best;
146: }
147: }
|