FileDocCategorySizeDatePackage
PolicyWSDLGeneratorExtension.javaAPI DocExample22682Fri Jul 06 01:24:54 BST 2007com.sun.xml.ws.policy.jaxws

PolicyWSDLGeneratorExtension

public class PolicyWSDLGeneratorExtension extends com.sun.xml.ws.api.wsdl.writer.WSDLGeneratorExtension
Marshals the contents of a policy map to WSDL.
author
Jakub Podlesak (jakub.podlesak at sun.com)

Fields Summary
private static final com.sun.xml.ws.policy.privateutil.PolicyLogger
LOGGER
private com.sun.xml.ws.policy.PolicyMap
policyMap
private com.sun.xml.ws.api.model.SEIModel
seiModel
private Class
endpointClass
private final Collection
subjects
private final com.sun.xml.ws.policy.sourcemodel.PolicyModelMarshaller
marshaller
private final com.sun.xml.ws.policy.PolicyMerger
merger
Constructors Summary
Methods Summary
public voidaddBindingExtension(com.sun.xml.txw2.TypedXmlWriter binding)

        LOGGER.entering();
        final QName bindingName = (null == seiModel) ? null : seiModel.getBoundPortTypeName();
        selectAndProcessSubject(binding, WSDLBoundPortType.class, ScopeType.ENDPOINT, bindingName);
        LOGGER.exiting();
    
public voidaddBindingOperationExtension(com.sun.xml.txw2.TypedXmlWriter operation, com.sun.xml.ws.api.model.JavaMethod method)

        LOGGER.entering();
        selectAndProcessSubject(operation, WSDLBoundOperation.class, ScopeType.OPERATION, method);
        LOGGER.exiting();
    
public voidaddBindingOperationFaultExtension(com.sun.xml.txw2.TypedXmlWriter writer, com.sun.xml.ws.api.model.JavaMethod method, com.sun.xml.ws.api.model.CheckedException exception)

        LOGGER.entering(writer, method, exception);
        if (subjects != null) {
            for (PolicySubject subject : subjects) { // iterate over all subjects in policy map
                if (this.policyMap.isFaultMessageSubject(subject)) {
                    final Object concreteSubject = subject.getSubject();
                    if (concreteSubject != null && WSDLBoundFaultContainer.class.isInstance(concreteSubject)) { // is it our class?
                        String exceptionName = exception == null ? null : exception.getMessageName();
                        if (exceptionName == null) { // no name provided to check
                            writePolicyOrReferenceIt(subject, writer);
                        } else {
                            WSDLBoundFaultContainer faultContainer = (WSDLBoundFaultContainer) concreteSubject;
                            WSDLBoundFault fault = faultContainer.getBoundFault();
                            WSDLBoundOperation operation = faultContainer.getBoundOperation();
                            if (exceptionName.equals(fault.getName()) &&
                                operation.getName().getLocalPart().equals(method.getOperationName())) {
                                writePolicyOrReferenceIt(subject, writer);
                            }
                        }
                    }
                }
            }
        }
        LOGGER.exiting();
    
public voidaddBindingOperationInputExtension(com.sun.xml.txw2.TypedXmlWriter input, com.sun.xml.ws.api.model.JavaMethod method)

        LOGGER.entering();
        final String messageName = (null == method) ? null : method.getOperationName();
        selectAndProcessSubject(input, WSDLBoundOperation.class, ScopeType.INPUT_MESSAGE, messageName);
        LOGGER.exiting();
    
public voidaddBindingOperationOutputExtension(com.sun.xml.txw2.TypedXmlWriter output, com.sun.xml.ws.api.model.JavaMethod method)

        LOGGER.entering();
        final String messageName = (null == method) ? null : method.getOperationName();
        selectAndProcessSubject(output, WSDLBoundOperation.class, ScopeType.OUTPUT_MESSAGE, messageName);
        LOGGER.exiting();
    
