Skip to content

Commit

Permalink
GH-69 Migrate from JAXB to Jackson (Resolve #69)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed May 21, 2020
1 parent 0cad249 commit 730576b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 121 deletions.
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<groupId>org.panda-lang</groupId>
<artifactId>reposilite</artifactId>
<version>2.3.2</version>
<version>2.3.4</version>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
Expand Down Expand Up @@ -88,14 +88,14 @@
<version>1.26</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.10.1</version>
</dependency>

<!-- Tests -->
Expand Down Expand Up @@ -138,7 +138,7 @@
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>log4j:log4j</artifact>
<artifact>com.fasterxml.woodstox:woodstox-core</artifact>
<includes>
<include>**</include>
</includes>
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/org/panda_lang/reposilite/metadata/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

package org.panda_lang.reposilite.metadata;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import java.io.Serializable;

@XmlRootElement(name = "metadata")
@XmlAccessorType(XmlAccessType.FIELD)
@JacksonXmlRootElement(localName = "metadata")
final class Metadata implements Serializable {

private String groupId;
Expand All @@ -41,19 +39,19 @@ final class Metadata implements Serializable {

}

String getGroupId() {
public String getGroupId() {
return groupId;
}

String getArtifactId() {
public String getArtifactId() {
return artifactId;
}

String getVersion() {
public String getVersion() {
return version;
}

Versioning getVersioning() {
public Versioning getVersioning() {
return versioning;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.panda_lang.reposilite.metadata;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.jetbrains.annotations.Nullable;
import org.panda_lang.reposilite.Reposilite;
import org.panda_lang.reposilite.repository.Repository;
Expand All @@ -24,9 +26,6 @@
import org.panda_lang.utilities.commons.FileUtils;
import org.panda_lang.utilities.commons.StringUtils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -39,6 +38,11 @@ public final class MetadataService {

private final Map<String, String> metadataCache = new HashMap<>();

private final XmlMapper xmlMapper = XmlMapper.xmlBuilder()
.serializationInclusion(Include.NON_NULL)
.defaultUseWrapper(false)
.build();

public @Nullable String generateMetadata(Repository repository, String[] requested) throws IOException {
File metadataFile = RepositoryUtils.toRequestedFile(repository, requested);
String cachedContent = metadataCache.get(metadataFile.getPath());
Expand All @@ -64,15 +68,14 @@ public final class MetadataService {
return generateSnapshotMetadata(metadataFile, MetadataUtils.toGroup(requested, 3), artifactDirectory);
}

private @Nullable String generateArtifactMetadata(File metadataFile, String groupId, File artifactDirectory, File[] list) throws IOException {
File latest = MetadataUtils.getLatest(list);
private @Nullable String generateArtifactMetadata(File metadataFile, String groupId, File artifactDirectory, File[] versions) throws IOException {
File latest = MetadataUtils.getLatest(versions);

if (latest == null) {
return null;
}

Versions versions = Versions.of(list);
Versioning versioning = new Versioning(latest.getName(), latest.getName(), versions, null, null, MetadataUtils.toUpdateTime(latest));
Versioning versioning = new Versioning(latest.getName(), latest.getName(), MetadataUtils.toNames(versions), null, null, MetadataUtils.toUpdateTime(latest));
Metadata metadata = new Metadata(groupId, artifactDirectory.getName(), null, versioning);

return toMetadataFile(metadataFile, metadata);
Expand Down Expand Up @@ -133,24 +136,15 @@ public final class MetadataService {
}

private String toMetadataFile(File metadataFile, Metadata metadata) throws IOException {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Metadata.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(metadata, metadataFile);
} catch (JAXBException e) {
Reposilite.getLogger().error("Internal error in " + metadataFile.getPath(), e);
return null;
}
String serializedMetadata = xmlMapper.writeValueAsString(metadata);
FileUtils.overrideFile(metadataFile, serializedMetadata);
metadataCache.put(metadataFile.getPath(), serializedMetadata);

if (!FilesUtils.writeFileChecksums(metadataFile.toPath())) {
Reposilite.getLogger().error("Cannot write metadata checksums");
}

String content = FileUtils.getContentOfFile(metadataFile);
metadataCache.put(metadataFile.getPath(), content);

return content;
return serializedMetadata;
}

public void clearMetadata(File metadataFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public final class MetadataUtils {
Expand Down Expand Up @@ -81,6 +82,12 @@ public static String toIdentifier(String artifact, String version, File build) {
return declassifyIdentifier(identifier);
}

public static List<String> toNames(File[] files) {
return Stream.of(files)
.map(File::getName)
.toJavaList();
}

private static String declassifyIdentifier(String identifier) {
int occurrences = StringUtils.countOccurrences(identifier, "-");

Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/panda_lang/reposilite/metadata/Snapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

package org.panda_lang.reposilite.metadata;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@XmlRootElement(name = "snapshot")
@XmlAccessorType(XmlAccessType.FIELD)
@JacksonXmlRootElement(localName = "snapshot")
final class Snapshot {

private String timestamp;
Expand All @@ -36,11 +33,11 @@ final class Snapshot {

}

String getBuildNumber() {
public String getBuildNumber() {
return buildNumber;
}

String getTimestamp() {
public String getTimestamp() {
return timestamp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

package org.panda_lang.reposilite.metadata;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@XmlRootElement(name = "snapshotVersion")
@XmlAccessorType(XmlAccessType.FIELD)
@JacksonXmlRootElement(localName = "snapshotVersion")
final class SnapshotVersion {

private String extension;
Expand All @@ -38,15 +35,15 @@ final class SnapshotVersion {

}

String getExtension() {
public String getExtension() {
return extension;
}

String getValue() {
public String getValue() {
return value;
}

String getUpdated() {
public String getUpdated() {
return updated;
}

Expand Down
34 changes: 17 additions & 17 deletions src/main/java/org/panda_lang/reposilite/metadata/Versioning.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@

package org.panda_lang.reposilite.metadata;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import java.util.Collection;

@XmlRootElement(name = "versioning")
@XmlAccessorType(XmlAccessType.FIELD)
@JacksonXmlRootElement(localName = "versioning")
final class Versioning {

private String release;
private String latest;
private Versions versions;
@JacksonXmlElementWrapper(localName = "versions")
@JacksonXmlProperty(localName = "version")
private Collection<String> versions;
private Snapshot snapshot;
@XmlElementWrapper(name = "snapshotVersions")
@XmlElement(name = "snapshotVersion")
@JacksonXmlElementWrapper(localName = "snapshotVersions")
@JacksonXmlProperty(localName = "snapshotVersion")
private Collection<SnapshotVersion> snapshotVersions;
private String lastUpdated;

Versioning(String release, String latest, Versions versions, Snapshot snapshot, Collection<SnapshotVersion> snapshotVersions, String lastUpdated) {
Versioning(String release, String latest, Collection<String> versions, Snapshot snapshot, Collection<SnapshotVersion> snapshotVersions, String lastUpdated) {
this.release = release;
this.latest = latest;
this.versions = versions;
Expand All @@ -49,27 +49,27 @@ final class Versioning {

}

String getRelease() {
public String getRelease() {
return release;
}

String getLatest() {
public String getLatest() {
return latest;
}

Versions getVersions() {
public Collection<String> getVersions() {
return versions;
}

Snapshot getSnapshot() {
public Snapshot getSnapshot() {
return snapshot;
}

Collection<SnapshotVersion> getSnapshotVersions() {
public Collection<SnapshotVersion> getSnapshotVersions() {
return snapshotVersions;
}

String getLastUpdated() {
public String getLastUpdated() {
return lastUpdated;
}

Expand Down
52 changes: 0 additions & 52 deletions src/main/java/org/panda_lang/reposilite/metadata/Versions.java

This file was deleted.

0 comments on commit 730576b

Please sign in to comment.