Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.apache.avro.logical;

import org.apache.avro.LogicalTypes;
import org.apache.avro.generic.GenericData;

/**
* Abstract container for LogicalType support
*/
public abstract class AbstractLogicalTypeContainer implements LogicalTypeContainer {

/**
* Performs integration with Avro ecosystem
*/
public void register() {
LogicalTypes.LogicalTypeFactory factory = this.getFactory();
LogicalTypes.register(getTypeName(), factory);
GenericData.get().addLogicalTypeConversion(getConversion());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.apache.avro.logical;

import org.apache.avro.Conversion;
import org.apache.avro.LogicalTypes;

/**
* Container for LogicalType support
*/
public interface LogicalTypeContainer {

/**
* Return logical type factory
*
* @return
*/
LogicalTypes.LogicalTypeFactory getFactory();

/**
* Return Conversion for logical type
*
* @return
*/
Conversion getConversion();

/**
* Get type name
*/
String getTypeName();

/**
* Performs integration with Avro ecosystem
*/
void register();

}
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,10 @@ public static boolean hasBuilder(Schema schema) {
}
}

public static void addLogicalTypeConversion(Conversion<?> conversion) {
SPECIFIC.addLogicalTypeConversion(conversion);
}

/**
* Generates the name of a field Builder accessor method.
* @param schema the schema in which the field is defined.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public String testName() {
public void run() throws Exception {
String output = generate();
String slurped = slurp(expectedOut);
assertEquals(slurped.trim(), output.replace("\r", "").trim());
assertEquals(slurped.trim(), output.replace("\\r", "").trim());
}

public void write() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
Expand All @@ -18,14 +18,15 @@

package org.apache.avro.mojo;

import org.apache.avro.Schema;
import org.apache.avro.compiler.specific.SpecificCompiler;
import org.apache.avro.generic.GenericData.StringType;
import org.apache.avro.logical.LogicalTypeContainer;
import org.apache.maven.plugin.MojoExecutionException;

import java.io.File;
import java.io.IOException;

import org.apache.avro.Schema;
import org.apache.avro.compiler.specific.SpecificCompiler;

/**
* Generate Java classes from Avro schema files (.avsc)
*
Expand All @@ -38,16 +39,16 @@ public class SchemaMojo extends AbstractAvroMojo {
* A parser used to parse all schema files. Using a common parser will
* facilitate the import of external schemas.
*/
private Schema.Parser schemaParser = new Schema.Parser();
private Schema.Parser schemaParser = new Schema.Parser();

/**
/**
* A set of Ant-like inclusion patterns used to select files from the source
* directory for processing. By default, the pattern
* <code>**&#47;*.avsc</code> is used to select grammar files.
*
* @parameter
*/
private String[] includes = new String[] { "**/*.avsc" };
private String[] includes = new String[]{"**/*.avsc"};

/**
* A set of Ant-like inclusion patterns used to select files from the source
Expand All @@ -56,8 +57,22 @@ public class SchemaMojo extends AbstractAvroMojo {
*
* @parameter
*/
private String[] testIncludes = new String[] { "**/*.avsc" };
private String[] testIncludes = new String[]{"**/*.avsc"};

/**
* A set of custom LogicalTypeContainers fully qualified class names to be used for code generation in maven plugin;
* expecting public constructors with no parameters
*
* @parameter
*/
private String[] logicalTypeContainers = new String[]{};

/**
* @param filename
* @param sourceDirectory
* @param outputDirectory
* @throws IOException
*/
@Override
protected void doCompile(String filename, File sourceDirectory, File outputDirectory) throws IOException {
File src = new File(sourceDirectory, filename);
Expand All @@ -83,6 +98,25 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec
compiler.compileToDestination(src, outputDirectory);
}

@Override
public void execute() throws MojoExecutionException {
registerContainers();
super.execute();
}

private void registerContainers() {
try {
for (String logicalTypeContainer : logicalTypeContainers) {
LogicalTypeContainer container = (LogicalTypeContainer) Class.forName(logicalTypeContainer).newInstance();
SpecificCompiler.addLogicalTypeConversion(container.getConversion());
container.register();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

@Override
protected String[] getIncludes() {
return includes;
Expand Down