Author: scamp
Date: Tue Apr 26 14:11:09 2005
New Revision: 164883

URL: http://svn.apache.org/viewcvs?rev=164883&view=rev
Log:
Apollo-8


This implements the generation of custom fault classes based on the user's 
wsdl...

Added:
    incubator/apollo/trunk/src/templates/BaseFaultExtension.vm
    incubator/apollo/trunk/src/templates/CustomFault.vm
Modified:
    
incubator/apollo/trunk/src/java/org/apache/ws/resource/handler/AbstractSoapMethodNameMap.java
    incubator/apollo/trunk/src/java/org/apache/ws/resource/i18n/Keys.java
    
incubator/apollo/trunk/src/java/org/apache/ws/resource/impl/AbstractResourceContext.java
    
incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/GenerationUtils.java
    incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/Wsdl2Java.java
    
incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/velocity/ServiceProperties.java
    incubator/apollo/trunk/src/java/org/apache/ws/util/OperationInfo.java

Modified: 
incubator/apollo/trunk/src/java/org/apache/ws/resource/handler/AbstractSoapMethodNameMap.java
URL: 
http://svn.apache.org/viewcvs/incubator/apollo/trunk/src/java/org/apache/ws/resource/handler/AbstractSoapMethodNameMap.java?rev=164883&r1=164882&r2=164883&view=diff
==============================================================================
--- 
incubator/apollo/trunk/src/java/org/apache/ws/resource/handler/AbstractSoapMethodNameMap.java
 (original)
+++ 
incubator/apollo/trunk/src/java/org/apache/ws/resource/handler/AbstractSoapMethodNameMap.java
 Tue Apr 26 14:11:09 2005
@@ -84,6 +84,8 @@
     */
    public String getMethodNameFromAction( String action )
    {
+      if(action == null) return null;
+       
       SoapMethodNameMap parent     = getParent(  );
       String            methodName = null;
       if ( parent != null )

Modified: incubator/apollo/trunk/src/java/org/apache/ws/resource/i18n/Keys.java
URL: 
http://svn.apache.org/viewcvs/incubator/apollo/trunk/src/java/org/apache/ws/resource/i18n/Keys.java?rev=164883&r1=164882&r2=164883&view=diff
==============================================================================
--- incubator/apollo/trunk/src/java/org/apache/ws/resource/i18n/Keys.java 
(original)
+++ incubator/apollo/trunk/src/java/org/apache/ws/resource/i18n/Keys.java Tue 
Apr 26 14:11:09 2005
@@ -592,4 +592,14 @@
      * @msg Retrieving the Action from the Addressing Headers.  Action is: {0}
      */
     String GET_ACTION_FROM_ADDR_HDR = "GET_ACTION_FROM_ADDR_HDR";
+
+    /**
+     * @msg There is no WS-Addressing Action Header in the request.
+     */
+    String NO_ACTION_IN_ADDR_HDR = "NO_ACTION_IN_ADDR_HDR";
+
+    /**
+     * @msg There were no WS-Addressing headers in the request.
+     */
+    String NO_ADDR_HDR = "NO_ADDR_HDR";
 }

Modified: 
incubator/apollo/trunk/src/java/org/apache/ws/resource/impl/AbstractResourceContext.java
URL: 
http://svn.apache.org/viewcvs/incubator/apollo/trunk/src/java/org/apache/ws/resource/impl/AbstractResourceContext.java?rev=164883&r1=164882&r2=164883&view=diff
==============================================================================
--- 
incubator/apollo/trunk/src/java/org/apache/ws/resource/impl/AbstractResourceContext.java
 (original)
+++ 
incubator/apollo/trunk/src/java/org/apache/ws/resource/impl/AbstractResourceContext.java
 Tue Apr 26 14:11:09 2005
@@ -524,8 +524,19 @@
 
        AddressingHeaders addrHeaders =
                (AddressingHeaders) 
msgContext.getProperty(org.apache.axis.message.addressing.Constants.ENV_ADDRESSING_REQUEST_HEADERS);
+       if(addrHeaders == null)
+       {
+           LOG.debug(MSG.getMessage(Keys.NO_ADDR_HDR));
+           return null;
+       }
 
