You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by sa...@apache.org on 2006/02/10 09:39:29 UTC

svn commit: r376593 - in /webservices/axis2/trunk/java/modules: codegen/src/org/apache/axis2/util/ codegen/src/org/apache/axis2/wsdl/codegen/emitter/ codegen/src/org/apache/axis2/wsdl/template/java/ core/src/org/apache/axis2/deployment/ core/src/org/ap...

Author: sanka
Date: Fri Feb 10 00:39:27 2006
New Revision: 376593

URL: http://svn.apache.org/viewcvs?rev=376593&view=rev
Log:

Added Codegen support for WS Policy in the generated stub



Added:
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/util/PolicyAttachmentUtil.java
Modified:
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/emitter/MultiLanguageClientEmitter.java
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/template/java/InterfaceImplementationTemplate.xsl
    webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/deployment/ModuleBuilder.java
    webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/util/PolicyUtil.java

Added: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/util/PolicyAttachmentUtil.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/util/PolicyAttachmentUtil.java?rev=376593&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/util/PolicyAttachmentUtil.java (added)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/util/PolicyAttachmentUtil.java Fri Feb 10 00:39:27 2006
@@ -0,0 +1,716 @@
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.axis2.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.net.URI;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.wsdl.WSDLException;
+import javax.xml.namespace.QName;
+
+import org.apache.axis2.wsdl.WSDLVersionWrapper;
+import org.apache.axis2.wsdl.builder.WOMBuilderFactory;
+import org.apache.ws.policy.Assertion;
+import org.apache.ws.policy.Policy;
+import org.apache.ws.policy.PolicyConstants;
+import org.apache.ws.policy.PolicyReference;
+import org.apache.ws.policy.util.PolicyFactory;
+import org.apache.ws.policy.util.PolicyReader;
+import org.apache.ws.policy.util.PolicyRegistry;
+import org.apache.ws.policy.util.SchemaRegistry;
+import org.apache.wsdl.Component;
+import org.apache.wsdl.MessageReference;
+import org.apache.wsdl.WSDLBinding;
+import org.apache.wsdl.WSDLBindingMessageReference;
+import org.apache.wsdl.WSDLBindingOperation;
+import org.apache.wsdl.WSDLConstants;
+import org.apache.wsdl.WSDLDescription;
+import org.apache.wsdl.WSDLEndpoint;
+import org.apache.wsdl.WSDLExtensibilityAttribute;
+import org.apache.wsdl.WSDLInterface;
+import org.apache.wsdl.WSDLOperation;
+import org.apache.wsdl.WSDLService;
+import org.apache.wsdl.WSDLTypes;
+import org.apache.wsdl.extensions.DefaultExtensibilityElement;
+import org.apache.wsdl.extensions.PolicyExtensibilityElement;
+import org.apache.wsdl.extensions.Schema;
+import org.w3c.dom.Element;
+
+import com.ibm.wsdl.util.xml.DOM2Writer;
+
+
+/**
+ * This util class which implements WSPolicyAttachment sepcification (September
+ * 2004).
+ * 
+ * @author Sanka Samaranayake <ss...@gmail.com>
+ *  
+ */
+public class PolicyAttachmentUtil {
+
+    private WSDLDescription wsdlDescription = null;
+
+    //private HashMap loadedPolicies = new HashMap();
+    private PolicyRegistry reg = new PolicyRegistry();
+
+    private SchemaRegistry schemaRegistry = new SchemaRegistry();
+
+    public PolicyAttachmentUtil() {
+    }
+
+    public PolicyAttachmentUtil(WSDLDescription wsdlDescription) {
+        this.wsdlDescription = wsdlDescription;
+
+        populatePolicyRegistry();
+        populateSchemaRegistry();
+
+    }
+
+    public PolicyAttachmentUtil(InputStream wsdlInputStream) {
+        try {
+            WSDLVersionWrapper build = WOMBuilderFactory.getBuilder(
+                    WSDLConstants.WSDL_1_1).build(wsdlInputStream);
+            wsdlDescription = build.getDescription();
+
+            populatePolicyRegistry();
+            populateSchemaRegistry();
+
+        } catch (WSDLException e) {
+            throw new IllegalArgumentException("error : " + e.getMessage());
+        }
+    }
+
+    public void setWSDLDescription(WSDLDescription wsdlDescription) {
+        this.wsdlDescription = wsdlDescription;
+        reg = new PolicyRegistry();
+
+        populatePolicyRegistry();
+        populateSchemaRegistry();
+    }
+
+    public WSDLDescription getWSDLDescription() {
+        try {
+            return getDescription();
+
+        } catch (IllegalStateException ex) {
+            return null;
+        }
+    }
+
+    private WSDLDescription getDescription() {
+        if (wsdlDescription != null) {
+            return wsdlDescription;
+        }
+        throw new IllegalStateException("ERROR: A WSDLDescription is not set");
+    }
+
+    /**
+     * Retruns the Effective policy for a service.
+     * 
+     * @param serviceName
+     * @return
+     */
+    public Policy getPolicyForService(QName serviceName) {
+        return getServicePolicy(serviceName);
+    }
+
+    /**
+     * Returns the Effective policy for an Endpoint.
+     * 
+     * @param epName
+     * @return
+     */
+    public Policy getPolicyForEndPoint(QName epName) {
+        Policy servicePolicy = null;
+        Policy endPointPolicy = null;
+
+        Iterator iterator = wsdlDescription.getServices().values().iterator();
+
+        while (iterator.hasNext()) {
+            WSDLService service = (WSDLService) iterator.next();
+            if (service.getEndpoints().containsKey(epName)) {
+                servicePolicy = getPolicyForService(service.getName());
+                break;
+            }
+        }
+
+        endPointPolicy = getEndPointPolicy(epName);
+        
+        if (servicePolicy != null) {
+        	if (endPointPolicy != null) {
+        		return (Policy) servicePolicy.merge(endPointPolicy);
+        	}
+        	return servicePolicy;        	
+        } 
+        return endPointPolicy;
+    }
+
+    /**
+     * Returns the Effective policy for an Operation.
+     * 
+     * @param endPoint
+     * @param operation
+     * @return
+     */
+    public Policy getPolicyForOperation(QName endPoint, QName operation) {
+        Policy endPointPolicy = getPolicyForEndPoint(endPoint);
+        Policy operationPolicy = getOperationPolicy(endPoint, operation);
+
+        //
+        if (operationPolicy == null) {
+            return endPointPolicy;
+
+        } else if (endPointPolicy == null) {
+            return operationPolicy;
+
+        } else {
+            return (Policy) endPointPolicy.merge(operationPolicy);
+        }
+    }
+
+    /**
+     * Returns the effective policy of an Input Message.
+     * 
+     * @param endPoint
+     * @param operation
+     * @return
+     */
+    public Policy getPolicyForInputMessage(QName endPoint, QName operation) {
+        Policy operationPolicy = getPolicyForOperation(endPoint, operation);
+        Policy inputMsgPolicy = getInputMeassagePolicy(endPoint, operation);
+
+        if (operationPolicy == null) {
+            return inputMsgPolicy;
+
+        } else if (inputMsgPolicy == null) {
+            return operationPolicy;
+
+        } else {
+            return (Policy) operationPolicy.merge(inputMsgPolicy);
+        }
+    }
+
+    /**
+     * Returns the effective policy of an Output Message.
+     * 
+     * @param endPoint
+     * @param operation
+     * @return
+     */
+    public Policy getPolicyForOutputMessage(QName endPoint, QName operation) {
+        Policy operationPolicy = getPolicyForOperation(endPoint, operation);
+        Policy outputMsgPolicy = getOutputMeassagePolicy(endPoint, operation);
+
+        if (operationPolicy == null) {
+            return outputMsgPolicy;
+
+        } else if (outputMsgPolicy == null) {
+            return operationPolicy;
+
+        } else {
+            return (Policy) operationPolicy.merge(outputMsgPolicy);
+        }
+    }
+
+    /**
+     * 
+     * @param qname
+     * @return
+     */
+    public Policy getServicePolicy(QName qname) throws IllegalArgumentException {
+
+        WSDLService service = getDescription().getService(qname);
+
+        if (service == null) {
+            throw new IllegalArgumentException("no such service:" + qname);
+        }
+
+        Policy policy = getPolicyFromComponent(service);
+        return (policy == null) ? null : (Policy) policy.normalize();
+    }
+
+    public Policy getEndPointPolicy(QName qname) {
+        WSDLEndpoint endpoint = getEndpoint(qname);
+        if (endpoint == null) {
+            throw new IllegalArgumentException("no such portType:" + qname);
+        }
+
+        ArrayList policies = new ArrayList();
+
+        // wsdl:port
+        Assertion epPolicy = getPolicyFromComponent(endpoint);
+        if (epPolicy != null) {
+            policies.add(getPolicyFromComponent(endpoint));
+        }
+
+        //wsdl:binding
+        WSDLBinding wsdlBinding = endpoint.getBinding();
+        Assertion wsdlBindingPolicy = getPolicyFromComponent(wsdlBinding);
+        if (wsdlBindingPolicy != null) {
+            policies.add(getPolicyFromComponent(wsdlBinding));
+        }
+
+        //wsdl:portType
+        WSDLInterface wsdlInterface = wsdlBinding.getBoundInterface();
+        Assertion portTypePolicy = getPolicyFromComponent(wsdlInterface);
+        if (portTypePolicy != null) {
+            policies.add(getPolicyFromComponent(wsdlInterface));
+        }
+
+        return getSinglePolicy(policies);
+    }
+
+    public Policy getOperationPolicy(QName eqame, QName oqname) {
+        WSDLEndpoint endPoint = getEndpoint(eqame);
+        ArrayList list = new ArrayList();
+
+        //wsdl:binding/wsdl:operation
+        WSDLBinding binding = endPoint.getBinding();
+        WSDLBindingOperation bindingOperation = (WSDLBindingOperation) binding
+                .getBindingOperations().get(oqname);
+
+        Assertion bindingPolicy = getPolicyFromComponent(bindingOperation);
+        if (bindingPolicy != null) {
+            list.add(bindingPolicy);
+        }
+
+        // wsdl:portType/wsdl:operation
+        WSDLOperation wsdlOperation = bindingOperation.getOperation();
+        Assertion interfacePolicy = getPolicyFromComponent(wsdlOperation);
+
+        if (interfacePolicy != null) {
+            list.add(interfacePolicy);
+
+        }
+        return getSinglePolicy(list);
+    }
+
+    public Policy getInputMeassagePolicy(QName eqname, QName oqname) {
+        List policies = new ArrayList();
+        WSDLEndpoint endPoint = getEndpoint(eqname);
+
+        // wsdl:binding/wsdl:operation/wsdl:input
+        WSDLBindingOperation wsdlBindingOperation = endPoint.getBinding()
+                .getBindingOperation(oqname);
+        WSDLBindingMessageReference bindingInput = wsdlBindingOperation
+                .getInput();
+
+        //List extensibilityAttributes =
+        // bindingInput.getExtensibilityAttributes();
+        Policy bindingInputPolicy = getSinglePolicy(getPoliciesAsExtensibleElements(bindingInput));
+        if (bindingInputPolicy != null) {
+            policies.add(bindingInputPolicy);
+        }
+
+        // wsdl:portType/wsdl:operation/wsdl:input
+        WSDLOperation wsdlOperation = wsdlBindingOperation.getOperation();
+        MessageReference operationInput = wsdlOperation.getInputMessage();
+        Policy operationInputPolicy = getSinglePolicy(getPoliciesAsExtensibilityAttribute(operationInput));
+        if (operationInputPolicy != null) {
+            policies.add(operationInputPolicy);
+        }
+
+        // wsdl:Message
+        // TODO
+
+        return getSinglePolicy(policies);
+    }
+
+    public Policy getOutputMeassagePolicy(QName endPointName, QName opName) {
+        List policies = new ArrayList();
+        WSDLEndpoint endPoint = getEndpoint(endPointName);
+
+        // wsdl:binding/wsdl:operation/wsdl:output
+        WSDLBindingOperation wsdlBindingOperation = endPoint.getBinding()
+                .getBindingOperation(opName);
+        WSDLBindingMessageReference bindingOutput = wsdlBindingOperation
+                .getOutput();
+        Policy bindingOutputPolicy = getSinglePolicy(getPoliciesAsExtensibleElements(bindingOutput));
+        if (bindingOutputPolicy != null) {
+            policies.add(getPolicyFromComponent(bindingOutput));
+        }
+
+        // wsdl:portType/wsdl:operation/wsdl:output
+        WSDLOperation wsdlOperation = wsdlBindingOperation.getOperation();
+        MessageReference operationOutput = wsdlOperation.getOutputMessage();
+        Policy operationOutputPolicy = getSinglePolicy(getPoliciesAsExtensibilityAttribute(operationOutput));
+        if (operationOutputPolicy != null) {
+            policies.add(operationOutputPolicy);
+        }
+
+        // wsdl:Message
+        // TODO
+
+        return getSinglePolicy(policies);
+    }
+
+    public Policy getFaultMeassagePolicy(QName endPointName, QName opName,
+            QName fault) {
+        throw new UnsupportedOperationException();
+    }
+
+    public PolicyRegistry getPolicyRegistry() {
+        return reg;
+    }
+
+    public Element getSchemaElement(String uri) {
+        return schemaRegistry.lookup(uri);
+    }
+
+    private Policy getSinglePolicy(List policies) {
+        Policy result = null;
+
+        if (!policies.isEmpty()) {
+            Iterator iter = policies.iterator();
+            result = (Policy) iter.next();
+            while (iter.hasNext()) {
+                Policy next = (Policy) iter.next();
+                result = (Policy) result.merge(next, reg);
+            }
+        }
+        return result;
+    }
+
+    private WSDLEndpoint getEndpoint(QName epName) {
+        Iterator iterator = wsdlDescription.getServices().values().iterator();
+        while (iterator.hasNext()) {
+            WSDLService service = (WSDLService) iterator.next();
+            if (service.getEndpoints().containsKey(epName)) {
+                return service.getEndpoint(epName);
+            }
+        }
+        return null;
+    }
+
+    private Policy getPolicyFromComponent(Component component) {
+        List list = new ArrayList();
+        List policiesAsAttributes = getPoliciesAsExtensibilityAttribute(component),
+        policiesAsElements = getPoliciesAsExtensibleElements(component);
+
+        list.addAll(policiesAsAttributes);
+        list.addAll(policiesAsElements);
+
+        return getSinglePolicy(list);
+    }
+
+    private List getPoliciesAsExtensibilityAttribute(Component component) {
+        Iterator iterator;
+        List policyURIStrings = new ArrayList();
+        List policies = new ArrayList();
+        iterator = component.getExtensibilityAttributes().iterator();
+
+        while (iterator.hasNext()) {
+        	WSDLExtensibilityAttribute exAttribute = (WSDLExtensibilityAttribute) iterator
+                    .next();
+            QName qname = exAttribute.getKey();
+
+            if (qname.getNamespaceURI().equals(
+                    PolicyConstants.WS_POLICY_NAMESPACE_URI)
+                    && qname.getLocalPart().equals("PolicyURIs")) {
+                String value = exAttribute.getValue().toString();
+                String[] uriStrings = value.split(" ");
+
+                for (int i = 0; i < uriStrings.length; i++) {
+                    policyURIStrings.add(uriStrings[i].trim());
+                }
+            }
+        }
+
+        if (!policyURIStrings.isEmpty()) {
+            iterator = policyURIStrings.iterator();
+
+            do {
+                String policyURIString = (String) iterator.next();
+                Policy policy = getPolicyFromURI(policyURIString);
+                policies.add(policy);
+            } while (iterator.hasNext());
+        }
+        return policies;
+    }
+
+    private List getPoliciesAsExtensibleElements(Component component) {
+        ArrayList policies = new ArrayList();
+        Iterator iterator = component.getExtensibilityElements().iterator();
+
+        while (iterator.hasNext()) {
+            Object extensibilityElement = iterator.next();
+
+            if (extensibilityElement instanceof PolicyExtensibilityElement) {
+            	PolicyExtensibilityElement policyExtensibilityElement = (PolicyExtensibilityElement) extensibilityElement;
+            	Object policyElement = policyExtensibilityElement.getPolicyElement();
+            	
+            	if (policyElement instanceof Policy) {
+            		policies.add(policyElement);
+            		
+            	} else if (policyElement instanceof PolicyReference) {
+            		policies.add(getPolicyFromURI(((PolicyReference) policyElement).getPolicyURIString()));
+            		
+            	}
+//            	
+//            	
+//                if (element.getNamespaceURI().equals(
+//                        PolicyConstants.WS_POLICY_NAMESPACE_URI)
+//                        && element.getLocalName().equals("PolicyReference")) {
+//                    policies.add(getPolicyFromPolicyRef(element));
+//
+//                } else if (element.getNamespaceURI().equals(
+//                        PolicyConstants.WS_POLICY_NAMESPACE_URI)
+//                        && element.getLocalName().equals("Policy")) {
+//                    policies.add(getPolicyFromElement(element));
+//                }
+            }
+            //          WSDLExtensibilityElement exElement = (WSDLExtensibilityElement)
+            // iterator.next();
+            //          Element element = (Element) exElement.getElement();
+            //          if
+            // (element.getNamespaceURI().equals(WSPConstants.WS_POLICY_NAMESPACE_URI)
+            // && element.getLocalName().equals("PolicyReference")) {
+            //              policyList.add(getPolicyAsPolicyRef(element));
+            //              
+            //          } else if
+            // (element.getNamespaceURI().equals(WSPConstants.WS_POLICY_NAMESPACE_URI)
+            // && element.getLocalName().equals("Policy")) {
+            //              policyList.add(getPolicyAsElement(element));
+            //          }
+
+        }
+        
+        return policies;
+    }
+
+    private Policy getPolicyFromPolicyRef(Element element) {
+        String policyURIString = element.getAttribute("URI");
+        if (policyURIString != null && policyURIString.length() != 0) {
+            return getPolicyFromURI(policyURIString);
+        }
+        return null;
+    }
+
+    private Policy getPolicyFromElement(Element element) {
+        InputStream policyInputStream = createInputStream(element);
+        PolicyReader reader = PolicyFactory
+                .getPolicyReader(PolicyFactory.OM_POLICY_READER);
+        return reader.readPolicy(policyInputStream);
+    }
+
+    private InputStream createInputStream(Element element) {
+        // some improvements ..???
+        StringWriter sw = new StringWriter();
+        DOM2Writer.serializeAsXML(element, sw);
+        return new ByteArrayInputStream(sw.toString().getBytes());
+    }
+
+    private Policy getPolicyFromURI(String policyURIString) {
+        return reg.lookup(policyURIString);
+    }
+
+    public String getTargetURI() {
+        return getDescription().getTargetNameSpace();
+    }
+
+    private void populateSchemaRegistry() {
+        WSDLDescription des = getDescription();
+        WSDLTypes types = des.getTypes();
+
+        Iterator iterator = types.getExtensibilityElements().iterator();
+        while (iterator.hasNext()) {
+            Object extElement = iterator.next();
+            if (extElement instanceof Schema) {
+                Element schemaElement = ((Schema) extElement).getElement();
+                schemaRegistry.register(schemaElement
+                        .getAttribute("targetNamespace"), schemaElement);
+            }
+        }
+    }
+
+    private void populatePolicyRegistry() {
+        Iterator iterator;
+        WSDLDescription des = getDescription();
+        List extElements = des.getExtensibilityElements();
+        registerPoliciesAsElements(extElements);
+
+        iterator = des.getWsdlInterfaces().values().iterator();
+        while (iterator.hasNext()) {
+            WSDLInterface interfaze = (WSDLInterface) iterator.next();
+            registerPoliciesInWSDLInterface(interfaze);
+        }
+
+        iterator = des.getBindings().values().iterator();
+        while (iterator.hasNext()) {
+            WSDLBinding wsdlBinding = (WSDLBinding) iterator.next();
+            registerPoliciesInWSDLBinding(wsdlBinding);
+        }
+
+        iterator = des.getServices().values().iterator();
+        while (iterator.hasNext()) {
+            WSDLService service = (WSDLService) iterator.next();
+            registerPoliciesInService(service);
+        }
+
+        iterator = reg.keys();
+        while (iterator.hasNext()) {
+            String uriString = (String) iterator.next();
+            Policy policy = reg.lookup(uriString);
+            if (policy == null) {
+                try {
+                    URI policyURI = new URI(uriString);
+                    URL policyURL = policyURI.toURL();
+                    PolicyReader reader = PolicyFactory
+                            .getPolicyReader(PolicyFactory.OM_POLICY_READER);
+                    Policy newPolicy = reader
+                            .readPolicy(policyURL.openStream());
+                    reg.register(uriString, newPolicy);
+
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    reg.unregister(uriString);
+                    iterator = reg.keys();
+                }
+            }
+        }
+    }
+
+    private void registerPoliciesInService(WSDLService service) {
+        List extensibilityElements = service.getExtensibilityElements();
+        registerPoliciesAsElements(extensibilityElements);
+
+        Iterator iterator = service.getEndpoints().values().iterator();
+        while (iterator.hasNext()) {
+            WSDLEndpoint wsdlEndpoint = (WSDLEndpoint) iterator.next();
+            extensibilityElements = wsdlEndpoint.getExtensibilityElements();
+            registerPoliciesAsElements(extensibilityElements);
+        }
+    }
+
+    private void registerPoliciesInWSDLBinding(WSDLBinding wsdlBinding) {
+        List extensibilityElements = wsdlBinding.getExtensibilityElements();
+        registerPoliciesAsElements(extensibilityElements);
+
+        Iterator iterator = wsdlBinding.getBindingOperations().values()
+                .iterator();
+        while (iterator.hasNext()) {
+            WSDLBindingOperation wsdlBindingOperation = (WSDLBindingOperation) iterator
+                    .next();
+            registerPoliciesInBindOperation(wsdlBindingOperation);
+        }
+    }
+
+    private void registerPoliciesInBindOperation(
+            WSDLBindingOperation wsdlBindingOperation) {
+        List extensibilityElements = wsdlBindingOperation
+                .getExtensibilityElements();
+        registerPoliciesAsElements(extensibilityElements);
+
+        if (wsdlBindingOperation.getInput() != null) {
+            extensibilityElements = wsdlBindingOperation.getInput()
+                    .getExtensibilityElements();
+            registerPoliciesAsElements(extensibilityElements);
+        }
+        if (wsdlBindingOperation.getOutput() != null) {
+            extensibilityElements = wsdlBindingOperation.getOutput()
+                    .getExtensibilityElements();
+            registerPoliciesAsElements(extensibilityElements);
+        }
+    }
+
+    private void registerPoliciesInWSDLInterface(WSDLInterface wsdlInterface) {
+        registerPoliciesInElement(wsdlInterface);
+        Iterator iterator = wsdlInterface.getOperations().values().iterator();
+        while (iterator.hasNext()) {
+            WSDLOperation wsdlOperation = (WSDLOperation) iterator.next();
+            registerPoliciesInWSDLOperation(wsdlOperation);
+        }
+    }
+
+    private void registerPoliciesInWSDLOperation(WSDLOperation wsdlOperation) {
+        List extensibilityElements = wsdlOperation.getExtensibilityElements();
+        registerPoliciesAsElements(extensibilityElements);
+
+        if (wsdlOperation.getInputMessage() != null) {
+            registerPoliciesInElement(wsdlOperation.getInputMessage());
+        }
+        if (wsdlOperation.getOutputMessage() != null) {
+            registerPoliciesInElement(wsdlOperation.getOutputMessage());
+        }
+    }
+
+    private void registerPoliciesInElement(Component component) {
+        registerPoliciesAsAttribute(component.getExtensibilityAttributes());
+        registerPoliciesAsElements(component.getExtensibilityElements());
+    }
+
+    private void registerPoliciesAsElements(List elements) {
+        Iterator iterator = elements.iterator();
+        while (iterator.hasNext()) {
+            Object extensibilityElement = iterator.next();
+
+            if (extensibilityElement instanceof PolicyExtensibilityElement) {
+                PolicyExtensibilityElement policyExtensibilityElement = (PolicyExtensibilityElement) extensibilityElement;
+                Object policyElement = policyExtensibilityElement.getPolicyElement();
+                
+                if (policyElement instanceof Policy) {
+                	String policyURI = ((Policy) policyElement).getPolicyURI();
+                	
+                	if (policyURI != null) {
+                		reg.register(policyURI, ((Policy) policyElement));
+                	}
+                	
+                } else if (policyElement instanceof PolicyReference) {
+                	String policyRefURI = ((PolicyReference) policyElement).getPolicyURIString();
+                	
+                	if (reg.lookup(policyRefURI) == null) {
+                		reg.register(policyRefURI, null);
+                	}
+                }   
+            }
+        }
+    }
+
+    private void registerPoliciesAsAttribute(List elements) {
+        Iterator iterator = elements.iterator();
+
+        while (iterator.hasNext()) {
+            WSDLExtensibilityAttribute wsdlExtensibilityAttribute = (WSDLExtensibilityAttribute) iterator
+                    .next();
+            QName qname = wsdlExtensibilityAttribute.getKey();
+
+            if (qname.getNamespaceURI().equals(
+                    PolicyConstants.WS_POLICY_NAMESPACE_URI)
+                    && qname.getLocalPart().equals("PolicyURIs")) {
+                String value = wsdlExtensibilityAttribute.getValue().toString();
+                String[] policyURIs = value.split(" ");
+                for (int i = 0; i < policyURIs.length; i++) {
+                    String policyURI = policyURIs[i].trim();
+
+                    if (reg.lookup(policyURI) == null) {
+                        reg.register(policyURI, null);
+                    }
+                }
+            }
+        }
+    }
+
+    public boolean hasPolicies() {
+    	return (reg.values().hasNext());
+    }
+}

Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/emitter/MultiLanguageClientEmitter.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/emitter/MultiLanguageClientEmitter.java?rev=376593&r1=376592&r2=376593&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/emitter/MultiLanguageClientEmitter.java (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/emitter/MultiLanguageClientEmitter.java Fri Feb 10 00:39:27 2006
@@ -19,6 +19,8 @@
 //~--- non-JDK imports --------------------------------------------------------
 
 import org.apache.axis2.util.JavaUtils;
+import org.apache.axis2.util.PolicyAttachmentUtil;
+import org.apache.axis2.util.PolicyUtil;
 import org.apache.axis2.util.XSLTUtils;
 import org.apache.axis2.wsdl.codegen.CodeGenConfiguration;
 import org.apache.axis2.wsdl.codegen.CodeGenerationException;
@@ -38,6 +40,7 @@
 import org.apache.axis2.wsdl.builder.SchemaUnwrapper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.ws.policy.Policy;
 import org.apache.wsdl.MessageReference;
 import org.apache.wsdl.WSDLBinding;
 import org.apache.wsdl.WSDLBindingOperation;
@@ -60,7 +63,13 @@
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+
 import javax.xml.transform.URIResolver;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -152,6 +161,8 @@
     protected CodeGenConfiguration configuration;
     protected TypeMapper mapper;
     protected URIResolver resolver;
+    
+    protected PolicyAttachmentUtil attachmentUtil;
 
 
     protected MultiLanguageClientEmitter() {
@@ -214,15 +225,14 @@
      */
     protected void addEndpoint(Document doc, Element rootElement) throws Exception {
         WSDLEndpoint endpoint = infoHolder.getPort();
-
-        // attach the policy for this endpoint here
-        //            String policyFileResourceName = null;
-        //            Policy policy =  (Policy)endpointPolicyMap.get(endpoint.getName());
-        //            if (policy!=null){
-        //                //process the policy for this end point
-        //                 policyFileResourceName = processPolicy(policy,endpoint.getName().getLocalPart());
-        //
-        //            }
+        
+        Policy endpointPolicy = attachmentUtil.getPolicyForEndPoint(endpoint.getName());
+        
+        if (endpointPolicy != null) {
+        	String policyString = PolicyUtil.getPolicyAsString(endpointPolicy);
+        	addAttribute(doc, "servicePolicy", policyString, rootElement);
+        }
+        
         Element endpointElement = doc.createElement("endpoint");
         org.apache.wsdl.extensions.SOAPAddress address = null;
         Iterator iterator = endpoint.getExtensibilityElements().iterator();
@@ -240,9 +250,6 @@
                 ? address.getLocationURI()
                 : "");
 
-//      if (policyFileResourceName!=null){
-//          addAttribute(doc,"policyRef",policyFileResourceName,endpointElement);
-//      }
         endpointElement.appendChild(text);
         rootElement.appendChild(endpointElement);
     }
@@ -429,7 +436,12 @@
         addAttribute(doc, "namespace", boundInterface.getName().getNamespaceURI(), rootElement);
         addAttribute(doc, "interfaceName", localPart, rootElement);
         addAttribute(doc, "callbackname", localPart + CALL_BACK_HANDLER_SUFFIX, rootElement);
-
+        
+        // add the wrapper for Policy Strings
+        if (attachmentUtil.hasPolicies()) {
+        	addAttribute(doc, "isPolicyEnabled", "yes", rootElement);
+        }
+                
         // add the wrap classes flag
         if (configuration.isPackClasses()) {
             addAttribute(doc, "wrapped", "yes", rootElement);
@@ -437,6 +449,8 @@
 
         // add SOAP version
         addSoapVersion(binding, doc, rootElement);
+        
+        
 
         // add the end point
         addEndpoint(doc, rootElement);
@@ -467,7 +481,7 @@
         // the stubs implementation and not visible outside
         rootElement.appendChild(createDOMElementforDatabinders(doc, binding));
         doc.appendChild(rootElement);
-
+        
         return doc;
     }
 
@@ -1239,7 +1253,7 @@
         InterfaceImplementationWriter writer =
                 new InterfaceImplementationWriter(getOutputDirectory(this.configuration.getOutputLocation(), "src"),
                         this.configuration.getOutputLanguage());
-
+        
         writeClass(interfaceImplModel, writer);
     }
 
@@ -1588,10 +1602,12 @@
      */
     public void setCodeGenConfiguration(CodeGenConfiguration configuration) {
         this.configuration = configuration;
+        attachmentUtil = new PolicyAttachmentUtil(configuration.getWom());
         resolver = new XSLTIncludeResolver(this.configuration.getProperties());
 
         // select necessary information from the WOM
         populateInformationHolder();
+        
     }
 
     /**
@@ -1615,7 +1631,10 @@
         private WSDLEndpoint port;
         private WSDLInterface WSDLinterface;
         private WSDLService service;
-
+        
+        private PolicyAttachmentUtil util;
+        
+        
         private HashMap propertyMap = new HashMap();
 
         public void putProperty(Object key,Object val){
@@ -1630,19 +1649,19 @@
         public WSDLBinding getBinding() {
             return binding;
         }
-
+        
         public WSDLEndpoint getPort() {
             return port;
         }
-
+        
         public WSDLInterface getWSDLinterface() {
             return WSDLinterface;
         }
-
+       
         public WSDLService getService() {
             return service;
         }
-
+        
         //~--- set methods ----------------------------------------------------
 
         public void setBinding(WSDLBinding binding) {

Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/template/java/InterfaceImplementationTemplate.xsl
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/template/java/InterfaceImplementationTemplate.xsl?rev=376593&r1=376592&r2=376593&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/template/java/InterfaceImplementationTemplate.xsl (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/template/java/InterfaceImplementationTemplate.xsl Fri Feb 10 00:39:27 2006
@@ -35,12 +35,28 @@
         //default axis home being null forces the system to pick up the mars from the axis2 library
         public static final String AXIS2_HOME = null;
         protected static org.apache.axis2.description.AxisOperation[] _operations;
-
+	
+	<xsl:if test="(@servicePolicy)">
+	protected static String _service_policy_string;
+	</xsl:if>
+	
         static{
 
         //creating the Service
         _service = new org.apache.axis2.description.AxisService("<xsl:value-of select="@servicename"/>");
-
+	
+	
+	<xsl:if test="(@servicePolicy)"> 
+	////////////////////////////////////////////////////////////////////////
+	
+	 _service_policy_string = "<xsl:value-of select="@servicePolicy"/>";
+	 org.apache.axis2.description.PolicyInclude servicePolicyInclude = _service.getPolicyInclude();
+	 servicePolicyInclude.addPolicyElement(org.apache.axis2.description.PolicyInclude.SERVICE_POLICY, 
+	 				getPolicyFromString(_service_policy_string));
+					
+	////////////////////////////////////////////////////////////////////////
+	</xsl:if>
+	
         //creating the operations
         org.apache.axis2.description.AxisOperation __operation;
         _operations = new org.apache.axis2.description.AxisOperation[<xsl:value-of select="count(method)"/>];
@@ -64,7 +80,29 @@
 
      public <xsl:value-of select="@name"/>(org.apache.axis2.context.ConfigurationContext configurationContext, String targetEndpoint)
         throws java.lang.Exception {
-
+	
+	<xsl:if test="@isPolicyEnabled">
+	
+	////////////////////////////////////////////////////////////////////////
+		
+	org.apache.axis2.engine.AxisConfiguration axisConfiguration = configurationContext.getAxisConfiguration();
+	java.util.Collection modules = axisConfiguration.getModules().values();
+		
+	for (java.util.Iterator iterator = modules.iterator(); iterator.hasNext(); iterator.next()) {
+		org.apache.axis2.description.AxisModule axisModule = (org.apache.axis2.description.AxisModule) iterator.next();
+		String[] namespaces = axisModule.getSupportedPolicyNamespaces();
+			
+		if (namespaces != null) {
+			for (int i = 0; i &lt; namespaces.length; i++) {
+				ns2Modules.put(namespaces[i], axisModule);
+			}
+		}
+	}
+				
+	////////////////////////////////////////////////////////////////////////
+		
+	</xsl:if>
+	
         _serviceClient = new org.apache.axis2.client.ServiceClient(configurationContext,_service);
         _serviceClient.getOptions().setTo(new org.apache.axis2.addressing.EndpointReference(
                 targetEndpoint));
@@ -72,6 +110,11 @@
             //Set the soap version
             _serviceClient.getOptions().setSoapVersionURI(org.apache.ws.commons.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
         </xsl:if>
+	
+	<xsl:if test="@isPolicyEnabled">
+	engage(_service, configurationContext.getAxisConfiguration());
+	</xsl:if>
+	
 
     }
 
@@ -402,6 +445,96 @@
            }
             </xsl:if>
         </xsl:for-each>
+	
+	<xsl:if test="(@isPolicyEnabled)">
+	
+	/** */
+	private java.util.HashMap ns2Modules = new java.util.HashMap();
+	
+	////////////////////////////////////////////////////////////////////////
+	
+	private static org.apache.ws.policy.Policy getPolicyFromString (String policyString) {
+		org.apache.ws.policy.util.PolicyReader prdr 
+			= org.apache.ws.policy.util.PolicyFactory.getPolicyReader(
+					org.apache.ws.policy.util.PolicyFactory.OM_POLICY_READER);
+		try {
+			if (policyString != null 
+				&amp;&amp; !policyString.trim().equals("")) {
+					return prdr.readPolicy(
+						new java.io.ByteArrayInputStream(
+						policyString.getBytes()));
+			}
+		
+		} catch (Exception e) {
+			throw new RuntimeException(
+				"cannot convert "+ policyString	+ " to policy", e);
+		}
+		return null;
+	}
+	
+	// /////////////////////////////////////////////////////////////////
+	
+	private java.util.ArrayList getModules(java.util.List termsList) {
+		java.util.ArrayList arrayList = new java.util.ArrayList();
+		java.util.Iterator iterator = termsList.iterator();
+		
+		org.apache.ws.policy.PrimitiveAssertion pa;
+		String namespace;
+		org.apache.axis2.description.AxisModule axisModule;
+		
+		while (iterator.hasNext()) {
+			pa = (org.apache.ws.policy.PrimitiveAssertion) iterator.next();
+			namespace = pa.getName().getNamespaceURI();
+			axisModule = (org.apache.axis2.description.AxisModule) ns2Modules.get(namespace);
+			
+			if (axisModule == null) {
+				// TODO
+				System.err.println("Warning: cannot find a module for process PrimitiveAssertion" + pa.getName());
+			}			
+			arrayList.add(axisModule);
+		}
+		
+		return arrayList;
+	}
+	
+	private void engage(org.apache.axis2.description.AxisDescription axisDescription, org.apache.axis2.engine.AxisConfiguration axisConfiguration) throws AxisFault {
+		
+		org.apache.axis2.description.PolicyInclude policyInclude = axisDescription.getPolicyInclude();
+		org.apache.ws.policy.Policy policy = policyInclude.getEffectivePolicy();
+		
+		if (policy == null) {
+			return;
+		}
+		
+		if (! policy.isNormalized()) {
+			policy = (org.apache.ws.policy.Policy) policy.normalize();
+		}
+		
+		org.apache.ws.policy.XorCompositeAssertion xor = (org.apache.ws.policy.XorCompositeAssertion) policy.getTerms().get(0);
+		if (xor.isEmpty()) {
+			// TODO
+			throw new RuntimeException("No policy alternative found");
+		}
+		org.apache.ws.policy.AndCompositeAssertion anAlternative = (org.apache.ws.policy.AndCompositeAssertion) xor.getTerms().get(0);
+		java.util.List moduleList = getModules(anAlternative.getTerms());
+		
+		if (axisDescription instanceof org.apache.axis2.description.AxisService) {
+			for (java.util.Iterator iterator = moduleList.iterator(); iterator.hasNext();) {
+				((org.apache.axis2.description.AxisService) axisDescription).engageModule((org.apache.axis2.description.AxisModule) iterator.next(), axisConfiguration);
+			}
+		} else if (axisDescription instanceof org.apache.axis2.description.AxisOperation) {
+			for (java.util.Iterator iterator = moduleList.iterator(); iterator.hasNext();) {
+				((org.apache.axis2.description.AxisOperation) axisDescription).engageModule((org.apache.axis2.description.AxisModule) iterator.next(), axisConfiguration);
+			}
+		}
+	}
+
+	
+	////////////////////////////////////////////////////////////////////////
+	
+	
+	</xsl:if>
+
 
         //<xsl:apply-templates/>
 