public voidaddDefinitionsExtension(com.sun.xml.txw2.TypedXmlWriter definitions)

        try {
            LOGGER.entering();
            if (policyMap == null) {
                LOGGER.fine(LocalizationMessages.WSP_1020_NOT_MARSHALLING_ANY_POLICIES_POLICY_MAP_IS_NULL());
            } else {
                subjects.addAll(policyMap.getPolicySubjects());
                boolean usingPolicy = false;
                final PolicyModelGenerator generator = PolicyModelGenerator.getGenerator();
                Set<String> policyIDsOrNamesWritten = null;
                for (PolicySubject subject : subjects) {
                    if (subject.getSubject() == null) {
                        LOGGER.fine(LocalizationMessages.WSP_1019_NOT_MARSHALLING_WSDL_SUBJ_NULL(subject));
                    } else {
                        if (!usingPolicy) {
                            definitions._element(PolicyConstants.USING_POLICY, TypedXmlWriter.class);
                            usingPolicy = true;
                            policyIDsOrNamesWritten = new HashSet<String>();
                        }
                        final Policy policy;
                        try {
                            policy = subject.getEffectivePolicy(merger);
                        } catch (PolicyException e) {
                            throw LOGGER.logSevereException(new WebServiceException(LocalizationMessages.WSP_1029_FAILED_TO_RETRIEVE_EFFECTIVE_POLICY_FOR_SUBJECT(subject.toString()), e));
                        }
                        if ((null == policy.getIdOrName()) || (policyIDsOrNamesWritten.contains(policy.getIdOrName()))) {
                            LOGGER.fine(LocalizationMessages.WSP_1047_POLICY_ID_NULL_OR_DUPLICATE(policy));
                        } else {
                            try {
                                final PolicySourceModel policyInfoset = generator.translate(policy);
                                marshaller.marshal(policyInfoset, definitions);
                            } catch (PolicyException e) {
                                throw LOGGER.logSevereException(new WebServiceException(LocalizationMessages.WSP_1051_FAILED_TO_MARSHALL_POLICY(policy.getIdOrName()), e));
                            }
                            policyIDsOrNamesWritten.add(policy.getIdOrName());
                        }
                    }
                }
            }
        } finally {
            LOGGER.exiting();
        }
    
public voidaddFaultMessageExtension(com.sun.xml.txw2.TypedXmlWriter message, com.sun.xml.ws.api.model.JavaMethod method, com.sun.xml.ws.api.model.CheckedException exception)

        LOGGER.entering();
        final String messageName = (null == exception) ? null : exception.getMessageName();
        selectAndProcessSubject(message, WSDLMessage.class, ScopeType.FAULT_MESSAGE, messageName);
        LOGGER.exiting();
    
public voidaddInputMessageExtension(com.sun.xml.txw2.TypedXmlWriter message, com.sun.xml.ws.api.model.JavaMethod method)

        LOGGER.entering();
        final String messageName = (null == method) ? null : method.getRequestMessageName();
        selectAndProcessSubject(message, WSDLMessage.class, ScopeType.INPUT_MESSAGE, messageName);
        LOGGER.exiting();
    
public voidaddOperationExtension(com.sun.xml.txw2.TypedXmlWriter operation, com.sun.xml.ws.api.model.JavaMethod method)

        LOGGER.entering();
        selectAndProcessSubject(operation, WSDLOperation.class, ScopeType.OPERATION, method);
        LOGGER.exiting();
    
public voidaddOperationFaultExtension(com.sun.xml.txw2.TypedXmlWriter fault, com.sun.xml.ws.api.model.JavaMethod method, com.sun.xml.ws.api.model.CheckedException exception)

        LOGGER.entering();
        final String messageName = (null == exception) ? null : exception.getMessageName();
        selectAndProcessSubject(fault, WSDLFault.class, ScopeType.FAULT_MESSAGE, messageName);
        LOGGER.exiting();
    
public voidaddOperationInputExtension(com.sun.xml.txw2.TypedXmlWriter input, com.sun.xml.ws.api.model.JavaMethod method)

        LOGGER.entering();
        final String messageName = (null == method) ? null : method.getRequestMessageName();
        selectAndProcessSubject(input, WSDLInput.class, ScopeType.INPUT_MESSAGE, messageName);
        LOGGER.exiting();
    
public voidaddOperationOutputExtension(com.sun.xml.txw2.TypedXmlWriter output, com.sun.xml.ws.api.model.JavaMethod method)

        LOGGER.entering();
        final String messageName = (null == method) ? null : method.getResponseMessageName();
        selectAndProcessSubject(output, WSDLOutput.class, ScopeType.OUTPUT_MESSAGE, messageName);
        LOGGER.exiting();
    
public voidaddOutputMessageExtension(com.sun.xml.txw2.TypedXmlWriter message, com.sun.xml.ws.api.model.JavaMethod method)

        LOGGER.entering();
        final String messageName = (null == method) ? null : method.getResponseMessageName();
        selectAndProcessSubject(message, WSDLMessage.class, ScopeType.OUTPUT_MESSAGE, messageName);
        LOGGER.exiting();
    
public voidaddPortExtension(com.sun.xml.txw2.TypedXmlWriter port)

        LOGGER.entering();
        final String portName = ((null == seiModel) || (null == endpointClass)) ?
            null :
            WSEndpoint.getDefaultPortName(seiModel.getServiceQName(), endpointClass).getLocalPart();
        selectAndProcessSubject(port, WSDLPort.class, ScopeType.ENDPOINT, portName);
        LOGGER.exiting();
    
