001: /*
002: * $Id: WSDLServices.java 6115 2006-01-30 04:50:47Z dfs $
003: *
004: * Copyright 2006 Daniel F. Savarese
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.savarese.org/software/ApacheLicense-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package org.savarese.unicorn.util;
020:
021: import java.util.*;
022: import javax.management.*;
023: import javax.xml.namespace.QName;
024:
025: import javax.wsdl.*;
026:
027: import org.apache.wsif.*;
028: import org.apache.wsif.util.*;
029:
030: /**
031: * A singleton class that stores convenience methods for converting
032: * javax.wsdl information types to MBean information types.
033: */
034: public final class WSDLServices {
035:
036: private WSDLServices() {
037: }
038:
039: /**
040: * Converts the WSDL definition of a service to an MBeanInfo type.
041: *
042: * @param wsdl The WSDL definition.
043: * @param service The Web service description.
044: * @param operationMap A map for mapping operation names to
045: * javax.wsdl.Operation instances.
046: * @param portMap A map formapping port names to WSIFPort instances.
047: * @param inputNames A map for storing the input parameter names for
048: * each operaton.
049: * @param outputNames A map for storing the output result names for
050: * each operation.
051: * @return The MBeanInfo representation of the WSDL service.
052: */
053: public static final MBeanInfo wsdlToMBeanInfo(Definition wsdl,
054: Service service, Map<String, Operation> operationMap,
055: Map<String, WSIFPort> portMap,
056: Map<String, List<String>> inputNames,
057: Map<String, List<String>> outputNames)
058: throws WSIFException, ClassNotFoundException {
059: ArrayList<MBeanOperationInfo> operations = new ArrayList<MBeanOperationInfo>();
060: Map ports = service.getPorts();
061: WSIFServiceFactory sfactory = WSIFServiceFactory.newInstance();
062:
063: for (Object portValue : ports.values()) {
064: Port port = (Port) portValue;
065: PortType portType = port.getBinding().getPortType();
066: WSIFService wsifService;
067: WSIFPort wsifPort;
068:
069: try {
070: wsifService = sfactory.getService(wsdl, service,
071: portType);
072: wsifPort = wsifService.getPort();
073: } catch (WSIFException wsife) {
074: continue;
075: }
076:
077: for (Object operation : portType.getOperations()) {
078: Operation op = (Operation) operation;
079: Input input = op.getInput();
080: MBeanParameterInfo[] paramInfo = null;
081: String inputName = null;
082: String outputName = null;
083:
084: portMap.put(op.getName(), wsifPort);
085:
086: if (input != null) {
087: inputName = input.getName();
088:
089: List<String> names = new LinkedList<String>();
090: List parts = input.getMessage().getOrderedParts(
091: null);
092: _unwrap(wsdl, op.getName(), parts);
093: paramInfo = new MBeanParameterInfo[parts.size()];
094:
095: Iterator it = parts.iterator();
096: for (int i = 0; i < paramInfo.length; ++i) {
097: Part part = (Part) it.next();
098: String name = part.getName();
099: QName typeName = part.getTypeName();
100:
101: if (typeName == null)
102: typeName = part.getElementName();
103: if (typeName == null)
104: throw new WSIFException(
105: "No type name defined for part "
106: + name);
107:
108: String type;
109:
110: try {
111: type = Types.getClass(
112: typeName.getLocalPart()).getName();
113: } catch (ClassNotFoundException cnfe) {
114: // This is incorrect. Have to support for complex types.
115: type = "java.util.HashMap";
116: }
117:
118: paramInfo[i] = new MBeanParameterInfo(name,
119: type, null);
120: names.add(name);
121: }
122:
123: inputNames.put(op.getName(), names);
124: } else
125: inputNames.put(op.getName(), null);
126:
127: Output output = op.getOutput();
128: String returnType;
129:
130: if (output != null) {
131: outputName = output.getName();
132:
133: List<String> names = new LinkedList<String>();
134: List parts = output.getMessage().getOrderedParts(
135: null);
136: returnType = "java.util.Map";
137: _unwrap(wsdl, op.getName() + "Response", parts);
138:
139: for (Object p : parts)
140: names.add(((Part) p).getName());
141:
142: outputNames.put(op.getName(), names);
143: } else {
144: returnType = "void";
145: outputNames.put(op.getName(), null);
146: }
147:
148: operationMap.put(op.getName(), op);
149:
150: operations.add(new MBeanOperationInfo(op.getName(),
151: "Port: " + port.getName(), paramInfo,
152: returnType, MBeanOperationInfo.UNKNOWN));
153: }
154: }
155:
156: MBeanOperationInfo[] opinfo = new MBeanOperationInfo[operations
157: .size()];
158: opinfo = operations.toArray(opinfo);
159:
160: QName qname = service.getQName();
161: String name = "unknown", desc = "unknown";
162:
163: if (qname != null) {
164: name = qname.getLocalPart();
165: desc = qname.toString();
166: }
167:
168: return new MBeanInfo(name, desc, null, null, opinfo, null);
169: }
170:
171: static void _unwrap(Definition wsdl, String op, List parts)
172: throws WSIFException {
173: Part part = WSIFUtils.getWrappedDocLiteralPart(parts, op);
174:
175: if (part != null) {
176: List unwrapped = WSIFUtils.unWrapPart(part, wsdl);
177: parts.remove(part);
178: parts.addAll(unwrapped);
179: }
180: }
181:
182: }
|