Switch to using plexus-build-api for file output
authorRobert Varga <rovarga@cisco.com>
Thu, 28 Nov 2013 19:30:18 +0000 (20:30 +0100)
committerRobert Varga <rovarga@cisco.com>
Fri, 29 Nov 2013 14:52:19 +0000 (15:52 +0100)
This wraps data output through plexus-build-api, which will prevent
files being overwritten and recompiled when they do not change.

Change-Id: Ifffacbd9b1cf1665ea0b1a8103bcb65f6db7a319
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-generator-spi/src/main/java/org/opendaylight/yangtools/sal/binding/generator/spi/TypeProvider.java
code-generator/binding-java-api-generator/pom.xml
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/GeneratorJavaFile.java
code-generator/maven-sal-api-gen-plugin/pom.xml
code-generator/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/yangtools/maven/sal/api/gen/plugin/CodeGeneratorImpl.java
code-generator/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/yangtools/yang/unified/doc/generator/GeneratorImpl.xtend

index 481d19cef68865bb302e68ed72aabf3fe007d39f..bf3a188f36ec93daa3cdd892c5d43ff66dd87ee4 100644 (file)
@@ -26,7 +26,7 @@ public interface TypeProvider {
      * Definition contains extended type defined via yang typedef statement\r
      * the method SHOULD return Generated Type or Generated Transfer Object\r
      * if that Type is correctly referenced to resolved imported yang module.\r
-     * The method will return <cdoe>null</cdoe> value in situations that\r
+     * The method will return <code>null</code> value in situations that\r
      * TypeDefinition can't be resolved (either due missing yang import or\r
      * incorrectly specified type).\r
      *\r
index 62ed373b010b89cf243e141444d47f57ca214e38..f7b8cce31708cd4c2bcb0606c68395d0003d69c5 100644 (file)
             <groupId>com.google.guava</groupId>\r
             <artifactId>guava</artifactId>\r
         </dependency>\r
+        <dependency>\r
+            <groupId>org.sonatype.plexus</groupId>\r
+            <artifactId>plexus-build-api</artifactId>\r
+        </dependency>\r
+        <dependency>\r
+            <groupId>org.codehaus.plexus</groupId>\r
+            <artifactId>plexus-slf4j-logging</artifactId>\r
+        </dependency>\r
     </dependencies>\r
 \r
 </project>\r
index 085fd1b018692b347cbd266bda4b56f1f95cd98a..e2d3fb28e0788293ff208a65599d697a286cbfb4 100644 (file)
@@ -9,8 +9,10 @@ package org.opendaylight.yangtools.sal.java.api.generator;
 
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileWriter;
 import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
@@ -19,6 +21,10 @@ import org.opendaylight.yangtools.sal.binding.model.api.CodeGenerator;
 import org.opendaylight.yangtools.sal.binding.model.api.Type;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonatype.plexus.build.incremental.BuildContext;
+import org.sonatype.plexus.build.incremental.DefaultBuildContext;
+
+import com.google.common.base.Preconditions;
 
 /**
  * Generates files with JAVA source codes for every specified type.
@@ -38,23 +44,45 @@ public final class GeneratorJavaFile {
      */
     private final Set<? extends Type> types;
 
+    /**
+     * BuildContext used for instantiating files
+     */
+    private final BuildContext buildContext;
+
     /**
      * Creates instance of this class with the set of <code>types</code> for
      * which the JAVA code is generated.
      *
      * The instances of concrete JAVA code generator are created.
      *
+     * @param buildContext
+     *            build context to use for accessing files
      * @param types
      *            set of types for which JAVA code should be generated
      */
