Juan Hernandez has uploaded a new change for review.

Change subject: codegen: Use explicit version of XJC
......................................................................

codegen: Use explicit version of XJC

The code generator uses the XJC compiler included in the JDK used to run
it. This means that the version of the compiler may be different and it
may produce different results. This is in particular true for the
versions of the XJC compiler included with Java 7 and Java 8. The
changes from version to version aren't usually problematic, but anyhow
they make it difficult to compare versions of the generated code,
because there is no certainty that the differences are real or just
quirks of the XJC compiler. To reduce the chances of this happening this
patch replaces the call to the external "xjc" command with the call to
the XJC class of a specific version managed by Maven.

Change-Id: I26984881cb68b5dca2a12d5976df80253c65abb1
Signed-off-by: Juan Hernandez <[email protected]>
---
M ovirt-engine-sdk-java-codegen/pom.xml
M 
ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/Main.java
D 
ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/common/AbstractCodegen.java
D 
ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/common/ICodegen.java
M 
ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/ApiCodegen.java
M 
ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/RsdlCodegen.java
M 
ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/AbstractTemplate.java
M 
ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/VariableTemplate
A 
ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/utils/ClassUtils.java
D 
ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/utils/OsUtil.java
M 
ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/xsd/XsdCodegen.java
11 files changed, 136 insertions(+), 417 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-sdk-java 
refs/changes/81/37781/1

diff --git a/ovirt-engine-sdk-java-codegen/pom.xml 
b/ovirt-engine-sdk-java-codegen/pom.xml
index 99d5e13..f4341f3 100644
--- a/ovirt-engine-sdk-java-codegen/pom.xml
+++ b/ovirt-engine-sdk-java-codegen/pom.xml
@@ -31,14 +31,23 @@
          the engine artifacts: -->
     <engine.version>3.6.0-SNAPSHOT</engine.version>
 
+    <!-- The version of the JAXB XJC compiler to use: -->
+    <jaxb.version>2.2.4-1</jaxb.version>
+
   </properties>
 
   <dependencies>
 
     <dependency>