-       String action = addrHeaders.getAction().toString();
+       Action headerAction = addrHeaders.getAction();
+       if(headerAction == null)
+       {
+          LOG.debug(MSG.getMessage(Keys.NO_ACTION_IN_ADDR_HDR));
+          return null;
+       }
+       String action = headerAction.toString();
        LOG.debug(MSG.getMessage(Keys.GET_ACTION_FROM_ADDR_HDR, action));
        return action;
    }

Modified: 
incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/GenerationUtils.java
URL: 
http://svn.apache.org/viewcvs/incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/GenerationUtils.java?rev=164883&r1=164882&r2=164883&view=diff
==============================================================================
--- 
incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/GenerationUtils.java
 (original)
+++ 
incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/GenerationUtils.java
 Tue Apr 26 14:11:09 2005
@@ -111,7 +111,7 @@
         for ( int i = 0; i < opNames.length; i++ )
         {
             Operation op = resourceDef.getPortType().getOperation( opNames[i], 
null, null );
-            OperationInfo opInfo = new OperationInfo( op );            
+            OperationInfo opInfo = new OperationInfo( op, 
resourceDef.getDefinition().getTargetNamespace() );            
             opInfoMap.put( opInfo.getRequestElementName(), opInfo );
         }
         return opInfoMap;

Modified: 
incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/Wsdl2Java.java
URL: 
http://svn.apache.org/viewcvs/incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/Wsdl2Java.java?rev=164883&r1=164882&r2=164883&view=diff
==============================================================================
--- incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/Wsdl2Java.java 
(original)
+++ incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/Wsdl2Java.java 
Tue Apr 26 14:11:09 2005
@@ -32,21 +32,28 @@
 import org.apache.ws.resource.tool.velocity.ImplementsListBuilder;
 import org.apache.ws.resource.tool.velocity.ServiceProperties;
 import org.apache.ws.util.XmlBeanNameUtils;
+import org.apache.ws.util.OperationInfo;
 import org.apache.ws.util.i18n.Messages;
 import org.apache.ws.util.velocity.CommonsLogLogSystem;
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.impl.common.XmlErrorPrinter;
+import org.apache.xmlbeans.impl.common.NameUtil;
 import org.apache.xmlbeans.impl.tool.SchemaCompiler;
 
+
 import javax.wsdl.Definition;
 import javax.wsdl.Port;
 import javax.wsdl.Service;
+import javax.wsdl.Fault;
+import javax.wsdl.Message;
+import javax.wsdl.Part;
 import javax.wsdl.factory.WSDLFactory;
 import javax.wsdl.xml.WSDLReader;
 import javax.xml.namespace.QName;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.FilenameFilter;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
