Juan Hernandez has uploaded a new change for review. Change subject: codegen: Use files instead of HTTP requests ......................................................................
codegen: Use files instead of HTTP requests Currently the code generator issues HTTP requests to a live engine in order to retrieve the XML schema of the entities and the RSDL. This complicates the execution of the code generator and it makes difficult to know exactly which schema and which RSDL was used to generate the code. This patch adds the XML schema and the RSDL document to the repository, in the following two files: src/main/resources/api.xsd src/main/resources/api.rsdl These files are initially added empty, to avoid mixing the changes to the code with the changes to these documents. The next patch will add the actual files from the latest master. The patch also modifies the generator so that it instead of connecting to a live engine it uses always these two files. When the XML schema or the RSDL need to be updated it can be done manually or using the new "update-metadata" profile: mvn validate -Pupdate-metadata \ -Dapi.url=https://localhost/ovirt-engine/api \ -Dapi.user=admin@internal \ -Dapi.password=letmein! This will download the files from the live engine and update the local copies, which will then need to be manually commited to the source repository. Change-Id: Id273997a87e1e1c0882fd5a789fa7e057b20734a Signed-off-by: Juan Hernandez <[email protected]> --- M .gitignore M ovirt-engine-sdk-java-codegen/pom.xml M ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/Main.java M ovirt-engine-sdk-java-codegen/src/main/java/org/ovirt/engine/sdk/codegen/common/AbstractCodegen.java M 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/xsd/XsdCodegen.java A ovirt-engine-sdk-java-codegen/src/main/resources/README A ovirt-engine-sdk-java-codegen/src/main/resources/api.rsdl A ovirt-engine-sdk-java-codegen/src/main/resources/api.xsd 11 files changed, 287 insertions(+), 215 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-sdk-java refs/changes/90/22990/1 diff --git a/.gitignore b/.gitignore index 3c60d34..c94ff3e 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,3 @@ *.iml .idea *.prefs - -# downloaded XSD schema -api.xsd diff --git a/ovirt-engine-sdk-java-codegen/pom.xml b/ovirt-engine-sdk-java-codegen/pom.xml index 10b93d6..77ab14c 100644 --- a/ovirt-engine-sdk-java-codegen/pom.xml +++ b/ovirt-engine-sdk-java-codegen/pom.xml @@ -23,7 +23,16 @@ </licenses> <properties> + + <!-- Make sure that we always compile with UTF-8 encoding: --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + + <!-- Default values used when retrieving the XML schema and the RSDL from + a live engine: --> + <api.url>https://localhost/ovirt-engine/api</api.url> + <api.user>admin@internal</api.user> + <api.password>letmein!</api.password> + </properties> <dependencies> @@ -74,4 +83,65 @@ </build> + <profiles> + + <!-- This profile is used to download the XML schema and the RSDL from a + live engine and update local copies. To run it use the validate phase + and enable the profile: + + mvn validate -Pupdate-metadata + + After doing this remember to add update the files in the source + control system. --> + <profile> + <id>update-metadata</id> + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <version>1.2.1</version> + <executions> + <execution> + <id>update-schema</id> + <phase>validate</phase> + <goals> + <goal>exec</goal> + </goals> + <configuration> + <executable>wget</executable> + <arguments> + <argument>--http-user=${api.user}</argument> + <argument>--http-password=${api.password}</argument> + <argument>--no-check-certificate</argument> + <argument>--output-document=${basedir}/src/main/resources/api.xsd</argument> + <argument>${api.url}?schema</argument> + </arguments> + </configuration> + </execution> + <execution> + <id>update-rsdl</id> + <phase>validate</phase> + <goals> + <goal>exec</goal> + </goals> + <configuration> + <executable>wget</executable> + <arguments> + <argument>--http-user=${api.user}</argument> + <argument>--http-password=${api.password}</argument> + <argument>--no-check-certificate</argument> + <argument>--output-document=${basedir}/src/main/resources/api.rsdl</argument> + <argument>${api.url}?rsdl</argument> + </arguments> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + + </profiles> + </project> 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 606820c..dcd1a8a 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 @@ -22,43 +22,27 @@ import org.ovirt.engine.sdk.codegen.rsdl.RsdlCodegen; import org.ovirt.engine.sdk.codegen.xsd.XsdCodegen; -import org.ovirt.engine.sdk.exceptions.ServerException; -import org.ovirt.engine.sdk.web.ConnectionsPool; -import org.ovirt.engine.sdk.web.ConnectionsPoolBuilder; -import org.ovirt.engine.sdk.web.HttpProxyBroker; -import org.ovirt.engine.sdk.web.HttpProxyBuilder; /** * oVirt ovirt-engine-sdk-java codegen suite */ public class Main { - private static final String DEFAULT_URL = "http://localhost:8080/ovirt-engine/api"; - private static final String DEFAULT_USER = "admin@internal"; - private static final String DEFAULT_PASSWORD = "letmein!"; - - public static void main(String[] args) throws ServerException, IOException, JAXBException { + public static void main(String[] args) throws IOException, JAXBException { // Parse the command line parameters: - String url = DEFAULT_URL; - String user = DEFAULT_USER; - String password = DEFAULT_PASSWORD; + String xsdPath = null; + String rsdlPath = null; for (int i = 0; i < args.length; i++) { switch (args[i]) { - case "--url": + case "--xsd": i++; if (i < args.length) { - url = args[i]; + xsdPath = args[i]; } break; - case "--user": + case "--rsdl": i++; if (i < args.length) { - user = args[i]; - } - break; - case "--password": - i++; - if (i < args.length) { - password = args[i]; + rsdlPath = args[i]; } break; default: @@ -66,21 +50,16 @@ System.exit(1); } } - - // Create a connection pool that allows us to connect to SSL protected servers without verificating the host - // name, as this verification is an unnecessary complication for the code generator: - ConnectionsPool pool = new ConnectionsPoolBuilder(url, user, password) - .noHostVerification(true) - .build(); - - HttpProxyBroker httpProxyBroker = new HttpProxyBroker( - new HttpProxyBuilder(pool).build()); + if (xsdPath == null || rsdlPath == null) { + System.err.println("Missing required parameters."); + System.exit(1); + } // #1 - generate api entities from the XSD schema - new XsdCodegen(httpProxyBroker).generate(); + new XsdCodegen(xsdPath).generate(); // #2 - generate api entities decorators by RSDL and SDK entry point - new RsdlCodegen(httpProxyBroker).generate(); + new RsdlCodegen(rsdlPath).generate(); // #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 index b3037e8..5cc6de2 100644 --- 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 @@ -23,7 +23,6 @@ import org.ovirt.engine.sdk.codegen.compile.CodeCompiler; import org.ovirt.engine.sdk.codegen.utils.FileUtils; -import org.ovirt.engine.sdk.exceptions.ServerException; /** * Base class for CodeGen generators @@ -61,12 +60,11 @@ /** * Cleans the package, generates new code and compiles it * - * @throws ServerException * @throws IOException * @throws JAXBException */ @Override - public void generate() throws ServerException, IOException, JAXBException { + public void generate() throws IOException, JAXBException { doCleanPackage(this.distPath); doGenerate(this.distPath); doCompile(); @@ -95,11 +93,10 @@ * @param distPath * directory to generates the code at * - * @throws ServerException * @throws IOException * @throws JAXBException */ - protected abstract void doGenerate(String distPath) throws ServerException, IOException, JAXBException; + protected abstract void doGenerate(String distPath) throws IOException, JAXBException; /** * Delete all files in given directory 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 index be1634e..a5489bd 100644 --- 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 @@ -20,15 +20,12 @@ import javax.xml.bind.JAXBException; -import org.ovirt.engine.sdk.exceptions.ServerException; - public interface ICodegen { /** * Cleans the package and generates the code * - * @throws ServerException * @throws IOException * @throws JAXBException */ - abstract void generate() throws ServerException, IOException, 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 71bdee4..363ccee 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 @@ -31,7 +31,6 @@ import org.ovirt.engine.sdk.codegen.templates.VariableTemplate; import org.ovirt.engine.sdk.codegen.utils.OsUtil; import org.ovirt.engine.sdk.entities.API; -import org.ovirt.engine.sdk.exceptions.ServerException; import org.ovirt.engine.sdk.utils.ArrayUtils; import org.ovirt.engine.sdk.utils.StringUtils; @@ -90,8 +89,7 @@ * Generates SDK entry point class */ @Override - protected void doGenerate(String distPath) throws ServerException, IOException, - JAXBException { + protected void doGenerate(String distPath) throws IOException, JAXBException { String collectionsVariables = produceCollectionVariables(); String collectionsGetters = produceCollectionGetters(); String rootMethods = produceRootMethods(); @@ -108,9 +106,8 @@ * @return /api resource methods * * @throws IOException - * @throws ServerException */ - private String produceRootMethods() throws ServerException, IOException { + private String produceRootMethods() throws IOException { String rootMethods = ""; String[] exceptions = 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 1477170..523ce8e 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 @@ -16,13 +16,19 @@ package org.ovirt.engine.sdk.codegen.rsdl; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; import org.ovirt.engine.sdk.codegen.common.AbstractCodegen; import org.ovirt.engine.sdk.codegen.documentation.DocsGen; @@ -55,17 +61,18 @@ import org.ovirt.engine.sdk.entities.HttpMethod; import org.ovirt.engine.sdk.entities.RSDL; import org.ovirt.engine.sdk.entities.VersionCaps; -import org.ovirt.engine.sdk.exceptions.ServerException; import org.ovirt.engine.sdk.utils.ArrayUtils; import org.ovirt.engine.sdk.utils.StringUtils; -import org.ovirt.engine.sdk.web.HttpProxyBroker; /** * Provides RSDL related codegen capabilities */ public class RsdlCodegen extends AbstractCodegen { + /** + * The location of the RSDL file. + */ + private String rsdlPath; - private HttpProxyBroker httpProxy; private Map<String, Class<?>> entitiesMap; private CollectionTemplate collectionTemplate; @@ -88,7 +95,6 @@ private Map<String, CollectionHolder> collectionsHolder; private Map<String, ResourceHolder> resourcesHolder; - private static final String RSDL_URL = "?rsdl"; 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 = @@ -108,10 +114,14 @@ private static final String[] RETURN_BODY_EXCEPTIONS = new String[] { "Response", "Responses" }; private static final String[] ACTION_NAME_EXCEPTIONS = new String[] { "import", "export" }; - public RsdlCodegen(HttpProxyBroker httpProxy) { + /** + * Create a code generator for the decorator classes. + * + * @param rsdlPath the path of the file containing the RSDL document + */ + public RsdlCodegen(String rsdlPath) { super(getDecoratorsPath()); - - this.httpProxy = httpProxy; + this.rsdlPath = rsdlPath; this.collectionTemplate = new CollectionTemplate(); this.resourceTemplate = new ResourceTemplate(); @@ -185,56 +195,135 @@ * @param distPath * directory to generates the code at * - * @throws ServerException * @throws IOException * @throws JAXBException */ @SuppressWarnings("unused") @Override - protected void doGenerate(String distPath) throws ServerException, IOException, JAXBException { + protected void doGenerate(String distPath) throws IOException, JAXBException { String url, rel, requestBodyType, responseBodyType, parent, collectionName, actualReturnType; HttpMethod requestMethod; - // #0 - get the root URL - String rootUrl = httpProxy.getRoot(); + // #1 - load RSDL + RSDL rsdl = loadRsdl(rsdlPath); - // #1 - fetch RSDL - RSDL rsdl = fetchRsdl(); + // The root element of the RSDL document contains the following: + // + // <rsdl href="/api?rsdl" ... + // + // Here we extract from the href the root URL of the API, as it is needed later to make relative other links + // that appear in the RSDL document. + String rootUrl = rsdl.getHref().replace("?rsdl", ""); // #2 - walk RSDL - if (rsdl != null) { - for (DetailedLink dl : rsdl.getLinks().getLinks()) { + for (DetailedLink dl : rsdl.getLinks().getLinks()) { - // Get the URL of the resource and make it relative, removing the root URL of the application and any - // additional leading slash: - url = dl.getHref().replace(rootUrl, ""); - while (url.startsWith(SLASH)) { - url = url.substring(SLASH.length()); - } + // Get the URL of the resource and make it relative, removing the root URL of the application and any + // additional leading slash: + url = dl.getHref().replace(rootUrl, ""); + while (url.startsWith(SLASH)) { + url = url.substring(SLASH.length()); + } - rel = dl.getRel(); - requestBodyType = dl.getRequest().getBody().getType(); - requestMethod = dl.getRequest().getHttpMethod(); - responseBodyType = getResponseBodyType(dl); + rel = dl.getRel(); + requestBodyType = dl.getRequest().getBody().getType(); + requestMethod = dl.getRequest().getHttpMethod(); + responseBodyType = getResponseBodyType(dl); - String[] periods = url.split(SLASH); - parent = ""; - collectionName = ""; + String[] periods = url.split(SLASH); + parent = ""; + collectionName = ""; - int i = 1; - for (String period : periods) { - actualReturnType = getActualReturnType(responseBodyType, period, i, periods); + int i = 1; + for (String period : periods) { + actualReturnType = getActualReturnType(responseBodyType, period, i, periods); - if (!ArrayUtils.contains(COLLECTION2ENTITY_EXCEPTIONS, period)) { + if (!ArrayUtils.contains(COLLECTION2ENTITY_EXCEPTIONS, period)) { + if (i == 1) { // root-collection + String collection = getRootCollectionName(period); + collectionName = collection; + parent = StringUtils.toUpperCase(StringUtils.toSingular(collection)); + String decoratorCollectionName = StringUtils.toUpperCase(collection); + String publicEntityName = getPublicEntity(StringUtils.toSingular(collection)); + String publicCollectionName = getPublicCollection(collection); + String decoratorEntityName = StringUtils.toSingular(decoratorCollectionName); + + addRootCollection(url, + rel, + dl, + i, + period, + decoratorCollectionName, + publicEntityName, + publicCollectionName, + decoratorEntityName); + + } else if (i == 2) { // root-resource + String resource = getRootResourceName(collectionName); + String decoratorResourceName = StringUtils.toUpperCase(resource); + String publicEntityName = getPublicEntity(StringUtils.toSingular(collectionName)); + + addRootResource(url, + rel, + dl, + resource, + decoratorResourceName, + publicEntityName); + + } else if (i % 2 != 0) { // sub-collection + String collection = getSubCollectionName(actualReturnType, parent, i, periods); + collectionName = collection; + ResourceHolder resourceHolder = this.resourcesHolder.get(parent.toLowerCase()); + String decoratorEntityName = getSubResourceName(collectionName, parent); + String publicEntityName = + getSubCollectionEntityName(rel, actualReturnType, requestMethod, periods, i, period); + String publicCollectionName = + getPublicCollection(StringUtils.toPlural(actualReturnType)); + + addSubCollection(url, + rel, + parent, + requestMethod, + dl, + periods, + i, + period, + collection, + resourceHolder, + decoratorEntityName, + publicEntityName, + publicCollectionName); + + } else { // sub-resource + if (!isAction(period, rel, requestMethod)) { + String resource = getSubResourceName(collectionName, parent); + String subResourceDecoratorName = resource; + String publicEntityName = getSubResourceEntityName(parent, collectionName); + + addSubResource(url, + rel, + dl, + resource, + subResourceDecoratorName, + publicEntityName); + + parent = resource; + } else { + // TODO: use extra params (besides action) defined by RSDL + addResourceAction(rel, parent, collectionName, period, dl); + } + } + } else { // unique treatment for COLLECTION2ENTITY_EXCEPTIONS + if (period.equals("capabilities") || parent.equalsIgnoreCase("capabilities")) { if (i == 1) { // root-collection String collection = getRootCollectionName(period); collectionName = collection; - parent = StringUtils.toUpperCase(StringUtils.toSingular(collection)); + parent = StringUtils.toUpperCase(collectionName); String decoratorCollectionName = StringUtils.toUpperCase(collection); - String publicEntityName = getPublicEntity(StringUtils.toSingular(collection)); - String publicCollectionName = getPublicCollection(collection); - String decoratorEntityName = StringUtils.toSingular(decoratorCollectionName); + String publicEntityName = getPublicEntity(VersionCaps.class.getSimpleName()); + String publicCollectionName = getPublicCollection(Capabilities.class.getSimpleName()); + String decoratorEntityName = VersionCaps.class.getSimpleName(); addRootCollection(url, rel, @@ -247,9 +336,9 @@ decoratorEntityName); } else if (i == 2) { // root-resource - String resource = getRootResourceName(collectionName); - String decoratorResourceName = StringUtils.toUpperCase(resource); - String publicEntityName = getPublicEntity(StringUtils.toSingular(collectionName)); + String resource = VersionCaps.class.getSimpleName(); + String decoratorResourceName = resource; + String publicEntityName = getPublicEntity(resource); addRootResource(url, rel, @@ -258,93 +347,14 @@ decoratorResourceName, publicEntityName); - } else if (i % 2 != 0) { // sub-collection - String collection = getSubCollectionName(actualReturnType, parent, i, periods); - collectionName = collection; - ResourceHolder resourceHolder = this.resourcesHolder.get(parent.toLowerCase()); - String decoratorEntityName = getSubResourceName(collectionName, parent); - String publicEntityName = - getSubCollectionEntityName(rel, actualReturnType, requestMethod, periods, i, period); - String publicCollectionName = - getPublicCollection(StringUtils.toPlural(actualReturnType)); - - addSubCollection(url, - rel, - parent, - requestMethod, - dl, - periods, - i, - period, - collection, - resourceHolder, - decoratorEntityName, - publicEntityName, - publicCollectionName); - - } else { // sub-resource - if (!isAction(period, rel, requestMethod)) { - String resource = getSubResourceName(collectionName, parent); - String subResourceDecoratorName = resource; - String publicEntityName = getSubResourceEntityName(parent, collectionName); - - addSubResource(url, - rel, - dl, - resource, - subResourceDecoratorName, - publicEntityName); - - parent = resource; - } else { - // TODO: use extra params (besides action) defined by RSDL - addResourceAction(rel, parent, collectionName, period, dl); - } } - } else { // unique treatment for COLLECTION2ENTITY_EXCEPTIONS - if (period.equals("capabilities") || parent.equalsIgnoreCase("capabilities")) { - if (i == 1) { // root-collection - String collection = getRootCollectionName(period); - collectionName = collection; - parent = StringUtils.toUpperCase(collectionName); - String decoratorCollectionName = StringUtils.toUpperCase(collection); - String publicEntityName = getPublicEntity(VersionCaps.class.getSimpleName()); - String publicCollectionName = getPublicCollection(Capabilities.class.getSimpleName()); - String decoratorEntityName = VersionCaps.class.getSimpleName(); - - addRootCollection(url, - rel, - dl, - i, - period, - decoratorCollectionName, - publicEntityName, - publicCollectionName, - decoratorEntityName); - - } else if (i == 2) { // root-resource - String resource = VersionCaps.class.getSimpleName(); - String decoratorResourceName = resource; - String publicEntityName = getPublicEntity(resource); - - addRootResource(url, - rel, - dl, - resource, - decoratorResourceName, - publicEntityName); - - } - } else { - // TODO: implement unique treatment for COLLECTION2ENTITY_EXCEPTIONS - break; - } + } else { + // TODO: implement unique treatment for COLLECTION2ENTITY_EXCEPTIONS + break; } - i++; } + i++; } - } else { - throw new RuntimeException("RSDL download failed."); } // #3 - Persist content @@ -960,15 +970,18 @@ } /** - * Fetches RSDL descriptor - * + * Loads the RSDL from a file. + * + * @param rsdlPath the path of the file containing the RDSL document * @return RSDL - * - * @throws ServerException * @throws IOException * @throws JAXBException */ - private RSDL fetchRsdl() throws ServerException, IOException, JAXBException { - return httpProxy.get(RSDL_URL, RSDL.class); + private RSDL loadRsdl(String rsdlPath) throws IOException, JAXBException { + JAXBContext context = JAXBContext.newInstance(RSDL.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + Source source = new StreamSource(new File(rsdlPath)); + JAXBElement<RSDL> element = unmarshaller.unmarshal(source, RSDL.class); + return element.getValue(); } } 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 7d38603..09bede6 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 @@ -21,7 +21,6 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; -import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -32,15 +31,11 @@ import org.ovirt.engine.sdk.codegen.templates.CopyrightTemplate; import org.ovirt.engine.sdk.codegen.utils.FileUtils; import org.ovirt.engine.sdk.codegen.utils.OsUtil; -import org.ovirt.engine.sdk.exceptions.ServerException; -import org.ovirt.engine.sdk.web.HttpProxyBroker; /** * Provides XSD schema related services */ public class XsdCodegen extends AbstractCodegen { - - private static final String SCHEMA_URL = "?schema"; private static final String WINDOWS_XJC_PATH = "%java_home%\\bin\\xjc"; private static final String NX_XJC_PATH = "xjc"; @@ -49,20 +44,18 @@ private static final String NX_ENTITIES_PATH = "../ovirt-engine-sdk-java/src/main/java/"; private static final String ENTITIES_PACKAGE = "org.ovirt.engine.sdk.entities"; - private static final String SCHEMA_FILE_NAME = "api.xsd"; private static final String XJC_FLAGS = " -extension -no-header -enableIntrospection "; private static final String copyrightTemplate = new CopyrightTemplate().getTemplate(); - private HttpProxyBroker httpProxy; - /** - * @param httpProxy + * The location of the XSD file. */ - public XsdCodegen(HttpProxyBroker httpProxy) { - super(getEntitiesPath()); + private String xsdPath; - this.httpProxy = httpProxy; + public XsdCodegen(String xsdPath) { + super(getEntitiesPath()); + this.xsdPath = xsdPath; } /** @@ -71,23 +64,20 @@ * @param distPath * directory to generates the code at * - * @throws ServerException * @throws IOException * @throws JAXBException */ @Override - public void doGenerate(String distPath) throws ServerException, IOException, JAXBException { + public void doGenerate(String distPath) throws IOException, JAXBException { String xjcOutput = null; - fetchScema(this.httpProxy); - if (OsUtil.isWindows()) { xjcOutput = runCommand(WINDOWS_XJC_PATH + " -d " + distPath + - " -p " + ENTITIES_PACKAGE + XJC_FLAGS + SCHEMA_FILE_NAME); + " -p " + ENTITIES_PACKAGE + XJC_FLAGS + xsdPath); } else if (OsUtil.isMac() || OsUtil.isUnix() || OsUtil.isSolaris()) { xjcOutput = runCommand(NX_XJC_PATH + " -d " + distPath + - " -p " + ENTITIES_PACKAGE + XJC_FLAGS + SCHEMA_FILE_NAME); + " -p " + ENTITIES_PACKAGE + XJC_FLAGS + xsdPath); } else { throw new RuntimeException("unsupported OS."); } @@ -95,24 +85,6 @@ if (xjcOutput == null || !xjcOutput.startsWith("parsing a schema...compiling a schema...")) { throw new RuntimeException("xjc codegen failed: " + xjcOutput); - } - } - - private void fetchScema(HttpProxyBroker httpProxy) throws ServerException, - JAXBException, IOException { - PrintWriter out = null; - String schema = httpProxy.get(SCHEMA_URL); - if (schema != null && !schema.equals("")) { - try { - out = new PrintWriter(SCHEMA_FILE_NAME); - out.println(schema); - } finally { - if (out != null) { - out.close(); - } - } - } else { - throw new RuntimeException("xsd schema download failed."); } } @@ -291,4 +263,9 @@ e.printStackTrace(); } } + + public static void main(String[] args) throws JAXBException, IOException { + XsdCodegen generator = new XsdCodegen(args[0]); + generator.generate(); + } } diff --git a/ovirt-engine-sdk-java-codegen/src/main/resources/README b/ovirt-engine-sdk-java-codegen/src/main/resources/README new file mode 100644 index 0000000..ba3bdc0 --- /dev/null +++ b/ovirt-engine-sdk-java-codegen/src/main/resources/README @@ -0,0 +1,45 @@ +This directory contains the files for the XML schema of the entities and the +RSDL used by the code generator. To regenerate the SDK download them from a +live engine as follows: + + $ mvn validate -Pupdate-metadata \ + -Dapi.url="https://localhost/ovirt-engine/api" \ + -Dapi.user="admin@internal" \ + -Dapi.password="letmein!" + +This will use the wget command to download the files, so make sure you have +it installed. + +The api.url, api.user and api.password properties are optional, and their +default values are as in the above example. If you are going to use this +frequently it may be convenient to put them in your ~/.m2/settins.xml file, +something like this: + + <settings + 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/xsd/settings-1.0.0.xsd"> + + <activeProfiles> + <activeProfile>api</activeProfile> + </activeProfiles> + + <profiles> + <profile> + <id>api</id> + <properties> + <api.url>https://localhost/ovirt-engine/api</api.url> + <api.user>admin@internal</api.user> + <api.password>mypass</api.password> + </properties> + </profile> + </profiles> + + </settings> + +If you do this then to download the files just run this: + + $ mvn validate -Pupdate-metadata + +After downloading the files make sure to update them in the source code +repository. diff --git a/ovirt-engine-sdk-java-codegen/src/main/resources/api.rsdl b/ovirt-engine-sdk-java-codegen/src/main/resources/api.rsdl new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/ovirt-engine-sdk-java-codegen/src/main/resources/api.rsdl diff --git a/ovirt-engine-sdk-java-codegen/src/main/resources/api.xsd b/ovirt-engine-sdk-java-codegen/src/main/resources/api.xsd new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/ovirt-engine-sdk-java-codegen/src/main/resources/api.xsd -- To view, visit http://gerrit.ovirt.org/22990 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id273997a87e1e1c0882fd5a789fa7e057b20734a 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