-      <groupId>com.sun.xsom</groupId>
-      <artifactId>xsom</artifactId>
-      <version>20110809</version>
+      <groupId>com.sun.xml.bind</groupId>
+      <artifactId>jaxb-xjc</artifactId>
+      <version>${jaxb.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>com.sun.xml.bind</groupId>
+      <artifactId>jaxb-impl</artifactId>
+      <version>${jaxb.version}</version>
     </dependency>
 
   </dependencies>
diff --git 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/Main.java
 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/Main.java
index 4e86e90..29c8d57 100644
--- 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/Main.java
+++ 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/Main.java
@@ -16,6 +16,7 @@
 
 package org.ovirt.engine.sdk.codegen;
 
+import java.io.File;
 import java.io.IOException;
 
 import javax.xml.bind.JAXBException;
@@ -27,6 +28,8 @@
  * oVirt ovirt-engine-sdk-java codegen suite
  */
 public class Main {
+    private static final String DIST_PATH = 
"../ovirt-engine-sdk-java/src/main/java";
+
     public static void main(String[] args) throws IOException, JAXBException {
         // Parse the command line parameters:
         String xsdPath = null;
@@ -62,11 +65,14 @@
             System.exit(1);
         }
 
+        // Adjust the destination path to the platform:
+        String distPath = DIST_PATH.replace('/', File.separatorChar);
+
         // #1 - generate api entities from the XSD schema
-        new XsdCodegen(xsdPath, xjbPath).generate();
+        new XsdCodegen(xsdPath, xjbPath).generate(distPath);
 
         // #2 - generate api entities decorators by RSDL and SDK entry point
-        new RsdlCodegen(xsdPath, rsdlPath).generate();
+        new RsdlCodegen(xsdPath, rsdlPath).generate(distPath);
 
         // #3 - exit
         System.exit(0);
diff --git 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/common/AbstractCodegen.java
 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/common/AbstractCodegen.java
deleted file mode 100644
index 27cfe5c..0000000
--- 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/common/AbstractCodegen.java
+++ /dev/null
@@ -1,89 +0,0 @@
-//
-// Copyright (c) 2012 Red Hat, Inc.
-//
-// Licensed 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.ovirt.engine.sdk.codegen.common;
-
-import java.io.IOException;
-
-import javax.xml.bind.JAXBException;
-
-import org.ovirt.engine.sdk.codegen.utils.FileUtils;
-
-/**
- * Base class for CodeGen generators
- */
-public abstract class AbstractCodegen implements ICodegen {
-
-    private final String distPath;
-
-    /**
-     * @param distPath
-     *            path to generate the code in
-     */
-    public AbstractCodegen(String distPath) {
-        super();
-        this.distPath = distPath;
-    }
-
-    /**
-     * Cleans the package, generates new code and compiles it
-     * 
-     * @throws IOException
-     * @throws JAXBException
-     */
-    @Override
-    public void generate() throws IOException, JAXBException {
-        doCleanPackage(this.distPath);
-        doGenerate(this.distPath);
-    }
-
-    /**
-     * Generates the code
-     * 
-     * @param distPath
-     *            directory to generates the code at
-     * 
-     * @throws IOException
-     * @throws JAXBException
-     */
-    protected abstract void doGenerate(String distPath) throws IOException, 
JAXBException;
-
-    /**
-     * Delete all files in given directory
-     * 
-     * @param dir
-     *            directory to clean
-     */
-    protected void doCleanPackage(String dir) {
-        FileUtils.deleteAllFiles(dir);
-    }
-
-    /**
-     * Persist Java class
-     * 
-     * @param name
-     *            class name (without suffix/extention)
-     * @param content
-     *            class content
-     * 
-     * @param outDir
-     *            directory to write the file to
-     */
-    protected void persistClass(String name, String content, String outDir) {
-        String fileName = outDir + name + ".java";
-        FileUtils.saveFile(fileName, content);
-    }
-}
diff --git 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/common/ICodegen.java
 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/common/ICodegen.java
deleted file mode 100644
index a5489bd..0000000
--- 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/common/ICodegen.java
+++ /dev/null
@@ -1,31 +0,0 @@
-//
-// Copyright (c) 2012 Red Hat, Inc.
-//
-// Licensed 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.ovirt.engine.sdk.codegen.common;
-
-import java.io.IOException;
-
-import javax.xml.bind.JAXBException;
-
-public interface ICodegen {
-    /**
-     * Cleans the package and generates the code
-     * 
-     * @throws IOException
-     * @throws JAXBException
-     */
-    abstract void generate() throws IOException, JAXBException;
-}
diff --git 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/ApiCodegen.java
 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/ApiCodegen.java
index 3c97cf4..e9ad68f 100644
--- 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/ApiCodegen.java
+++ 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/ApiCodegen.java
@@ -24,9 +24,6 @@
 import java.util.List;
 import java.util.Map;
 
-import javax.xml.bind.JAXBException;
-
-import org.ovirt.engine.sdk.codegen.common.AbstractCodegen;
 import org.ovirt.engine.sdk.codegen.holders.CollectionHolder;
 import org.ovirt.engine.sdk.codegen.templates.ApiTemplate;
 import org.ovirt.engine.sdk.codegen.templates.CollectionGetterTemplate;
@@ -34,20 +31,15 @@
 import org.ovirt.engine.sdk.codegen.templates.RootResourceStaticTemplate;
 import org.ovirt.engine.sdk.codegen.templates.VariableTemplate;
 import org.ovirt.engine.sdk.codegen.utils.ArrayUtils;
-import org.ovirt.engine.sdk.codegen.utils.OsUtil;
+import org.ovirt.engine.sdk.codegen.utils.ClassUtils;
 import org.ovirt.engine.sdk.codegen.utils.StringUtils;
 import org.ovirt.engine.sdk.entities.API;
 
 /**
  * Provides SDK entry point codegen capabilities
  */
-public class ApiCodegen extends AbstractCodegen {
-
-    private static final String NX_API_PATH =
-            "../ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/";
-    private static final String WINDOWS_API_PATH =
-            
"..\\ovirt-engine-sdk-java\\src\\main\\java\\org\\ovirt\\engine\\sdk\\";
-    private static final String EP_NAME = "Api";
+public class ApiCodegen {
+    private static final String API_CLASS = "org.ovirt.engine.sdk.Api";
 
     private ApiTemplate apiTemplate;
     private Map<String, CollectionHolder> collectionsHolder;
@@ -58,15 +50,9 @@
     private RootResourceStaticTemplate rootResourceStaticTemplate;
     private RootResourceDynamicTemplate rootResourceDynamicTemplate;
 
-    /**
-     * @param collectionsHolder
-     * @param variableTemplate
-     * @param collectionGetterTemplate
-     */
     public ApiCodegen(Map<String, CollectionHolder> collectionsHolder,
             VariableTemplate variableTemplate,
             CollectionGetterTemplate collectionGetterTemplate) {
-        super(getApiPath());
         this.apiTemplate = new ApiTemplate();
         this.rootResourceStaticTemplate = new RootResourceStaticTemplate();
         this.rootResourceDynamicTemplate = new RootResourceDynamicTemplate();
@@ -77,33 +63,15 @@
     }
 
     /**
-     * @return Path to generate SDK entry point at
+     * Generates SDK entry point class.
      */
-    private static String getApiPath() {
-        if (OsUtil.isWindows()) {
-            return WINDOWS_API_PATH;
-        } else if (OsUtil.isMac() || OsUtil.isUnix() || OsUtil.isSolaris()) {
-            return NX_API_PATH;
-        } else {
-            throw new RuntimeException("unsupported OS.");
-        }
-    }
-
-    /**
-     * Generates SDK entry point class
-     */
-    @Override
-    protected void doGenerate(String distPath) throws IOException, 
JAXBException {
+    protected void generate(String distPath) throws IOException {
         String collectionsVariables = produceCollectionVariables();
         String collectionsGetters = produceCollectionGetters();
         String rootMethods = produceRootMethods();
 
-        persistClass(EP_NAME,
-                this.apiTemplate.getTemplate(collectionsVariables,
-                        collectionsGetters,
-                        rootMethods),
-                distPath);
-
+        String apiCode = apiTemplate.getTemplate(collectionsVariables, 
collectionsGetters, rootMethods);
+        ClassUtils.saveClass(distPath, API_CLASS, apiCode);
     }
 
     /**
@@ -179,14 +147,5 @@
             buffer.append(variableTemplate.getTemplate(name, 
StringUtils.toLowerCase(name)));
         }
         return buffer.toString();
-    }
-
-    /**
-     * Cleans destionation path
-     */
-    @Override
-    protected void doCleanPackage(String dir) {
-        ;
-        // Do not clean /sdk package
     }
 }
diff --git 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/RsdlCodegen.java
 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/RsdlCodegen.java
index 6e60e00..a63fed8 100644
--- 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/RsdlCodegen.java
+++ 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/RsdlCodegen.java
@@ -35,7 +35,6 @@
 import com.sun.xml.xsom.XSComplexType;
 import com.sun.xml.xsom.XSSchemaSet;
 import com.sun.xml.xsom.parser.XSOMParser;
-import org.ovirt.engine.sdk.codegen.common.AbstractCodegen;
 import org.ovirt.engine.sdk.codegen.documentation.DocsGen;
 import org.ovirt.engine.sdk.codegen.holders.CollectionHolder;
 import org.ovirt.engine.sdk.codegen.holders.ResourceHolder;
@@ -54,7 +53,8 @@
 import org.ovirt.engine.sdk.codegen.templates.SubResourceTemplate;
 import org.ovirt.engine.sdk.codegen.templates.UpdateMethodTemplate;
 import org.ovirt.engine.sdk.codegen.templates.VariableTemplate;
-import org.ovirt.engine.sdk.codegen.utils.OsUtil;
+import org.ovirt.engine.sdk.codegen.utils.ClassUtils;
+import org.ovirt.engine.sdk.codegen.utils.FileUtils;
 import org.ovirt.engine.sdk.codegen.xsd.XsdCodegen;
 import org.ovirt.engine.sdk.entities.Capabilities;
 import org.ovirt.engine.sdk.entities.DetailedLink;
@@ -67,7 +67,7 @@
 /**
  * Provides RSDL related codegen capabilities
  */
-public class RsdlCodegen extends AbstractCodegen {
+public class RsdlCodegen {
     /**
      * The location of the XSD file.
      */
@@ -104,10 +104,8 @@
     private Map<String, CollectionHolder> collectionsHolder;
     private Map<String, ResourceHolder> resourcesHolder;
 
-    private static final String NX_DECORATORS_PATH =
-            
"../ovirt-engine-sdk-java/src/main/java/org/ovirt/engine/sdk/decorators/";
-    private static final String WINDOWS_DECORATORS_PATH =
-            
"..\\ovirt-engine-sdk-java\\src\\main\\java\\org\\ovirt\\engine\\sdk\\decorators\\";
+    private static final String DECORATORS_PACKAGE = 
"org.ovirt.engine.sdk.decorators";
+
     private static final String SLASH = "/";
     private static final String DELETE_REL = "delete";
     private static final String UPDATE_REL = "update";
@@ -203,10 +201,10 @@
     /**
      * Create a code generator for the decorator classes.
      *
+     * @param xsdPath the path of the file containing the XML schema
      * @param rsdlPath the path of the file containing the RSDL document
      */
     public RsdlCodegen(String xsdPath, String rsdlPath) {
-        super(getDecoratorsPath());
         this.xsdPath = xsdPath;
         this.rsdlPath = rsdlPath;
 
@@ -290,9 +288,11 @@
      * @throws IOException
      * @throws JAXBException
      */
-    @SuppressWarnings("unused")
-    @Override
-    protected void doGenerate(String distPath) throws IOException, 
JAXBException {
+    public void generate(String distPath) throws IOException, JAXBException {
+        // Remove all the previously generate classes, so that classes 
corresponding to types that have been
+        // removed from the XML schema will be later removed from the source 
code repository:
+        String packagePath = distPath + File.separatorChar + 
DECORATORS_PACKAGE.replace('.', File.separatorChar);
+        FileUtils.deleteAllFiles(packagePath);
 
         String url, rel, requestBodyType, responseBodyType, parent, 
collectionName, actualReturnType;
         HttpMethod requestMethod;
@@ -447,11 +447,11 @@
         persistContent(distPath);
 
         // #4 - generate SDK entry point
-        new ApiCodegen(collectionsHolder, variableTemplate, 
collectionGetterTemplate).generate();
+        new ApiCodegen(collectionsHolder, variableTemplate, 
collectionGetterTemplate).generate(distPath);
 
         // #5 - remove collection getters/setters from the public entities
         // (as they being shadowed by the decorators getters)
-        XsdCodegen.modifyGetters(getPublicAccessors());
+        XsdCodegen.modifyGetters(distPath, getPublicAccessors());
     }
 
     /**
@@ -976,12 +976,12 @@
      */
     private void persistContent(String distPath) {
         for (CollectionHolder collection : this.collectionsHolder.values()) {
-            persistClass(collection.getName(), collection.produce(), distPath);
+            ClassUtils.saveClass(distPath, DECORATORS_PACKAGE + "." 
+collection.getName(), collection.produce());
         }
         for (ResourceHolder resource : this.resourcesHolder.values()) {
-            persistClass(resource.getName(), resource.produce(), distPath);
+            ClassUtils.saveClass(distPath, DECORATORS_PACKAGE + "." + 
resource.getName(), resource.produce());
             for (CollectionHolder subCollection : 
resource.getSubcollections().values()) {
-                persistClass(subCollection.getName(), subCollection.produce(), 
distPath);
+                ClassUtils.saveClass(distPath, DECORATORS_PACKAGE + "." + 
subCollection.getName(), subCollection.produce());
             }
         }
     }
@@ -1047,19 +1047,6 @@
             if (!throwError)
                 return null;
             throw rte;
-        }
-    }
-
-    /**
-     * @return Decorators path according to OS
-     */
-    private static String getDecoratorsPath() {
-        if (OsUtil.isWindows()) {
-            return WINDOWS_DECORATORS_PATH;
-        } else if (OsUtil.isMac() || OsUtil.isUnix() || OsUtil.isSolaris()) {
-            return NX_DECORATORS_PATH;
-        } else {
-            throw new RuntimeException("unsupported OS.");
         }
     }
 
diff --git 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/AbstractTemplate.java
 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/AbstractTemplate.java
index f4df2ed..dda3196 100644
--- 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/AbstractTemplate.java
+++ 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/AbstractTemplate.java
@@ -16,10 +16,9 @@
 
 package org.ovirt.engine.sdk.codegen.templates;
 
-import java.io.FileNotFoundException;
-
-import org.ovirt.engine.sdk.codegen.utils.FileUtils;
-import org.ovirt.engine.sdk.codegen.utils.OsUtil;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 
 /**
  * Base Template class
@@ -29,11 +28,6 @@
     private String name;
     private String template;
     private String copyrightTemplate;
-
-    private static final String NX_TEMPLATES_PATH =
-            "/src/main/java/org/ovirt/engine/sdk/codegen/templates/";
-    private static final String WINDOWS_TEMPLATES_PATH =
-            "\\src\\main\\java\\org\\ovirt\\engine\\sdk\\codegen\\templates\\";
 
     /**
      * Generic .ctr
@@ -65,40 +59,22 @@
      */
     @Override
     public String loadTemplate() {
-        try {
-            return readFileTemplate();
-        } catch (FileNotFoundException e) {
-            // TODO: Log error
-            e.printStackTrace();
-            throw new RuntimeException("Template \"" + getName() + "\" not 
found.", e);
+        try (InputStream in = this.getClass().getResourceAsStream(name)) {
+            if (in == null) {
+                throw new RuntimeException("Template \"" + name + "\" not 
found.");
+            }
+            try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+                byte[] buffer = new byte[1024];
+                int count;
+                while ((count = in.read(buffer)) != -1) {
+                    out.write(buffer, 0, count);
+                }
+                return new String(out.toByteArray(), "UTF-8");
+            }
         }
-    }
-
-    /**
-     * Reads actual template file
-     * 
-     * @return template content
-     * 
-     * @throws FileNotFoundException
-     */
-    private String readFileTemplate() throws FileNotFoundException {
-        return FileUtils.getFileContent(getTemplatePath() + getName());
-    }
-
-    /**
-     * @return this template path
-     */
-    private String getTemplatePath() {
-        String path;
-
-        if (OsUtil.isWindows()) {
-            path = System.getProperty("user.dir") + WINDOWS_TEMPLATES_PATH;
-        } else if (OsUtil.isMac() || OsUtil.isUnix() || OsUtil.isSolaris()) {
-            path = System.getProperty("user.dir") + NX_TEMPLATES_PATH;
-        } else {
-            throw new RuntimeException("unsupported OS.");
+        catch (IOException exception) {
+            throw new RuntimeException("Error loading template \"" + name + 
"\".", exception);
         }
-        return path;
     }
 
     /**
diff --git 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/VariableTemplate
 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/VariableTemplate
index 5278fb6..6a206f2 100644
--- 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/VariableTemplate
+++ 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/templates/VariableTemplate
@@ -1 +1 @@
-    private volatile $type$ $name$;
\ No newline at end of file
+    private volatile $type$ $name$;
diff --git 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/utils/ClassUtils.java
 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/utils/ClassUtils.java
new file mode 100644
index 0000000..57ae57c
--- /dev/null
+++ 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/utils/ClassUtils.java
@@ -0,0 +1,42 @@
+//
+// Copyright (c) 2012 Red Hat, Inc.
+//
+// Licensed 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.ovirt.engine.sdk.codegen.utils;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Scanner;
+
+/**
+ * Utilities for handling class files.
+ */
+public class ClassUtils {
+    /**
+     * Saves the code of a class file.
+     *
+     * @param distPath the top level directory of the java sources
+     * @param className the fully qualified name of the class
+     * @param classCode the source code of the class
+     */
+    public static void saveClass(String distPath, String className, String 
classCode) {
+        String classPath = distPath + File.separator + className.replace('.', 
File.separatorChar) + ".java";
+        FileUtils.saveFile(classPath, classCode);
+    }
+}
diff --git 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/utils/OsUtil.java
 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/utils/OsUtil.java
deleted file mode 100644
index 19bb8bb..0000000
--- 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/utils/OsUtil.java
+++ /dev/null
@@ -1,38 +0,0 @@
-//
-// Copyright (c) 2012 Red Hat, Inc.
-//
-// Licensed 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.ovirt.engine.sdk.codegen.utils;
-
-public class OsUtil {
-
-    private static final String OS = 
System.getProperty("os.name").toLowerCase();
-
-    public static boolean isWindows() {
-        return (OS.indexOf("win") >= 0);
-    }
-
-    public static boolean isMac() {
-        return (OS.indexOf("mac") >= 0);
-    }
-
-    public static boolean isUnix() {
-        return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || 
OS.indexOf("aix") > 0);
-    }
-
-    public static boolean isSolaris() {
-        return (OS.indexOf("sunos") >= 0);
-    }
-}
diff --git 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/xsd/XsdCodegen.java
 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/xsd/XsdCodegen.java
index 1314c30..9ea3f04 100644
--- 
a/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/xsd/XsdCodegen.java
+++ 
b/ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/xsd/XsdCodegen.java
@@ -16,35 +16,21 @@
 
 package org.ovirt.engine.sdk.codegen.xsd;
 
-import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
-import javax.xml.bind.JAXBException;
-
-import org.ovirt.engine.sdk.codegen.common.AbstractCodegen;
 import org.ovirt.engine.sdk.codegen.templates.CopyrightTemplate;
 import org.ovirt.engine.sdk.codegen.utils.FileUtils;
-import org.ovirt.engine.sdk.codegen.utils.OsUtil;
 
 /**
  * Provides XSD schema related services
  */
-public class XsdCodegen extends AbstractCodegen {
-
-    private static final String WINDOWS_XJC_PATH = "%java_home%\\bin\\xjc";
-    private static final String NX_XJC_PATH = "xjc";
-
-    private static final String WINDOWS_ENTITIES_PATH = 
"..\\ovirt-engine-sdk-java\\src\\main\\java\\";
-    private static final String NX_ENTITIES_PATH = 
"../ovirt-engine-sdk-java/src/main/java/";
-
+public class XsdCodegen {
     private static final String ENTITIES_PACKAGE = 
"org.ovirt.engine.sdk.entities";
-    private static final String XJC_FLAGS = " -extension -no-header 
-enableIntrospection ";
 
     private static final String copyrightTemplate = new 
CopyrightTemplate().getTemplate();
 
@@ -59,135 +45,51 @@
     private String xjbPath;
 
     public XsdCodegen(String xsdPath, String xjbPath) {
-        super(getEntitiesPath());
         this.xsdPath = xsdPath;
         this.xjbPath = xjbPath;
     }
 
     /**
-     * Generates entities classes
+     * Generates entity classes.
      *
-     * @param distPath
-     *            directory to generates the code at
-     *
-     * @throws IOException
-     * @throws JAXBException
+     * @param distPath directory to generates the code at
      */
-    @Override
-    public void doGenerate(String distPath) throws IOException, JAXBException {
+    public void generate(String distPath) throws IOException {
+        // Remove all the previously generate classes, so that classes 
corresponding to types that have been
+        // removed from the XML schema will be later removed from the source 
code repository:
+        String packagePath = distPath + File.separatorChar + 
ENTITIES_PACKAGE.replace('.', File.separatorChar);
+        FileUtils.deleteAllFiles(packagePath);
 
-        String xjcOutput = null;
-
-        if (OsUtil.isWindows()) {
-            xjcOutput = runCommand(WINDOWS_XJC_PATH + " -d " + distPath +
-                    " -p " + ENTITIES_PACKAGE + XJC_FLAGS + xsdPath + " -b " + 
xjbPath);
-        } else if (OsUtil.isMac() || OsUtil.isUnix() || OsUtil.isSolaris()) {
-            xjcOutput = runCommand(NX_XJC_PATH + " -d " + distPath +
-                    " -p " + ENTITIES_PACKAGE + XJC_FLAGS + xsdPath + " -b " + 
xjbPath);
-        } else {
-            throw new RuntimeException("unsupported OS.");
+        // Run the XJC compiler to generate all the classes:
+        System.setProperty("javax.xml.accessExternalSchema", "all");
+        try {
+            com.sun.tools.xjc.Driver.run(
+                new String[] {
+                    "-extension",
+                    "-no-header",
+                    "-enableIntrospection",
+                    "-d", distPath,
+                    "-p", ENTITIES_PACKAGE,
+                    "-b", xjbPath,
+                    xsdPath
+                },
+                System.out,
+                System.err
+            );
         }
-
-        if (xjcOutput == null ||
-                !xjcOutput.startsWith("parsing a schema...compiling a 
schema...")) {
-            throw new RuntimeException("xjc codegen failed: " + xjcOutput);
+        catch (Exception exception) {
+            throw new IOException(exception);
         }
     }
 
     /**
-     * Runs system command
+     * Modifies public getters to return {@code Object} so inheriting classes 
could override them with different
+     * signature.
      *
-     * @param command
-     *            command to run
-     *
-     * @return command output
-     *
-     * @throws IOException
+     * @param distPath the top level directory of the source code
+     * @param accessors a list of possible getter types
      */
-    private String runCommand(String command) throws IOException {
-        String stdout = "";
-        String stderr = "";
-        String s = null;
-
-        Process p = Runtime.getRuntime().exec(command);
-
-        BufferedReader stdInput = new BufferedReader(new
-                InputStreamReader(p.getInputStream()));
-
-        BufferedReader stdError = new BufferedReader(new
-                InputStreamReader(p.getErrorStream()));
-
-        while ((s = stdInput.readLine()) != null) {
-            stdout += s;
-        }
-
-        while ((s = stdError.readLine()) != null) {
-            stderr += s;
-        }
-
-        if (!stderr.equals(""))
-            throw new RuntimeException(stderr);
-
-        return stdout;
-    }
-
-    /**
-     * Deletes all entities files
-     *
-     * @param dir
-     *            directory to clean
-     */
-    @Override
-    public void doCleanPackage(String dir) {
-        FileUtils.deleteAllFiles(dir);
-    }
-
-    /**
-     * @return Entities path according to OS
-     */
-    private static String getEntitiesPath() {
-        if (OsUtil.isWindows()) {
-            return WINDOWS_ENTITIES_PATH;
-        } else if (OsUtil.isMac() || OsUtil.isUnix() || OsUtil.isSolaris()) {
-            return NX_ENTITIES_PATH;
-        } else {
-            throw new RuntimeException("unsupported OS.");
-        }
-    }
-
-    /**
-     * @return Entities absolete path according to OS
-     */
-    private static String getAbsoleteEntitiesPath() {
-        if (OsUtil.isWindows()) {
-            return WINDOWS_ENTITIES_PATH + 
"org\\ovirt\\engine\\sdk\\entities\\";
-        } else if (OsUtil.isMac() || OsUtil.isUnix() || OsUtil.isSolaris()) {
-            return NX_ENTITIES_PATH + "org/ovirt/engine/sdk/entities/";
-        } else {
-            throw new RuntimeException("unsupported OS.");
-        }
-    }
-
-    /**
-     * Modifies public getters to return Object so inheriting classes could 
override them with different signature
-     *
-     * @param accessors
-     *            a list of possible getter types
-     */
-    public static void modifyGetters(Map<String, List<String>> accessors) {
-        String path = getAbsoleteEntitiesPath();
-        processFiles(accessors, path);
-    }
-
-    /**
-     * Modifies public getters (decorators shadows public getters with own 
ones)
-     *
-     * @param accessors
-     *            accessors to be removed (get/set/is)
-     * @param path
-     *            directory to loook at
-     */
-    private static void processFiles(Map<String, List<String>> accessors, 
String path) {
+    public static void modifyGetters(String distPath, Map<String, 
List<String>> accessors) {
         StringBuffer finalContent, tempContent = new StringBuffer();
         List<String> accessorsToCheck;
         String templateOriginal = "    public $accessor$ get$accessor$() {" + 
"\n";
@@ -196,7 +98,8 @@
         boolean isInAccessor = false;
 
         // change shadowed getters
-        for (File file : FileUtils.list(path)) {
+        String entitiesPath = distPath + File.separator + 
ENTITIES_PACKAGE.replace('.', File.separatorChar);
+        for (File file : FileUtils.list(entitiesPath)) {
             finalContent = new StringBuffer();
             finalContent.append(copyrightTemplate);
             tempContent = new StringBuffer();
@@ -268,10 +171,5 @@
             // TODO: Log error
             e.printStackTrace();
         }
-    }
-
-    public static void main(String[] args) throws JAXBException, IOException {
-        XsdCodegen generator = new XsdCodegen(args[0], args[1]);
-        generator.generate();
     }
 }


-- 
To view, visit http://gerrit.ovirt.org/37781
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I26984881cb68b5dca2a12d5976df80253c65abb1
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-sdk-java
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to