Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0FFBFD63D for ; Tue, 5 Mar 2013 02:28:04 +0000 (UTC) Received: (qmail 77935 invoked by uid 500); 5 Mar 2013 02:28:04 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 77882 invoked by uid 500); 5 Mar 2013 02:28:03 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 77875 invoked by uid 99); 5 Mar 2013 02:28:03 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 Mar 2013 02:28:03 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 Mar 2013 02:28:01 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id AA54223889DE; Tue, 5 Mar 2013 02:27:42 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1452640 - in /cxf/trunk: api/src/main/java/org/apache/cxf/helpers/ tools/common/src/main/java/org/apache/cxf/tools/common/ tools/common/src/main/java/org/apache/cxf/tools/util/ tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/ t... Date: Tue, 05 Mar 2013 02:27:42 -0000 To: commits@cxf.apache.org From: ema@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130305022742.AA54223889DE@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ema Date: Tue Mar 5 02:27:41 2013 New Revision: 1452640 URL: http://svn.apache.org/r1452640 Log: [CXF-4871]:add -clientjar to wsdl2java tool Modified: cxf/trunk/api/src/main/java/org/apache/cxf/helpers/FileUtils.java cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/common/ToolConstants.java cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/util/ClassCollector.java cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/Messages.properties cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/WSDLToJavaContainer.java cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/jaxws-toolspec.xml cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/service.vm cxf/trunk/tools/wsdlto/test/src/test/java/org/apache/cxf/tools/wsdlto/jaxws/CodeGenTest.java Modified: cxf/trunk/api/src/main/java/org/apache/cxf/helpers/FileUtils.java URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/helpers/FileUtils.java?rev=1452640&r1=1452639&r2=1452640&view=diff ============================================================================== --- cxf/trunk/api/src/main/java/org/apache/cxf/helpers/FileUtils.java (original) +++ cxf/trunk/api/src/main/java/org/apache/cxf/helpers/FileUtils.java Tue Mar 5 02:27:41 2013 @@ -58,38 +58,43 @@ public final class FileUtils { } } if (defaultTempDir == null) { - int x = (int)(Math.random() * 1000000); - s = SystemPropertyAction.getProperty("java.io.tmpdir"); - File checkExists = new File(s); - if (!checkExists.exists() || !checkExists.isDirectory()) { - throw new RuntimeException("The directory " - + checkExists.getAbsolutePath() - + " does not exist, please set java.io.tempdir" - + " to an existing directory"); - } - if (!checkExists.canWrite()) { - throw new RuntimeException("The directory " - + checkExists.getAbsolutePath() - + " is now writable, please set java.io.tempdir" - + " to an writable directory"); - } - File f = new File(s, "cxf-tmp-" + x); - while (!f.mkdir()) { - x = (int)(Math.random() * 1000000); - f = new File(s, "cxf-tmp-" + x); - } - defaultTempDir = f; - final File f2 = f; - Thread hook = new Thread() { - @Override - public void run() { - removeDir(f2, true); - } - }; - Runtime.getRuntime().addShutdownHook(hook); + defaultTempDir = createTmpDir(); } return defaultTempDir; } + + public static File createTmpDir() { + int x = (int)(Math.random() * 1000000); + String s = SystemPropertyAction.getProperty("java.io.tmpdir"); + File checkExists = new File(s); + if (!checkExists.exists() || !checkExists.isDirectory()) { + throw new RuntimeException("The directory " + + checkExists.getAbsolutePath() + + " does not exist, please set java.io.tempdir" + + " to an existing directory"); + } + if (!checkExists.canWrite()) { + throw new RuntimeException("The directory " + + checkExists.getAbsolutePath() + + " is now writable, please set java.io.tempdir" + + " to an writable directory"); + } + File f = new File(s, "cxf-tmp-" + x); + while (!f.mkdir()) { + x = (int)(Math.random() * 1000000); + f = new File(s, "cxf-tmp-" + x); + } + File newTmpDir = f; + final File f2 = f; + Thread hook = new Thread() { + @Override + public void run() { + removeDir(f2, true); + } + }; + Runtime.getRuntime().addShutdownHook(hook); + return newTmpDir; + } public static void mkDir(File dir) { if (dir == null) { Modified: cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/common/ToolConstants.java URL: http://svn.apache.org/viewvc/cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/common/ToolConstants.java?rev=1452640&r1=1452639&r2=1452640&view=diff ============================================================================== --- cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/common/ToolConstants.java (original) +++ cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/common/ToolConstants.java Tue Mar 5 02:27:41 2013 @@ -50,6 +50,7 @@ public final class ToolConstants { public static final String CFG_WEBSERVICE = "webservice"; public static final String CFG_SERVER = "server"; public static final String CFG_CLIENT = "client"; + public static final String CFG_CLIENT_JAR = "clientjar"; public static final String CFG_ALL = "all"; public static final String CFG_IMPL = "impl"; public static final String CFG_PACKAGENAME = "packagename"; Modified: cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/util/ClassCollector.java URL: http://svn.apache.org/viewvc/cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/util/ClassCollector.java?rev=1452640&r1=1452639&r2=1452640&view=diff ============================================================================== --- cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/util/ClassCollector.java (original) +++ cxf/trunk/tools/common/src/main/java/org/apache/cxf/tools/util/ClassCollector.java Tue Mar 5 02:27:41 2013 @@ -29,12 +29,12 @@ import java.util.TreeSet; public class ClassCollector { - private final Map seiClassNames + private Map seiClassNames = new TreeMap(String.CASE_INSENSITIVE_ORDER); - private final Map typesClassNames = new HashMap(); - private final Map exceptionClassNames = new HashMap(); - private final Map serviceClassNames = new HashMap(); - private final Map implClassNames = new HashMap(); + private Map typesClassNames = new HashMap(); + private Map exceptionClassNames = new HashMap(); + private Map serviceClassNames = new HashMap(); + private Map implClassNames = new HashMap(); private final Map clientClassNames = new HashMap(); private final Map serverClassNames = new HashMap(); private final Map reservedClassNames = new HashMap(); @@ -154,5 +154,36 @@ public class ClassCollector { generatedFileList.addAll(clientClassNames.values()); return generatedFileList; } - + + public Map getSeiClassNames() { + return seiClassNames; + } + public void setSeiClassNames(Map seiClassNames) { + this.seiClassNames = seiClassNames; + } + public Map getTypesClassNames() { + return typesClassNames; + } + public void setTypesClassNames(Map typesClassNames) { + this.typesClassNames = typesClassNames; + } + public Map getExceptionClassNames() { + return exceptionClassNames; + } + public void setExceptionClassNames(Map exceptionClassNames) { + this.exceptionClassNames = exceptionClassNames; + } + public Map getServiceClassNames() { + return serviceClassNames; + } + public void setServiceClassNames(Map serviceClassNames) { + this.serviceClassNames = serviceClassNames; + } + public Map getImplClassNames() { + return implClassNames; + } + public void setImplClassNames(Map implClassNames) { + this.implClassNames = implClassNames; + } + } Modified: cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/Messages.properties URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/Messages.properties?rev=1452640&r1=1452639&r2=1452640&view=diff ============================================================================== --- cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/Messages.properties (original) +++ cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/Messages.properties Tue Mar 5 02:27:41 2013 @@ -27,3 +27,8 @@ FOUND_NO_DATABINDING = Can not find data FOUND_NO_FRONTEND = Can not find frontend NO_WSDL_URL = WSDL URL can not be null + +FAILED_TO_CREAT_CLIENTJAR = Failed to create client jar +FAILED_ADD_JARENTRY = Failed to add jarentry into client jar file +FAILED_TO_GEN_LOCAL_WSDL = Failed to generate local wsdl for clientjar + Modified: cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/WSDLToJavaContainer.java URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/WSDLToJavaContainer.java?rev=1452640&r1=1452639&r2=1452640&view=diff ============================================================================== --- cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/WSDLToJavaContainer.java (original) +++ cxf/trunk/tools/wsdlto/core/src/main/java/org/apache/cxf/tools/wsdlto/WSDLToJavaContainer.java Tue Mar 5 02:27:41 2013 @@ -19,14 +19,22 @@ package org.apache.cxf.tools.wsdlto; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.Writer; import java.net.URI; import java.net.URL; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; @@ -34,12 +42,19 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; +import java.util.jar.JarEntry; +import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; import java.util.logging.Level; import java.util.logging.Logger; import javax.wsdl.Definition; +import javax.wsdl.factory.WSDLFactory; +import javax.wsdl.xml.WSDLWriter; import javax.xml.namespace.QName; +import org.w3c.dom.Element; + import org.apache.cxf.Bus; import org.apache.cxf.common.WSDLConstants; import org.apache.cxf.common.i18n.Message; @@ -49,6 +64,10 @@ import org.apache.cxf.common.util.String import org.apache.cxf.common.util.URIParserUtil; import org.apache.cxf.common.xmlschema.SchemaCollection; import org.apache.cxf.helpers.CastUtils; +import org.apache.cxf.helpers.DOMUtils; +import org.apache.cxf.helpers.FileUtils; +import org.apache.cxf.helpers.IOUtils; +import org.apache.cxf.helpers.XMLUtils; import org.apache.cxf.service.model.InterfaceInfo; import org.apache.cxf.service.model.ServiceInfo; import org.apache.cxf.tools.common.AbstractCXFToolContainer; @@ -64,12 +83,15 @@ import org.apache.cxf.tools.common.tools import org.apache.cxf.tools.common.toolspec.parser.CommandDocument; import org.apache.cxf.tools.common.toolspec.parser.ErrorVisitor; import org.apache.cxf.tools.util.ClassCollector; +import org.apache.cxf.tools.util.FileWriterUtil; +import org.apache.cxf.tools.util.OutputStreamCreator; import org.apache.cxf.tools.validator.ServiceValidator; import org.apache.cxf.tools.wsdlto.core.AbstractWSDLBuilder; import org.apache.cxf.tools.wsdlto.core.DataBindingProfile; import org.apache.cxf.tools.wsdlto.core.FrontEndProfile; import org.apache.cxf.wsdl.WSDLManager; import org.apache.cxf.wsdl11.WSDLServiceBuilder; +import org.apache.ws.commons.schema.XmlSchema; public class WSDLToJavaContainer extends AbstractCXFToolContainer { @@ -266,6 +288,11 @@ public class WSDLToJavaContainer extends if (context.getErrorListener().getErrorCount() > 0) { return; } + + if (context.optionSet(ToolConstants.CFG_CLIENT_JAR)) { + enforceWSDLLocation(context); + } + if (!isSuppressCodeGen()) { // Generate artifacts for (FrontEndGenerator generator : frontend.getGenerators()) { @@ -286,6 +313,91 @@ public class WSDLToJavaContainer extends throw new ToolException(e); } } + if (context.optionSet(ToolConstants.CFG_CLIENT_JAR)) { + processClientJar(context); + } + } + + private void enforceWSDLLocation(ToolContext context) { + String wsdlURL = (String)context.get(ToolConstants.CFG_WSDLURL); + @SuppressWarnings("unchecked") + List serviceList = (List)context.get(ToolConstants.SERVICE_LIST); + int slashIndex = wsdlURL.lastIndexOf("/"); + int dotIndex = wsdlURL.indexOf(".", slashIndex); + String wsdlLocation = null; + if (slashIndex > -1 && dotIndex > -1) { + wsdlLocation = wsdlURL.substring(slashIndex + 1, dotIndex) + ".wsdl"; + } + if (wsdlLocation == null) { + wsdlLocation = serviceList.get(0).getName().getLocalPart() + ".wsdl"; + } + context.put(ToolConstants.CFG_WSDLLOCATION, wsdlLocation); + } + + private void processClientJar(ToolContext context) { + ClassCollector oldCollector = context.get(ClassCollector.class); + ClassCollector newCollector = new ClassCollector(); + String oldClassDir = (String)context.get(ToolConstants.CFG_CLASSDIR); + File tmpDir = FileUtils.createTmpDir(); + context.put(ToolConstants.CFG_CLASSDIR, tmpDir.getAbsolutePath()); + + newCollector.setTypesClassNames(oldCollector.getTypesClassNames()); + newCollector.setSeiClassNames(oldCollector.getSeiClassNames()); + newCollector.setExceptionClassNames(oldCollector.getExceptionClassNames()); + newCollector.setServiceClassNames(oldCollector.getServiceClassNames()); + context.put(ClassCollector.class, newCollector); + new ClassUtils().compile(context); + + generateLocalWSDL(context); + + + File clientJarFile = new File((String)context.get(ToolConstants.CFG_OUTPUTDIR), + (String)context.get(ToolConstants.CFG_CLIENT_JAR)); + JarOutputStream jarout = null; + try { + jarout = new JarOutputStream(new FileOutputStream(clientJarFile), new Manifest()); + createClientJar(tmpDir, jarout); + jarout.close(); + } catch (Exception e) { + LOG.log(Level.SEVERE, "FAILED_TO_CREAT_CLIENTJAR", e); + Message msg = new Message("FAILED_TO_CREAT_CLIENTJAR", LOG); + throw new ToolException(msg, e); + } + context.put(ToolConstants.CFG_CLASSDIR, oldClassDir); + context.put(ClassCollector.class, oldCollector); + } + + private void createClientJar(File tmpDirectory, JarOutputStream jarout) { + try { + URI parentFile = new URI((String)context.get(ToolConstants.CFG_CLASSDIR)); + for (File file : tmpDirectory.listFiles()) { + URI relativePath = parentFile.relativize(new URI(file.getPath())); + String name = relativePath.toString().replace("\\", "/"); + if (file.isDirectory()) { + if (!name.isEmpty()) { + if (!name.endsWith("/")) { + name += "/"; + } + JarEntry entry = new JarEntry(name); + entry.setTime(file.lastModified()); + jarout.putNextEntry(entry); + jarout.closeEntry(); + } + createClientJar(file, jarout); + continue; + } + JarEntry entry = new JarEntry(name); + entry.setTime(file.lastModified()); + jarout.putNextEntry(entry); + InputStream input = new BufferedInputStream(new FileInputStream(file)); + IOUtils.copy(input, jarout); + input.close(); + jarout.closeEntry(); + } + } catch (Exception e) { + Message msg = new Message("FAILED_ADD_JARENTRY", LOG); + throw new ToolException(msg, e); + } } private boolean isSuppressCodeGen() { @@ -646,4 +758,121 @@ public class WSDLToJavaContainer extends } return validators; } + + @SuppressWarnings("unchecked") + private void generateLocalWSDL(ToolContext context) { + String outputdir = (String)context.get(ToolConstants.CFG_CLASSDIR); + File wsdlFile = new File(outputdir, (String)context.get(ToolConstants.CFG_WSDLLOCATION)); + Definition def = context.get(Definition.class); + try { + //get imported schemas + int xsdCount = 0; + SchemaCollection schemas = (SchemaCollection) context.get(ToolConstants.XML_SCHEMA_COLLECTION); + Map sourceMap = new HashMap(); + for (XmlSchema imp : schemas.getXmlSchemas()) { + if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) { + String schemaFileName = "schema" + (++xsdCount) + ".xsd"; + sourceMap.put(imp.getTargetNamespace(), schemaFileName); + } + } + + //get imported wsdls + List defs = (List)context.get(ToolConstants.IMPORTED_DEFINITION); + Map importWSDLMap = new HashMap(); + for (Definition importDef : defs) { + File importedWsdlFile = null; + if (!StringUtils.isEmpty(importDef.getDocumentBaseURI())) { + importedWsdlFile = new File(importDef.getDocumentBaseURI()); + } else { + importedWsdlFile = new File(importDef.getQName().getLocalPart() + ".wsdl"); + } + importWSDLMap.put(importDef.getTargetNamespace(), importedWsdlFile.getName()); + } + + + OutputStreamCreator outputStreamCreator = null; + if (context.get(OutputStreamCreator.class) != null) { + outputStreamCreator = context.get(OutputStreamCreator.class); + } else { + outputStreamCreator = new OutputStreamCreator(); + context.put(OutputStreamCreator.class, outputStreamCreator); + } + Writer os = null; + + + + for (XmlSchema imp : schemas.getXmlSchemas()) { + if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) { + String schemaFileName = sourceMap.get(imp.getTargetNamespace()); + File impfile = new File(wsdlFile.getParentFile(), schemaFileName); + Element el = imp.getSchemaDocument().getDocumentElement(); + updateImports(el, sourceMap); + os = new FileWriterUtil(impfile.getParent(), context.get(OutputStreamCreator.class)) + .getWriter(impfile, "UTF-8"); + XMLUtils.writeTo(el, os, 2); + os.close(); + } + } + + //change the import location in wsdl file + OutputStream wsdloutput = new BufferedOutputStream(new FileOutputStream(wsdlFile)); + WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter(); + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + wsdlWriter.writeWSDL(def, bout); + Element defEle = XMLUtils.parse(bout.toByteArray()).getDocumentElement(); + List xsdElements = DOMUtils.findAllElementsByTagNameNS(defEle, + WSDLConstants.NS_SCHEMA_XSD, + "schema"); + for (Element xsdEle : xsdElements) { + updateImports(xsdEle, sourceMap); + } + updateWSDLImports(defEle, importWSDLMap); + DOMUtils.writeXml(defEle, wsdloutput); + wsdloutput.close(); + + + for (Definition importDef : defs) { + File importWsdlFile = new File(outputdir, importWSDLMap.get(importDef.getTargetNamespace())); + OutputStream wsdlOs = new BufferedOutputStream(new FileOutputStream(importWsdlFile)); + bout = new ByteArrayOutputStream(); + wsdlWriter.writeWSDL(importDef, bout); + Element importEle = XMLUtils.parse(bout.toByteArray()).getDocumentElement(); + + xsdElements = DOMUtils.findAllElementsByTagNameNS(importEle, WSDLConstants.NS_SCHEMA_XSD, + "schema"); + for (Element xsdEle : xsdElements) { + updateImports(xsdEle, sourceMap); + } + updateWSDLImports(importEle, importWSDLMap); + DOMUtils.writeXml(importEle, wsdlOs); + wsdlOs.close(); + + } + } catch (Exception ex) { + LOG.log(Level.SEVERE, "FAILED_TO_GEN_LOCAL_WSDL", ex); + Message msg = new Message("FAILED_TO_GEN_LOCAL_WSDL", LOG); + throw new ToolException(msg, ex); + } + } + + private void updateImports(Element el, Map sourceMap) { + List imps = DOMUtils.getChildrenWithName(el, + WSDLConstants.NS_SCHEMA_XSD, + "import"); + for (Element e : imps) { + String ns = e.getAttribute("namespace"); + e.setAttribute("schemaLocation", sourceMap.get(ns)); + + } + } + + private void updateWSDLImports(Element el, Map wsdlSourceMap) { + List imps = DOMUtils.getChildrenWithName(el, + WSDLConstants.QNAME_IMPORT.getNamespaceURI(), + "import"); + for (Element e : imps) { + String ns = e.getAttribute("namespace"); + e.setAttribute("location", wsdlSourceMap.get(ns)); + } + } } Modified: cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/jaxws-toolspec.xml URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/jaxws-toolspec.xml?rev=1452640&r1=1452639&r2=1452640&view=diff ============================================================================== --- cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/jaxws-toolspec.xml (original) +++ cxf/trunk/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/jaxws-toolspec.xml Tue Mar 5 02:27:41 2013 @@ -166,6 +166,16 @@ Examples: client + +