You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2011/05/27 18:54:28 UTC
svn commit: r1128392 - 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/jaxrs/
tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/
tools/wadlto...
Author: sergeyb
Date: Fri May 27 16:54:28 2011
New Revision: 1128392
URL: http://svn.apache.org/viewvc?rev=1128392&view=rev
Log:
[CXF-3498] Initial support for WADL resources referencing abstract resource_type definitions
Added:
cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreImportResultType.xml (with props)
cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreResourceRef.xml (with props)
cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResourceWithRefs.xml (with props)
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/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=1128392&r1=1128391&r2=1128392&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 Fri May 27 16:54:28 2011
@@ -165,10 +165,18 @@ public class SourceGenerator {
}
public void generateSource(String wadl, File srcDir, String codeType) {
- Element appElement = readWadl(wadl);
+ Application app = readWadl(wadl, wadlPath);
Set<String> typeClassNames = new HashSet<String>();
- List<SchemaInfo> schemaElements = getSchemaElements(appElement);
+ GrammarInfo gInfo = generateSchemaCodeAndInfo(app, typeClassNames, srcDir);
+ if (!CODE_TYPE_GRAMMAR.equals(codeType)) {
+ generateResourceClasses(app, gInfo, typeClassNames, srcDir);
+ }
+ }
+
+ private GrammarInfo generateSchemaCodeAndInfo(Application app, Set<String> typeClassNames,
+ File srcDir) {
+ List<SchemaInfo> schemaElements = getSchemaElements(app);
if (schemaElements != null && !schemaElements.isEmpty()) {
// generate classes from schema
JCodeModel codeModel = createCodeModel(schemaElements, typeClassNames);
@@ -176,14 +184,12 @@ public class SourceGenerator {
generateClassesFromSchema(codeModel, srcDir);
}
}
-
- if (!CODE_TYPE_GRAMMAR.equals(codeType)) {
- generateResourceClasses(appElement, schemaElements, typeClassNames, srcDir);
- }
+ return getGrammarInfo(app.getAppElement(), schemaElements);
}
- private void generateResourceClasses(Element appElement, List<SchemaInfo> schemaElements,
+ private void generateResourceClasses(Application app, GrammarInfo gInfo,
Set<String> typeClassNames, File src) {
+ Element appElement = app.getAppElement();
List<Element> resourcesEls = DOMUtils.getChildrenWithName(appElement,
WadlGenerator.WADL_NS, "resources");
if (resourcesEls.size() != 1) {
@@ -196,12 +202,12 @@ public class SourceGenerator {
throw new IllegalStateException("WADL has no resource elements");
}
- GrammarInfo gInfo = getGrammarInfo(appElement, schemaElements);
for (int i = 0; i < resourceEls.size(); i++) {
- Element resource = resourceEls.get(i);
- writeResourceClass(resource, typeClassNames, gInfo, src, true, generateInterfaces);
+ Element resource = getResourceElement(app, resourceEls.get(i), gInfo, typeClassNames,
+ resourceEls.get(i).getAttribute("type"), src);
+ writeResourceClass(app, resource, typeClassNames, gInfo, src, true, generateInterfaces);
if (generateInterfaces && generateImpl) {
- writeResourceClass(resource, typeClassNames, gInfo, src, true, false);
+ writeResourceClass(app, resource, typeClassNames, gInfo, src, true, false);
}
if (resourceName != null) {
break;
@@ -212,10 +218,49 @@ public class SourceGenerator {
}
+ //TODO: similar procedure should work for representation, method and param
+ // thus some of the code here will need to be moved into a sep function to be
+ // reused by relevant handlers
+ private Element getResourceElement(Application app, Element resElement,
+ GrammarInfo gInfo, Set<String> typeClassNames,
+ String type, File srcDir) {
+ if (type.length() > 0) {
+ if (type.startsWith("#")) {
+ String refId = type.substring(1);
+ List<Element> resourceTypes =
+ DOMUtils.getChildrenWithName(app.getAppElement(), WadlGenerator.WADL_NS,
+ "resource_type");
+ for (Element resourceType : resourceTypes) {
+ if (refId.equals(resourceType.getAttribute("id"))) {
+ Element realElement = (Element)resourceType.cloneNode(true);
+ DOMUtils.setAttribute(realElement, "id", resElement.getAttribute("id"));
+ DOMUtils.setAttribute(realElement, "path", resElement.getAttribute("path"));
+ return realElement;
+ }
+ }
+ } else {
+ URI wadlRef = URI.create(type);
+ String wadlRefPath = app.getWadlPath() != null
+ ? getBaseWadlPath(app.getWadlPath()) + wadlRef.getPath() : wadlRef.getPath();
+ Application refApp = new Application(readIncludedDocument(wadlRefPath),
+ wadlRefPath);
+ GrammarInfo gInfoBase = generateSchemaCodeAndInfo(refApp, typeClassNames, srcDir);
+ if (gInfoBase != null) {
+ gInfo.getElementTypeMap().putAll(gInfoBase.getElementTypeMap());
+ gInfo.getNsMap().putAll(gInfo.getNsMap());
+ }
+ return getResourceElement(refApp, resElement, gInfo, typeClassNames,
+ "#" + wadlRef.getFragment(), srcDir);
+ }
+ }
+ return resElement;
+
+ }
+
private GrammarInfo getGrammarInfo(Element appElement, List<SchemaInfo> schemaElements) {
if (schemaElements == null || schemaElements.isEmpty()) {
- return null;
+ return new GrammarInfo();
}
Map<String, String> nsMap = new HashMap<String, String>();
@@ -247,7 +292,7 @@ public class SourceGenerator {
}
- private void writeResourceClass(Element rElement, Set<String> typeClassNames,
+ private void writeResourceClass(Application app, Element rElement, Set<String> typeClassNames,
GrammarInfo gInfo, File src, boolean isRoot,
boolean interfaceIsGenerated) {
String resourceId = resourceName != null
@@ -304,7 +349,10 @@ public class SourceGenerator {
String id = subEl.getAttribute("id");
if (id.length() > 0 && !resourceId.equals(id) && !id.startsWith("{java")
&& !id.startsWith("java")) {
- writeResourceClass(subEl, typeClassNames, gInfo, src, false, interfaceIsGenerated);
+ writeResourceClass(app,
+ getResourceElement(app, subEl, gInfo, typeClassNames,
+ subEl.getAttribute("type"), src),
+ typeClassNames, gInfo, src, false, interfaceIsGenerated);
}
}
}
@@ -735,8 +783,8 @@ public class SourceGenerator {
}
}
- private Element readWadl(String wadl) {
- return readXmlDocument(new StringReader(wadl));
+ private Application readWadl(String wadl, String docPath) {
+ return new Application(readXmlDocument(new StringReader(wadl)), docPath);
}
private Element readXmlDocument(Reader reader) {
@@ -758,8 +806,8 @@ public class SourceGenerator {
}
}
- private List<SchemaInfo> getSchemaElements(Element appElement) {
- List<Element> grammarEls = DOMUtils.getChildrenWithName(appElement,
+ private List<SchemaInfo> getSchemaElements(Application app) {
+ List<Element> grammarEls = DOMUtils.getChildrenWithName(app.getAppElement(),
WadlGenerator.WADL_NS, "grammars");
if (grammarEls.size() != 1) {
return null;
@@ -769,7 +817,7 @@ public class SourceGenerator {
List<Element> schemasEls = DOMUtils.getChildrenWithName(grammarEls.get(0),
XmlSchemaConstants.XSD_NAMESPACE_URI, "schema");
for (Element schemaEl : schemasEls) {
- schemas.add(createSchemaInfo(schemaEl, wadlPath));
+ schemas.add(createSchemaInfo(schemaEl, app.getWadlPath()));
}
List<Element> includeEls = DOMUtils.getChildrenWithName(grammarEls.get(0),
WadlGenerator.WADL_NS, "include");
@@ -778,17 +826,17 @@ public class SourceGenerator {
String schemaURI = resolveLocationWithCatalog(href);
if (schemaURI == null) {
- schemaURI = wadlPath != null ? getBaseWadlPath() + href : href;
+ schemaURI = app.getWadlPath() != null ? getBaseWadlPath(app.getWadlPath()) + href : href;
}
- schemas.add(createSchemaInfo(readIncludedSchema(schemaURI),
+ schemas.add(createSchemaInfo(readIncludedDocument(schemaURI),
schemaURI));
}
return schemas;
}
- private String getBaseWadlPath() {
- int lastSep = wadlPath.lastIndexOf("/");
- return lastSep != -1 ? wadlPath.substring(0, lastSep + 1) : wadlPath;
+ private static String getBaseWadlPath(String docPath) {
+ int lastSep = docPath.lastIndexOf("/");
+ return lastSep != -1 ? docPath.substring(0, lastSep + 1) : docPath;
}
private SchemaInfo createSchemaInfo(Element schemaEl, String systemId) {
@@ -812,7 +860,7 @@ public class SourceGenerator {
}
}
- private Element readIncludedSchema(String href) {
+ private Element readIncludedDocument(String href) {
try {
InputStream is = null;
@@ -824,7 +872,7 @@ public class SourceGenerator {
}
return readXmlDocument(new InputStreamReader(is, "UTF-8"));
} catch (Exception ex) {
- throw new RuntimeException("Schema " + href + " can not be read");
+ throw new RuntimeException("Resource " + href + " can not be read");
}
}
@@ -941,8 +989,12 @@ public class SourceGenerator {
}
private static class GrammarInfo {
- private Map<String, String> nsMap;
- private Map<String, String> elementTypeMap;
+ private Map<String, String> nsMap = new HashMap<String, String>();
+ private Map<String, String> elementTypeMap = new HashMap<String, String>();
+
+ public GrammarInfo() {
+
+ }
public GrammarInfo(Map<String, String> nsMap, Map<String, String> elementTypeMap) {
this.nsMap = nsMap;
@@ -994,4 +1046,21 @@ public class SourceGenerator {
// ignore
}
}
+
+ private class Application {
+ private Element appElement;
+ private String wadlPath;
+ public Application(Element appElement, String wadlPath) {
+ this.appElement = appElement;
+ this.wadlPath = wadlPath;
+ }
+
+ public Element getAppElement() {
+ return appElement;
+ }
+
+ public String getWadlPath() {
+ return wadlPath;
+ }
+ }
}
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=1128392&r1=1128391&r2=1128392&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 Fri May 27 16:54:28 2011
@@ -108,7 +108,7 @@ public class JAXRSContainer extends Abst
sg.setPackageName((String)context.get(WadlToolConstants.CFG_PACKAGENAME));
sg.setResourceName((String)context.get(WadlToolConstants.CFG_RESOURCENAME));
- // find the base path
+ // set the base path
sg.setWadlPath(wadlURL);
CustomizationParser parser = new CustomizationParser(context);
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=1128392&r1=1128391&r2=1128392&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 Fri May 27 16:54:28 2011
@@ -106,6 +106,30 @@ public class JAXRSContainerTest extends
}
@Test
+ public void testCodeGenWithImportedSchemaAndResourceSet() {
+ try {
+ JAXRSContainer container = new JAXRSContainer(null);
+
+ ToolContext context = new ToolContext();
+ context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
+ context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/bookstoreResourceRef.xml"));
+ context.put(WadlToolConstants.CFG_COMPILE, "true");
+
+ container.setContext(context);
+ container.execute();
+
+ assertNotNull(output.list());
+
+ 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();
+ fail();
+ }
+ }
+
+ @Test
public void testCodeGenWithImportedSchemaAndBinding() {
try {
JAXRSContainer container = new JAXRSContainer(null);
@@ -185,6 +209,35 @@ public class JAXRSContainerTest extends
}
@Test
+ public void testCodeGenWithResourceSet() {
+ try {
+ JAXRSContainer container = new JAXRSContainer(null);
+
+ ToolContext context = new ToolContext();
+ context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
+ context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/singleResourceWithRefs.xml"));
+ context.put(WadlToolConstants.CFG_RESOURCENAME, "CustomResource");
+ context.put(WadlToolConstants.CFG_COMPILE, "true");
+
+ container.setContext(context);
+ container.execute();
+
+ assertNotNull(output.list());
+
+ List<File> javaFiles = FileUtils.getFilesRecurse(output, ".+\\." + "java" + "$");
+ assertEquals(1, javaFiles.size());
+ assertTrue(checkContains(javaFiles, "application.CustomResource.java"));
+
+ List<File> classFiles = FileUtils.getFilesRecurse(output, ".+\\." + "class" + "$");
+ assertEquals(1, classFiles.size());
+ assertTrue(checkContains(classFiles, "application.CustomResource.class"));
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ @Test
public void testCodeGenInterfacesCustomPackage() {
try {
JAXRSContainer container = new JAXRSContainer(null);
Added: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreImportResultType.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreImportResultType.xml?rev=1128392&view=auto
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreImportResultType.xml (added)
+++ cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreImportResultType.xml Fri May 27 16:54:28 2011
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+-->
+<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:prefix1="http://superbooks">
+
+ <grammars>
+ <include href="schemas/book.xsd"/>
+ </grammars>
+
+ <resource_type id="main-resource">
+ <resource path="/books/{bookid}">
+ <param name="bookid" style="template" type="xs:int"/>
+
+ <method name="POST" id="addBook">
+ <request>
+ <representation mediaType="application/xml" element="prefix1:thebook"/>
+ </request>
+ </method>
+ </resource>
+ </resource_type>
+
+</application>
Propchange: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreImportResultType.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreImportResultType.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreImportResultType.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
Added: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreResourceRef.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreResourceRef.xml?rev=1128392&view=auto
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreResourceRef.xml (added)
+++ cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreResourceRef.xml Fri May 27 16:54:28 2011
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+-->
+<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:prefix1="http://superbooks">
+
+ <resources base="http://localhost:8080/baz">
+ <resource path="/bookstore" id="{wadl.model.jaxrs.cxf.apache.org}BookStore"
+ type="bookstoreImportResultType.xml#main-resource"/>
+ </resources>
+
+</application>
Propchange: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreResourceRef.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreResourceRef.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreResourceRef.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
Added: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResourceWithRefs.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResourceWithRefs.xml?rev=1128392&view=auto
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResourceWithRefs.xml (added)
+++ cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResourceWithRefs.xml Fri May 27 16:54:28 2011
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+-->
+<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:prefix1="http://superbooks">
+
+ <resource_type id="main-resource">
+ <param name="id" style="template" type="xs:long"/>
+ <method name="GET">
+ <request/>
+ <response>
+ <representation mediaType="text/plain">
+ <param name="result" style="plain" type="xs:string"/>
+ </representation>
+ </response>
+ </method>
+ <resource path="/books">
+ <method name="GET">
+ <response>
+ <representation mediaType="text/plain">
+ <param name="result" style="plain" type="xs:string"/>
+ </representation>
+ </response>
+ </method>
+ </resource>
+ </resource_type>
+
+ <resources base="http://localhost:8080/baz">
+ <resource path="/bookstore/{id}" type="#main-resource"/>
+ </resources>
+</application>
Propchange: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResourceWithRefs.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResourceWithRefs.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResourceWithRefs.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml