/**
 * 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.mojo;

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

import org.apache.avro.specific.SpecificCompiler;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.model.fileset.FileSet;
import org.apache.maven.shared.model.fileset.util.FileSetManager;

/**
 * Compile an Avro protocol schema file.
 *
 * @goal protocol
 * @phase generate-sources
 */
public class ProtocolMojo extends AbstractMojo {
    /**
     * @parameter expression="${sourceDirectory}" default-value="${basedir}/src/main/avro"
     */
    private File sourceDirectory;

    /**
     * @parameter expression="${outputDirectory}" default-value="${project.build.directory}/generated-sources/avro"
     */
    private File outputDirectory;

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

    /**
     * A set of Ant-like exclusion patterns used to prevent certain
     * files from being processed. By default, this set is empty such
     * that no files are excluded.
     *
     * @parameter
     */
    private String[] excludes = new String[0];

    /**
     * The current Maven project.
     *
     * @parameter default-value="${project}"
     * @readonly
     * @required
     */
    private MavenProject project;

    private FileSetManager fileSetManager = new FileSetManager();

    public void execute() throws MojoExecutionException {
        if (!sourceDirectory.isDirectory()) {
            throw new MojoExecutionException(sourceDirectory
                    + "is not a directory");
        }

        FileSet fs = new FileSet();
        fs.setDirectory(sourceDirectory.getAbsolutePath());
        fs.setFollowSymlinks( false );

        for (String include : includes) {
            fs.addInclude(include);
        }
        for (String exclude : excludes) {
            fs.addExclude(exclude);
        }

        String[] includedFiles = fileSetManager.getIncludedFiles(fs);

        for (String filename : includedFiles) {
            try {
                SpecificCompiler.compileProtocol(
                        new File(sourceDirectory, filename),
                        outputDirectory);
            } catch (IOException e) {
                throw new MojoExecutionException("Error compiling protocol file "
                        + filename + " to " + outputDirectory, e);
            }
        }

        project.addCompileSourceRoot(outputDirectory.getAbsolutePath());
    }
}

@@ -836,58 +836,8 @@
 o;%0A%0A
-import java.io.File;%0Aimport java.io.IOException;%0A%0A
 impo
@@ -882,16 +882,16 @@
 mpiler;%0A
+
 import o
@@ -1149,16 +1149,66 @@
 nager;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0A%0A
 /**%0A * C
@@ -1948,11 +1948,9 @@
 ng%5B%5D
- %7B 
+%7B
 %22**/
@@ -1956,17 +1956,29 @@
 /*.avpr%22
- 
+, %22**/*.avsc%22
 %7D;%0A%0A    
@@ -2406,16 +2406,102 @@
 oject;%0A%0A
+    /**%0A     * @parameter default-value=%22false%22%0A     */%0A    private boolean schemas;%0A%0A
     priv
@@ -2909,15 +2909,13 @@
 nks(
- 
 false
- 
 );%0A%0A
@@ -3228,16 +3228,269 @@
   try %7B%0A
+                if (schemas %7C%7C filename.endsWith(%22.avsc%22)) %7B%0A                    SpecificCompiler.compileSchema(%0A                            new File(sourceDirectory, filename),%0A                            outputDirectory);%0A                %7D else %7B%0A    
         
@@ -3531,16 +3531,20 @@
 otocol(%0A
+    
         
@@ -3612,32 +3612,36 @@
                 
+    
 outputDirectory)
@@ -3634,32 +3634,50 @@
 tputDirectory);%0A
+                %7D%0A
             %7D ca

support schemas