You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gora.apache.org by rm...@apache.org on 2012/12/27 23:01:01 UTC
svn commit: r1426340 - in
/gora/trunk/gora-core/src/main/java/org/apache/gora:
compiler/GoraCompiler.java util/LicenseHeaders.java util/TimingUtil.java
Author: rmarroquin
Date: Thu Dec 27 22:01:01 2012
New Revision: 1426340
URL: http://svn.apache.org/viewvc?rev=1426340&view=rev
Log:
Adding new features to help GoraCompiler provide new licenses.
Added:
gora/trunk/gora-core/src/main/java/org/apache/gora/util/LicenseHeaders.java
gora/trunk/gora-core/src/main/java/org/apache/gora/util/TimingUtil.java
Modified:
gora/trunk/gora-core/src/main/java/org/apache/gora/compiler/GoraCompiler.java
Modified: gora/trunk/gora-core/src/main/java/org/apache/gora/compiler/GoraCompiler.java
URL: http://svn.apache.org/viewvc/gora/trunk/gora-core/src/main/java/org/apache/gora/compiler/GoraCompiler.java?rev=1426340&r1=1426339&r2=1426340&view=diff
==============================================================================
--- gora/trunk/gora-core/src/main/java/org/apache/gora/compiler/GoraCompiler.java (original)
+++ gora/trunk/gora-core/src/main/java/org/apache/gora/compiler/GoraCompiler.java Thu Dec 27 22:01:01 2012
@@ -22,6 +22,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
+import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -32,28 +33,37 @@ import org.apache.avro.Schema;
import org.apache.avro.Protocol.Message;
import org.apache.avro.Schema.Field;
import org.apache.avro.specific.SpecificData;
+import org.apache.gora.util.LicenseHeaders;
+import org.apache.gora.util.TimingUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-/** Generate specific Java interfaces and classes for protocols and schemas. */
+/** Generate specific Java interfaces and classes for protocols and schemas.
+ * GoraCompiler takes its inspiration from, and is largely based on Avro's {@link SpecificCompiler}.
+ */
public class GoraCompiler {
private File dest;
private Writer out;
private Set<Schema> queue = new HashSet<Schema>();
private static final Logger log = LoggerFactory.getLogger(GoraCompiler.class);
+ private static LicenseHeaders licenseHeader = new LicenseHeaders(null);
private GoraCompiler(File dest) {
this.dest = dest; // root directory for output
}
-
+
/** Generates Java interface and classes for a protocol.
* @param src the source Avro protocol file
* @param dest the directory to place generated files in
*/
public static void compileProtocol(File src, File dest) throws IOException {
+ log.info("Compiling Protocol: " + src + " to: " + dest);
+ if(licenseHeader != null) {
+ log.info("The generated file will be " + licenseHeader.getLicenseName() + " licensed.");
+ }
GoraCompiler compiler = new GoraCompiler(dest);
Protocol protocol = Protocol.parse(src);
- for (Schema s : protocol.getTypes()) // enqueue types
+ for (Schema s : protocol.getTypes()) // enqueue types
compiler.enqueue(s);
compiler.compileInterface(protocol); // generate interface
compiler.compile(); // generate classes for types
@@ -61,7 +71,10 @@ public class GoraCompiler {
/** Generates Java classes for a schema. */
public static void compileSchema(File src, File dest) throws IOException {
- log.info("Compiling " + src + " to " + dest );
+ log.info("Compiling Schema: " + src + " to: " + dest);
+ if(licenseHeader != null) {
+ log.info("The generated artifact will be " + licenseHeader.getLicenseName() + " licensed.");
+ }
GoraCompiler compiler = new GoraCompiler(dest);
compiler.enqueue(Schema.parse(src)); // enqueue types
compiler.compile(); // generate classes for types
@@ -158,6 +171,9 @@ public class GoraCompiler {
}
private void header(String namespace) throws IOException {
+ if (licenseHeader != null) {
+ line(0, licenseHeader.getLicense());
+ }
if(namespace != null) {
line(0, "package "+namespace+";\n");
}
@@ -445,13 +461,45 @@ public class GoraCompiler {
private static String esc(Object o) {
return o.toString().replace("\"", "\\\"");
}
-
+
+ /**
+ * The main method used to invoke the GoraCompiler. It accepts an input (JSON) avsc
+ * schema file, the target output directory and an optional parameter defining the
+ * license header to be used when compiling the avsc into the generated class.
+ * If no license is explicitely defined, an ASFv2.0 license header is attributed
+ * to all generated files by default.
+ */
public static void main(String[] args) throws Exception {
if (args.length < 2) {
- System.err.println("Usage: Compiler <schema file> <output dir>");
+ System.err.println("Usage: GoraCompiler <schema file> <output dir> [-license <id>]");
+ System.err.println(" <schema file> - individual avsc file to be compiled");
+ System.err.println(" <output dir> - output directory for generated Java files");
+ System.err.println(" [-license <id>] - the preferred license header to add to the\n" +
+ "\t\t generated Java file. Current 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)\n") ;
System.exit(1);
}
+ for (int i = 1; i < args.length; i++) {
+ licenseHeader.setLicenseName("ASLv2");
+ if ("-license".equals(args[i])) {
+ licenseHeader.setLicenseName(args[++i]);
+ }
+ }
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ long start = System.currentTimeMillis();
+ log.info("GoraCompiler: starting at " + sdf.format(start));
compileSchema(new File(args[0]), new File(args[1]));
+ long end = System.currentTimeMillis();
+ log.info("GoraCompiler: finished at " + sdf.format(end) + ", elapsed: " + TimingUtil.elapsedTime(start, end));
+ return;
}
}
Added: gora/trunk/gora-core/src/main/java/org/apache/gora/util/LicenseHeaders.java
URL: http://svn.apache.org/viewvc/gora/trunk/gora-core/src/main/java/org/apache/gora/util/LicenseHeaders.java?rev=1426340&view=auto
==============================================================================
--- gora/trunk/gora-core/src/main/java/org/apache/gora/util/LicenseHeaders.java (added)
+++ gora/trunk/gora-core/src/main/java/org/apache/gora/util/LicenseHeaders.java Thu Dec 27 22:01:01 2012
@@ -0,0 +1,257 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.gora.util;
+
+import java.util.HashMap;
+
+/**
+ * Utility class which specifies a collection of license headers which can be
+ * used within the GoraCompiler for generating alternative license headers for
+ * Java interfaces and classes generated from protocols and schemas.
+ */
+public class LicenseHeaders {
+
+ /**
+ * Chosen license to be included within the generated classes
+ */
+ private String licenseName;
+
+ /**
+ * Licenses supported by Gora Compilers
+ */
+ private static final String supportedLicenses[] = {"ASLv2", "AGPLv3", "CDDLv1", "FDLv13", "GPLv1", "GPLv2", "GPLv3", "LGPLv21", "LGPLv3"};
+
+ /**
+ * HashMap containing supported licenses' names and their corresponding text.
+ */
+ private HashMap<String, String> relatedLicenses;
+
+ // ASLv2 license header
+ private static final String ASLv2 =
+ "/**\n" +
+ " *Licensed to the Apache Software Foundation (ASF) under one\n" +
+ " *or more contributor license agreements. See the NOTICE file\n" +
+ " *distributed with this work for additional information\n" +
+ " *regarding copyright ownership. The ASF licenses this file\n" +
+ " *to you under the Apache License, Version 2.0 (the\"\n" +
+ " *License\"); you may not use this file except in compliance\n" +
+ " *with the License. You may obtain a copy of the License at\n" +
+ " *\n " +
+ " * http://www.apache.org/licenses/LICENSE-2.0\n" +
+ " * \n" +
+ " *Unless required by applicable law or agreed to in writing, software\n" +
+ " *distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
+ " *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
+ " *See the License for the specific language governing permissions and\n" +
+ " *limitations under the License.\n" +
+ " */\n";
+
+ // AGPLv3 license header
+ private static final String AGPLv3 =
+ "/**\n" +
+ " * This program is free software: you can redistribute it and/or modify\n" +
+ " * it under the terms of the GNU Affero General Public License as published by\n" +
+ " * the Free Software Foundation, either version 3 of the License, or\n" +
+ " * (at your option) any later version.\n" +
+ " *\n " +
+ " * This program is distributed in the hope that it will be useful,\n" +
+ " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
+ " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
+ " * GNU General Public License for more details.\n" +
+ " */\n";
+
+ // CDDLv1 license header
+ private static final String CDDLv1 =
+ "/**\n" +
+ " * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0\n" +
+ " *\n " +
+ " * This program is free software: you can redistribute it and/or modify\n" +
+ " * it under the terms of the Common Development and Distrubtion License as\n" +
+ " * published by the Sun Microsystems, either version 1.0 of the\n" +
+ " * License, or (at your option) any later version.\n" +
+ " *\n " +
+ " * This program is distributed in the hope that it will be useful,\n" +
+ " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
+ " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
+ " * GNU General Lesser Public License for more details.\n" +
+ " *\n " +
+ " * You should have received a copy of the Common Development and Distrubtion\n" +
+ " * License along with this program. If not, see\n" +
+ " * <http://www.gnu.org/licenses/gpl-1.0.html>.\n" +
+ " */\n";
+
+ // FDLv1.3 license header
+ private static final String FDLv13 =
+ "/**\n" +
+ " * This program is free software: you can redistribute it and/or modify\n" +
+ " * it under the terms of the GNU Free Documentation License as published by\n" +
+ " * the Free Software Foundation, either version 1.3 of the License, or\n" +
+ " * (at your option) any later version.\n" +
+ " *\n " +
+ " * This program is distributed in the hope that it will be useful,\n" +
+ " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
+ " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
+ " * GNU General Public License for more details.\n" +
+ " *\n " +
+ " * You should have received a copy of the GNU Free Documentation License\n" +
+ " * along with this program. If not, see <http://www.gnu.org/licenses/>.\n" +
+ " */\n";
+
+ // GPLv1 license header
+ private static final String GPLv1 =
+ "/**\n" +
+ " * This program is free software: you can redistribute it and/or modify\n" +
+ " * it under the terms of the GNU General Public License as\n" +
+ " * published by the Free Software Foundation, either version 1 of the\n" +
+ " * License, or (at your option) any later version.\n" +
+ " *\n " +
+ " * This program is distributed in the hope that it will be useful,\n" +
+ " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
+ " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
+ " * GNU General Public License for more details.\n" +
+ " *\n " +
+ " * You should have received a copy of the GNU General Public\n" +
+ " * License along with this program. If not, see\n" +
+ " * <http://www.gnu.org/licenses/gpl-1.0.html>.\n" +
+ " */\n";
+
+ // GPLv2 license header
+ private static final String GPLv2 =
+ "/**\n" +
+ " * This program is free software: you can redistribute it and/or modify\n" +
+ " * it under the terms of the GNU General Public License as\n" +
+ " * published by the Free Software Foundation, either version 2 of the\n" +
+ " * License, or (at your option) any later version.\n" +
+ " *\n " +
+ " * This program is distributed in the hope that it will be useful,\n" +
+ " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
+ " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
+ " * GNU General Public License for more details.\n" +
+ " *\n " +
+ " * You should have received a copy of the GNU General Public\n" +
+ " * License along with this program. If not, see\n" +
+ " * <http://www.gnu.org/licenses/gpl-2.0.html>.\n" +
+ " */\n";
+
+ // GPLv3 license header
+ private static final String GPLv3 =
+ "/**\n" +
+ " * This program is free software: you can redistribute it and/or modify\n" +
+ " * it under the terms of the GNU General Public License as\n" +
+ " * published by the Free Software Foundation, either version 3 of the\n" +
+ " * License, or (at your option) any later version.\n" +
+ " *\n " +
+ " * This program is distributed in the hope that it will be useful,\n" +
+ " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
+ " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
+ " * GNU General Public License for more details.\n" +
+ " *\n " +
+ " * You should have received a copy of the GNU General Public\n" +
+ " * License along with this program. If not, see\n" +
+ " * <http://www.gnu.org/licenses/gpl-3.0.html>.\n" +
+ " */\n";
+
+ // LGPLv21 license header
+ private static final String LGPLv21 =
+ "/**\n" +
+ " * This program is free software: you can redistribute it and/or modify\n" +
+ " * it under the terms of the GNU Lesser General Public License as\n" +
+ " * published by the Free Software Foundation, either version 2.1 of the\n" +
+ " * License, or (at your option) any later version.\n" +
+ " *\n " +
+ " * This program is distributed in the hope that it will be useful,\n" +
+ " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
+ " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
+ " * GNU General Public License for more details.\n" +
+ " *\n " +
+ " * You should have received a copy of the GNU Lesser General Public\n" +
+ " * License along with this program. If not, see\n" +
+ " * <http://www.gnu.org/licenses/lgpl-2.1.html>.\n" +
+ " */\n";
+
+ // LGPLv3 license header
+ private static final String LGPLv3 =
+ "/**\n" +
+ " * This program is free software: you can redistribute it and/or modify\n" +
+ " * it under the terms of the GNU Lesser General Public License as\n" +
+ " * published by the Free Software Foundation, either version 3 of the\n" +
+ " * License, or (at your option) any later version.\n" +
+ " *\n " +
+ " * This program is distributed in the hope that it will be useful,\n" +
+ " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
+ " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
+ " * GNU General Public License for more details.\n" +
+ " *\n " +
+ " * You should have received a copy of the GNU Lesser General Public\n" +
+ " * License along with this program. If not, see\n" +
+ " * <http://www.gnu.org/licenses/lgpl-3.0.html>.\n" +
+ " */\n";
+
+ /**
+ * @param license
+ */
+ public LicenseHeaders(String pLicenseName) {
+ this.initializeRelations();
+ this.setLicenseName(pLicenseName);
+ }
+
+ /**
+ * Initializes relations between supported licenses and license text
+ */
+ public void initializeRelations(){
+ relatedLicenses = new HashMap<String, String>();
+ try {
+ for (String licenseValue : supportedLicenses) {
+ String var = (String) this.getClass().getDeclaredField(licenseValue).get(licenseValue);
+ relatedLicenses.put(licenseValue,var);
+ }
+ } catch (SecurityException e) {
+ e.printStackTrace();
+ } catch (NoSuchFieldException e) {
+ e.printStackTrace();
+ } catch (IllegalArgumentException e) {
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ *Set the license header for the LicenseHeader object.
+ *
+ */
+ public void setLicenseName(String pLicenseName) {
+ this.licenseName = pLicenseName;
+ }
+
+ /**
+ * Get the license header for the LicenseHeader object.
+ * @return
+ */
+ public String getLicense() {
+ return relatedLicenses.get(licenseName)!=null?relatedLicenses.get(licenseName):"";
+ }
+
+ /**
+ * Get the license name for the LicenseHeader object.
+ * @return
+ */
+ public String getLicenseName(){
+ return licenseName;
+ }
+}
Added: gora/trunk/gora-core/src/main/java/org/apache/gora/util/TimingUtil.java
URL: http://svn.apache.org/viewvc/gora/trunk/gora-core/src/main/java/org/apache/gora/util/TimingUtil.java?rev=1426340&view=auto
==============================================================================
--- gora/trunk/gora-core/src/main/java/org/apache/gora/util/TimingUtil.java (added)
+++ gora/trunk/gora-core/src/main/java/org/apache/gora/util/TimingUtil.java Thu Dec 27 22:01:01 2012
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.gora.util;
+
+import java.text.NumberFormat;
+
+public class TimingUtil {
+
+ private static long[] TIME_FACTOR = { 60 * 60 * 1000, 60 * 1000, 1000 };
+
+ /**
+ * Calculate the elapsed time between two times specified in milliseconds.
+ * @param start The start of the time period
+ * @param end The end of the time period
+ * @return a string of the form "XhYmZs" when the elapsed time is X hours, Y minutes and Z seconds or null if start > end.
+ */
+ public static String elapsedTime(long start, long end){
+ if (start > end) {
+ return null;
+ }
+
+ long[] elapsedTime = new long[TIME_FACTOR.length];
+
+ for (int i = 0; i < TIME_FACTOR.length; i++) {
+ elapsedTime[i] = start > end ? -1 : (end - start) / TIME_FACTOR[i];
+ start += TIME_FACTOR[i] * elapsedTime[i];
+ }
+
+ NumberFormat nf = NumberFormat.getInstance();
+ nf.setMinimumIntegerDigits(2);
+ StringBuffer buf = new StringBuffer();
+ for (int i = 0; i < elapsedTime.length; i++) {
+ if (i > 0) {
+ buf.append(":");
+ }
+ buf.append(nf.format(elapsedTime[i]));
+ }
+ return buf.toString();
+ }
+}