Skip to content

Commit

Permalink
Merge pull request mvnpm#1537 from phillip-kruger/store_sync
Browse files Browse the repository at this point in the history
Store sync queue in Database
  • Loading branch information
phillip-kruger authored Nov 6, 2023
2 parents 92c6e71 + 801a144 commit ea5080a
Show file tree
Hide file tree
Showing 39 changed files with 681 additions and 592 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.mvnpm</groupId>
<artifactId>mvnpm</artifactId>
<version>2.0.27-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>

<name>mvnpm</name>
<description>Maven on NPM</description>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/mvnpm/composite/CompositeCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.mvnpm.npm.model.Name;
import io.mvnpm.npm.model.NameParser;
import io.quarkus.logging.Log;
import io.smallrye.common.annotation.Blocking;

/**
* This group a set of artifacts into one
Expand All @@ -69,13 +70,15 @@ public class CompositeCreator {

private final MavenXpp3Reader mavenXpp3Writer = new MavenXpp3Reader();

@Blocking
public void buildAllComposites() {
Set<Path> poms = getAllCompositePoms();
for (Path pom : poms) {
build(pom, null); // TODO: Get latest version
}
}

@Blocking
public byte[] buildComposite(String artifactId, String version) {
Path compositesFolder = Paths.get(compositesDirectory);
Path pom = compositesFolder.resolve(artifactId + ".xml");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/mvnpm/composite/CompositeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class CompositeService {
FileStore fileStore;

public byte[] getFile(Name fullName, String version, FileType type) {
compositeCreator.buildComposite(fullName.mvnArtifactId(), version);
compositeCreator.buildComposite(fullName.mvnArtifactId, version);
Path localFilePath = fileStore.getLocalFullPath(type, fullName, version, Optional.empty());
return fileStore.readFile(localFilePath);
}
Expand All @@ -53,14 +53,14 @@ private byte[] getSignedFile(Name fullName, String version, FileType type, Strin

public Map<String, Date> getVersions(Name name) {
try {
Path groupRoute = fileStore.getGroupRoot(name.mvnGroupIdPath()).resolve(name.mvnArtifactId());
Path groupRoute = fileStore.getGroupRoot(name.mvnGroupIdPath()).resolve(name.mvnArtifactId);
List<Path> versionDirs = Files.walk(groupRoute)
.filter(Files::isDirectory)
.collect(Collectors.toList());
Map<String, Date> nameTimeMap = new HashMap<>();
for (Path versionDir : versionDirs) {
String v = versionDir.getFileName().toString();
if (!v.equalsIgnoreCase(name.mvnArtifactId())) {
if (!v.equalsIgnoreCase(name.mvnArtifactId)) {
BasicFileAttributes attr = Files.readAttributes(versionDir, BasicFileAttributes.class);
Date lastModified = Date.from(attr.lastModifiedTime().toInstant());
nameTimeMap.put(versionDir.getFileName().toString(), lastModified);
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/io/mvnpm/error/ErrorHandlingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.inject.Inject;

import io.mvnpm.log.EventLogEntry;
import io.mvnpm.mavencentral.sync.CentralSyncItem;
import io.mvnpm.mavencentral.sync.Stage;
import io.mvnpm.npm.model.Name;
import io.quarkus.logging.Log;
Expand All @@ -23,16 +24,20 @@ public class ErrorHandlingService {
@Inject
EventBus bus;

public void handle(CentralSyncItem centralSyncItem, Throwable t) {
handle(centralSyncItem.name, centralSyncItem.version, t);
}

public void handle(Name name, String version, Throwable t) {
handle(name.mvnGroupId(), name.mvnArtifactId(), version, t);
handle(name.mvnGroupId, name.mvnArtifactId, version, t);
}

public void handle(Name name, String version, String message) {
handle(name.mvnGroupId(), name.mvnArtifactId(), version, message, new RuntimeException(""));
handle(name.mvnGroupId, name.mvnArtifactId, version, message, new RuntimeException(""));
}

public void handle(Name name, String version, String message, Throwable t) {
handle(name.mvnGroupId(), name.mvnArtifactId(), version, message, t);
handle(name.mvnGroupId, name.mvnArtifactId, version, message, t);
}

public void handle(String groupId, String artifactId, String version, Throwable t) {
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/io/mvnpm/file/FileStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ public byte[] createFile(Path localFilePath, byte[] content) {
try {
Files.createDirectories(localFilePath.getParent());
Files.write(localFilePath, content);
String sha1 = FileUtil.getSha1(content);
Path localSha1FilePath = Paths.get(localFilePath.toString() + Constants.DOT_SHA1);
Files.writeString(localSha1FilePath, sha1);
return content;
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down Expand Up @@ -119,8 +116,8 @@ public Path getGroupRoot(String groupId) {
}

public Path getLocalDirectory(Name name, String version) {
return getGroupRoot(name.mvnPath()).resolve(
Paths.get(name.mvnArtifactId(), version));
return getGroupRoot(name.mvnPath).resolve(
Paths.get(name.mvnArtifactId, version));
}

public Path getLocalSha1FullPath(FileType type, io.mvnpm.npm.model.Package p) {
Expand Down Expand Up @@ -149,7 +146,7 @@ public Path getLocalFullPath(FileType type, io.mvnpm.npm.model.Package p, Option
}

public Path getLocalFullPath(FileType type, Name name, String version, Optional<String> dotSigned) {
return getLocalDirectory(name, version).resolve(getLocalFileName(type, name.mvnArtifactId(), version, dotSigned));
return getLocalDirectory(name, version).resolve(getLocalFileName(type, name.mvnArtifactId, version, dotSigned));
}

public Path getLocalFullPath(FileType type, String groupId, String artifactId, String version) {
Expand All @@ -161,7 +158,7 @@ public Path getLocalFullPath(FileType type, String groupId, String artifactId, S
}

public String getLocalFileName(FileType type, io.mvnpm.npm.model.Package p, Optional<String> dotSigned) {
return getLocalFileName(type, p.name().mvnArtifactId(), p.version(), dotSigned);
return getLocalFileName(type, p.name().mvnArtifactId, p.version(), dotSigned);
}

public String getLocalFileName(FileType type, String artifactId, String version, Optional<String> dotSigned) {
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/io/mvnpm/file/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import io.mvnpm.Constants;

Expand Down Expand Up @@ -76,22 +75,32 @@ public static void createMd5(Path outputPath) {
}
}

public static void createAsc(Path localFilePath) {
public static boolean createAsc(Path localFilePath) {
String outputFile = localFilePath.toString() + Constants.DOT_ASC;
Path f = Paths.get(outputFile);
if (!Files.exists(f)) {
try {
synchronized (f) {
synchronized (f) {
if (!Files.exists(f)) {
try {
Process process = Runtime.getRuntime().exec(GPG_COMMAND + localFilePath.toString());
ProcessHandle processHandle = process.toHandle();
// Set a timeout of 10 seconds
long timeout = 10;
boolean processFinished = process.waitFor(timeout, TimeUnit.SECONDS);

CompletableFuture<ProcessHandle> onProcessExit = processHandle.onExit();
onProcessExit.get();
if (!processFinished) {
process.destroy(); // If the process doesn't finish, we can destroy it
return false;
} else {
// Process finished within the timeout
int exitCode = process.exitValue();
process.destroy();
return true;
}
} catch (InterruptedException | IOException ex) {
throw new IllegalStateException(ex);
}
} catch (InterruptedException | ExecutionException | IOException ex) {
throw new IllegalStateException(ex);
}
}
return true;
}

private static final String GPG_COMMAND = "gpg -ab ";
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/mvnpm/file/ImportMapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public static byte[] createImportMap(io.mvnpm.npm.model.Package p) {
String module = getModule(p);
Map<String, String> v = new HashMap<>();

v.put(p.name().npmFullName(), root + module);
v.put(p.name().npmFullName() + Constants.SLASH, root + getModuleRoot(module));
v.put(p.name().npmFullName, root + module);
v.put(p.name().npmFullName + Constants.SLASH, root + getModuleRoot(module));

Imports imports = new Imports(v);

Expand All @@ -31,7 +31,7 @@ public static byte[] createImportMap(io.mvnpm.npm.model.Package p) {
}

public static String getImportMapRoot(io.mvnpm.npm.model.Package p) {
String root = STATIC_ROOT + p.name().npmName();
String root = STATIC_ROOT + p.name().npmName;
if (p.repository() != null && p.repository().directory() != null && !p.repository().directory().isEmpty()) {
String d = p.repository().directory();
if (d.startsWith(PACKAGES + Constants.SLASH)) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/io/mvnpm/file/metadata/MetadataClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public MetadataAndHash getMetadataAndHash(Name name) {

private Metadata getMetadata(Name name) {
Metadata metadata = new Metadata();
metadata.setGroupId(name.mvnGroupId());
metadata.setArtifactId(name.mvnArtifactId());
metadata.setGroupId(name.mvnGroupId);
metadata.setArtifactId(name.mvnArtifactId);
metadata.setVersioning(getVersioning(name));
return metadata;

Expand All @@ -84,7 +84,7 @@ private Versioning getInternalVersioning(Name name) {

Map<String, Date> versions = compositeService.getVersions(name);
if (versions.isEmpty())
throw new RuntimeException("No version found for " + name.displayName());
throw new RuntimeException("No version found for " + name.displayName);

boolean isFirst = true;
for (Map.Entry<String, Date> version : versions.entrySet()) {
Expand All @@ -102,15 +102,15 @@ private Versioning getInternalVersioning(Name name) {
versioning.addVersion(v.toString());
}
} catch (InvalidVersionException ive) {
Log.warn("Ignoring version [" + ive.getVersion() + "] for " + name.displayName());
Log.warn("Ignoring version [" + ive.getVersion() + "] for " + name.displayName);
}
}

return versioning;
}

private Versioning getNpmVersioning(Name name) {
Project project = npmRegistryFacade.getProject(name.npmFullName());
Project project = npmRegistryFacade.getProject(name.npmFullName);
Versioning versioning = new Versioning();
String latest = getLatest(project);
versioning.setLatest(latest);
Expand All @@ -126,7 +126,7 @@ private Versioning getNpmVersioning(Name name) {
}
}
} catch (InvalidVersionException ive) {
Log.warn("Ignoring version [" + ive.getVersion() + "] for " + name.displayName());
Log.warn("Ignoring version [" + ive.getVersion() + "] for " + name.displayName);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/mvnpm/file/type/JarClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private byte[] jarInput(io.mvnpm.npm.model.Package p, Path localFilePath, byte[]
JarArchiveOutputStream jarOutput = new JarArchiveOutputStream(byteOutput)) {

// Pom details
String pomXmlDir = POM_ROOT + p.name().mvnGroupId() + Constants.SLASH + p.name().mvnArtifactId() + Constants.SLASH;
String pomXmlDir = POM_ROOT + p.name().mvnGroupId + Constants.SLASH + p.name().mvnArtifactId + Constants.SLASH;

// Pom xml entry
writeJarEntry(jarOutput, pomXmlDir + POM_DOT_XML, pomBytes);
Expand Down Expand Up @@ -150,8 +150,8 @@ private boolean shouldIgnore(String name) {
private byte[] createPomProperties(io.mvnpm.npm.model.Package p) throws IOException {
Properties properties = new Properties();
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
properties.setProperty(Constants.GROUP_ID, p.name().mvnGroupId());
properties.setProperty(Constants.ARTIFACT_ID, p.name().mvnArtifactId());
properties.setProperty(Constants.GROUP_ID, p.name().mvnGroupId);
properties.setProperty(Constants.ARTIFACT_ID, p.name().mvnArtifactId);
properties.setProperty(Constants.VERSION, p.version());
properties.store(baos, POM_DOT_PROPERTIES_COMMENT);
return baos.toByteArray();
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/io/mvnpm/file/type/PomClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ private byte[] writePomToBytes(io.mvnpm.npm.model.Package p) {
Model model = new Model();

model.setModelVersion(MODEL_VERSION);
model.setGroupId(p.name().mvnGroupId());
model.setArtifactId(p.name().mvnArtifactId());
model.setGroupId(p.name().mvnGroupId);
model.setArtifactId(p.name().mvnArtifactId);
model.setVersion(p.version());
model.setPackaging(JAR);
model.setName(p.name().displayName());
model.setName(p.name().displayName);
if (p.description() == null || p.description().isEmpty()) {
model.setDescription(p.name().displayName());
model.setDescription(p.name().displayName);
} else {
model.setDescription(p.description());
}
Expand Down Expand Up @@ -140,7 +140,7 @@ private Organization toOrganization(io.mvnpm.npm.model.Package p) {
if (p.author() != null) {
o.setName(p.author().name());
} else {
o.setName(p.name().displayName());
o.setName(p.name().displayName);
}
if (p.homepage() != null) {
o.setUrl(p.homepage().toString());
Expand Down Expand Up @@ -220,8 +220,8 @@ private List<Dependency> toDependencies(Map<Name, String> dependencies) {

private Dependency toDependency(Name name, String version) {
Dependency d = new Dependency();
d.setGroupId(name.mvnGroupId());
d.setArtifactId(name.mvnArtifactId());
d.setGroupId(name.mvnGroupId);
d.setArtifactId(name.mvnArtifactId);
d.setVersion(toVersion(name, version));
return d;
}
Expand All @@ -231,7 +231,7 @@ private String toVersion(Name name, String version) {

// This is an open ended range. Let's get the latest for a bottom boundary
if (trimVersion.equals(OPEN_BLOCK + COMMA + CLOSE_ROUND)) {
Project project = npmRegistryFacade.getProject(name.npmFullName());
Project project = npmRegistryFacade.getProject(name.npmFullName);
return OPEN_BLOCK + project.distTags().latest() + COMMA + CLOSE_ROUND;
}
// TODO: Make other ranges more effient too ?
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/io/mvnpm/log/EventLogEntryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private EventLogEntryUtil() {

public static EventLogEntry toEventLogEntry(CentralSyncItem centralSyncItem) {
return EventLogEntryUtil.toEventLogEntry(centralSyncItem,
EventLogEntryUtil.generateMessage(centralSyncItem.getStage()));
EventLogEntryUtil.generateMessage(centralSyncItem.stage));
}

public static EventLogEntry toEventLogEntry(CentralSyncItem centralSyncItem, String message) {
Expand All @@ -27,12 +27,12 @@ public static EventLogEntry toEventLogEntry(CentralSyncItem centralSyncItem, Str
public static EventLogEntry toEventLogEntry(CentralSyncItem centralSyncItem, String message, String color) {
EventLogEntry eventLogEntry = new EventLogEntry();

eventLogEntry.groupId = centralSyncItem.getNameVersionType().name().mvnGroupId();
eventLogEntry.artifactId = centralSyncItem.getNameVersionType().name().mvnArtifactId();
eventLogEntry.version = centralSyncItem.getNameVersionType().version();
eventLogEntry.stage = centralSyncItem.getStage();
eventLogEntry.groupId = centralSyncItem.name.mvnGroupId;
eventLogEntry.artifactId = centralSyncItem.name.mvnArtifactId;
eventLogEntry.version = centralSyncItem.version;
eventLogEntry.stage = centralSyncItem.stage;
eventLogEntry.message = message;
eventLogEntry.time = centralSyncItem.getStageChangeTime();
eventLogEntry.time = centralSyncItem.stageChangeTime;
eventLogEntry.color = color;
return eventLogEntry;
}
Expand All @@ -42,7 +42,7 @@ private static String generateMessage(Stage stage) {
case INIT -> "Syncing initialized";
case UPLOADING -> "Uploading to OSS sonatype";
case UPLOADED -> "Uploaded to OSS sonatype, now validating";
case CLOSED -> "Validated, now closing";
case CLOSED -> "Closed and validated. Will be auto releasing soon";
case RELEASING -> "Closed, now releasing to Maven central";
case RELEASED -> "Released to Maven central";
default -> stage.name().toLowerCase();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/mvnpm/maven/MavenRepositoryApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Response getPackageJson(@PathParam("gavt") String gavt) {
if (nameVersionType.name().isInternal()) {
return Response.ok().build(); // TODO: Can we return this in some format ?
} else {
return Response.ok(npmRegistryFacade.getPackage(nameVersionType.name().npmFullName(), nameVersionType.version()))
return Response.ok(npmRegistryFacade.getPackage(nameVersionType.name().npmFullName, nameVersionType.version()))
.build();
}
}
Expand All @@ -93,7 +93,7 @@ public Response getImportMap(@PathParam("gavt") String gavt) {
byte[] importMap = compositeService.getImportMap(nameVersionType.name(), nameVersionType.version());
return Response.ok(importMap).build();
} else {
Package npmPackage = npmRegistryFacade.getPackage(nameVersionType.name().npmFullName(), nameVersionType.version());
Package npmPackage = npmRegistryFacade.getPackage(nameVersionType.name().npmFullName, nameVersionType.version());
return Response.ok(ImportMapUtil.createImportMap(npmPackage)).build();
}
}
Expand Down
Loading

0 comments on commit ea5080a

Please sign in to comment.