Dear Fellow Axis2 Devs,

I would like to propose the following patch which allow the
AxisConfiguration to hold policy information define in axis2.xml

Thoughts ?

Best,
Sanka
Index: core/src/org/apache/axis2/deployment/AxisConfigBuilder.java
===================================================================
--- core/src/org/apache/axis2/deployment/AxisConfigBuilder.java	(revision 358452)
+++ core/src/org/apache/axis2/deployment/AxisConfigBuilder.java	(working copy)
@@ -112,6 +112,21 @@
             if (hostElement != null) {
                 processHostCongiguration(hostElement, axisConfiguration);
             }
+            
+            // processing <wsp:Policy> .. </..> elements
+            Iterator policyElements = config_element.getChildrenWithName(new QName(POLICY_NS_URI, TAG_POLICY));
+            
+            if (policyElements != null) {
+                processPolicyElements(policyElements, axisConfiguration.getPolicyInclude());
+            }
+            
+            // processing <wsp:PolicyReference> .. </..> elements
+            Iterator policyRefElements = config_element.getChildrenWithName(new QName(POLICY_NS_URI, TAG_POLICY_REF));
+            
+            if (policyRefElements != null) {
+                processPolicyRefElements(policyElements, axisConfiguration.getPolicyInclude());
+            }
+            
         } catch (XMLStreamException e) {
             throw new DeploymentException(e);
         }
Index: core/src/org/apache/axis2/deployment/DeploymentConstants.java
===================================================================
--- core/src/org/apache/axis2/deployment/DeploymentConstants.java	(revision 358452)
+++ core/src/org/apache/axis2/deployment/DeploymentConstants.java	(working copy)
@@ -90,4 +90,9 @@
     String BOOLEAN_FALSE = "false";
     char SEPARATOR_DOT = '.';
     char SEPARATOR_COLON = ':';
+    
+    String POLICY_NS_URI = "http://schemas.xmlsoap.org/ws/2004/09/policy";;
+    String TAG_POLICY = "Policy";
+    String TAG_POLICY_REF = "PolicyReference";
+    
 }
Index: core/src/org/apache/axis2/deployment/DescriptionBuilder.java
===================================================================
--- core/src/org/apache/axis2/deployment/DescriptionBuilder.java	(revision 358452)
+++ core/src/org/apache/axis2/deployment/DescriptionBuilder.java	(working copy)
@@ -1,20 +1,19 @@
 /*
-* Copyright 2004,2005 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.
-*/
+ * Copyright 2004,2005 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.deployment;
 
 import org.apache.axis2.AxisFault;
@@ -26,6 +25,7 @@
 import org.apache.axis2.description.Parameter;
 import org.apache.axis2.description.ParameterImpl;
 import org.apache.axis2.description.ParameterInclude;
+import org.apache.axis2.description.PolicyInclude;
 import org.apache.axis2.engine.AxisConfiguration;
 import org.apache.axis2.engine.MessageReceiver;
 import org.apache.axis2.i18n.Messages;
@@ -36,6 +36,10 @@
 import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.ws.policy.Policy;