public voidaddPortTypeExtension(com.sun.xml.txw2.TypedXmlWriter portType)

        LOGGER.entering();
        final String portTypeName = (null == seiModel) ? null : seiModel.getPortTypeName().getLocalPart();
        selectAndProcessSubject(portType, WSDLPortType.class, ScopeType.ENDPOINT, portTypeName);
        LOGGER.exiting();
    
public voidaddServiceExtension(com.sun.xml.txw2.TypedXmlWriter service)

        LOGGER.entering();
        final String serviceName = ((null == seiModel) || (null == endpointClass)) ?
            null :
            WSEndpoint.getDefaultServiceName(endpointClass).getLocalPart();
        selectAndProcessSubject(service, WSDLService.class, ScopeType.SERVICE, serviceName);
        LOGGER.exiting();
    
private static final booleanisCorrectType(com.sun.xml.ws.policy.PolicyMap map, com.sun.xml.ws.policy.PolicySubject subject, com.sun.xml.ws.policy.jaxws.PolicyWSDLGeneratorExtension$ScopeType type)

        switch (type) {
            case OPERATION :
                return ! (map.isInputMessageSubject(subject) || map.isOutputMessageSubject(subject) || map.isFaultMessageSubject(subject));
            case INPUT_MESSAGE :
                return map.isInputMessageSubject(subject);
            case OUTPUT_MESSAGE :
                return map.isOutputMessageSubject(subject);
            case FAULT_MESSAGE :
                return map.isFaultMessageSubject(subject);
            default:
                return true;
        }
    
private voidselectAndProcessSubject(com.sun.xml.txw2.TypedXmlWriter xmlWriter, java.lang.Class clazz, com.sun.xml.ws.policy.jaxws.PolicyWSDLGeneratorExtension$ScopeType scopeType, com.sun.xml.ws.api.model.JavaMethod method)
This method should only be invoked by interface methods that deal with operations because they may use JavaMethod as PolicySubject instead of a WSDL object.

        LOGGER.entering(xmlWriter, clazz, scopeType, method);
        if (method == null) {
            selectAndProcessSubject(xmlWriter, clazz, scopeType, (String) null);
        } else {
            if (subjects != null) {
                for (PolicySubject subject : subjects) {
                    if (method.equals(subject.getSubject())) {
                        writePolicyOrReferenceIt(subject, xmlWriter);
                    }
                }
            }
            selectAndProcessSubject(xmlWriter, clazz, scopeType, method.getOperationName());
        }
        LOGGER.exiting();
    
private voidselectAndProcessSubject(com.sun.xml.txw2.TypedXmlWriter xmlWriter, java.lang.Class clazz, com.sun.xml.ws.policy.jaxws.PolicyWSDLGeneratorExtension$ScopeType scopeType, javax.xml.namespace.QName bindingName)
This method should only be invoked by interface methods that deal with WSDL binding because they may use the QName of the WSDL binding element as PolicySubject instead of a WSDL object.

        LOGGER.entering(xmlWriter, clazz, scopeType, bindingName);
        if (bindingName == null) {
            selectAndProcessSubject(xmlWriter, clazz, scopeType, (String) null);
        } else {
            if (subjects != null) {
                for (PolicySubject subject : subjects) {
                    if (bindingName.equals(subject.getSubject())) {
                        writePolicyOrReferenceIt(subject, xmlWriter);
                    }
                }
            }
            selectAndProcessSubject(xmlWriter, clazz, scopeType, bindingName.getLocalPart());
        }
        LOGGER.exiting();
    
private voidselectAndProcessSubject(com.sun.xml.txw2.TypedXmlWriter xmlWriter, java.lang.Class clazz, com.sun.xml.ws.policy.jaxws.PolicyWSDLGeneratorExtension$ScopeType scopeType, java.lang.String wsdlName)

        LOGGER.entering();
        if (subjects != null) {
            for (PolicySubject subject : subjects) { // iterate over all subjects in policy map
                if (isCorrectType(policyMap, subject, scopeType)) {
                    final Object concreteSubject = subject.getSubject();
                    if (concreteSubject != null && clazz.isInstance(concreteSubject)) { // is it our class?
                        if (null == wsdlName) { // no name provided to check
                            writePolicyOrReferenceIt(subject, xmlWriter);
                        } else {
                            try {
                                final Method getNameMethod = clazz.getDeclaredMethod("getName");
                                if (stringEqualsToStringOrQName(wsdlName, getNameMethod.invoke(concreteSubject))) {
                                    writePolicyOrReferenceIt(subject, xmlWriter);
                                }
                            } catch (NoSuchMethodException e) {
                                throw LOGGER.logSevereException(new WebServiceException(LocalizationMessages.WSP_1011_UNABLE_TO_CHECK_ELEMENT_NAME(clazz.getName(), wsdlName), e));
                            } catch (IllegalAccessException e) {
                                throw LOGGER.logSevereException(new WebServiceException(LocalizationMessages.WSP_1011_UNABLE_TO_CHECK_ELEMENT_NAME(clazz.getName(), wsdlName), e));
                            } catch (InvocationTargetException e) {
                                throw LOGGER.logSevereException(new WebServiceException(LocalizationMessages.WSP_1011_UNABLE_TO_CHECK_ELEMENT_NAME(clazz.getName(), wsdlName), e));
                            }
                        }
                    }
                }
            }
        }
        LOGGER.exiting();
    
