Repository: cxf Updated Branches: refs/heads/2.7.x-fixes af593943e -> 567a22f26
[CXF-5479]Create a Maven plugin for generating WADL at build time Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/567a22f2 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/567a22f2 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/567a22f2 Branch: refs/heads/2.7.x-fixes Commit: 567a22f26cd21318634b26216451ff2000ab8ea8 Parents: af59394 Author: Freeman Fang <[email protected]> Authored: Fri Apr 4 17:44:57 2014 +0800 Committer: Freeman Fang <[email protected]> Committed: Fri Apr 4 17:44:57 2014 +0800 ---------------------------------------------------------------------- maven-plugins/java2wadl-plugin/pom.xml | 138 +++++++++++ .../maven_plugin/javatowadl/Java2WADLMojo.java | 248 +++++++++++++++++++ maven-plugins/pom.xml | 1 + .../cxf/jaxrs/model/wadl/WadlGenerator.java | 23 +- 4 files changed, 402 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/567a22f2/maven-plugins/java2wadl-plugin/pom.xml ---------------------------------------------------------------------- diff --git a/maven-plugins/java2wadl-plugin/pom.xml b/maven-plugins/java2wadl-plugin/pom.xml new file mode 100644 index 0000000..4a13517 --- /dev/null +++ b/maven-plugins/java2wadl-plugin/pom.xml @@ -0,0 +1,138 @@ +<?xml version="1.0"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-java2wadl-plugin</artifactId> + <packaging>maven-plugin</packaging> + <version>2.7.11-SNAPSHOT</version> + <name>Apache CXF Java2WADL Maven2 Plugin</name> + <description>Apache CXF Java2WADL Maven2 Plugin</description> + <url>http://cxf.apache.org</url> + + + <parent> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-maven-plugins</artifactId> + <version>2.7.11-SNAPSHOT</version> + </parent> + <properties> + <cxf.manifest.location /> + </properties> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.maven.shared</groupId> + <artifactId>maven-artifact-resolver</artifactId> + <version>1.0</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-core</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-artifact</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-api</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-project</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-utils</artifactId> + <version>2.0.5</version> + </dependency> + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-archiver</artifactId> + <version>1.2</version> + </dependency> + + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-tools-common</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-tools-wadlto-jaxrs</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-core</artifactId> + <optional>true</optional> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-beans</artifactId> + <optional>true</optional> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + <optional>true</optional> + </dependency> + + <dependency> + <groupId>org.apache.ant</groupId> + <artifactId>ant</artifactId> + </dependency> + <dependency> + <groupId>org.apache.ant</groupId> + <artifactId>ant-nodeps</artifactId> + </dependency> + </dependencies> + + + <profiles> + <profile> + <id>ibmjdk</id> + <activation> + <property> + <name>java.vendor</name> + <value>IBM Corporation</value> + </property> + </activation> + <dependencies> + <dependency> + <groupId>xerces</groupId> + <artifactId>xercesImpl</artifactId> + </dependency> + </dependencies> + </profile> + </profiles> + +</project> http://git-wip-us.apache.org/repos/asf/cxf/blob/567a22f2/maven-plugins/java2wadl-plugin/src/main/java/org/apache/cxf/maven_plugin/javatowadl/Java2WADLMojo.java ---------------------------------------------------------------------- diff --git a/maven-plugins/java2wadl-plugin/src/main/java/org/apache/cxf/maven_plugin/javatowadl/Java2WADLMojo.java b/maven-plugins/java2wadl-plugin/src/main/java/org/apache/cxf/maven_plugin/javatowadl/Java2WADLMojo.java new file mode 100644 index 0000000..1071c70 --- /dev/null +++ b/maven-plugins/java2wadl-plugin/src/main/java/org/apache/cxf/maven_plugin/javatowadl/Java2WADLMojo.java @@ -0,0 +1,248 @@ +/** + * 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. + */ + +package org.apache.cxf.maven_plugin.javatowadl; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; + +import org.apache.cxf.Bus; +import org.apache.cxf.BusFactory; +import org.apache.cxf.common.util.ClassHelper; +import org.apache.cxf.helpers.FileUtils; +import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider; +import org.apache.cxf.jaxrs.model.ClassResourceInfo; +import org.apache.cxf.jaxrs.model.wadl.WadlGenerator; +import org.apache.cxf.jaxrs.utils.InjectionUtils; +import org.apache.cxf.jaxrs.utils.ResourceUtils; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectHelper; + +/** + * @goal java2wadl + * @description CXF Java To WADL Tool + * @requiresDependencyResolution test + * @threadSafe +*/ +public class Java2WADLMojo extends AbstractMojo { + + public static final String WADL_NS = "http://wadl.dev.java.net/2009/02"; + + + + private List<ClassResourceInfo> classResourceInfos = new ArrayList<ClassResourceInfo>(); + + + /** + * @parameter + */ + private String outputFile; + + + + /** + * @parameter + */ + private String address; + + + + /** + * Attach the generated wadl file to the list of files to be deployed + * on install. This means the wadl file will be copied to the repository + * with groupId, artifactId and version of the project and type "wadl". + * + * With this option you can use the maven repository as a Service Repository. + * + * @parameter default-value="true" + */ + private Boolean attachWadl; + + + /** + * @parameter + */ + private String classifier; + + /** + * @parameter + * @required + */ + private List<String> classResourceNames; + + + /** + * @parameter expression="${project}" + * @required + */ + private MavenProject project; + + + /** + * Maven ProjectHelper. + * + * @component + * @readonly + */ + private MavenProjectHelper projectHelper; + + + /** + * @parameter default-value="false" + */ + private boolean useJson; + + + + public void execute() throws MojoExecutionException { + getResourcesList(); + WadlGenerator wadlGenernator = new WadlGenerator(getBus()); + StringBuilder sbMain = wadlGenernator.generateWADL(getBaseURI(), classResourceInfos, useJson, null, null); + getLog().debug("the wadl is =====> \n" + sbMain.toString()); + generateWadl(sbMain.toString()); + } + + private void generateWadl(String wadl) throws MojoExecutionException { + + if (outputFile == null && project != null) { + // Put the wadl in target/generated/wadl + String className = classResourceNames.get(0); + int i = className.lastIndexOf('.'); + String name = className.substring(i + 1); + outputFile = (project.getBuild().getDirectory() + "/generated/wadl/" + name + ".wadl") + .replace("/", File.separator); + } + + BufferedWriter writer = null; + try { + FileUtils.mkDir(new File(outputFile).getParentFile()); + /*File wadlFile = new File(outputFile); + if (!wadlFile.exists()) { + wadlFile.createNewFile(); + }*/ + writer = new BufferedWriter(new FileWriter(outputFile)); + writer.write(wadl); + + } catch (IOException e) { + throw new MojoExecutionException(e.getMessage(), e); + } finally { + try { + if (writer != null) { + writer.close(); + } + } catch (IOException e) { + throw new MojoExecutionException(e.getMessage(), e); + } + } + // Attach the generated wadl file to the artifacts that get deployed + // with the enclosing project + if (attachWadl && outputFile != null) { + File wadlFile = new File(outputFile); + if (wadlFile.exists()) { + if (classifier != null) { + projectHelper.attachArtifact(project, "wadl", classifier, wadlFile); + } else { + projectHelper.attachArtifact(project, "wadl", wadlFile); + } + + } + } + } + + private String getBaseURI() { + if (address != null) { + return address; + } else { + return "http://localhost/cxf/"; + } + } + + + + private ClassLoader getClassLoader() throws MojoExecutionException { + try { + List<?> runtimeClasspathElements = project.getRuntimeClasspathElements(); + URL[] runtimeUrls = new URL[runtimeClasspathElements.size()]; + for (int i = 0; i < runtimeClasspathElements.size(); i++) { + String element = (String)runtimeClasspathElements.get(i); + runtimeUrls[i] = new File(element).toURI().toURL(); + } + URLClassLoader newLoader = new URLClassLoader(runtimeUrls, Thread.currentThread() + .getContextClassLoader()); + return newLoader; + } catch (Exception e) { + throw new MojoExecutionException(e.getMessage(), e); + } + } + + private void getResourcesList() throws MojoExecutionException { + for (String className : classResourceNames) { + Object bean = null; + try { + bean = getClassLoader().loadClass(className).newInstance(); + } catch (Exception e) { + throw new MojoExecutionException(e.getMessage(), e); + } + Class<?> realClass = ClassHelper.getRealClass(bean); + ClassResourceInfo cri = getCreatedFromModel(realClass); + if (cri != null) { + if (!InjectionUtils.isConcreteClass(cri.getServiceClass())) { + cri = new ClassResourceInfo(cri); + classResourceInfos.add(cri); + } + cri.setResourceClass(bean.getClass()); + cri.setResourceProvider(new SingletonResourceProvider(bean)); + continue; + } + + cri = ResourceUtils.createClassResourceInfo(bean.getClass(), realClass, true, true, + getBus()); + if (cri != null) { + classResourceInfos.add(cri); + cri.setResourceProvider( + new SingletonResourceProvider(bean)); + } + } + } + + private Bus getBus() { + return BusFactory.getDefaultBus(); + } + + private ClassResourceInfo getCreatedFromModel(Class<?> realClass) { + + for (ClassResourceInfo cri : classResourceInfos) { + if (cri.isCreatedFromModel() + && cri.isRoot() && cri.getServiceClass().isAssignableFrom(realClass)) { + return cri; + } + } + return null; + } + +} + http://git-wip-us.apache.org/repos/asf/cxf/blob/567a22f2/maven-plugins/pom.xml ---------------------------------------------------------------------- diff --git a/maven-plugins/pom.xml b/maven-plugins/pom.xml index 79646f4..4f07209 100644 --- a/maven-plugins/pom.xml +++ b/maven-plugins/pom.xml @@ -36,6 +36,7 @@ <modules> <module>codegen-plugin</module> <module>java2ws-plugin</module> + <module>java2wadl-plugin</module> <module>wsdl-validator-plugin</module> <module>wadl2java-plugin</module> <module>corba</module> http://git-wip-us.apache.org/repos/asf/cxf/blob/567a22f2/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java index 68b6d5e..7807407 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java @@ -221,6 +221,17 @@ public class WadlGenerator implements RequestHandler { boolean isJson = type == MediaType.APPLICATION_JSON_TYPE; + StringBuilder sbMain = generateWADL(getBaseURI(m, ui), getResourcesList(m, resource), isJson, m, ui); + + m.getExchange().put(JAXRSUtils.IGNORE_MESSAGE_WRITERS, !isJson && ignoreMessageWriters); + return Response.ok().type(type).entity(createResponseEntity(sbMain.toString(), isJson)).build(); + } + + public StringBuilder generateWADL(String baseURI, + List<ClassResourceInfo> cris, + boolean isJson, + Message m, + UriInfo ui) { StringBuilder sbMain = new StringBuilder(); sbMain.append("<application"); if (!isJson) { @@ -231,12 +242,10 @@ public class WadlGenerator implements RequestHandler { sbGrammars.append("<grammars>"); StringBuilder sbResources = new StringBuilder(); - sbResources.append("<resources base=\"").append(getBaseURI(m, ui)).append("\">"); + sbResources.append("<resources base=\"").append(baseURI).append("\">"); - List<ClassResourceInfo> cris = getResourcesList(m, resource); - - MessageBodyWriter<?> jaxbWriter = useJaxbContextForQnames - ? ProviderFactory.getInstance(m).getRegisteredJaxbWriter() : null; + MessageBodyWriter<?> jaxbWriter = (m != null && useJaxbContextForQnames) + ? ProviderFactory.getInstance(m).getRegisteredJaxbWriter() : null; ResourceTypes resourceTypes = ResourceUtils.getAllRequestResponseTypes(cris, useJaxbContextForQnames, jaxbWriter); @@ -274,9 +283,7 @@ public class WadlGenerator implements RequestHandler { sbMain.append(sbGrammars.toString()); sbMain.append(sbResources.toString()); sbMain.append("</application>"); - - m.getExchange().put(JAXRSUtils.IGNORE_MESSAGE_WRITERS, !isJson && ignoreMessageWriters); - return Response.ok().type(type).entity(createResponseEntity(sbMain.toString(), isJson)).build(); + return sbMain; } private Object createResponseEntity(String entity, boolean isJson) {