+import org.apache.ws.policy.PolicyReference;
+import org.apache.ws.policy.util.OMPolicyReader;
+import org.apache.ws.policy.util.PolicyFactory;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLInputFactory;
@@ -50,6 +54,7 @@
  */
 public class DescriptionBuilder implements DeploymentConstants {
     protected Log log = LogFactory.getLog(getClass());
+
     protected AxisConfiguration axisConfig;
 
     protected InputStream descriptionStream;
@@ -57,22 +62,23 @@
     public DescriptionBuilder() {
     }
 
-    public DescriptionBuilder(InputStream serviceInputStream, AxisConfiguration axisConfig) {
+    public DescriptionBuilder(InputStream serviceInputStream,
+            AxisConfiguration axisConfig) {
         this.descriptionStream = serviceInputStream;
         this.axisConfig = axisConfig;
     }
 
     /**
-     * This will creat OMElemnt for a given descrition document (axis2.xml , services.xml and
-     * module.xml)
-     *
+     * This will creat OMElemnt for a given descrition document (axis2.xml ,
+     * services.xml and module.xml)
+     * 
      * @return OMElement <code>OMElement</code>
      * @throws javax.xml.stream.XMLStreamException
-     *
+     *  
      */
     public OMElement buildOM() throws XMLStreamException {
-        XMLStreamReader xmlReader =
-                XMLInputFactory.newInstance().createXMLStreamReader(descriptionStream);
+        XMLStreamReader xmlReader = XMLInputFactory.newInstance()
+                .createXMLStreamReader(descriptionStream);
         OMFactory fac = OMAbstractFactory.getOMFactory();
         StAXOMBuilder staxOMBuilder = new StAXOMBuilder(fac, xmlReader);
         OMElement element = staxOMBuilder.getDocumentElement();
@@ -82,7 +88,8 @@
         return element;
     }
 
-    protected MessageReceiver loadDefaultMessageReceiver() throws DeploymentException {
+    protected MessageReceiver loadDefaultMessageReceiver()
+            throws DeploymentException {
         MessageReceiver receiver;
         String defaultMessageReceiver = DEFAULT_MESSAGE_RECEIVER;
 
@@ -91,33 +98,33 @@
             /**
              * Setting default Message Recive as Message Receiver
              */
-            ClassLoader loader1 = Thread.currentThread().getContextClassLoader();
-            Class messageReceiver = Class.forName(defaultMessageReceiver, true, loader1);
+            ClassLoader loader1 = Thread.currentThread()
+                    .getContextClassLoader();
+            Class messageReceiver = Class.forName(defaultMessageReceiver, true,
+                    loader1);
 
             receiver = ((MessageReceiver) messageReceiver.newInstance());
         } catch (ClassNotFoundException e) {
-            throw new DeploymentException(
-                    Messages.getMessage(
-                            DeploymentErrorMsgs.ERROR_IN_LOADING_MESSAGE_RECEIVER, "ClassNotFoundException",
-                            defaultMessageReceiver));
+            throw new DeploymentException(Messages.getMessage(
+                    DeploymentErrorMsgs.ERROR_IN_LOADING_MESSAGE_RECEIVER,
+                    "ClassNotFoundException", defaultMessageReceiver));
         } catch (IllegalAccessException e) {
-            throw new DeploymentException(
-                    Messages.getMessage(
-                            DeploymentErrorMsgs.ERROR_IN_LOADING_MESSAGE_RECEIVER, "IllegalAccessException",
-                            defaultMessageReceiver));
+            throw new DeploymentException(Messages.getMessage(
+                    DeploymentErrorMsgs.ERROR_IN_LOADING_MESSAGE_RECEIVER,
+                    "IllegalAccessException", defaultMessageReceiver));
         } catch (InstantiationException e) {
-            throw new DeploymentException(
-                    Messages.getMessage(
-                            DeploymentErrorMsgs.ERROR_IN_LOADING_MESSAGE_RECEIVER, "InstantiationException",
-                            defaultMessageReceiver));
+            throw new DeploymentException(Messages.getMessage(
+                    DeploymentErrorMsgs.ERROR_IN_LOADING_MESSAGE_RECEIVER,
+                    "InstantiationException", defaultMessageReceiver));
         }
 
         return receiver;
     }
 
-    protected MessageReceiver loadMessageReceiver(ClassLoader loader, OMElement reciverElement)
-            throws DeploymentException {
-        OMAttribute recieverName = reciverElement.getAttribute(new QName(TAG_CLASS_NAME));
+    protected MessageReceiver loadMessageReceiver(ClassLoader loader,
+            OMElement reciverElement) throws DeploymentException {
+        OMAttribute recieverName = reciverElement.getAttribute(new QName(
+                TAG_CLASS_NAME));
         String className = recieverName.getAttributeValue();
         MessageReceiver receiver = null;
 
@@ -129,17 +136,17 @@
                 receiver = (MessageReceiver) messageReceiver.newInstance();
             }
         } catch (ClassNotFoundException e) {
-            throw new DeploymentException(
-                    Messages.getMessage(
-                            DeploymentErrorMsgs.ERROR_IN_LOADING_MESSAGE_RECEIVER, "ClassNotFoundException", className));
+            throw new DeploymentException(Messages.getMessage(
+                    DeploymentErrorMsgs.ERROR_IN_LOADING_MESSAGE_RECEIVER,
+                    "ClassNotFoundException", className));
         } catch (IllegalAccessException e) {
-            throw new DeploymentException(
-                    Messages.getMessage(
-                            DeploymentErrorMsgs.ERROR_IN_LOADING_MESSAGE_RECEIVER, "IllegalAccessException", className));
+            throw new DeploymentException(Messages.getMessage(
+                    DeploymentErrorMsgs.ERROR_IN_LOADING_MESSAGE_RECEIVER,
+                    "IllegalAccessException", className));
         } catch (InstantiationException e) {
-            throw new DeploymentException(
-                    Messages.getMessage(
-                            DeploymentErrorMsgs.ERROR_IN_LOADING_MESSAGE_RECEIVER, "InstantiationException", className));
+            throw new DeploymentException(Messages.getMessage(
+                    DeploymentErrorMsgs.ERROR_IN_LOADING_MESSAGE_RECEIVER,
+                    "InstantiationException", className));
         }
 
         return receiver;
@@ -147,10 +154,12 @@
 
     /**
      * To process Flow elements in services.xml
-     *
-     * @param flowelement <code>OMElement</code>
+     * 
+     * @param flowelement
+     *           <code>OMElement</code>
      * @return
-     * @throws DeploymentException <code>DeploymentException</code>
+     * @throws DeploymentException
+     *             <code>DeploymentException</code>
      */
     protected Flow processFlow(OMElement flowelement, ParameterInclude parent)
             throws DeploymentException {
@@ -160,7 +169,8 @@
             return flow;
         }
 
-        Iterator handlers = flowelement.getChildrenWithName(new QName(TAG_HANDLER));
+        Iterator handlers = flowelement.getChildrenWithName(new QName(
+                TAG_HANDLER));
 
         while (handlers.hasNext()) {
             OMElement handlerElement = (OMElement) handlers.next();
@@ -173,40 +183,47 @@
 
     /**
      * To process Handler element
-     *
-     * @param handler_element <code>OMElement</code>
+     * 
+     * @param handler_element
+     *           <code>OMElement</code>
      * @return
-     * @throws DeploymentException <code>DeploymentException</code>
+     * @throws DeploymentException
+     *             <code>DeploymentException</code>
      */
-    protected HandlerDescription processHandler(OMElement handler_element, ParameterInclude parent)
-            throws DeploymentException {
+    protected HandlerDescription processHandler(OMElement handler_element,
+            ParameterInclude parent) throws DeploymentException {
         HandlerDescription handler = new HandlerDescription();
 
         // Setting Handler name
-        OMAttribute name_attribute = handler_element.getAttribute(new QName(ATTRIBUTE_NAME));
+        OMAttribute name_attribute = handler_element.getAttribute(new QName(
+                ATTRIBUTE_NAME));
 
         if (name_attribute == null) {
-            throw new DeploymentException(Messages.getMessage(DeploymentErrorMsgs.INVALID_HANDLER,
-                    "Name missing"));
+            throw new DeploymentException(Messages.getMessage(
+                    DeploymentErrorMsgs.INVALID_HANDLER, "Name missing"));
         } else {
             handler.setName(new QName(name_attribute.getAttributeValue()));
         }
 
         // Setting Handler Class name
-        OMAttribute class_attribute = handler_element.getAttribute(new QName(TAG_CLASS_NAME));
+        OMAttribute class_attribute = handler_element.getAttribute(new QName(
+                TAG_CLASS_NAME));
 
         if (class_attribute == null) {
-            throw new DeploymentException((Messages.getMessage(DeploymentErrorMsgs.INVALID_HANDLER,
+            throw new DeploymentException((Messages.getMessage(
+                    DeploymentErrorMsgs.INVALID_HANDLER,
                     "class name is missing")));
         } else {
             handler.setClassName(class_attribute.getAttributeValue());
         }
 
         // processing phase Rules (order)
-        OMElement order_element = handler_element.getFirstChildWithName(new QName(TAG_ORDER));
+        OMElement order_element = handler_element
+                .getFirstChildWithName(new QName(TAG_ORDER));
 
         if (order_element == null) {
-            throw new DeploymentException((Messages.getMessage(DeploymentErrorMsgs.INVALID_HANDLER,
+            throw new DeploymentException((Messages.getMessage(
+                    DeploymentErrorMsgs.INVALID_HANDLER,
                     "phase rule has not been specified")));
         } else {
             Iterator order_itr = order_element.getAllAttributes();
@@ -241,7 +258,8 @@
                 }
             }
 
-            Iterator parameters = handler_element.getChildrenWithName(new QName(TAG_PARAMETER));
+            Iterator parameters = handler_element
+                    .getChildrenWithName(new QName(TAG_PARAMETER));
 
             processParameters(parameters, handler, parent);
         }
@@ -251,47 +269,53 @@
         return handler;
     }
 
-    protected void processOperationModuleRefs(Iterator moduleRefs, AxisOperation operation)
-            throws DeploymentException {
+    protected void processOperationModuleRefs(Iterator moduleRefs,
+            AxisOperation operation) throws DeploymentException {
         try {
             while (moduleRefs.hasNext()) {
                 OMElement moduleref = (OMElement) moduleRefs.next();
-                OMAttribute moduleRefAttribute = moduleref.getAttribute(new QName(TAG_REFERENCE));
+                OMAttribute moduleRefAttribute = moduleref
+                        .getAttribute(new QName(TAG_REFERENCE));
 
                 if (moduleRefAttribute != null) {
                     String refName = moduleRefAttribute.getAttributeValue();
 
                     if (axisConfig.getModule(new QName(refName)) == null) {
-                        throw new DeploymentException(
-                                Messages.getMessage(DeploymentErrorMsgs.MODULE_NOT_FOUND, refName));
+                        throw new DeploymentException(Messages.getMessage(
+                                DeploymentErrorMsgs.MODULE_NOT_FOUND, refName));
                     } else {
                         operation.addModule(new QName(refName));
                     }
                 }
             }
         } catch (AxisFault axisFault) {
-            throw new DeploymentException(Messages.getMessage(DeploymentErrorMsgs.MODULE_NOT_FOUND,
-                    axisFault.getMessage()));
+            throw new DeploymentException(Messages.getMessage(
+                    DeploymentErrorMsgs.MODULE_NOT_FOUND, axisFault
+                            .getMessage()));
         }
     }
 
     /**
      * To get the Parameter object out from the OM
-     *
-     * @param parameters       <code>Parameter</code>
-     * @param parameterInclude <code>ParameterInclude</code>
-     * @param parent           <code>ParameterInclude</code>
-     *                         return : will return parameters , wchih name is WSA-Mapping , since we need to treat them
-     *                         separately
+     * 
+     * @param parameters
+     *           <code>Parameter</code>
+     * @param parameterInclude
+     *           <code>ParameterInclude</code>
+     * @param parent
+     *           <code>ParameterInclude</code> return : will return
+     *           parameters , wchih name is WSA-Mapping , since we need to
+     *           treat them separately
      */
-    protected ArrayList processParameters(Iterator parameters, ParameterInclude parameterInclude,
-                                          ParameterInclude parent)
+    protected ArrayList processParameters(Iterator parameters,
+            ParameterInclude parameterInclude, ParameterInclude parent)
             throws DeploymentException {
         ArrayList wsamapping = new ArrayList();
 
         while (parameters.hasNext()) {
 
-            // this is to check whether some one has locked the parmter at the top level
+            // this is to check whether some one has locked the parmter at the
+            // top level
             OMElement parameterElement = (OMElement) parameters.next();
             Parameter parameter = new ParameterImpl();
 
@@ -299,11 +323,12 @@
             parameter.setParameterElement(parameterElement);
 
             // setting parameter Name
-            OMAttribute paramName = parameterElement.getAttribute(new QName(ATTRIBUTE_NAME));
+            OMAttribute paramName = parameterElement.getAttribute(new QName(
+                    ATTRIBUTE_NAME));
 
             if (paramName == null) {
-                throw new DeploymentException(
-                        Messages.getMessage(DeploymentErrorMsgs.BAD_PARAMETER_ARGUMENT));
+                throw new DeploymentException(Messages
+                        .getMessage(DeploymentErrorMsgs.BAD_PARAMETER_ARGUMENT));
             }
 
             parameter.setName(paramName.getAttributeValue());
@@ -322,7 +347,8 @@
             }
 
             // setting locking attribute
-            OMAttribute paramLocked = parameterElement.getAttribute(new QName(ATTRIBUTE_LOCKED));
+            OMAttribute paramLocked = parameterElement.getAttribute(new QName(
+                    ATTRIBUTE_LOCKED));
             Parameter parentParam = null;
 
             if (parent != null) {
@@ -334,11 +360,13 @@
 
                 if (BOOLEAN_TRUE.equals(lockedValue)) {
 
-                    // if the parameter is locked at some level parameter value replace by that
-                    if ((parent != null) && parent.isParameterLocked(parameter.getName())) {
-                        throw new DeploymentException(
-                                Messages.getMessage(
-                                        DeploymentErrorMsgs.CONFIG_NOT_FOUND, parameter.getName()));
+                    // if the parameter is locked at some level parameter value
+                    // replace by that
+                    if ((parent != null)
+                            && parent.isParameterLocked(parameter.getName())) {
+                        throw new DeploymentException(Messages.getMessage(
+                                DeploymentErrorMsgs.CONFIG_NOT_FOUND, parameter
+                                        .getName()));
                     } else {
                         parameter.setLocked(true);
                     }
@@ -350,13 +378,15 @@
             if (Constants.WSA_ACTION.equals(paramName.getAttributeValue())) {
                 wsamapping.add(parameter);
 
-                // no need to add this parameter , since this is just for mapping
+                // no need to add this parameter , since this is just for
+                // mapping
                 continue;
             }
 
             try {
                 if (parent != null) {
-                    if ((parentParam == null) || !parent.isParameterLocked(parameter.getName())) {
+                    if ((parentParam == null)
+                            || !parent.isParameterLocked(parameter.getName())) {
                         parameterInclude.addParameter(parameter);
                     }
                 } else {
@@ -370,10 +400,36 @@
         return wsamapping;
     }
 
+    protected void processPolicyElements(Iterator policyElements,
+            PolicyInclude policyInclude) {
+        OMPolicyReader reader = (OMPolicyReader) PolicyFactory
+                .getPolicyReader(PolicyFactory.OM_POLICY_READER);
+        OMElement policyElement;
+
+        while (policyElements.hasNext()) {
+            Policy p = reader.readPolicy((OMElement) policyElements.next());
+            policyInclude.addPolicy(p);
+        }
+    }
+
+    protected void processPolicyRefElements(Iterator policyRefElements,
+            PolicyInclude policyInclude) {
+        OMPolicyReader reader = (OMPolicyReader) PolicyFactory
+                .getPolicyReader(PolicyFactory.OM_POLICY_READER);
+        OMElement policyRefElement;
+
+        while (policyRefElements.hasNext()) {
+            PolicyReference policyReference = reader
+                    .readPolicyReference((OMElement) policyRefElements.next());
+            policyInclude.addPolicyReference(policyReference);
+        }
+    }
+
     /**
      * This method is used to retrive service name form the arechive file name
-     * if the archive file name is service1.aar , then axis service name would be service1
-     *
+     * if the archive file name is service1.aar , then axis service name would
+     * be service1
+     * 
      * @param fileName
      * @return String
      */
@@ -392,9 +448,8 @@
     }
 
     /**
-     * this method is to get the value of attribue
-     * eg xsd:anyVal --> anyVal
-     *
+     * this method is to get the value of attribue eg xsd:anyVal --> anyVal
+     * 
      * @return String
      */
     protected String getValue(String in) {
Index: core/src/org/apache/axis2/description/PolicyInclude.java
===================================================================
--- core/src/org/apache/axis2/description/PolicyInclude.java	(revision 0)
+++ core/src/org/apache/axis2/description/PolicyInclude.java	(revision 0)
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2004,2005 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.description;
+
+import org.apache.ws.policy.Policy;
+import org.apache.ws.policy.PolicyReference;
+import org.apache.ws.policy.util.PolicyRegistry;
+
+/**
+ * @author Sanka Samaranayake ([EMAIL PROTECTED])
+ */
+public class PolicyInclude {
+    PolicyRegistry reg = null;
+
+    Policy policy = null;
+
+    public PolicyInclude() {
+        reg = new PolicyRegistry();
+    }
+
+    public PolicyInclude(PolicyRegistry reg) {
+        this.reg = reg;
+    }
+
+    public void setPolicy(Policy policy) {
+        this.policy = policy;
+    }
+
+    public Policy getPolicy() {
+        return policy;
+    }
+
+    public void setPolicyRegistry(PolicyRegistry reg) {
+        this.reg = reg;
+    }
+
+    public PolicyRegistry getPolicyRegistry() {
+        return reg;
+    }
+    
+    public void addPolicy(Policy nPolicy) {
+
+        if (policy == null) {
+            policy = nPolicy;
+
+        } else {
+            policy = (Policy) policy.merge(nPolicy, reg);
+        }
+        
+        if (policy.getPolicyURI() != null) {
+            reg.register(policy.getPolicyURI(), policy);
+        }
+    }
+
+    public void addPolicyReference(PolicyReference policyReference) {
+        addPolicy((Policy) policyReference.normalize(reg));
+    }
+}
Index: core/src/org/apache/axis2/engine/AxisConfiguration.java
===================================================================
--- core/src/org/apache/axis2/engine/AxisConfiguration.java	(revision 358452)
+++ core/src/org/apache/axis2/engine/AxisConfiguration.java	(working copy)
@@ -71,6 +71,9 @@
     private ArrayList inFaultPhases;
     private ArrayList inPhasesUptoAndIncludingPostDispatch;
     protected HashMap messagReceivers;
+    
+    // to store policies which are valid for entire system
+    private PolicyInclude policyInclude;
 
     private ClassLoader moduleClassLoader;
     private HashMap moduleConfigmap;
@@ -96,6 +99,9 @@
         faultyModules = new Hashtable();
         observersList = new ArrayList();
         inPhasesUptoAndIncludingPostDispatch = new ArrayList();
+        
+        policyInclude = new PolicyInclude();
+        
         systemClassLoader = Thread.currentThread().getContextClassLoader();
         serviceClassLoader = Thread.currentThread().getContextClassLoader();
         moduleClassLoader = Thread.currentThread().getContextClassLoader();
@@ -547,4 +553,12 @@
     public void setSystemClassLoader(ClassLoader classLoader) {
         this.systemClassLoader = classLoader;
     }
+    
+    public void setPolicyInclude(PolicyInclude policyInclude) {
+        this.policyInclude = policyInclude;
+    }
+    
+    public PolicyInclude getPolicyInclude() {
+        return policyInclude;
+    }
 }

Reply via email to