public voidstart(com.sun.xml.ws.api.wsdl.writer.WSDLGenExtnContext context)

    
         
        LOGGER.entering();
        try {
            this.seiModel = context.getModel();
            this.endpointClass = context.getEndpointClass();
            
            final PolicyMapUpdateProvider[] policyMapUpdateProviders = PolicyUtils.ServiceProvider.load(PolicyMapUpdateProvider.class);
            final PolicyMapExtender[] extenders = new PolicyMapExtender[policyMapUpdateProviders.length];
            for (int i=0; i < policyMapUpdateProviders.length; i++) {
                extenders[i] = PolicyMapExtender.createPolicyMapExtender();
            }
            final String configId = context.getEndpointClass().getName();
            try {
                policyMap = PolicyConfigParser.parse(configId, context.getContainer(), extenders);
            } catch (PolicyException e) {
                LOGGER.fine(LocalizationMessages.WSP_1027_FAILED_TO_READ_WSIT_CONFIG_FOR_ID(configId), e);
            }
            if (policyMap == null) {
                LOGGER.fine(LocalizationMessages.WSP_1034_CREATE_POLICY_MAP_FOR_CONFIG(configId));
                policyMap = PolicyMap.createPolicyMap(Arrays.asList(extenders));
            }
            
            final TypedXmlWriter root = context.getRoot();
            root._namespace(PolicyConstants.POLICY_NAMESPACE_URI, PolicyConstants.POLICY_NAMESPACE_PREFIX);
            root._namespace(PolicyConstants.WSU_NAMESPACE_URI, PolicyConstants.WSU_NAMESPACE_PREFIX);
            final WSBinding binding = context.getBinding();
            
            try {
                for (int i = 0; i < policyMapUpdateProviders.length; i++) {
                    policyMapUpdateProviders[i].update(extenders[i], policyMap, seiModel, binding);
                    extenders[i].disconnect();
                }
            } catch (PolicyException e) {
                throw LOGGER.logSevereException(new WebServiceException(LocalizationMessages.WSP_1048_MAP_UPDATE_FAILED(), e));
            }
        } finally {
            LOGGER.exiting();
        }
    
private booleanstringEqualsToStringOrQName(java.lang.String first, java.lang.Object second)

        return (second instanceof QName) ? first.equals(((QName)second).getLocalPart()) : first.equals(second) ;
    
private voidwritePolicyOrReferenceIt(com.sun.xml.ws.policy.PolicySubject subject, com.sun.xml.txw2.TypedXmlWriter writer)
Adds a PolicyReference element that points to the policy of the element, if the policy does not have any id or name. Writes policy inside the element otherwise.

param
policy to be referenced or marshalled
param
element A TXW element to which we shall add the PolicyReference

        final Policy policy;
        try {
            policy = subject.getEffectivePolicy(merger);
        } catch (PolicyException e) {
            throw LOGGER.logSevereException(new WebServiceException(LocalizationMessages.WSP_1029_FAILED_TO_RETRIEVE_EFFECTIVE_POLICY_FOR_SUBJECT(subject.toString()), e));
        }
        if (policy != null) {
            if (null == policy.getIdOrName()) {
                final PolicyModelGenerator generator = PolicyModelGenerator.getGenerator();
                try {
                    final PolicySourceModel policyInfoset = generator.translate(policy);
                    marshaller.marshal(policyInfoset, writer);
                } catch (PolicyException pe) {
                    throw LOGGER.logSevereException(new WebServiceException(LocalizationMessages.WSP_1010_UNABLE_TO_MARSHALL_POLICY_OR_POLICY_REFERENCE(), pe));
                }
            } else {
                final TypedXmlWriter policyReference = writer._element(PolicyConstants.POLICY_REFERENCE, TypedXmlWriter.class);
                policyReference._attribute(PolicyConstants.POLICY_URI.getLocalPart(), '#" + policy.getIdOrName());
            }
        }