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/13 19:50:52 UTC

svn commit: r377455 - in /webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen: ./ emitter/ extension/

Author: sanka
Date: Mon Feb 13 10:50:51 2006
New Revision: 377455

URL: http://svn.apache.org/viewcvs?rev=377455&view=rev
Log:
Added: PolicyEvalutor which evaluates any policy information per operation basis and hands them off to an appropriate PolicyExtension for further processing.

PolicyExtension which processes a set of module specific policy assertions and adds "method" elements to "stubElement" which will be resovled into stub methods. Those stub methods are a function of policy information which is presented in the WSDL and they contirbutes to a generic api which is used to take only the required input from the user. For instance in the username password case it adds only two method (setUsername(), setPassword()) to the stub, not a method like setCallbackClass(). That is because setCallbackClass is internal information required by WSS4J. Hence that information should be generated internally in AxisModule and the user of the stub needs not to provide such information.

CodeGenPolicyExtension is an interface which must be implemeted by Module author who intend to support Policies at codegen time. It contains a single method to retrieve a PolicyExtension for that Module.

Added:
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/CodeGenPolicyExtension.java
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/PolicyExtension.java
Modified:
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/CodeGenConfiguration.java
    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/codegen/extension/PolicyEvaluator.java

Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/CodeGenConfiguration.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/CodeGenConfiguration.java?rev=377455&r1=377454&r2=377455&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/CodeGenConfiguration.java (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/CodeGenConfiguration.java Mon Feb 13 10:50:51 2006
@@ -32,6 +32,8 @@
     private WSDLDescription wom;
 
     private String baseURI;
+    
+    private String repositoryPath;
 
     public String getBaseURI() {
         return baseURI;
@@ -347,5 +349,13 @@
 
     public void setWriteMessageReceiver(boolean writeMessageReceiver) {
         this.writeMessageReceiver = writeMessageReceiver;
+    }
+    
+    public void setRepositoryPath(String repositoryPath) {
+    	this.repositoryPath = repositoryPath;    	
+    }
+    
+    public String getRepositoryPath() {
+    	return repositoryPath;    	
     }
 }

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=377455&r1=377454&r2=377455&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 Mon Feb 13 10:50:51 2006
@@ -63,7 +63,16 @@
 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.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
 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;
@@ -164,27 +173,6 @@
          // put whatever is needed here
     }
 
