cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1124353 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/ tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/ tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxb/ tools/wadlto/jaxrs/...
Date Wed, 18 May 2011 17:49:52 GMT
Author: sergeyb
Date: Wed May 18 17:49:52 2011
New Revision: 1124353

URL: http://svn.apache.org/viewvc?rev=1124353&view=rev
Log:
[CXF-3948] Initial support for customizing schema package names

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java
    cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
    cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxb/CustomizationParser.java
    cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
    cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java?rev=1124353&r1=1124352&r2=1124353&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java
Wed May 18 17:49:52 2011
@@ -132,6 +132,8 @@ public class SourceGenerator {
     private List<String> generatedServiceClasses = new ArrayList<String>(); 
     private List<String> generatedTypeClasses = new ArrayList<String>();
     private List<InputSource> bindingFiles = Collections.emptyList();
+    private List<InputSource> schemaPackageFiles = Collections.emptyList();
+    private Map<String, String> schemaPackageMap = Collections.emptyMap();
     private Bus bus;
     
     public SourceGenerator() {
@@ -490,7 +492,12 @@ public class SourceGenerator {
     }
 
     private String possiblyConvertNamespaceURI(String nsURI, boolean expandedQName) {
-        return expandedQName ? PackageUtils.getPackageNameByNameSpaceURI(nsURI) : nsURI;
+        return expandedQName ? getPackageFromNamespace(nsURI) : nsURI;
+    }
+    
+    private String getPackageFromNamespace(String nsURI) {
+        return schemaPackageMap.containsKey(nsURI) ? schemaPackageMap.get(nsURI)
+            : PackageUtils.getPackageNameByNameSpaceURI(nsURI);
     }
     
     private void generateEmptyMethodBody(StringBuilder sbCode, boolean responseTypeAvailable)
{
@@ -638,7 +645,7 @@ public class SourceGenerator {
                 if (namespace == null) {
                     return null;
                 }
-                String packageName = PackageUtils.getPackageNameByNameSpaceURI(namespace);
+                String packageName = getPackageFromNamespace(namespace);
                 String clsName = getSchemaClassName(packageName, gInfo, pair[1], typeClassNames);
                 if (clsName != null) {
                     addImport(imports, clsName);
@@ -846,6 +853,10 @@ public class SourceGenerator {
     }
     
     private void addSchemas(List<SchemaInfo> schemas, SchemaCompiler compiler) {
+        // handle package customizations first
+        for (int i = 0; i < schemaPackageFiles.size(); i++) {
+            compiler.parseSchema(schemaPackageFiles.get(i));
+        }
         
         for (int i = 0; i < schemas.size(); i++) {
             SchemaInfo schema = schemas.get(i);
@@ -909,6 +920,14 @@ public class SourceGenerator {
         this.bindingFiles = files;
     }
     
+    public void setSchemaPackageFiles(List<InputSource> files) {
+        this.schemaPackageFiles = files;
+    }
+
+    public void setSchemaPackageMap(Map<String, String> map) {
+        this.schemaPackageMap = map;
+    }
+    
     public void setBus(Bus bus) {
         this.bus = bus;
     }

Modified: cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java?rev=1124353&r1=1124352&r2=1124353&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
(original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
Wed May 18 17:49:52 2011
@@ -34,6 +34,7 @@ public final class WadlToolConstants {
     public static final String CFG_SERVER = ToolConstants.CFG_SERVER;
     public static final String CFG_TYPES = ToolConstants.CFG_TYPES;
     public static final String CFG_PACKAGENAME = ToolConstants.CFG_PACKAGENAME;
+    public static final String CFG_TYPES_PACKAGENAME = "typesPackagename";
     public static final String CFG_RESOURCENAME = "resourcename";
     
     public static final String CFG_CATALOG = ToolConstants.CFG_CATALOG;

Modified: cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxb/CustomizationParser.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxb/CustomizationParser.java?rev=1124353&r1=1124352&r2=1124353&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxb/CustomizationParser.java
(original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxb/CustomizationParser.java
Wed May 18 17:49:52 2011
@@ -45,27 +45,27 @@ import org.apache.cxf.staxutils.StaxUtil
 import org.apache.cxf.tools.common.ToolConstants;
 import org.apache.cxf.tools.common.ToolContext;
 import org.apache.cxf.tools.common.ToolException;
+import org.apache.cxf.tools.util.JAXBUtils;
 import org.apache.cxf.tools.util.URIParserUtil;
 import org.apache.cxf.tools.wadlto.WadlToolConstants;
 
 public final class CustomizationParser {
     private static final Logger LOG = LogUtils.getL7dLogger(CustomizationParser.class);
     private final List<InputSource> jaxbBindings = new ArrayList<InputSource>();
+    private final List<InputSource> packageFiles = new ArrayList<InputSource>();
+    
     private Bus bus;
-    private String[] bindingFiles = new String[]{};
     private String wadlPath;
     
     public CustomizationParser(ToolContext env) {
         bus = (Bus)env.get(Bus.class);
-        Object files = env.get(WadlToolConstants.CFG_BINDING);
-        if (files != null) {
-            bindingFiles = files instanceof String ? new String[]{(String)files}
-                                                   : (String[])files;
-        }
         wadlPath = (String)env.get(WadlToolConstants.CFG_WADLURL);
     }
 
-    public void parse() {
+    public void parse(ToolContext env) {
+        // JAXB schema customizations
+        String[] bindingFiles = getBindingFiles(env);
+        
         for (int i = 0; i < bindingFiles.length; i++) {
             try {
                 addBinding(bindingFiles[i]);
@@ -74,6 +74,11 @@ public final class CustomizationParser {
                 throw new ToolException(msg, xse);
             }
         }
+        // Schema Namespace to Package customizations
+        for (String ns : env.getNamespacePackageMap().keySet()) {
+            File file = JAXBUtils.getPackageMappingSchemaBindingFile(ns, env.mapPackageName(ns));
+            packageFiles.add(new InputSource(file.toURI().toString()));
+        }
     }
 
     private void addBinding(String bindingFile) throws XMLStreamException {
@@ -145,9 +150,21 @@ public final class CustomizationParser {
         return false;
     }
 
+    private String[] getBindingFiles(ToolContext env) {
+        Object files = env.get(WadlToolConstants.CFG_BINDING);
+        if (files != null) {
+            return files instanceof String ? new String[]{(String)files}
+                                                   : (String[])files;
+        } else {
+            return new String[] {};
+        }
+    }
+    
     public List<InputSource> getJaxbBindings() {
         return this.jaxbBindings;
     }
 
-
+    public List<InputSource> getSchemaPackageFiles() {
+        return this.packageFiles;
+    }
 }

Modified: cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java?rev=1124353&r1=1124352&r2=1124353&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
(original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
Wed May 18 17:49:52 2011
@@ -81,6 +81,7 @@ public class JAXRSContainer extends Abst
         if (context.get(WadlToolConstants.CFG_OUTPUTDIR) == null) {
             context.put(WadlToolConstants.CFG_OUTPUTDIR, ".");
         }
+        setPackageAndNamespaces();
     }
 
     private void processWadl() {
@@ -104,10 +105,16 @@ public class JAXRSContainer extends Abst
         sg.setWadlPath(wadlURL);
                 
         CustomizationParser parser = new CustomizationParser(context);
-        parser.parse();
+        parser.parse(context);
+        
         List<InputSource> bindingFiles = parser.getJaxbBindings();
         sg.setBindingFiles(bindingFiles);
         
+        List<InputSource> schemaPackageFiles = parser.getSchemaPackageFiles();
+        sg.setSchemaPackageFiles(schemaPackageFiles);
+        sg.setSchemaPackageMap(context.getNamespacePackageMap());
+        // sg.setSchemaPackageName((String)context.get(WadlToolConstants.CFG_TOOLS_PACKAGENAME)));
+        
         // generate
         String codeType = context.optionSet(WadlToolConstants.CFG_TYPES)
             ? SourceGenerator.CODE_TYPE_GRAMMAR : SourceGenerator.CODE_TYPE_PROXY;
@@ -153,4 +160,30 @@ public class JAXRSContainer extends Abst
         context.put(WadlToolConstants.CFG_WADLURL, absoluteWadlURL);
         return absoluteWadlURL;
     }
+    
+    //TODO: this belongs to JAXB Databinding, should we just reuse 
+    // org.apache.cxf.tools.wsdlto.databinding.jaxb ?
+    private void setPackageAndNamespaces() {
+        String[] schemaPackageNamespaces = new String[]{};
+        Object value = context.get(WadlToolConstants.CFG_TYPES_PACKAGENAME);
+        if (value != null) {
+            schemaPackageNamespaces = value instanceof String ? new String[]{(String)value}
+                                                   : (String[])value;
+        }
+        for (int i = 0; i < schemaPackageNamespaces.length; i++) {
+            int pos = schemaPackageNamespaces[i].indexOf("=");
+            String packagename = schemaPackageNamespaces[i];
+            if (pos != -1) {
+                String ns = schemaPackageNamespaces[i].substring(0, pos);
+                packagename = schemaPackageNamespaces[i].substring(pos + 1);
+                context.addNamespacePackageMap(ns, packagename);
+            } else {
+                // this is the default schema package name
+                // if CFG_PACKAGENAME is set then it's only used for JAX-RS resource 
+                // classes
+                context.put(WadlToolConstants.CFG_TYPES_PACKAGENAME, packagename);
+            }
+        }
+        
+    }    
 }

Modified: cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java?rev=1124353&r1=1124352&r2=1124353&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
(original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
Wed May 18 17:49:52 2011
@@ -47,8 +47,8 @@ public class JAXRSContainerTest extends 
 
             assertNotNull(output.list());
             
-            verifyFiles("java", true, false, "org.apache.cxf.jaxrs.model.wadl");
-            verifyFiles("class", true, false, "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("java", true, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("class", true, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
             
         } catch (Exception e) {
             e.printStackTrace();
@@ -72,8 +72,8 @@ public class JAXRSContainerTest extends 
 
             assertNotNull(output.list());
             
-            verifyFiles("java", true, false, "org.apache.cxf.jaxrs.model.wadl");
-            verifyFiles("class", true, false, "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("java", true, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("class", true, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
             
         } catch (Exception e) {
             e.printStackTrace();
@@ -96,8 +96,8 @@ public class JAXRSContainerTest extends 
 
             assertNotNull(output.list());
             
-            verifyFiles("java", false, false, "org.apache.cxf.jaxrs.model.wadl");
-            verifyFiles("class", false, false, "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("java", false, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("class", false, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
             
         } catch (Exception e) {
             e.printStackTrace();
@@ -121,8 +121,8 @@ public class JAXRSContainerTest extends 
 
             assertNotNull(output.list());
             
-            verifyFiles("java", false, false, "org.apache.cxf.jaxrs.model.wadl");
-            verifyFiles("class", false, false, "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("java", false, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("class", false, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
             
         } catch (Exception e) {
             e.printStackTrace();
@@ -146,8 +146,8 @@ public class JAXRSContainerTest extends 
 
             assertNotNull(output.list());
             
-            verifyFiles("java", false, false, "org.apache.cxf.jaxrs.model.wadl");
-            verifyFiles("class", false, false, "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("java", false, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("class", false, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
             
         } catch (Exception e) {
             e.printStackTrace();
@@ -200,8 +200,34 @@ public class JAXRSContainerTest extends 
 
             assertNotNull(output.list());
             
-            verifyFiles("java", true, false, "custom.books");
-            verifyFiles("class", true, false, "custom.books");
+            verifyFiles("java", true, false, "superbooks", "custom.books");
+            verifyFiles("class", true, false, "superbooks", "custom.books");
+            
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+    
+    @Test    
+    public void testCodeGenInterfacesCustomPackageForResourcesAndSchemas() {
+        try {
+            JAXRSContainer container = new JAXRSContainer(null);
+            
+            ToolContext context = new ToolContext();
+            context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
+            context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/bookstore.xml"));
+            context.put(WadlToolConstants.CFG_PACKAGENAME, "custom.books.service");
+            context.put(WadlToolConstants.CFG_TYPES_PACKAGENAME, "http://superbooks=custom.books.schema");
+            context.put(WadlToolConstants.CFG_COMPILE, "true");
+
+            container.setContext(context);
+            container.execute();
+
+            assertNotNull(output.list());
+            
+            verifyFiles("java", true, false, "custom.books.schema", "custom.books.service");
+            verifyFiles("class", true, false, "custom.books.schema", "custom.books.service");
             
         } catch (Exception e) {
             e.printStackTrace();
@@ -225,8 +251,8 @@ public class JAXRSContainerTest extends 
 
             assertNotNull(output.list());
             
-            verifyFiles("java", true, false, "org.apache.cxf.jaxrs.model.wadl");
-            verifyFiles("class", true, false, "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("java", true, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("class", true, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
         } catch (Exception e) {
             fail();
             e.printStackTrace();
@@ -250,8 +276,8 @@ public class JAXRSContainerTest extends 
 
             assertNotNull(output.list());
             
-            verifyFiles("java", true, true, "org.apache.cxf.jaxrs.model.wadl");
-            verifyFiles("class", true, true, "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("java", true, true, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
+            verifyFiles("class", true, true, "superbooks", "org.apache.cxf.jaxrs.model.wadl");
         } catch (Exception e) {
             fail();
             e.printStackTrace();
@@ -273,7 +299,7 @@ public class JAXRSContainerTest extends 
 
             assertNotNull(output.list());
             
-            verifyTypes("java");
+            verifyTypes("superbooks", "java");
             
         } catch (Exception e) {
             fail();
@@ -282,14 +308,14 @@ public class JAXRSContainerTest extends 
     }
     
     private void verifyFiles(String ext, boolean subresourceExpected, boolean interfacesAndImpl,

-                             String resourcePackage) {
+                             String schemaPackage, String resourcePackage) {    
         List<File> files = FileUtils.getFilesRecurse(output, ".+\\." + ext + "$");
         int size = interfacesAndImpl ? 9 : 7;
         if (!subresourceExpected) {
             size--;
         }
         assertEquals(size, files.size());
-        doVerifyTypes(files, ext);
+        doVerifyTypes(files, schemaPackage, ext);
         if (subresourceExpected) {
             assertTrue(checkContains(files, resourcePackage + ".FormInterface." + ext));
         }
@@ -302,18 +328,18 @@ public class JAXRSContainerTest extends 
         }
     }
     
-    private void verifyTypes(String ext) {
+    private void verifyTypes(String schemaPackage, String ext) {
         List<File> files = FileUtils.getFilesRecurse(output, ".+\\." + ext + "$");
         assertEquals(5, files.size());
-        doVerifyTypes(files, ext);
+        doVerifyTypes(files, schemaPackage, ext);
     }
     
-    private void doVerifyTypes(List<File> files, String ext) {
-        assertTrue(checkContains(files, "superbooks.Book." + ext));
-        assertTrue(checkContains(files, "superbooks.Book2." + ext));
-        assertTrue(checkContains(files, "superbooks.Chapter." + ext));
-        assertTrue(checkContains(files, "superbooks.ObjectFactory." + ext));
-        assertTrue(checkContains(files, "superbooks.package-info." + ext));
+    private void doVerifyTypes(List<File> files, String schemaPackage, String ext)
{
+        assertTrue(checkContains(files, schemaPackage + ".Book." + ext));
+        assertTrue(checkContains(files, schemaPackage + ".Book2." + ext));
+        assertTrue(checkContains(files, schemaPackage + ".Chapter." + ext));
+        assertTrue(checkContains(files, schemaPackage + ".ObjectFactory." + ext));
+        assertTrue(checkContains(files, schemaPackage + ".package-info." + ext));
     }
     
     private boolean checkContains(List<File> clsFiles, String name) {



Mime
View raw message