@@ -57,6 +64,8 @@
 import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
 
 /**
  * Generates Java Web service types and skeletons from a WSRF WSDL.
@@ -204,6 +213,7 @@
     {
         System.out.println( "Processing WSDL file \"" + wsdlFile + "\"..." );
         Definition def = m_wsdlReader.readWSDL( wsdlFile.getPath() );
+        generateXmlBeans( wsdlFile );
         Iterator serviceIter = def.getServices().values().iterator();
         while ( serviceIter.hasNext() )
         {
@@ -219,7 +229,7 @@
                 copyWsdlFile( wsdlFile, serviceDir );
             }
         }
-        generateXmlBeans( wsdlFile );
+
     }
 
     /**
@@ -446,6 +456,7 @@
             
props.setNamespaceVersionHolder(org.apache.ws.resource.properties.v2004_11.impl.NamespaceVersionHolderImpl.class);
         }
 
+
         /**
          * These props are for JIRA Apollo-4 and could be added to context obj
          *  The question is whether the service name here should be the real 
wsdl
@@ -644,6 +655,65 @@
                 processTemplate( context, "templates/Service.vm", outputFile );
             }
 
+            //generate fault classes
+            Iterator customOperations = 
props.getCustomMappings().values().iterator();
+            while (customOperations.hasNext())
+            {
+                OperationInfo oparationInfo = (OperationInfo) 
customOperations.next();
+                //need to determine if fault is instanceof BaseFault
+                Iterator faultIterator = 
oparationInfo.getFaults().values().iterator();
+                while (faultIterator.hasNext())
+                {
+                    Fault fault = (Fault) faultIterator.next();
+                    Message message = fault.getMessage();
+                    Map parts = message.getParts();
+                    if (parts.size() != 1)
+                    {
+                        throw new RuntimeException( "Wsdl fault message should 
have exactly one part." );
+                    }
+                    Part part = (Part) parts.values().iterator().next();
+
+                    QName faultName = part.getTypeName();
+                    boolean isType=true;
+                    if (faultName == null)
+                    {
+                        faultName = part.getElementName();
+                        isType = false;
+                    }
+                    if (faultName == null)
+                    {
+                       throw new RuntimeException( "Wsdl fault maeesage part 
should have either an element or a type." );
+                    }
+
+                    String exceptionFaultName = 
StringUtils.capitalize(faultName.getLocalPart()) + "Exception";
+                    //ok heres the file we'll generate
+                    outputFile = new File(packageDir, exceptionFaultName + 
".java");
+                    context.put("faultName", exceptionFaultName);
+
+                    if (!outputFile.exists())
+                    {
+                        try
+                        {
+                            if (isBaseFaultExtension(faultName, isType))
+                            {
+                                processTemplate(context, 
"templates/BaseFaultExtension.vm", outputFile);
+                            }
+                            else
+                            {
+                                processTemplate(context, 
"templates/CustomFault.vm", outputFile);
+                            }
+                        }
+                        catch (Exception e)
+                        {
+                            throw new RuntimeException( "Unable to generate 
Wsdl fault. Cause: " + e.getLocalizedMessage() );
+                        }
+                    }
+
+                }
+
+            }
+
+
             outputFile = new File( packageDir, "Abstract" + 
capitalizedServiceName + "Resource.java" );
             processTemplate( context, "templates/AbstractResource.vm", 
outputFile );
 
@@ -684,6 +754,7 @@
 
             outputFile = new File( serviceDir, "build.properties" );
             processTemplate( context, "templates/build.properties.vm", 
outputFile );
+
         }
         catch ( Exception e )
         {
@@ -692,6 +763,47 @@
     }
 
     /**
+     * Determines if the passed in QName is for a type which extends
+     * BaseFault.
+     *
+     * @param faultName
+     * @param type
+     * @return true if the "type" extends BaseFault.
+     */
+    private boolean isBaseFaultExtension(QName faultName, boolean type) throws 
ClassNotFoundException, IllegalAccessException, InvocationTargetException, 
NoSuchMethodException
+    {
+        String xmlbeanFaultClassName = null;
+        Class faultClass = null;
+        URLClassLoader generatedXmlBeanClassloader = 
getGeneratedXmlBeanClassloader();
+        if(type)
+        {
+            xmlbeanFaultClassName = NameUtil.getClassNameFromQName(faultName);
+            //type to get an instance of the class
+            faultClass = 
generatedXmlBeanClassloader.loadClass(xmlbeanFaultClassName);
+        }
+        else
+        {
+            //attempt to get the generated "Document" class name
+            xmlbeanFaultClassName = 
XmlBeanNameUtils.getDocumentElementXmlBeanFactoryClassName(new 
QName(faultName.getNamespaceURI(), faultName.getLocalPart()));
+            //try to load the document class
+            Class xmlbeanGeneratedClass = 
generatedXmlBeanClassloader.loadClass(xmlbeanFaultClassName);
+            //invoke newInstance in the Xmlbean's Factory class
+            Method method = xmlbeanGeneratedClass.getMethod("newInstance", 
null);
+            Object o = method.invoke(xmlbeanGeneratedClass,null);
+
+            //invoke the addNew... method on the object to get its type
+            xmlbeanGeneratedClass = o.getClass();
+            method = xmlbeanGeneratedClass.getMethod("addNew" + 
faultName.getLocalPart(), null);
+            //get the types's Class
+            faultClass = method.invoke(o,null).getClass();
+        }
+
+        //determine if the class is assignable from any known versions of 
BaseType
+        return 
(org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSBaseFaults12Draft01.BaseFaultType.class.isAssignableFrom(faultClass)
 ||
+        
org.oasisOpen.docs.wsrf.x2004.x11.wsrfWSBaseFaults12Draft03.BaseFaultType.class.isAssignableFrom(faultClass));
+    }
+
+    /**
      * DOCUMENT_ME
      *
      * @author $author$
@@ -810,6 +922,33 @@
             classpathItems.add( new File( tokenizer.nextToken() ) );
         }
         return (File[]) classpathItems.toArray( new File[0] );
+    }
+
+    private URLClassLoader getGeneratedXmlBeanClassloader()
+    {
+        File dir = new File(m_xmlbeansDir, "lib");
+        File[] files = dir.listFiles(new FilenameFilter(){
+            public boolean accept(File dir, String name)
+            {
+                if(name.endsWith("jar")) return true;
+                return false;
+            }
+        });
+        URL[] urls = new URL[files.length];
+        for (int i = 0; i < files.length; i++)
+        {
+            try
+            {
+                urls[i] = files[i].toURL();
+            }
+            catch (MalformedURLException e)
+            {
+                throw new RuntimeException( "Unable to load XmlBeans-generated 
jars and convert to URLs. Cause: " + e.getLocalizedMessage() );
+            }
+
+        }
+      return new URLClassLoader(urls, this.getClass().getClassLoader());
+
     }
 
 }

Modified: 
incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/velocity/ServiceProperties.java
URL: 
http://svn.apache.org/viewcvs/incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/velocity/ServiceProperties.java?rev=164883&r1=164882&r2=164883&view=diff
==============================================================================
--- 
incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/velocity/ServiceProperties.java
 (original)
+++ 
incubator/apollo/trunk/src/java/org/apache/ws/resource/tool/velocity/ServiceProperties.java
 Tue Apr 26 14:11:09 2005
@@ -46,6 +46,7 @@
     private QName m_portTypeQName;
     private List m_implementedProps = new ArrayList();
     private String m_webbAppName;
+    private String m_namespaceVersionHolderClassName;
 
     public ServiceProperties(ResourceDefinition resourceDef)
     {
@@ -286,9 +287,19 @@
     {
         if(namespaceValueHolder != null)
         {
+            setNamespaceVersionHolderClassName(namespaceValueHolder);
             String className = namespaceValueHolder.getName();
         m_namespaceVersionHolder = "public static final "+className+" 
"+SPEC_NAMESPACE_MEMBER_NAME+" = new " + className + "();";
         }
+    }
+    public void setNamespaceVersionHolderClassName(Class namespaceValueHolder)
+    {
+        m_namespaceVersionHolderClassName = namespaceValueHolder.getName();
+    }
+
+    public String getNamespaceVersionHolderClassName()
+    {
+        return m_namespaceVersionHolderClassName;
     }
 
     public void setPortName(String portName)

Modified: incubator/apollo/trunk/src/java/org/apache/ws/util/OperationInfo.java
URL: 
http://svn.apache.org/viewcvs/incubator/apollo/trunk/src/java/org/apache/ws/util/OperationInfo.java?rev=164883&r1=164882&r2=164883&view=diff
==============================================================================
--- incubator/apollo/trunk/src/java/org/apache/ws/util/OperationInfo.java 
(original)
+++ incubator/apollo/trunk/src/java/org/apache/ws/util/OperationInfo.java Tue 
Apr 26 14:11:09 2005
@@ -16,13 +16,16 @@
 package org.apache.ws.util;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.ws.resource.tool.GenerationUtils;
 
 import javax.wsdl.Input;
 import javax.wsdl.Operation;
 import javax.wsdl.Output;
 import javax.wsdl.Part;
+import javax.wsdl.Fault;
 import javax.xml.namespace.QName;
 import java.util.Map;
+import java.util.Iterator;
 
 /**
  * TODO
@@ -40,7 +43,7 @@
     private String m_fullyQualifiedRequestClassName;
 
 
-    public OperationInfo( Operation op )
+    public OperationInfo(Operation op, String targetNamespace)
     {
         m_methodName = StringUtils.capitalize( op.getName() );
         Input input = op.getInput();
@@ -75,9 +78,38 @@
         {
             m_fullyQualifiedResponseClassName = void.class.getName();
         }
+        m_faults = op.getFaults();
+
+
         m_methodSig = m_fullyQualifiedResponseClassName + " " + m_methodName + 
"( " + m_fullyQualifiedRequestClassName + " requestDoc )";
 
-        m_faults = op.getFaults();
+        //add faults to signature....
+        if(m_faults.size() > 0)
+        {
+            m_methodSig = m_methodSig + " throws ";
+            Iterator iterator = m_faults.keySet().iterator();
+            while (iterator.hasNext())
+            {
+                String faultName = (String) iterator.next();
+                Fault fault = (Fault) m_faults.get(faultName);
+                Part part = (Part) 
fault.getMessage().getParts().values().iterator().next();
+                String javaPackageName = GenerationUtils.getJavaPackageName( 
targetNamespace );
+                if(part.getElementName() == null)
+                {
+                    //its a type..get the name of the type
+                    faultName = part.getTypeName().getLocalPart();
+                }
+
+                faultName = javaPackageName + "." + faultName + "Exception";
+                m_methodSig = m_methodSig + faultName;
+
+                if(iterator.hasNext())
+                {
+                    m_methodSig = m_methodSig + ", ";
+                }
+            }
+        }
+
     }
 
     public String getJavaMethodName()

Added: incubator/apollo/trunk/src/templates/BaseFaultExtension.vm
URL: 
http://svn.apache.org/viewcvs/incubator/apollo/trunk/src/templates/BaseFaultExtension.vm?rev=164883&view=auto
==============================================================================
--- incubator/apollo/trunk/src/templates/BaseFaultExtension.vm (added)
+++ incubator/apollo/trunk/src/templates/BaseFaultExtension.vm Tue Apr 26 
14:11:09 2005
@@ -0,0 +1,46 @@
+#set( $package = $generated.packageName)
+#set( $namespace = $generated.namespace)
+
+package $package;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ws.resource.i18n.MessagesImpl;
+import org.apache.ws.util.i18n.Messages;
+import javax.xml.namespace.QName;
+import org.apache.ws.resource.faults.AbstractBaseFaultException;
+import org.apache.ws.resource.properties.NamespaceVersionHolder;
+
+
+public class $faultName
+        extends AbstractBaseFaultException
+{
+
+    private static final Messages MSG = MessagesImpl.getInstance();
+    private static final Log LOG = LogFactory.getLog( ${faultName}.class );
+
+    private QName m_name;
+
+    /**
+     * Constructs a new $faultName .
+     *
+     * @param namespaces
+     * @param faultString 
+     */
+    public $faultName( NamespaceVersionHolder namespaces, String faultString )
+    {
+        super( namespaces, faultString );
+        m_name = new QName( "$namespace", "$faultName");        
+    }
+    
+    /**
+     * Returns the element name for this base fault.
+     *
+     * @return the element name for this base fault
+     */
+    public QName getBaseFaultName()
+    {
+        return m_name;
+    }
+
+}
\ No newline at end of file

