axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sc...@apache.org
Subject svn commit: r494087 - in /webservices/axis2/trunk/java/modules: jaxws/src/org/apache/axis2/jaxws/message/databinding/ jaxws/test/org/apache/axis2/jaxws/framework/ jaxws/test/org/apache/axis2/jaxws/misc/ metadata/src/org/apache/axis2/jaxws/util/
Date Mon, 08 Jan 2007 15:02:37 GMT
Author: scheu
Date: Mon Jan  8 07:02:36 2007
New Revision: 494087

URL: http://svn.apache.org/viewvc?view=rev&rev=494087
Log:
AXIS2-1945
Contributor: Rich Scheuerle
Corrected NS->PKG algorithm.  Improved code that detects and discards invalid packages.

Added:
    webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/misc/
    webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/misc/NS2PkgTest.java
Modified:
    webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBUtils.java
    webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/framework/JAXWSTest.java
    webservices/axis2/trunk/java/modules/metadata/src/org/apache/axis2/jaxws/util/JavaUtils.java

Modified: webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBUtils.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBUtils.java?view=diff&rev=494087&r1=494086&r2=494087
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBUtils.java
(original)
+++ webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBUtils.java
Mon Jan  8 07:02:36 2007
@@ -96,59 +96,8 @@
 		JAXBContext context = innerMap.get(contextPackages);
 		if (context == null) {
             synchronized(innerMap) {
-                try{
-                    // There are two ways to construct the context.
-                    // 1) USE A CONTEXTPATH, which is a string containing
-                    //    all of the packages separated by colons.
-                    // 2) USE A CLASS[], which is an array of all of the classes
-                    //    involved in the marshal/unmarshal.
-                    //   
-                    // There are pros/cons with both approaches.
-                    // USE A CONTEXTPATH: 
-                    //    Pros: preferred way of doing this.  
-                    //          performant
-                    //          most dynamic
-                    //    Cons: Each package in context path must have an ObjectFactory
-                    //        
-                    //
-                    // USE CLASS[]:
-                    //    Pros: Doesn't require ObjectFactory in each package
-                    //    Cons: Hard to set up, must account for JAX-WS classes, etc.
-                    //          Does not work if arrays of classes are needed
-                    //          slower
-                    //
-                    //  The following code attempts to build a context path.  It then
-                    //  choose one of the two constructions above (prefer USE A CONTEXT_PATH)
-                    //
-                    
-                    if (log.isDebugEnabled()) {
-                        log.debug("First try to create JAXBContext with contextPath");
-                    }
-                    if (useJAXBContextWithContextPath(contextPackages, cl)) {   
-                        context = createJAXBContextUsingContextPath(contextPackages, cl);
-                    }
-                    
-                    if (context == null) {
-                        // Unsuccessful, USE CLASS[]
-                        if (log.isDebugEnabled()) {
-                            log.debug("Unsuccessful.. Now attempting to create JAXBContext
with Class[]");
-                        }
-                        Iterator<String> it = contextPackages.iterator();
-                        List<Class> fullList = new ArrayList<Class>();
-                        while (it.hasNext()) {
-                            String pkg = it.next();
-                    		fullList.addAll(getAllClassesFromPackage(pkg, cl));
-                    	}
-                    	Class[] classArray = fullList.toArray(new Class[0]);
-                        context = JAXBContext.newInstance(classArray);
-                    }
-                    if (log.isDebugEnabled()) {
-                        log.debug("Successfully created JAXBContext " + context.toString());
-                    }
-                    innerMap.put(contextPackages, context);	
-                }catch(ClassNotFoundException e){
-                	throw new JAXBException(e);
-                }
+                context = createJAXBContext(contextPackages, cl);
+                innerMap.put(contextPackages, context);	
                 if (log.isDebugEnabled()) {
                     log.debug("JAXBContext [created] for " + contextPackages.toString());
                 }
@@ -160,6 +109,134 @@
         }
 		return context;
 	}
+    
+    /**
+     * Create a JAXBContext using the contextPackages
+     * @param contextPackages Set<String>
+     * @param cl ClassLoader
+     * @return JAXBContext
+     * @throws JAXBException
+     */
+    private static JAXBContext createJAXBContext(Set<String> contextPackages, ClassLoader
cl) throws JAXBException {
+
+       JAXBContext context = null;
+       
+        // The contextPackages is a set of package names that are constructed using PackageSetBuilder.
+        // PackageSetBuilder gets the packages names from the following sources.
+        //   a) It walks the various annotations on the WebService collecting package names.
+        //   b) It walks the wsdl/schemas and builds package names for each target namespace.
+        //
+        // The combination of these two sources should produce all of the package names.
+        // -------------
+        // Note that (b) is necessary for the following case:
+        // An operation has a parameter named BASE.
+        // Object DERIVED is an extension of BASE and is defined in a different package/schema.
+        // In this case, there will not be any annotations on the WebService that reference
DERIVED.
+        // The only way to find the package for DERIVED is to walk the schemas.
+        // -------------
+    
+        Iterator<String> it = contextPackages.iterator();
+        while(it.hasNext()) {
+            String p = it.next();
+            // Don't consider java and javax packages
+            // REVIEW: We might have to refine this
+            if (p.startsWith("java.") ||
+                p.startsWith("javax.")) {
+              it.remove();
+            }
+        }
+        
+        // There are two ways to construct the context.
+        // 1) USE A CONTEXTPATH, which is a string containing
+        //    all of the packages separated by colons.
+        // 2) USE A CLASS[], which is an array of all of the classes
+        //    involved in the marshal/unmarshal.
+        //   
+        // There are pros/cons with both approaches.
+        // USE A CONTEXTPATH: 
+        //    Pros: preferred way of doing this.  
+        //          performant
+        //          most dynamic
+        //    Cons: Each package in context path must have an ObjectFactory
+        //        
+        //
+        // USE CLASS[]:
+        //    Pros: Doesn't require ObjectFactory in each package
+        //    Cons: Hard to set up, must account for JAX-WS classes, etc.
+        //          Does not work if arrays of classes are needed
+        //          slower
+        //
+        //  The following code attempts to build a context path.  It then
+        //  choose one of the two constructions above (prefer USE A CONTEXT_PATH)
+        //
+        
+        // The packages are examined to see if they have ObjectFactory/package-info classes.
+        // Invalid packages are removed from the list
+        it = contextPackages.iterator();
+        boolean contextConstruction = true;
+        while(it.hasNext()) {
+            String p = it.next();
+            // See if this package has an ObjectFactory or package-info
+            if (checkPackage(p, cl)) {
+                // Flow to here indicates package can be used for CONTEXT construction
+                if (log.isDebugEnabled()) {
+                    log.debug("Package " + p + " contains an ObjectFactory or package-info
class.");
+                }
+            } else {
+                // Flow to here indicates that the package is not valid for context construction.
+                // Perhaps the package is invalid.
+                if (log.isDebugEnabled()) {
+                    log.debug("Package " + p + " does not contain an ObjectFactory or package-info
class.  Searching for JAXB classes");
+                }
+                List<Class> classes = null;
+                try {
+                    classes = getAllClassesFromPackage(p, cl);
+                    if (classes == null || classes.size() == 0) {
+                        if (log.isDebugEnabled()) {
+                            log.debug("Package " + p + " does not have any JAXB classes.
 It is removed from the JAXB context path.");
+                        }
+                        it.remove();
+                    } else {
+                        // Classes are found in the package.  We cannot use the CONTEXT construction
+                        contextConstruction = false;
+                        if (log.isDebugEnabled()) {
+                            log.debug("Package " + p + " does not contain ObjectFactory,
but it does contain other JAXB classes.");
+                        }
+                    }
+                } catch(Exception e) {
+                    if (log.isDebugEnabled()) {
+                        log.debug("Package " + p + " does not appear to be a valid package.
 It is excluded.");
+                    }
+                    it.remove();
+                }               
+            }
+        }
+        
+        // CONTEXT construction
+        if (contextConstruction) {
+            context = createJAXBContextUsingContextPath(contextPackages, cl);
+        }
+        
+        // CLASS construction
+        if (context == null) {
+            it = contextPackages.iterator();
+            List<Class> fullList = new ArrayList<Class>();
+            while (it.hasNext()) {
+                String pkg = it.next();
+                try {
+                    fullList.addAll(getAllClassesFromPackage(pkg, cl));
+                } catch (ClassNotFoundException e) {
+                    throw new JAXBException(e);
+                }
+            }
+            Class[] classArray = fullList.toArray(new Class[0]);
+            context = JAXBContext.newInstance(classArray);
+        }
+        if (log.isDebugEnabled()) {
+            log.debug("Successfully created JAXBContext " + context.toString());
+        }
+        return context;
+    }
 	
 	/**
 	 * Get the unmarshaller.  You must call releaseUnmarshaller to put it back into the pool
@@ -290,41 +367,35 @@
 	}
     
     /**
-     * @param packages
+     * @param p Package
      * @param cl
-     * @return
+     * @return true if each package has a ObjectFactory class or package-info
      */
-    private static boolean useJAXBContextWithContextPath(Set<String> packages, ClassLoader
cl) {
-        
-        // Each package must have 
-        Iterator<String> it = packages.iterator();
-        while(it.hasNext()) {
-            String p = it.next();
-            if (p.startsWith("java.") ||
-                    p.startsWith("javax.")) {
-                   ; // Assume that these packages don't need an object factory
-            } else {
-                try {
-                    Class cls = Class.forName(p + ".ObjectFactory",false, cl);
-                    if (cl == null) {
-                        return false;
-                    }
-                    // REVIEW: Do we need to check for .package-info
-                    //cls = Class.forName(p + ".package-info",false, cl);
-                    //if (cl == null) {
-                    //    return false;
-                    //}
-                } catch (Exception e) {
-                    if (log.isDebugEnabled()) {
-                        log.debug(e);
-                    }
-                    return false;
-                }
-                
+	private static boolean checkPackage(String p, ClassLoader cl) {
+	    
+	    // Each package must have an ObjectFactory
+	    
+	    try {
+	        Class cls = Class.forName(p + ".ObjectFactory",false, cl);
+	        if (cls != null) {
+	            return true;
+	        }
+	    } catch (Exception e) {
+	        // Exception is thrown if class does not exist.  
+	    }
+	    
+        try {
+            Class cls = Class.forName(p + ".package-info",false, cl);
+            if (cls != null) {
+                return true;
             }
+        } catch (Exception e) {
+            // Exception is thrown if class does not exist.  
         }
-        return true;
+
+	    return false;
     }
+    
     /**
      * Create a JAXBContext using the contextpath approach
      * @param packages
@@ -339,15 +410,11 @@
         Iterator<String> it = packages.iterator();
         while(it.hasNext()) {
             String p = it.next();
-            if (p.startsWith("java.") ||
-                p.startsWith("javax.")) {
-               ; // Assume that these packages don't have an object factory
-            } else {
-                if (contextpath.length() != 0) {
-                    contextpath +=":";
-                }
-                contextpath += p;
+            if (contextpath.length() != 0) {
+                contextpath +=":";
             }
+            contextpath += p;
+            
         }
         try {
             if (log.isDebugEnabled()) {
@@ -371,7 +438,7 @@
      * @param pkg Package
      * @param ClassLoader cl
      * @return
-     * @throws ClassNotFoundException
+     * @throws ClassNotFoundException if error occurs getting package
      */
     private static List<Class> getAllClassesFromPackage(String pkg, ClassLoader cl)
throws ClassNotFoundException {
         if (pkg == null) {
@@ -406,7 +473,7 @@
         // For every directory identified capture all the .class files
         for (File directory : directories) {
             if (log.isDebugEnabled()) {
-                log.debug("Adding classes from: " + directory.getName());
+                log.debug("  Adding JAXB classes from directory: " + directory.getName());
             }
             if (directory.exists()) {
                 // Get the list of the files contained in the package
@@ -453,11 +520,6 @@
                         
                     }
                 }
-                
-                // REVIEW Load and add the common array classes
-                // Support of RPC list (and possibly other scenarios) requires that the array
classes should also be present.
-                // This is a hack until we can determine how to get this information.
-                //addCommonArrayClasses(classes);
             }
         }
         return classes;

Modified: webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/framework/JAXWSTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/framework/JAXWSTest.java?view=diff&rev=494087&r1=494086&r2=494087
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/framework/JAXWSTest.java
(original)
+++ webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/framework/JAXWSTest.java
Mon Jan  8 07:02:36 2007
@@ -46,6 +46,7 @@
 import org.apache.axis2.jaxws.message.SAAJConverterTests;
 import org.apache.axis2.jaxws.message.SOAP12Tests;
 import org.apache.axis2.jaxws.message.XMLStreamReaderSplitterTests;
+import org.apache.axis2.jaxws.misc.NS2PkgTest;
 import org.apache.axis2.jaxws.nonanonymous.complextype.NonAnonymousComplexTypeTests;
 import org.apache.axis2.jaxws.polymorphic.shape.tests.PolymorphicTests;
 import org.apache.axis2.jaxws.provider.JAXBProviderTests;
@@ -141,6 +142,7 @@
         suite.addTestSuite(ResourceInjectionTests.class);
         suite.addTestSuite(AnyTypeTests.class);
         suite.addTestSuite(PolymorphicTests.class);
+        suite.addTestSuite(NS2PkgTest.class);
         // Start (and stop) the server only once for all the tests
         TestSetup testSetup = new TestSetup(suite) {
             public void setUp() {

Added: webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/misc/NS2PkgTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/misc/NS2PkgTest.java?view=auto&rev=494087
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/misc/NS2PkgTest.java
(added)
+++ webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/misc/NS2PkgTest.java
Mon Jan  8 07:02:36 2007
@@ -0,0 +1,23 @@
+/**
+ * 
+ */
+package org.apache.axis2.jaxws.misc;
+
+import org.apache.axis2.jaxws.util.JavaUtils;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests Namespace to Package Algorithmh
+ *
+ */
+public class NS2PkgTest extends TestCase {
+
+    public void test01() throws Exception {
+        String ns1 = "http://example.org/NewBusiness/";
+        String expectedPkg1 = "org.example.newbusiness";
+        
+        String pkg = JavaUtils.getPackageFromNamespace(ns1);
+        assertTrue(expectedPkg1.equals(pkg));
+    }
+}

Modified: webservices/axis2/trunk/java/modules/metadata/src/org/apache/axis2/jaxws/util/JavaUtils.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/metadata/src/org/apache/axis2/jaxws/util/JavaUtils.java?view=diff&rev=494087&r1=494086&r2=494087
==============================================================================
--- webservices/axis2/trunk/java/modules/metadata/src/org/apache/axis2/jaxws/util/JavaUtils.java
(original)
+++ webservices/axis2/trunk/java/modules/metadata/src/org/apache/axis2/jaxws/util/JavaUtils.java
Mon Jan  8 07:02:36 2007
@@ -77,6 +77,7 @@
 	        }
         }
         String[] words = wordList.toArray(new String[0]);
+        
         // Now do step 2: Strip off the trailing "." (i.e. strip off .html)
         if(words !=null && words.length > 1 ){
 	        String lastWord = words[words.length-1];
@@ -113,16 +114,27 @@
             list.add(words[i]);
         }
         
-        // Step 7: make into and an appropriate java word
+        // Step 7: lowercase each word
+        for (int i =0; i<list.size(); i++) {
+            String word = list.remove(i);
+            word = word.toLowerCase();
+            list.add(i, word);
+        }
+        
+        // Step 8: make into and an appropriate java word
         for (int i =0; i<list.size(); i++) {
             String word = list.get(i);
 
+            // 8a: Convert special characters to underscore
             // Convert non-java words to underscore.
             // TODO: Need to do this for all chars..not just hyphens
             word = replace(word, "-", "_");
+            
+            // 8b: Append _ to java keywords
             if (JavaUtils.isJavaKeyword(word)) {
                 word = word + "_";
             }
+            // 8c: prepend _ if first character cannot be the first character of a java identifier
             if (!Character.isJavaIdentifierPart(word.charAt(0)) ) {
                 word = "_" + word;
             }
@@ -130,7 +142,7 @@
             list.set(i, word);
         }
         
-        // Step 8: Concatenate and return
+        // Step 9: Concatenate and return
         String name = "";
         for (int i =0; i<list.size(); i++) {
             if (i == 0) {



---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org


Mime
View raw message