-    public GeneratorJavaFile(final Set<? extends Type> types) {
-        this.types = types;
+    public GeneratorJavaFile(final BuildContext buildContext, final Set<? extends Type> types) {
+        this.buildContext = Preconditions.checkNotNull(buildContext);
+        this.types = Preconditions.checkNotNull(types);
         generators.add(new InterfaceGenerator());
         generators.add(new TOGenerator());
         generators.add(new EnumGenerator());
         generators.add(new BuilderGenerator());
     }
 
+    /**
+     * Creates instance of this class with the set of <code>types</code> for
+     * which the JAVA code is generated. Generator instantiated this way uses
+     * the default build context, e.g. it will re-generate any and all files.
+     *
+     * The instances of concrete JAVA code generator are created.
+     *
+     * @param types
+     *            set of types for which JAVA code should be generated
+     */
+    public GeneratorJavaFile(final Set<? extends Type> types) {
+        this(new DefaultBuildContext(), types);
+    }
+
     /**
      * Generates list of files with JAVA source code. Only the suitable code
      * generator is used to generate the source code for the concrete type.
@@ -63,7 +91,7 @@ public final class GeneratorJavaFile {
      *            directory to which the output source codes should be generated
      * @return list of output files
      * @throws IOException
-     *             if the error during writting to the file occures
+     *             if the error during writing to the file occurs
      */
     public List<File> generateToFile(final File parentDirectory) throws IOException {
         final List<File> result = new ArrayList<>();
@@ -96,7 +124,7 @@ public final class GeneratorJavaFile {
      *            code generator which is used for generating of the source code
      * @return file which contains JAVA source code
      * @throws IOException
-     *             if the error during writting to the file occures
+     *             if the error during writing to the file occurs
      * @throws IllegalArgumentException
      *             if <code>type</code> equals <code>null</code>
      * @throws IllegalStateException
@@ -123,19 +151,20 @@ public final class GeneratorJavaFile {
         }
 
         if (generator.isAcceptable(type)) {
-            String generatedCode = generator.generate(type);
+            final String generatedCode = generator.generate(type);
             if (generatedCode.isEmpty()) {
                 throw new IllegalStateException("Generated code should not be empty!");
             }
             final File file = new File(packageDir, generator.getUnitName(type) + ".java");
-            try (final FileWriter fw = new FileWriter(file)) {
-                file.createNewFile();
-                try (final BufferedWriter bw = new BufferedWriter(fw)) {
-                    bw.write(generatedCode);
+            try (final OutputStream stream = buildContext.newFileOutputStream(file)) {
+                try (final Writer fw = new OutputStreamWriter(stream)) {
+                    try (final BufferedWriter bw = new BufferedWriter(fw)) {
+                        bw.write(generatedCode);
+                    }
+                } catch (IOException e) {
+                    LOG.error("Failed to write generate output into {}", file.getPath(), e);
+                    throw e;
                 }
-            } catch (IOException e) {
-                LOG.error(e.getMessage());
-                throw new IOException(e);
             }
             return file;
         }
index ed004151367715fbe3de283e22d4bda123664764..990b2fda2e750a231f00fcd971291a1ae8227ce2 100644 (file)
             <artifactId>yang-data-api</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.sonatype.plexus</groupId>
+            <artifactId>plexus-build-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.codehaus.plexus</groupId>
+            <artifactId>plexus-slf4j-logging</artifactId>
+        </dependency>
 
         <dependency>
             <groupId>junit</groupId>
index 6a7359f3664639d9839c69e2b6bf7813b786beab..8fde4501486af236b764b5ae4f77e958cfc917f5 100644 (file)
@@ -23,9 +23,14 @@ import org.opendaylight.yangtools.sal.binding.model.api.Type;
 import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang2sources.spi.BuildContextAware;
 import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
+import org.sonatype.plexus.build.incremental.BuildContext;
 
-public final class CodeGeneratorImpl implements CodeGenerator {
+import com.google.common.base.Preconditions;
+
+public final class CodeGeneratorImpl implements CodeGenerator, BuildContextAware {
+    private BuildContext buildContext;
 
     @Override
     public Collection<File> generateSources(final SchemaContext context, final File outputDir,
@@ -40,7 +45,7 @@ public final class CodeGeneratorImpl implements CodeGenerator {
 
         final BindingGenerator bindingGenerator = new BindingGeneratorImpl();
         final List<Type> types = bindingGenerator.generateTypes(context, yangModules);
-        final GeneratorJavaFile generator = new GeneratorJavaFile(new HashSet<>(types));
+        final GeneratorJavaFile generator = new GeneratorJavaFile(buildContext, new HashSet<>(types));
 
         return generator.generateToFile(outputBaseDir);
     }
@@ -66,4 +71,9 @@ public final class CodeGeneratorImpl implements CodeGenerator {
         // no additional information needed
     }
 
+    @Override
+    public void setBuildContext(BuildContext buildContext) {
+        this.buildContext = Preconditions.checkNotNull(buildContext);
+    }
+
 }
index 97ee2d8d8a28af19331d0a6a34b79439c2e64e97..3bfc2f3d59154715cbec0ee30482e79db130185c 100644 (file)
@@ -6,8 +6,9 @@ import java.util.Set
 import org.opendaylight.yangtools.yang.model.api.Module\r
 import java.io.IOException\r
 import java.util.HashSet\r
-import java.io.FileWriter\r
 import java.io.BufferedWriter\r
+import java.io.OutputStream;\r
+import java.io.OutputStreamWriter;\r
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode\r
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode\r
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition\r
@@ -35,6 +36,9 @@ import java.util.ArrayList
 import java.util.Map\r
 import org.opendaylight.yangtools.yang.model.api.SchemaPath\r
 \r
+import org.sonatype.plexus.build.incremental.BuildContext;\r
+import org.sonatype.plexus.build.incremental.DefaultBuildContext;\r
+\r
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode\r
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode\r
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode\r
@@ -53,6 +57,7 @@ class GeneratorImpl {
     File path\r
     static val REVISION_FORMAT = new SimpleDateFormat("yyyy-MM-dd")\r
     static val Logger LOG = LoggerFactory.getLogger(GeneratorImpl)\r
+    static val BuildContext CTX = new DefaultBuildContext();\r
     var Module currentModule;\r
 \r
 \r
@@ -69,8 +74,7 @@ class GeneratorImpl {
     def generateDocumentation(Module module) {\r
         val destination = new File(path, '''«module.name».html''')\r
         try {\r
-            val fw = new FileWriter(destination)\r
-            destination.createNewFile();\r
+            val fw = new OutputStreamWriter(CTX.newFileOutputStream(destination))\r
             val bw = new BufferedWriter(fw)\r
             currentModule = module;\r
             bw.append(module.generate);\r