Added: incubator/apollo/trunk/src/templates/CustomFault.vm
URL: 
http://svn.apache.org/viewcvs/incubator/apollo/trunk/src/templates/CustomFault.vm?rev=164883&view=auto
==============================================================================
--- incubator/apollo/trunk/src/templates/CustomFault.vm (added)
+++ incubator/apollo/trunk/src/templates/CustomFault.vm Tue Apr 26 14:11:09 2005
@@ -0,0 +1,52 @@
+#set( $package = $generated.packageName)
+#set( $namespace = $generated.namespace)
+
+package $package;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ws.resource.i18n.MessagesImpl;
+import org.apache.ws.util.XmlBeanUtils;
+import org.apache.ws.util.i18n.Messages;
+import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.ws.resource.faults.FaultException;
+import javax.xml.namespace.QName;
+
+
+public class $faultName
+        extends FaultException
+{
+
+    private static final Messages MSG = MessagesImpl.getInstance();
+
+    private static final Log LOG = LogFactory.getLog( ${faultName}.class );
+
+    /**
+     * Element "local" name for the fault which is added as an element under 
the Detail.
+     */
+    private static final String FAULT_LOCAL_NAME = "$faultName";
+
+    /**
+     * Creates a new [EMAIL PROTECTED] $faultName} object.
+     *
+     * @param faultCode The fault code for this fault. [EMAIL PROTECTED] 
org.apache.ws.Soap1_1Constants#FAULT_CLIENT for examples}
+     * @param faultString The fault message
+     */
+    public $faultName( QName faultCode, String faultString )
+    {
+        super( faultCode, faultString );
+        try
+        {
+            XmlObject xBean = XmlObject.Factory.parse(
+                    "<" + FAULT_LOCAL_NAME + " xmlns=\"" + "$namespace" + "\" 
/>" );
+            XmlBeanUtils.setValue( xBean, "YOUR_DETAIL_MESSAGE_HERE" );
+            addDetailEntry( xBean );
+        }
+        catch ( XmlException xe )
+        {
+            LOG.error( xe );
+        }
+    }
+
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to