-    //~--- methods ------------------------------------------------------------
-
-    /**
-     * Commented for now for the
-     * @param policy
-     * @return the file name and the package of this policy
-     */
-
-//  private String processPolicy(Policy policy,String name) throws Exception{
-//      PolicyFileWriter writer = new PolicyFileWriter(configuration.getOutputLocation());
-//      String fileName = name + "_policy";
-//      writer.createOutFile(configuration.getPackageName(),fileName);
-//      FileOutputStream stream = writer.getStream();
-//      if (stream!=null) WSPolicyParser.getInstance().printModel(policy,stream);
-//
-//      String packageName = configuration.getPackageName();
-//      String fullFileName = "\\" + packageName.replace('.','\\') +"\\"+fileName+ ".xml";
-//      return fullFileName;
-//
-//  }
-
     /**
      * Utility method to add an attribute to a given element.
      *
@@ -1267,6 +1255,23 @@
         InterfaceImplementationWriter writer =
                 new InterfaceImplementationWriter(getOutputDirectory(this.configuration.getOutputLocation(), "src"),
                         this.configuration.getOutputLanguage());
+        // TODO
+        try {
+            // Prepare the DOM document for writing
+            Source source = new DOMSource(interfaceImplModel);
+    
+            // Prepare the output file
+            File file = new File("/home/sanka/Desktop/test1.xml");
+            Result result = new StreamResult(file);
+    
+            // Write the DOM document to the file
+            Transformer xformer = TransformerFactory.newInstance().newTransformer();
+            xformer.transform(source, result);
+        } catch (TransformerConfigurationException e) {
+        } catch (TransformerException e) {
+        }
+        
+        
         
         writeClass(interfaceImplModel, writer);
     }

Added: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/CodeGenPolicyExtension.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/CodeGenPolicyExtension.java?rev=377455&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/CodeGenPolicyExtension.java (added)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/CodeGenPolicyExtension.java Mon Feb 13 10:50:51 2006
@@ -0,0 +1,29 @@
+/*
+ * 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.wsdl.codegen.extension;
+
+
+/**
+ * 
+ * @author Sanka Samaranayake (sanka@apache.org)
+ *
+ */
+public interface CodeGenPolicyExtension {
+	
+	public PolicyExtension getPolicyExtension();
+
+}

Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/PolicyEvaluator.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/PolicyEvaluator.java?rev=377455&r1=377454&r2=377455&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/PolicyEvaluator.java (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/PolicyEvaluator.java Mon Feb 13 10:50:51 2006
@@ -14,29 +14,184 @@
  * limitations under the License.
  */
 
-package org.apache.axis2.wsdl.codegen.extension;
 
-import org.apache.axis2.wsdl.codegen.CodeGenConfiguration;
+package org.apache.axis2.wsdl.codegen.extension;
 
+import java.util.HashMap;
+import java.util.Iterator;
 
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.context.ConfigurationContextFactory;
+import org.apache.axis2.description.AxisModule;
+import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.axis2.util.PolicyAttachmentUtil;
+import org.apache.axis2.wsdl.codegen.CodeGenConfiguration;
+import org.apache.ws.policy.AndCompositeAssertion;
+import org.apache.ws.policy.Policy;
+import org.apache.ws.policy.PrimitiveAssertion;
+import org.apache.ws.policy.XorCompositeAssertion;
+import org.apache.wsdl.WSDLBinding;
+import org.apache.wsdl.WSDLDescription;
+import org.apache.wsdl.WSDLEndpoint;
+import org.apache.wsdl.WSDLInterface;
+import org.apache.wsdl.WSDLOperation;
+import org.apache.wsdl.WSDLService;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * 
+ * @author Sanka Samaranayake (sanka@apache.org)
+ *
+ */
 public class PolicyEvaluator implements CodeGenExtension {
 
-    CodeGenConfiguration configuration;
-
-    public PolicyEvaluator() {
-    }
-
-    /**
-     * Goes through the WSDL and extracts the WS-Policy elements
-     * and maps it into an Axis Module.
-     */
-    public void init(CodeGenConfiguration configuration) {
-        this.configuration = configuration;
-    }
-
-    public void engage() {
-
+	CodeGenConfiguration configuration;
 
-    }
+	HashMap ns2modules = new HashMap();
 
+	PolicyAttachmentUtil util;
+	
+	Element rootElement;
+	
+	public PolicyEvaluator() {
+	}
+
+	public void init(CodeGenConfiguration configuration) {
+		this.configuration = configuration;
+		util = new PolicyAttachmentUtil(configuration.getWom());
+		
+		String repository = configuration.getRepositoryPath();
+		repository = "/home/sanka/jakarta-tomcat-4.1.30/webapps/axis2/WEB-INF"; // configuration.getRepository;
+
+		try {
+			ConfigurationContext configurationCtx = ConfigurationContextFactory
+					.createConfigurationContextFromFileSystem(repository, null);
+			AxisConfiguration axisConfiguration = configurationCtx
+					.getAxisConfiguration();
+
+			for (Iterator iterator = axisConfiguration.getModules().values()
+					.iterator(); iterator.hasNext();) {
+				AxisModule axisModule = (AxisModule) iterator.next();
+				String[] namespaces = axisModule.getSupportedPolicyNamespaces();
+
+				for (int i = 0; i < namespaces.length; i++) {
+					ns2modules.put(namespaces[i], axisModule);
+				}
+			}
+
+		} catch (Exception e) {
+			System.err
+					.println("cannot locate repository : policy will not be supported");
+		}
+	}
+
+	public void engage() {
+		WSDLDescription womDescription = configuration.getWom();
+
+		String serviceName = configuration.getServiceName();
+		WSDLService wsdlService = womDescription.getService(new QName(
+				serviceName));
+
+		String port = configuration.getPortName();
+		WSDLEndpoint wsdlEndpoint = wsdlService.getEndpoint(new QName(port));
+
+		WSDLBinding wsdlBinding = wsdlEndpoint.getBinding();
+		WSDLInterface wsdlInterface = wsdlBinding.getBoundInterface();
+		
+		Element rootElement = getRootElement();
+
+		for (Iterator iterator = wsdlInterface.getOperations().values()
+				.iterator(); iterator.hasNext();) {
+			WSDLOperation wsdlOperation = (WSDLOperation) iterator.next();
+			Policy policy = util.getOperationPolicy(wsdlEndpoint.getName(),
+					wsdlOperation.getName());
+			
+			processPolicies(policy, wsdlEndpoint, wsdlOperation, rootElement);
+		}
+	}
+
+	private void processPolicies(Policy policy, WSDLEndpoint wsdlEndpoint,
+			WSDLOperation operation, Element element) {
+
+		HashMap map = new HashMap();
+
+		XorCompositeAssertion XOR = (XorCompositeAssertion) policy.getTerms()
+				.get(0);
+		AndCompositeAssertion AND = (AndCompositeAssertion) XOR.getTerms().get(
+				0);
+
+		for (Iterator iterator = AND.getTerms().iterator(); iterator.hasNext();) {
+
+			AndCompositeAssertion nAND = new AndCompositeAssertion();
+			PrimitiveAssertion pa = (PrimitiveAssertion) iterator.next();
+
+			String namespace = pa.getName().getNamespaceURI();
+
+			while (iterator.hasNext()) {
+				pa = (PrimitiveAssertion) iterator.next();
+
+				if (namespace.equals(pa.getName().getNamespaceURI())) {
+					nAND.addTerm(pa);
+				}
+			}
+
+			map.put(namespace, nAND);
+			AND.getTerms().removeAll(nAND.getTerms());
+		}
+
+		for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
+			String namespace = (String) iterator.next();
+			AxisModule axisModule = (AxisModule) ns2modules.get(namespace);
+
+			if (axisModule == null) {
+				System.err.println("cannot find a module to process "
+						+ namespace + "type assertions");
+				continue;
+			}
+
+			if (!(axisModule instanceof CodeGenPolicyExtension)) {
+				System.err
+						.println(axisModule.getName()
+								+ " module doesnt provde a PolicyExtension to process policies");
+				continue;
+			}
+
+			PolicyExtension policyExtension = ((CodeGenPolicyExtension) axisModule)
+					.getPolicyExtension();
+
+			Policy nPolicy = new Policy();
+			XorCompositeAssertion nXOR = new XorCompositeAssertion();
+			nPolicy.addTerm(nXOR);
+
+			AndCompositeAssertion nAND = (AndCompositeAssertion) map
+					.get(namespace);
+			nXOR.addTerm(nAND);
+
+			policyExtension.addMethodsToStub(nPolicy, element);
+		}
+
+		configuration.putProperty("stubMethods", element);
+	}
+
+	private Element getRootElement() {
+		Document document = getEmptyDocument();
+		return document.createElement("stubMethods");		
+	}
+	
+	private Document getEmptyDocument() {
+		try {
+			DocumentBuilder documentBuilder = DocumentBuilderFactory
+					.newInstance().newDocumentBuilder();
+
+			return documentBuilder.newDocument();
+		} catch (ParserConfigurationException e) {
+			throw new RuntimeException(e);
+		}
+	}
 }

Added: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/PolicyExtension.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/PolicyExtension.java?rev=377455&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/PolicyExtension.java (added)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/codegen/extension/PolicyExtension.java Mon Feb 13 10:50:51 2006
@@ -0,0 +1,29 @@
+/*
+ * 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.wsdl.codegen.extension;
+
+import org.apache.ws.policy.Policy;
+import org.w3c.dom.Element;
+
+/**
+ * 
+ * @author Sanka Samaranayake (sanka@apache.org)
+ */
+public interface PolicyExtension {
+	public void addMethodsToStub(Policy policy, Element element);
+
+}