Modified: webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/deployment/ModuleBuilder.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/deployment/ModuleBuilder.java?rev=376593&r1=376592&r2=376593&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/deployment/ModuleBuilder.java (original)
+++ webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/deployment/ModuleBuilder.java Fri Feb 10 00:39:27 2006
@@ -160,8 +160,6 @@
             
             if (supportedPolicyNamespaces != null) {
             	module.setSupportedPolicyNamespaces(processSupportedPolicyNamespaces(supportedPolicyNamespaces));
-            	
-            	System.out.println("done");
             }
 
             // processing Operations

Modified: webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/util/PolicyUtil.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/util/PolicyUtil.java?rev=376593&r1=376592&r2=376593&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/util/PolicyUtil.java (original)
+++ webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/util/PolicyUtil.java Fri Feb 10 00:39:27 2006
@@ -23,6 +23,7 @@
 import org.apache.ws.policy.PolicyReference;
 import org.apache.ws.policy.util.PolicyFactory;
 import org.apache.ws.policy.util.PolicyWriter;
+import org.apache.ws.policy.util.StAXPolicyWriter;
 import org.apache.wsdl.*;
 import org.apache.wsdl.extensions.ExtensionConstants;
 import org.apache.wsdl.extensions.PolicyExtensibilityElement;
