Skip to content

Commit 4fd9f87

Browse files
authored
Merge pull request #501 from TheSnoozer/jackson-databind
replace jackson-databind with lightweight javax.json
2 parents b5d5320 + 858cfae commit 4fd9f87

File tree

6 files changed

+319
-61
lines changed

6 files changed

+319
-61
lines changed

core/pom.xml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<java.target>1.8</java.target>
1919

2020
<jgit.version>5.9.0.202009080501-r</jgit.version>
21+
<junit.version>4.12</junit.version>
2122
</properties>
2223

2324
<dependencies>
@@ -39,11 +40,6 @@
3940
<artifactId>plexus-build-api</artifactId>
4041
<version>0.0.7</version>
4142
</dependency>
42-
<dependency>
43-
<groupId>com.fasterxml.jackson.core</groupId>
44-
<artifactId>jackson-databind</artifactId>
45-
<version>2.11.2</version>
46-
</dependency>
4743
<dependency>
4844
<groupId>com.google.code.findbugs</groupId>
4945
<artifactId>jsr305</artifactId>
@@ -53,5 +49,24 @@
5349
<artifactId>java-ordered-properties</artifactId>
5450
<version>1.0.4</version>
5551
</dependency>
52+
<!-- json stuff -->
53+
<dependency>
54+
<groupId>javax.json</groupId>
55+
<artifactId>javax.json-api</artifactId>
56+
<version>1.1.4</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.glassfish</groupId>
60+
<artifactId>javax.json</artifactId>
61+
<version>1.1.4</version>
62+
</dependency>
63+
64+
<!-- Test stuff -->
65+
<dependency>
66+
<groupId>junit</groupId>
67+
<artifactId>junit</artifactId>
68+
<version>${junit.version}</version>
69+
<scope>test</scope>
70+
</dependency>
5671
</dependencies>
5772
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.core;
19+
20+
public class CannotReadFileException extends Exception {
21+
private static final long serialVersionUID = -9080356227094128542L;
22+
23+
public CannotReadFileException(Throwable cause) {
24+
super(cause);
25+
}
26+
}

core/src/main/java/pl/project13/core/PropertiesFileGenerator.java

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,19 @@
1717

1818
package pl.project13.core;
1919

20-
import com.fasterxml.jackson.core.type.TypeReference;
21-
import com.fasterxml.jackson.databind.MapperFeature;
22-
import com.fasterxml.jackson.databind.ObjectMapper;
2320
import nu.studer.java.util.OrderedProperties;
2421
import org.sonatype.plexus.build.incremental.BuildContext;
2522
import pl.project13.core.log.LoggerBridge;
23+
import pl.project13.core.util.JsonManager;
2624

2725
import javax.annotation.Nonnull;
2826
import java.io.*;
2927
import java.nio.charset.Charset;
3028
import java.nio.file.Files;
3129
import java.util.Comparator;
32-
import java.util.HashMap;
33-
import java.util.Map;
3430
import java.util.Properties;
3531

