Skip to content

Commit 0d7c5b7

Browse files
authored
Use a caching writer to avoid overwriting identical files (#116)
1 parent 7440d71 commit 0d7c5b7

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

modello-core/src/main/java/org/codehaus/modello/plugin/AbstractModelloGenerator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
*/
2424

2525
import java.io.File;
26+
import java.io.Writer;
27+
import java.nio.charset.Charset;
28+
import java.nio.file.Path;
2629
import java.util.ArrayList;
2730
import java.util.List;
2831
import java.util.Properties;
@@ -287,4 +290,11 @@ protected BuildContext getBuildContext()
287290
{
288291
return buildContext;
289292
}
293+
294+
protected Writer newWriter( Path path )
295+
{
296+
Charset charset = getEncoding() != null ? Charset.forName( getEncoding() ) : Charset.defaultCharset();
297+
return new CachingWriter( getBuildContext(), path, charset );
298+
}
299+
290300
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.codehaus.modello.plugin;
2+
3+
import java.io.IOException;
4+
import java.io.StringWriter;
5+
import java.nio.charset.Charset;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.util.Objects;
9+
10+
import org.sonatype.plexus.build.incremental.BuildContext;
11+
12+
public class CachingWriter extends StringWriter
13+
{
14+
private final BuildContext buildContext;
15+
private final Path path;
16+
private final Charset charset;
17+
18+
public CachingWriter( BuildContext buildContext, Path path, Charset charset )
19+
{
20+
this.buildContext = buildContext;
21+
this.path = Objects.requireNonNull( path );
22+
this.charset = Objects.requireNonNull( charset );
23+
}
24+
25+
@Override
26+
public void close() throws IOException
27+
{
28+
String str = getBuffer().toString();
29+
if ( Files.exists( path ) )
30+
{
31+
String old = readString( path, charset );
32+
if ( str.equals( old ) )
33+
{
34+
return;
35+
}
36+
}
37+
writeString( path, str, charset );
38+
if ( buildContext != null )
39+
{
40+
buildContext.refresh( path.toFile() );
41+
}
42+
}
43+
44+
private static String readString( Path path, Charset charset ) throws IOException
45+
{
46+
byte[] ba = Files.readAllBytes( path );
47+
return new String( ba, charset );
48+
}
49+
50+
private static void writeString( Path path, String str, Charset charset ) throws IOException
51+
{
52+
byte[] ba = str.getBytes( charset );
53+
Files.write( path, ba );
54+
}
55+
}

modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/AbstractJavaModelloGenerator.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424

2525
import java.io.File;
2626
import java.io.IOException;
27-
import java.io.OutputStream;
2827
import java.io.Writer;
28+
import java.nio.charset.Charset;
29+
import java.nio.file.Path;
2930
import java.text.DateFormat;
3031
import java.text.ParseException;
3132
import java.text.SimpleDateFormat;
@@ -47,6 +48,7 @@
4748
import org.codehaus.modello.model.ModelInterface;
4849
import org.codehaus.modello.model.ModelType;
4950
import org.codehaus.modello.plugin.AbstractModelloGenerator;
51+
import org.codehaus.modello.plugin.CachingWriter;
5052
import org.codehaus.modello.plugin.java.javasource.JClass;
5153
import org.codehaus.modello.plugin.java.javasource.JComment;
5254
import org.codehaus.modello.plugin.java.javasource.JInterface;
@@ -57,7 +59,6 @@
5759
import org.codehaus.modello.plugin.java.metadata.JavaModelMetadata;
5860
import org.codehaus.modello.plugin.model.ModelClassMetadata;
5961
import org.codehaus.plexus.util.StringUtils;
60-
import org.codehaus.plexus.util.WriterFactory;
6162

6263
/**
6364
* AbstractJavaModelloGenerator - similar in scope to {@link AbstractModelloGenerator} but with features that
@@ -102,12 +103,7 @@ protected JSourceWriter newJSourceWriter( String packageName, String className )
102103
f.getParentFile().mkdirs();
103104
}
104105

105-
OutputStream os = getBuildContext().newFileOutputStream( f );
106-
107-
Writer writer = ( getEncoding() == null ) ? WriterFactory.newPlatformWriter( os )
108-
: WriterFactory.newWriter( os, getEncoding() );
109-
110-
return new JSourceWriter( writer );
106+
return new JSourceWriter( newWriter( f.toPath() ) );
111107
}
112108

113109
private JComment getHeaderComment()

modello-plugins/modello-plugin-jsonschema/src/main/java/org/codehaus/modello/plugin/jsonschema/JsonSchemaGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private void generateJsonSchema( Properties parameters )
107107
.enable( JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature() )
108108
.enable( JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature() )
109109
.disable( JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS.mappedFeature() )
110-
.createGenerator( schemaFile, JsonEncoding.UTF8 );
110+
.createGenerator( newWriter( schemaFile.toPath() ) );
111111

112112
generator.useDefaultPrettyPrinter();
113113

0 commit comments

Comments
 (0)