@@ -30,6 +31,8 @@
 import org.apache.wsdl.impl.WSDLProcessingException;
 
 import javax.xml.namespace.QName;
+
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintWriter;
@@ -38,321 +41,362 @@
 
 public class PolicyUtil {
 
-    public static void populatePolicy(WSDLDescription description,
-                                      AxisService axisService) {
-        WSDLService wsdlService = description.getService(new QName(axisService
-                .getName()));
-        populatePolicy(description, wsdlService, axisService);
-    }
-
-    private static void populatePolicy(WSDLDescription description,
-                                       WSDLService wsdlService, AxisService axisService) {
-
-        AxisServiceGroup axisServiceGroup = null;
-        AxisConfiguration axisConfiguration = null;
-
-        axisServiceGroup = (AxisServiceGroup) axisService.getParent();
-
-        if (axisServiceGroup != null) {
-            axisConfiguration = (AxisConfiguration) axisServiceGroup.getParent();
-        }
-
-        PolicyInclude servicePolicyInclude = axisService.getPolicyInclude();
-
-        List policyList;
-
-        // Policies defined in Axis2.xml
-        if (axisConfiguration != null) {
-            policyList = axisConfiguration.getPolicyInclude().getPolicyElements(
-                    PolicyInclude.AXIS_POLICY);
-            addPolicyAsExtElements(description, policyList, wsdlService,
-                    servicePolicyInclude);
-        }
-
-        // Policies defined in wsdl:Service
-        policyList = servicePolicyInclude
-                .getPolicyElements(PolicyInclude.SERVICE_POLICY);
-        addPolicyAsExtElements(description, policyList, wsdlService,
-                servicePolicyInclude);
-
-        Iterator wsdlEndpoints = wsdlService.getEndpoints().values().iterator();
-        if (!wsdlEndpoints.hasNext()) {
-            throw new WSDLProcessingException("should at least one endpoints");
-        }
-
-        WSDLEndpoint wsdlEndpoint = (WSDLEndpoint) wsdlEndpoints.next();
-        populatePolicy(description, wsdlEndpoint, axisService);
-    }
-
-    private static void populatePolicy(WSDLDescription description,
-                                       WSDLEndpoint wsdlEndpoint, AxisService axisService) {
-        PolicyInclude policyInclude = axisService.getPolicyInclude();
-        List policyList = policyInclude
-                .getPolicyElements(PolicyInclude.PORT_POLICY);
-        addPolicyAsExtElements(description, policyList, wsdlEndpoint,
-                policyInclude);
-
-        WSDLBinding wsdlBinding = wsdlEndpoint.getBinding();
-        populatePolicy(description, wsdlBinding, axisService);
-        WSDLInterface wsdlInterface = wsdlBinding.getBoundInterface();
-        populatePolicy(description, wsdlInterface, axisService);
-
-    }
-
-    private static void populatePolicy(WSDLDescription description,
-                                       WSDLInterface wsdlInterface, AxisService axisService) {
-        PolicyInclude policyInclude = axisService.getPolicyInclude();
-        List policyList = policyInclude
-                .getPolicyElements(PolicyInclude.PORT_TYPE_POLICY);
-//        addPolicyAsExtAttributes(description, policyList, wsdlInterface,
-//                policyInclude);
-
-        Iterator wsdlOperations = wsdlInterface.getOperations().values()
-                .iterator();
-        WSDLOperation wsdlOperation;
-
-        while (wsdlOperations.hasNext()) {
-            wsdlOperation = (WSDLOperation) wsdlOperations.next();
-            populatePolicy(description, wsdlOperation, axisService
-                    .getOperation(wsdlOperation.getName()));
-
-        }
-    }
-
-    private static void populatePolicy(WSDLDescription description,
-                                       WSDLOperation wsdlOperation, AxisOperation axisOperation) {
-
-        PolicyInclude policyInclude = axisOperation.getPolicyInclude();
-
-        // wsdl:PortType -> wsdl:Operation
-        List policyList = policyInclude
-                .getPolicyElements(PolicyInclude.OPERATION_POLICY);
-        addPolicyAsExtElements(description, policyList, wsdlOperation,
-                policyInclude);
-
-        if (WSDLConstants.MEP_URI_IN_ONLY.equals(axisOperation
-                .getMessageExchangePattern())) {
-            AxisMessage input = axisOperation
-                    .getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
-            PolicyInclude policyInclude2 = input.getPolicyInclude();
-
-            // wsdl:PortType -> wsdl:Operation -> wsdl:Input
-            List policyList2 = policyInclude2
-                    .getPolicyElements(PolicyInclude.INPUT_POLICY);
-            addPolicyAsExtAttributes(description, policyList2, wsdlOperation
-                    .getInputMessage(), policyInclude2);
-
-        } else if (WSDLConstants.MEP_URI_IN_OUT.equals(axisOperation
-                .getMessageExchangePattern())) {
-            PolicyInclude policyInclude2;
-            List policyList2;
-
-            AxisMessage input = axisOperation
-                    .getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
-            policyInclude2 = input.getPolicyInclude();
-
-            // wsdl:PortType -> wsdl:Operation -> wsdl:Input
-            policyList2 = policyInclude2
-                    .getPolicyElements(PolicyInclude.INPUT_POLICY);
-            addPolicyAsExtAttributes(description, policyList2, wsdlOperation
-                    .getInputMessage(), policyInclude2);
-
-            AxisMessage output = axisOperation
-                    .getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
-            policyInclude2 = output.getPolicyInclude();
-
-            // wsdl:PortType -> wsdl:Operation -> wsdl:Output
-            policyList2 = policyInclude2
-                    .getPolicyElements(PolicyInclude.OUTPUT_POLICY);
-            addPolicyAsExtAttributes(description, policyList2, wsdlOperation
-                    .getOutputMessage(), policyInclude2);
-        }
-    }
-
-    private static void populatePolicy(WSDLDescription description,
-                                       WSDLBinding wsdlBinding, AxisService axisService) {
-        PolicyInclude policyInclude = axisService.getPolicyInclude();
-
-        List policyList = policyInclude
-                .getPolicyElements(PolicyInclude.AXIS_SERVICE_POLICY);
-        addPolicyAsExtElements(description, policyList, wsdlBinding,
-                policyInclude);
-
-        policyList = policyInclude
-                .getPolicyElements(PolicyInclude.BINDING_POLICY);
-        addPolicyAsExtElements(description, policyList, wsdlBinding,
-                policyInclude);
-
-        Iterator wsdlOperations = wsdlBinding.getBindingOperations().values()
-                .iterator();
-        WSDLBindingOperation wsdlBindingOperation;
-
-        while (wsdlOperations.hasNext()) {
-            wsdlBindingOperation = (WSDLBindingOperation) wsdlOperations.next();
-            populatePolicy(description, wsdlBindingOperation, axisService
-                    .getOperation(wsdlBindingOperation.getName()));
-        }
-    }
-
-    private static void populatePolicy(WSDLDescription description,
-                                       WSDLBindingOperation wsdlBindingOperation,
-                                       AxisOperation axisOperation) {
-
-        PolicyInclude policyInclude = axisOperation.getPolicyInclude();
-        List policyList = policyInclude
-                .getPolicyElements(PolicyInclude.BINDING_OPERATION_POLICY);
-        addPolicyAsExtElements(description, policyList, wsdlBindingOperation,
-                policyInclude);
-
-        //
-        policyList = policyInclude
-                .getPolicyElements(PolicyInclude.AXIS_OPERATION_POLICY);
-        addPolicyAsExtElements(description, policyList, wsdlBindingOperation,
-                policyInclude);
-
-        if (WSDLConstants.MEP_URI_IN_ONLY.equals(axisOperation
-                .getMessageExchangePattern())) {
-            AxisMessage input = axisOperation
-                    .getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
-            PolicyInclude policyInclude2 = input.getPolicyInclude();
-
-            // wsdl:Binding -> wsdl:Operation -> wsdl:Input
-            List policyList2 = policyInclude2
-                    .getPolicyElements(PolicyInclude.INPUT_POLICY);
-            addPolicyAsExtElements(description, policyList2,
-                    wsdlBindingOperation.getInput(), policyInclude2);
-
-            //
-            policyList2 = policyInclude2
-                    .getPolicyElements(PolicyInclude.AXIS_MESSAGE_POLICY);
-            addPolicyAsExtElements(description, policyList2,
-                    wsdlBindingOperation.getInput(), policyInclude2);
-
-        } else if (WSDLConstants.MEP_URI_IN_OUT.equals(axisOperation
-                .getMessageExchangePattern())) {
-            PolicyInclude policyInclude2;
-            List policyList2;
-
-            AxisMessage input = axisOperation
-                    .getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
-            policyInclude2 = input.getPolicyInclude();
-
-            // wsdl:Binding -> wsdl:Operation -> wsdl:Input
-            policyList2 = policyInclude2
-                    .getPolicyElements(PolicyInclude.INPUT_POLICY);
-            addPolicyAsExtElements(description, policyList2,
-                    wsdlBindingOperation.getInput(), policyInclude2);
-            // 
-            policyList2 = policyInclude2
-                    .getPolicyElements(PolicyInclude.AXIS_MESSAGE_POLICY);
-            addPolicyAsExtElements(description, policyList2,
-                    wsdlBindingOperation.getInput(), policyInclude2);
-
-            AxisMessage output = axisOperation
-                    .getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
-            policyInclude2 = output.getPolicyInclude();
-
-            // wsdl:Binding -> wsdl:Operation -> wsdl:Output
-            policyList2 = policyInclude2
-                    .getPolicyElements(PolicyInclude.OUTPUT_POLICY);
-            addPolicyAsExtElements(description, policyList2,
-                    wsdlBindingOperation.getOutput(), policyInclude2);
-            // 
-            policyList2 = policyInclude2
-                    .getPolicyElements(PolicyInclude.AXIS_MESSAGE_POLICY);
-            addPolicyAsExtElements(description, policyList2,
-                    wsdlBindingOperation.getInput(), policyInclude2);
-        }
-    }
-
-    //    private static void addPolicyToComponent(Policy policy, Component
-    // component) {
-    //        component.addExtensibilityElement(getExtensibilityElement(policy));
-    //    }
-
-    private static PolicyExtensibilityElement getExtensibilityElement(
-            Object policyElement) {
-        PolicyExtensibilityElement element = (PolicyExtensibilityElement) (new ExtensionFactoryImpl())
-                .getExtensionElement(ExtensionConstants.POLICY);
-        element.setPolicyElement(policyElement);
-        return element;
-    }
-
-    private static WSDLExtensibilityAttribute getExtensibilitiyAttribute(
-            PolicyReference policyReference) {
-        WSDLExtensibilityAttribute extensibilityAttribute = new AxisDescWSDLComponentFactory()
-                .createWSDLExtensibilityAttribute();
-        extensibilityAttribute.setKey(new QName(
-                PolicyConstants.WSU_NAMESPACE_URI, "PolicyURIs"));
-        extensibilityAttribute.setValue(new QName(policyReference
-                .getPolicyURIString()));
-        return extensibilityAttribute;
-    }
-
-    private static void addPolicyAsExtElements(WSDLDescription description,
-                                               List policyList, Component component, PolicyInclude policyInclude) {
-        Iterator policyElementIterator = policyList.iterator();
-        Object policyElement;
-
-        while (policyElementIterator.hasNext()) {
-            policyElement = policyElementIterator.next();
-
-            if (policyElement instanceof PolicyReference) {
-                String policyURIString = ((PolicyReference) policyElement)
-                        .getPolicyURIString();
-                description
-                        .addExtensibilityElement(getExtensibilityElement(policyInclude
-                                .getPolicy(policyURIString)));
-            }
-
-            component
-                    .addExtensibilityElement(getExtensibilityElement(policyElement));
-        }
-    }
-
-    private static void addPolicyAsExtAttributes(WSDLDescription description,
-                                                 List policyList, Component component, PolicyInclude policyInclude) {
-        Iterator policyElementIterator = policyList.iterator();
-        Object policyElement;
-
-        while (policyElementIterator.hasNext()) {
-            policyElement = policyElementIterator.next();
-
-            if (policyElement instanceof PolicyReference) {
-                String policyURIString = ((PolicyReference) policyElement)
-                        .getPolicyURIString();
-                component
-                        .addExtensibleAttributes(getExtensibilitiyAttribute((PolicyReference) policyElement));
-                description
-                        .addExtensibilityElement(getExtensibilityElement(policyInclude
-                                .getPolicy(policyURIString)));
-
-            }
-        }
-    }
-
-    public static void writePolicy(PolicyInclude policy, OutputStream out) {
-        if (policy != null) {
-            Policy pl = policy.getEffectivePolicy();
-            if (pl != null) {
-                PolicyWriter write = PolicyFactory.getPolicyWriter(PolicyFactory.StAX_POLICY_WRITER);
-                write.writePolicy(pl, out);
-                try {
-                    out.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            } else {
-                PrintWriter write = new PrintWriter(out);
-                write.write("<policy>no policy found</policy>");
-                write.flush();
-                write.close();
-            }
-        } else {
-            PrintWriter write = new PrintWriter(out);
-            write.write("<policy>no policy found</policy>");
-            write.flush();
-            write.close();
-        }
-    }
+	public static void populatePolicy(WSDLDescription description,
+			AxisService axisService) {
+		WSDLService wsdlService = description.getService(new QName(axisService
+				.getName()));
+		populatePolicy(description, wsdlService, axisService);
+	}
+
+	private static void populatePolicy(WSDLDescription description,
+			WSDLService wsdlService, AxisService axisService) {
+
+		AxisServiceGroup axisServiceGroup = null;
+		AxisConfiguration axisConfiguration = null;
+
+		axisServiceGroup = (AxisServiceGroup) axisService.getParent();
+
+		if (axisServiceGroup != null) {
+			axisConfiguration = (AxisConfiguration) axisServiceGroup
+					.getParent();
+		}
+
+		PolicyInclude servicePolicyInclude = axisService.getPolicyInclude();
+
+		List policyList;
+
+		// Policies defined in Axis2.xml
+		if (axisConfiguration != null) {
+			policyList = axisConfiguration.getPolicyInclude()
+					.getPolicyElements(PolicyInclude.AXIS_POLICY);
+			addPolicyAsExtElements(description, policyList, wsdlService,
+					servicePolicyInclude);
+		}
+
+		// Policies defined in wsdl:Service
+		policyList = servicePolicyInclude
+				.getPolicyElements(PolicyInclude.SERVICE_POLICY);
+		addPolicyAsExtElements(description, policyList, wsdlService,
+				servicePolicyInclude);
+
+		Iterator wsdlEndpoints = wsdlService.getEndpoints().values().iterator();
+		if (!wsdlEndpoints.hasNext()) {
+			throw new WSDLProcessingException("should at least one endpoints");
+		}
+
+		WSDLEndpoint wsdlEndpoint = (WSDLEndpoint) wsdlEndpoints.next();
+		populatePolicy(description, wsdlEndpoint, axisService);
+	}
+
+	private static void populatePolicy(WSDLDescription description,
+			WSDLEndpoint wsdlEndpoint, AxisService axisService) {
+		PolicyInclude policyInclude = axisService.getPolicyInclude();
+		List policyList = policyInclude
+				.getPolicyElements(PolicyInclude.PORT_POLICY);
+		addPolicyAsExtElements(description, policyList, wsdlEndpoint,
+				policyInclude);
+
+		WSDLBinding wsdlBinding = wsdlEndpoint.getBinding();
+		populatePolicy(description, wsdlBinding, axisService);
+		WSDLInterface wsdlInterface = wsdlBinding.getBoundInterface();
+		populatePolicy(description, wsdlInterface, axisService);
+
+	}
+
+	private static void populatePolicy(WSDLDescription description,
+			WSDLInterface wsdlInterface, AxisService axisService) {
+		PolicyInclude policyInclude = axisService.getPolicyInclude();
+		List policyList = policyInclude
+				.getPolicyElements(PolicyInclude.PORT_TYPE_POLICY);
+		// addPolicyAsExtAttributes(description, policyList, wsdlInterface,
+		// policyInclude);
+
+		Iterator wsdlOperations = wsdlInterface.getOperations().values()
+				.iterator();
+		WSDLOperation wsdlOperation;
+
+		while (wsdlOperations.hasNext()) {
+			wsdlOperation = (WSDLOperation) wsdlOperations.next();
+			populatePolicy(description, wsdlOperation, axisService
+					.getOperation(wsdlOperation.getName()));
+
+		}
+	}
+
+	private static void populatePolicy(WSDLDescription description,
+			WSDLOperation wsdlOperation, AxisOperation axisOperation) {
+
+		PolicyInclude policyInclude = axisOperation.getPolicyInclude();
+
+		// wsdl:PortType -> wsdl:Operation
+		List policyList = policyInclude
+				.getPolicyElements(PolicyInclude.OPERATION_POLICY);
+		addPolicyAsExtElements(description, policyList, wsdlOperation,
+				policyInclude);
+
+		if (WSDLConstants.MEP_URI_IN_ONLY.equals(axisOperation
+				.getMessageExchangePattern())) {
+			AxisMessage input = axisOperation
+					.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
+			PolicyInclude policyInclude2 = input.getPolicyInclude();
+
+			// wsdl:PortType -> wsdl:Operation -> wsdl:Input
+			List policyList2 = policyInclude2
+					.getPolicyElements(PolicyInclude.INPUT_POLICY);
+			addPolicyAsExtAttributes(description, policyList2, wsdlOperation
+					.getInputMessage(), policyInclude2);
+
+		} else if (WSDLConstants.MEP_URI_IN_OUT.equals(axisOperation
+				.getMessageExchangePattern())) {
+			PolicyInclude policyInclude2;
+			List policyList2;
+
+			AxisMessage input = axisOperation
+					.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
+			policyInclude2 = input.getPolicyInclude();
+
+			// wsdl:PortType -> wsdl:Operation -> wsdl:Input
+			policyList2 = policyInclude2
+					.getPolicyElements(PolicyInclude.INPUT_POLICY);
+			addPolicyAsExtAttributes(description, policyList2, wsdlOperation
+					.getInputMessage(), policyInclude2);
+
+			AxisMessage output = axisOperation
+					.getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
+			policyInclude2 = output.getPolicyInclude();
+
+			// wsdl:PortType -> wsdl:Operation -> wsdl:Output
+			policyList2 = policyInclude2
+					.getPolicyElements(PolicyInclude.OUTPUT_POLICY);
+			addPolicyAsExtAttributes(description, policyList2, wsdlOperation
+					.getOutputMessage(), policyInclude2);
+		}
+	}
+
+	private static void populatePolicy(WSDLDescription description,
+			WSDLBinding wsdlBinding, AxisService axisService) {
+		PolicyInclude policyInclude = axisService.getPolicyInclude();
+
+		List policyList = policyInclude
+				.getPolicyElements(PolicyInclude.AXIS_SERVICE_POLICY);
+		addPolicyAsExtElements(description, policyList, wsdlBinding,
+				policyInclude);
+
+		policyList = policyInclude
+				.getPolicyElements(PolicyInclude.BINDING_POLICY);
+		addPolicyAsExtElements(description, policyList, wsdlBinding,
+				policyInclude);
+
+		Iterator wsdlOperations = wsdlBinding.getBindingOperations().values()
+				.iterator();
+		WSDLBindingOperation wsdlBindingOperation;
+
+		while (wsdlOperations.hasNext()) {
+			wsdlBindingOperation = (WSDLBindingOperation) wsdlOperations.next();
+			populatePolicy(description, wsdlBindingOperation, axisService
+					.getOperation(wsdlBindingOperation.getName()));
+		}
+	}
+
+	private static void populatePolicy(WSDLDescription description,
+			WSDLBindingOperation wsdlBindingOperation,
+			AxisOperation axisOperation) {
+
+		PolicyInclude policyInclude = axisOperation.getPolicyInclude();
+		List policyList = policyInclude
+				.getPolicyElements(PolicyInclude.BINDING_OPERATION_POLICY);
+		addPolicyAsExtElements(description, policyList, wsdlBindingOperation,
+				policyInclude);
+
+		//
+		policyList = policyInclude
+				.getPolicyElements(PolicyInclude.AXIS_OPERATION_POLICY);
+		addPolicyAsExtElements(description, policyList, wsdlBindingOperation,
+				policyInclude);
+
+		if (WSDLConstants.MEP_URI_IN_ONLY.equals(axisOperation
+				.getMessageExchangePattern())) {
+			AxisMessage input = axisOperation
+					.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
+			PolicyInclude policyInclude2 = input.getPolicyInclude();
+
+			// wsdl:Binding -> wsdl:Operation -> wsdl:Input
+			List policyList2 = policyInclude2
+					.getPolicyElements(PolicyInclude.INPUT_POLICY);
+			addPolicyAsExtElements(description, policyList2,
+					wsdlBindingOperation.getInput(), policyInclude2);
+
+			//
+			policyList2 = policyInclude2
+					.getPolicyElements(PolicyInclude.AXIS_MESSAGE_POLICY);
+			addPolicyAsExtElements(description, policyList2,
+					wsdlBindingOperation.getInput(), policyInclude2);
+
+		} else if (WSDLConstants.MEP_URI_IN_OUT.equals(axisOperation
+				.getMessageExchangePattern())) {
+			PolicyInclude policyInclude2;
+			List policyList2;
+
+			AxisMessage input = axisOperation
+					.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
+			policyInclude2 = input.getPolicyInclude();
+
+			// wsdl:Binding -> wsdl:Operation -> wsdl:Input
+			policyList2 = policyInclude2
+					.getPolicyElements(PolicyInclude.INPUT_POLICY);
+			addPolicyAsExtElements(description, policyList2,
+					wsdlBindingOperation.getInput(), policyInclude2);
+			// 
+			policyList2 = policyInclude2
+					.getPolicyElements(PolicyInclude.AXIS_MESSAGE_POLICY);
+			addPolicyAsExtElements(description, policyList2,
+					wsdlBindingOperation.getInput(), policyInclude2);
+
+			AxisMessage output = axisOperation
+					.getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
+			policyInclude2 = output.getPolicyInclude();
+
+			// wsdl:Binding -> wsdl:Operation -> wsdl:Output
+			policyList2 = policyInclude2
+					.getPolicyElements(PolicyInclude.OUTPUT_POLICY);
+			addPolicyAsExtElements(description, policyList2,
+					wsdlBindingOperation.getOutput(), policyInclude2);
+			// 
+			policyList2 = policyInclude2
+					.getPolicyElements(PolicyInclude.AXIS_MESSAGE_POLICY);
+			addPolicyAsExtElements(description, policyList2,
+					wsdlBindingOperation.getInput(), policyInclude2);
+		}
+	}
+
+	// private static void addPolicyToComponent(Policy policy, Component
+	// component) {
+	// component.addExtensibilityElement(getExtensibilityElement(policy));
+	// }
+
+	private static PolicyExtensibilityElement getExtensibilityElement(
+			Object policyElement) {
+		PolicyExtensibilityElement element = (PolicyExtensibilityElement) (new ExtensionFactoryImpl())
+				.getExtensionElement(ExtensionConstants.POLICY);
+		element.setPolicyElement(policyElement);
+		return element;
+	}
+
+	private static WSDLExtensibilityAttribute getExtensibilitiyAttribute(
+			PolicyReference policyReference) {
+		WSDLExtensibilityAttribute extensibilityAttribute = new AxisDescWSDLComponentFactory()
+				.createWSDLExtensibilityAttribute();
+		extensibilityAttribute.setKey(new QName(
+				PolicyConstants.WSU_NAMESPACE_URI, "PolicyURIs"));
+		extensibilityAttribute.setValue(new QName(policyReference
+				.getPolicyURIString()));
+		return extensibilityAttribute;
+	}
+
+	private static void addPolicyAsExtElements(WSDLDescription description,
+			List policyList, Component component, PolicyInclude policyInclude) {
+		Iterator policyElementIterator = policyList.iterator();
+		Object policyElement;
+
+		while (policyElementIterator.hasNext()) {
+			policyElement = policyElementIterator.next();
+
+			if (policyElement instanceof PolicyReference) {
+				String policyURIString = ((PolicyReference) policyElement)
+						.getPolicyURIString();
+				description
+						.addExtensibilityElement(getExtensibilityElement(policyInclude
+								.getPolicy(policyURIString)));
+			}
+
+			component
+					.addExtensibilityElement(getExtensibilityElement(policyElement));
+		}
+	}
+
+	private static void addPolicyAsExtAttributes(WSDLDescription description,
+			List policyList, Component component, PolicyInclude policyInclude) {
+		Iterator policyElementIterator = policyList.iterator();
+		Object policyElement;
+
+		while (policyElementIterator.hasNext()) {
+			policyElement = policyElementIterator.next();
+
+			if (policyElement instanceof PolicyReference) {
+				String policyURIString = ((PolicyReference) policyElement)
+						.getPolicyURIString();
+				component
+						.addExtensibleAttributes(getExtensibilitiyAttribute((PolicyReference) policyElement));
+				description
+						.addExtensibilityElement(getExtensibilityElement(policyInclude
+								.getPolicy(policyURIString)));
+
+			}
+		}
+	}
+
+	public static void writePolicy(PolicyInclude policy, OutputStream out) {
+		if (policy != null) {
+			Policy pl = policy.getEffectivePolicy();
+			if (pl != null) {
+				PolicyWriter write = PolicyFactory
+						.getPolicyWriter(PolicyFactory.StAX_POLICY_WRITER);
+				write.writePolicy(pl, out);
+				try {
+					out.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			} else {
+				PrintWriter write = new PrintWriter(out);
+				write.write("<policy>no policy found</policy>");
+				write.flush();
+				write.close();
+			}
+		} else {
+			PrintWriter write = new PrintWriter(out);
+			write.write("<policy>no policy found</policy>");
+			write.flush();
+			write.close();
+		}
+	}
+
+	public static String getPolicyAsString(Policy policy) {
+		
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+		StAXPolicyWriter pwtr = (StAXPolicyWriter) PolicyFactory
+				.getPolicyWriter(PolicyFactory.StAX_POLICY_WRITER);
+		
+		pwtr.writePolicy(policy, baos);
+		return getSafeString(baos.toString());
+	}
+	
+	private static String getSafeString(String unsafeString) {
+		StringBuffer sbuf = new StringBuffer();
+		
+		char[] chars = unsafeString.toCharArray();
+		
+		for (int i = 0; i < chars.length; i++) {
+			char c = chars[i];
+			
+			switch (c) {
+				case '\\' :
+					sbuf.append('\\'); sbuf.append('\\');
+					break;
+				case '"' :
+					sbuf.append('\\'); sbuf.append('"');
+					break;
+				case '\n':
+					sbuf.append('\\'); sbuf.append('n'); 
+					break;
+				case '\r':
+					sbuf.append('\\'); sbuf.append('r'); 
+					break;
+				default :
+					sbuf.append(c);					
+			}			
+		}
+		
+		return sbuf.toString();
+	}
 }