3632
public class PropertiesFileGenerator {
37-
private static final ObjectMapper MAPPER = new ObjectMapper().enable(MapperFeature.BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES);
3833

3934
private LoggerBridge log;
4035
private BuildContext buildContext;
@@ -63,11 +58,9 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
6358
try {
6459
if (isJsonFormat) {
6560
log.info("Reading existing json file [{}] (for module {})...", gitPropsFile.getAbsolutePath(), projectName);
66-
67-
persistedProperties = readJsonProperties(gitPropsFile, sourceCharset);
61+
persistedProperties = JsonManager.readJsonProperties(gitPropsFile, sourceCharset);
6862
} else {
6963
log.info("Reading existing properties file [{}] (for module {})...", gitPropsFile.getAbsolutePath(), projectName);
70-
7164
persistedProperties = readProperties(gitPropsFile, sourceCharset);
7265
}
7366

@@ -89,16 +82,11 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
8982
if (shouldGenerate) {
9083
Files.createDirectories(gitPropsFile.getParentFile().toPath());
9184
try (OutputStream outputStream = new FileOutputStream(gitPropsFile)) {
92-
OrderedProperties sortedLocalProperties = new OrderedProperties.OrderedPropertiesBuilder()
93-
.withSuppressDateInComment(true)
94-
.withOrdering(Comparator.nullsLast(Comparator.naturalOrder()))
95-
.build();
85+
OrderedProperties sortedLocalProperties = PropertiesFileGenerator.createOrderedProperties();
9686
localProperties.forEach((key, value) -> sortedLocalProperties.setProperty((String) key, (String) value));
9787
if (isJsonFormat) {
98-
try (Writer outputWriter = new OutputStreamWriter(outputStream, sourceCharset)) {
99-
log.info("Writing json file to [{}] (for module {})...", gitPropsFile.getAbsolutePath(), projectName);
100-
MAPPER.writerWithDefaultPrettyPrinter().writeValue(outputWriter, sortedLocalProperties);
101-
}
88+
log.info("Writing json file to [{}] (for module {})...", gitPropsFile.getAbsolutePath(), projectName);
89+
JsonManager.dumpJson(outputStream, sortedLocalProperties, sourceCharset);
10290
} else {
10391
log.info("Writing properties file to [{}] (for module {})...", gitPropsFile.getAbsolutePath(), projectName);
10492
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
@@ -120,6 +108,13 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
120108
}
121109
}
122110

111+
public static OrderedProperties createOrderedProperties() {
112+
return new OrderedProperties.OrderedPropertiesBuilder()
113+
.withSuppressDateInComment(true)
114+
.withOrdering(Comparator.nullsLast(Comparator.naturalOrder()))
115+
.build();
116+
}
117+
123118
public static File craftPropertiesOutputFile(File base, String propertiesFilename) {
124119
File returnPath = new File(base, propertiesFilename);
125120

@@ -131,29 +126,6 @@ public static File craftPropertiesOutputFile(File base, String propertiesFilenam
131126
return returnPath;
132127
}
133128

134-
private Properties readJsonProperties(@Nonnull File jsonFile, Charset sourceCharset) throws CannotReadFileException {
135-
final HashMap<String, Object> propertiesMap;
136-
137-
try (final FileInputStream fis = new FileInputStream(jsonFile)) {
138-
try (final InputStreamReader reader = new InputStreamReader(fis, sourceCharset)) {
139-
final TypeReference<HashMap<String, Object>> mapTypeRef =
140-
new TypeReference<HashMap<String, Object>>() {};
141-
142-
propertiesMap = MAPPER.readValue(reader, mapTypeRef);
143-
}
144-
} catch (final Exception ex) {
145-
throw new CannotReadFileException(ex);
146-
}
147-
148-
final Properties retVal = new Properties();
149-
150-
for (final Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
151-
retVal.setProperty(entry.getKey(), String.valueOf(entry.getValue()));
152-
}
153-
154-
return retVal;
155-
}
156-
157129
private Properties readProperties(@Nonnull File propertiesFile, Charset sourceCharset) throws CannotReadFileException {
158130
try (final FileInputStream fis = new FileInputStream(propertiesFile)) {
159131
try (final InputStreamReader reader = new InputStreamReader(fis, sourceCharset)) {
@@ -165,12 +137,4 @@ private Properties readProperties(@Nonnull File propertiesFile, Charset sourceCh
165137
throw new CannotReadFileException(ex);
166138
}
167139
}
168-
169-
static class CannotReadFileException extends Exception {
170-
private static final long serialVersionUID = -6290782570018307756L;
171-
172-
CannotReadFileException(Throwable cause) {
173-
super(cause);
174-
}
175-
}
176140
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.core.util;
19+
20+
import nu.studer.java.util.OrderedProperties;
21+
import pl.project13.core.CannotReadFileException;
22+
23+
import javax.annotation.Nonnull;
24+
import javax.json.Json;
25+
import javax.json.JsonReader;
26+
import javax.json.JsonString;
27+
import javax.json.stream.JsonGenerator;
28+
import javax.json.stream.JsonGeneratorFactory;
29+
import java.io.*;
30+
import java.nio.charset.Charset;
31+
import java.util.Collections;
32+
import java.util.Map;
33+
import java.util.Properties;
34+
35+
public class JsonManager {
36+
public static void dumpJson(OutputStream outputStream, OrderedProperties sortedLocalProperties, Charset sourceCharset) throws IOException {
37+
JsonGeneratorFactory jgf = Json.createGeneratorFactory(
38+
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true));
39+
40+
try (Writer outputWriter = new OutputStreamWriter(outputStream, sourceCharset)) {
41+
JsonGenerator jg = jgf.createGenerator(outputWriter);
42+
jg.writeStartObject();
43+
44+
for (Map.Entry e : sortedLocalProperties.entrySet()) {
45+
jg.write((String) e.getKey(), (String) e.getValue());
46+
}
47+
jg.writeEnd();
48+
jg.close();
49+
}
50+
51+
}
52+
53+
public static Properties readJsonProperties(@Nonnull File jsonFile, Charset sourceCharset) throws CannotReadFileException {
54+
Properties retVal = new Properties();
55+
try (FileInputStream fis = new FileInputStream(jsonFile)) {
56+
try (InputStreamReader reader = new InputStreamReader(fis, sourceCharset)) {
57+
try (JsonReader jsonReader = Json.createReader(reader)) {
58+
jsonReader.readObject().forEach((key, val) -> {
59+
if (val instanceof JsonString) {
60+
retVal.setProperty(key, ((JsonString) val).getString());
61+
} else {
62+
// TODO warning?
63+
}
64+
});
65+
}
66+
}
67+
} catch (IOException e) {
68+
throw new CannotReadFileException(e);
69+
}
70+
return retVal;
71+
}
72+
}

0 commit comments

Comments
 (0)