Generate license to the compiled files
Project: http://git-wip-us.apache.org/repos/asf/gora/repo Commit: http://git-wip-us.apache.org/repos/asf/gora/commit/1242a61d Tree: http://git-wip-us.apache.org/repos/asf/gora/tree/1242a61d Diff: http://git-wip-us.apache.org/repos/asf/gora/diff/1242a61d Branch: refs/heads/master Commit: 1242a61db95dd10786a50caec1877544ad95e332 Parents: 3a4f139 Author: nishadi <ndime...@gmail.com> Authored: Fri May 26 20:10:17 2017 +0530 Committer: nishadi <ndime...@gmail.com> Committed: Fri May 26 20:49:38 2017 +0530 ---------------------------------------------------------------------- .../gora/compiler/cli/GoraCompilerCLI.java | 39 ++++++++++++++++++-- .../org/apache/gora/compiler/GoraCompiler.java | 18 ++++++++- .../gora/compiler/utils/LicenseHeaders.java | 13 +++++++ 3 files changed, 66 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/gora/blob/1242a61d/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java ---------------------------------------------------------------------- diff --git a/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java b/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java index 39cd5a6..ca4b0ee 100644 --- a/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java +++ b/gora-compiler-cli/src/main/java/org/apache/gora/compiler/cli/GoraCompilerCLI.java @@ -20,8 +20,9 @@ package org.apache.gora.compiler.cli; import java.io.File; import java.io.IOException; +import org.apache.commons.lang.ArrayUtils; import org.apache.gora.compiler.GoraCompiler; - +import org.apache.gora.compiler.utils.LicenseHeaders; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,6 +41,28 @@ public class GoraCompilerCLI { printHelp(); System.exit(1); } + // Setting the default license header to ASLv2 + LicenseHeaders licenseHeader = new LicenseHeaders("ASLv2"); + // Checking for user provided license + for (int i = 0; i < args.length; i++) { + if ("-license".equals(args[i])) { + if (i == args.length - 1) { + LOG.error("Must supply a valid license id."); + printHelp(); + System.exit(1); + } + if (licenseHeader.isValidLicense(args[i + 1])) { + licenseHeader.setLicenseName(args[i + 1]); + args = (String[]) ArrayUtils.removeElement(args, args[i + 1]); + args = (String[]) ArrayUtils.removeElement(args, args[i]); + } else { + LOG.error("Must supply a valid license id."); + printHelp(); + System.exit(1); + } + } + } + File outputDir = new File(args[args.length-1]); if(!outputDir.isDirectory()){ LOG.error("Must supply a directory for output"); @@ -70,7 +93,7 @@ public class GoraCompilerCLI { } } try { - GoraCompiler.compileSchema(inputFiles, outputDir); + GoraCompiler.compileSchema(inputFiles, outputDir, licenseHeader); LOG.info("Compiler executed SUCCESSFULL."); } catch (IOException e) { LOG.error("Error while compiling schema files. Check that the schemas are properly formatted."); @@ -80,6 +103,16 @@ public class GoraCompilerCLI { } private static void printHelp() { - LOG.info("Usage: gora-compiler ( -h | --help ) | (<input> [<input>...] <output>)"); + LOG.info("Usage: gora-compiler ( -h | --help ) | (<input> [<input>...] <output> [-license <id>])"); + LOG.error("License header options include;\n" + + "\t\t ASLv2 (Apache Software License v2.0) \n" + + "\t\t AGPLv3 (GNU Affero General Public License) \n" + + "\t\t CDDLv1 (Common Development and Distribution License v1.0) \n" + + "\t\t FDLv13 (GNU Free Documentation License v1.3) \n" + + "\t\t GPLv1 (GNU General Public License v1.0) \n" + + "\t\t GPLv2 (GNU General Public License v2.0) \n" + + "\t\t GPLv3 (GNU General Public License v3.0) \n" + + "\t\t LGPLv21 (GNU Lesser General Public License v2.1) \n" + + "\t\t LGPLv3 (GNU Lesser General Public License v2.1)"); } } http://git-wip-us.apache.org/repos/asf/gora/blob/1242a61d/gora-compiler/src/main/java/org/apache/gora/compiler/GoraCompiler.java ---------------------------------------------------------------------- diff --git a/gora-compiler/src/main/java/org/apache/gora/compiler/GoraCompiler.java b/gora-compiler/src/main/java/org/apache/gora/compiler/GoraCompiler.java index 469bec2..b313bd4 100644 --- a/gora-compiler/src/main/java/org/apache/gora/compiler/GoraCompiler.java +++ b/gora-compiler/src/main/java/org/apache/gora/compiler/GoraCompiler.java @@ -19,6 +19,10 @@ package org.apache.gora.compiler; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -31,6 +35,7 @@ import org.apache.avro.Schema.Field; import org.apache.avro.Schema.Type; import org.apache.avro.SchemaNormalization; import org.apache.avro.compiler.specific.SpecificCompiler; +import org.apache.gora.compiler.utils.LicenseHeaders; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.node.JsonNodeFactory; @@ -57,7 +62,7 @@ public class GoraCompiler extends SpecificCompiler { GORA_HIDDEN_FIELD_NAMES.add(DIRTY_BYTES_FIELD_NAME); } - public static void compileSchema(File[] srcFiles, File dest) + public static void compileSchema(File[] srcFiles, File dest, LicenseHeaders licenseHeader) throws IOException { Schema.Parser parser = new Schema.Parser(); @@ -70,6 +75,13 @@ public class GoraCompiler extends SpecificCompiler { GoraCompiler compiler = new GoraCompiler(newSchema); compiler.setTemplateDir("/org/apache/gora/compiler/templates/"); compiler.compileToDestination(src, dest); + + //Adding the license to the compiled file + Path path = Paths.get(generateDestinationFileName(dest.toString(), newSchema)); + String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8); + content = licenseHeader.getLicense() + content.substring(content.indexOf("package")); + Files.write(path, content.getBytes(StandardCharsets.UTF_8)); + LOG.info("Compiled into: {}", dest.getAbsolutePath()); } } @@ -284,4 +296,8 @@ public class GoraCompiler extends SpecificCompiler { return SchemaNormalization.parsingFingerprint64(schema); } + public static String generateDestinationFileName(String destDir, Schema schema) { + return destDir + File.separatorChar + schema.getNamespace().replace('.', File.separatorChar) + + File.separatorChar + schema.getName() + ".java"; + } } http://git-wip-us.apache.org/repos/asf/gora/blob/1242a61d/gora-compiler/src/main/java/org/apache/gora/compiler/utils/LicenseHeaders.java ---------------------------------------------------------------------- diff --git a/gora-compiler/src/main/java/org/apache/gora/compiler/utils/LicenseHeaders.java b/gora-compiler/src/main/java/org/apache/gora/compiler/utils/LicenseHeaders.java index 155e2d2..54e9676 100644 --- a/gora-compiler/src/main/java/org/apache/gora/compiler/utils/LicenseHeaders.java +++ b/gora-compiler/src/main/java/org/apache/gora/compiler/utils/LicenseHeaders.java @@ -263,4 +263,17 @@ public class LicenseHeaders { public String getLicenseName(){ return licenseName; } + + /** + * Validate a given license name against the supported license + * @param licenseName name of the license + * @return a booolean whether the license is supported or not + */ + public boolean isValidLicense(String licenseName) { + for (int i = 0; i < supportedLicenses.length; i++) { + if (supportedLicenses[i].equals(licenseName)) + return true; + } + return false; + } }