You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2012/05/18 23:29:10 UTC

svn commit: r1340263 - in /avro/trunk: ./ lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/ lang/java/compiler/src/test/java/org/apache/avro/compiler/ lang/java/compiler/src/test/resources/

Author: cutting
Date: Fri May 18 21:29:10 2012
New Revision: 1340263

URL: http://svn.apache.org/viewvc?rev=1340263&view=rev
Log:
AVRO-1094. Java: Fix specific compiler to better support non-default templates.  Contributed by Ed Kohlwey.

Added:
    avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/TestSpecificCompiler.java   (with props)
    avro/trunk/lang/java/compiler/src/test/resources/
    avro/trunk/lang/java/compiler/src/test/resources/simple_record.avsc
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1340263&r1=1340262&r2=1340263&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri May 18 21:29:10 2012
@@ -77,6 +77,9 @@ Avro 1.7.0 (unreleased)
     AVRO-1076. Java: Fix Protocol#equals() to consider
     properties. (Karthik K via cutting)
 
+    AVRO-1094. Java: Fix specific compiler to better support
+    non-default templates.  (Ed Kohlwey via cutting)
+
 Avro 1.6.3 (5 March 2012)
 
     AVRO-1077. Missing 'inline' for union set function. (thiru)

Modified: avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java?rev=1340263&r1=1340262&r2=1340263&view=diff
==============================================================================
--- avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java (original)
+++ avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java Fri May 18 21:29:10 2012
@@ -115,7 +115,9 @@ public class SpecificCompiler {
     initializeVelocity();
   }
 
-  /** Set the CLASSPATH resource directory where templates reside. */
+  /** Set the resource directory where templates reside. First, the compiler checks
+   * the system path for the specified file, if not it is assumed that it is
+   * present on the classpath.*/
   public void setTemplateDir(String templateDir) {
     this.templateDir = templateDir;
   }
@@ -125,12 +127,16 @@ public class SpecificCompiler {
   private void initializeVelocity() {
     this.velocityEngine = new VelocityEngine();
 
-    // These two properties tell Velocity to use its own classpath-based
-    // loader
-    velocityEngine.addProperty("resource.loader", "class");
+    // These  properties tell Velocity to use its own classpath-based
+    // loader, then drop down to check the root and the current folder
+    velocityEngine.addProperty("resource.loader", "class, file");
     velocityEngine.addProperty("class.resource.loader.class",
         "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
+    velocityEngine.addProperty("file.resource.loader.class", 
+        "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
+    velocityEngine.addProperty("file.resource.loader.path", "/, .");
     velocityEngine.setProperty("runtime.references.strict", true);
+
     // try to use Slf4jLogChute, but if we can't use the null one.
     if (null == logChuteName) {
       // multiple threads can get here concurrently, but that's ok.

Added: avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/TestSpecificCompiler.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/TestSpecificCompiler.java?rev=1340263&view=auto
==============================================================================
--- avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/TestSpecificCompiler.java (added)
+++ avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/TestSpecificCompiler.java Fri May 18 21:29:10 2012
@@ -0,0 +1,50 @@
+/**
+ * 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.avro.compiler;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+
+import org.apache.avro.Schema;
+import org.apache.avro.compiler.specific.SpecificCompiler;
+import org.apache.avro.generic.GenericData.StringType;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class TestSpecificCompiler {
+
+  @Test
+  public void testCanReadTemplateFilesOnTheFilesystem() throws IOException, URISyntaxException{
+    String schemaSrcPath = "src/test/resources/simple_record.avsc";
+    String velocityTemplateDir = "src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/";
+    File src = new File(schemaSrcPath);
+    Schema.Parser parser = new Schema.Parser();
+    Schema schema = parser.parse(src);
+    SpecificCompiler compiler = new SpecificCompiler(schema);
+    compiler.setTemplateDir(velocityTemplateDir);
+    compiler.setStringType(StringType.CharSequence);
+    File outputDir = File.createTempFile("avro-tmp", "");
+    outputDir.delete();
+    outputDir.mkdir();
+    compiler.compileToDestination(src, outputDir);
+    new File(outputDir, "SimpleRecord.java").delete();
+  }
+}

Propchange: avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/TestSpecificCompiler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: avro/trunk/lang/java/compiler/src/test/resources/simple_record.avsc
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/compiler/src/test/resources/simple_record.avsc?rev=1340263&view=auto
==============================================================================
--- avro/trunk/lang/java/compiler/src/test/resources/simple_record.avsc (added)
+++ avro/trunk/lang/java/compiler/src/test/resources/simple_record.avsc Fri May 18 21:29:10 2012
@@ -0,0 +1,7 @@
+{
+  "type": "record", 
+  "name": "SimpleRecord",
+  "fields" : [
+    {"name": "value", "type": "int"}
+  ]
+}
\ No newline at end of file