001: /**
002: * Copyright 2003 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.providers.simplewebservice.rpc;
013:
014: import com.sun.portal.providers.simplewebservice.WebServiceDescriptor;
015: import com.sun.portal.providers.simplewebservice.SimpleWebServiceParameter;
016: import com.sun.portal.providers.simplewebservice.ParameterDescriptor;
017: import com.sun.portal.providers.simplewebservice.util.XList;
018: import com.sun.xml.rpc.encoding.CombinedSerializer;
019: import com.sun.xml.rpc.encoding.SingletonSerializerFactory;
020: import com.sun.xml.rpc.encoding.SingletonDeserializerFactory;
021: import com.sun.xml.rpc.encoding.literal.LiteralFragmentSerializer;
022:
023: import javax.xml.rpc.Call;
024: import javax.xml.rpc.Service;
025: import javax.xml.rpc.ServiceException;
026: import javax.xml.rpc.encoding.TypeMappingRegistry;
027: import javax.xml.rpc.encoding.TypeMapping;
028: import javax.xml.namespace.QName;
029: import javax.xml.soap.SOAPElement;
030: import java.util.Iterator;
031:
032: class DocLiteralCallHandler {
033: public Call createCall(Service service,
034: WebServiceDescriptor descriptor) throws ServiceException {
035:
036: ParameterDescriptor[] inParams = descriptor.getInputParams();
037: if (inParams != null) {
038: for (int i = 0; i < inParams.length; i++) {
039: registerSerializers(service, inParams[i], descriptor
040: .getInputEncodingStyleURI());
041: }
042: }
043:
044: ParameterDescriptor[] outParam = descriptor.getOutputParams();
045: if (outParam != null && (outParam.length > 0)) {
046: registerSerializerForDocLiteralOutput(service, outParam[0],
047: descriptor.getOutputEncodingStyleURI());
048: }
049:
050: // Create call from service
051: String inputNamespace = descriptor.getInputNamespace();
052: if (inputNamespace == null)
053: inputNamespace = "";
054:
055: QName portQName = new QName(descriptor.getPortName());
056: QName methodQName = new QName(inputNamespace, descriptor
057: .getMethodName());
058:
059: javax.xml.rpc.Call call = service.createCall(portQName,
060: methodQName);
061:
062: return call;
063: }
064:
065: private void registerSerializerForDocLiteralOutput(Service service,
066: ParameterDescriptor parameterDescriptor,
067: String outputEncodingStyleURI) {
068:
069: SerializationContext outputSerializationCtx = null;
070: if (parameterDescriptor.isSimpleType()) {
071: outputSerializationCtx = SerializationContext
072: .createSimpleSerializationContext(parameterDescriptor);
073:
074: } else {
075: outputSerializationCtx = SerializationContext
076: .createComplexSerializationContext(parameterDescriptor);
077:
078: }
079:
080: CombinedSerializer combineSerializer = new LiteralFragmentSerializer(
081: outputSerializationCtx.typeQName, true,
082: outputEncodingStyleURI);
083: SingletonSerializerFactory serializerFactory = new SingletonSerializerFactory(
084: combineSerializer);
085: SingletonDeserializerFactory deserializerFact = new SingletonDeserializerFactory(
086: combineSerializer);
087:
088: TypeMappingRegistry tMapRegistry = service
089: .getTypeMappingRegistry();
090: TypeMapping typeMapping = tMapRegistry
091: .getTypeMapping(outputEncodingStyleURI);
092:
093: typeMapping.register(SOAPElement.class,
094: outputSerializationCtx.typeQName, serializerFactory,
095: deserializerFact);
096:
097: }
098:
099: /*
100: * For each of the input parameters and for the output parameter, register
101: * the corresponding serializers & deserializers. This also results in the
102: * the QName to Java class mapping being determined.
103: *
104: * This function also registers the serializer/deserializers of the constituent
105: * types, if the current parameter's element is a complex type or an array of
106: * complex type, by recursively calling itself.
107: */
108: private void registerSerializers(Service service,
109: ParameterDescriptor inParam, String encodingToUse) {
110: // Register the serializers & deserializers for this parameter
111: registerParameterDescriptor(service, inParam, encodingToUse);
112:
113: if (inParam.isArrayType()) {
114:
115: if (!inParam.isSimpleTypeArray()) {
116: XList value = (XList) inParam.getValue();
117: Iterator iterator = value.iterator();
118: while (iterator.hasNext()) {
119: ParameterDescriptor elementParam = (ParameterDescriptor) iterator
120: .next();
121: if (!elementParam.isSimpleType()) {
122: registerSerializers(service, elementParam,
123: encodingToUse);
124: }
125: }
126: }
127: } else {
128: if (!inParam.isSimpleType()) {
129: XList value = (XList) inParam.getValue();
130: Iterator iterator = value.iterator();
131: while (iterator.hasNext()) {
132: ParameterDescriptor elementParam = (ParameterDescriptor) iterator
133: .next();
134: if (!elementParam.isSimpleType()) {
135: registerSerializers(service, elementParam,
136: encodingToUse);
137: }
138: }
139: }
140: }
141: }
142:
143: /*
144: * Registers the serializers/deserializers for the type of the parameter.
145: */
146: private void registerParameterDescriptor(Service service,
147: ParameterDescriptor param, String encodingToUse) {
148:
149: if (param.isArrayType()) {
150: // ArrayTypeName of a ParameterDescriptor holds the type name of the elements
151: // of the array. Hence, if the type name of the elements is primitive, then
152: // register this param as primitive type array, else as a complex type array,
153: // using the appropriate registration handler.
154: if (param.isSimpleTypeArray()) {
155: SimpleTypeHandler simpleHandler = TypeHandlerFactory
156: .getSimpleTypeHandler(service, encodingToUse);
157: simpleHandler.registerSimpleTypeArrayHandlers(param);
158:
159: } else {
160: ComplexTypeHandler complexHandler = TypeHandlerFactory
161: .getComplexTypeHandler(service, encodingToUse);
162:
163: complexHandler
164: .registerComplexTypeHandler(
165: param,
166: ComplexTypeHandler.COMPLEX_ARRAY_TYPE_SERIALIZER);
167: }
168:
169: } else {
170: // Determine if it is a complex type or a simple type.
171: // Register accordingly using the appropriate registration handler.
172: if (!param.isSimpleType()) {
173: ComplexTypeHandler complexHandler = TypeHandlerFactory
174: .getComplexTypeHandler(service, encodingToUse);
175:
176: complexHandler.registerComplexTypeHandler(param,
177: ComplexTypeHandler.COMPLEX_TYPE_SERIALIZER);
178: } else {
179: SimpleTypeHandler simpleHandler = TypeHandlerFactory
180: .getSimpleTypeHandler(service, encodingToUse);
181: simpleHandler.registerSimpleTypeHandlers(param);
182: }
183: }
184: }
185:
186: }
|