From 5e71f7673b9826e43434d20c95f202c9f3f5c386 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sat, 1 Oct 2022 15:45:33 +0200 Subject: [PATCH] ---No commit message--- --- src/org/atriasoft/island/Manifest.java | 17 +-- .../island/actions/DependencySync.java | 122 +++++++++--------- .../island/model/manifest/ConfigManifest.java | 18 +-- .../island/model/manifest/ManifestFile.java | 1 + .../island/model/maven/MavenMetadata.java | 3 +- .../island/model/maven/PomMaven.java | 1 + 6 files changed, 80 insertions(+), 82 deletions(-) diff --git a/src/org/atriasoft/island/Manifest.java b/src/org/atriasoft/island/Manifest.java index 4ace1b0..84d8092 100644 --- a/src/org/atriasoft/island/Manifest.java +++ b/src/org/atriasoft/island/Manifest.java @@ -10,7 +10,7 @@ import java.util.Map; import java.util.Map.Entry; import org.atriasoft.aknot.exception.AknotException; -import org.atriasoft.exml.Exml; +import org.atriasoft.exml.XmlMapper; import org.atriasoft.exml.exception.ExmlBuilderException; import org.atriasoft.exml.exception.ExmlException; import org.atriasoft.exml.exception.ExmlParserErrorMulti; @@ -283,12 +283,10 @@ public class Manifest { Log.debug("PArse main XML config " + rootDirectory); ManifestFile parsedElements = null; + final XmlMapper mapper = new XmlMapper(); try { - parsedElements = Exml.parseOne(this.manifestXml, ManifestFile.class, "manifest"); - } catch (final ExmlException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } catch (final AknotException e1) { + parsedElements = mapper.parse(this.manifestXml, ManifestFile.class); + } catch (final ExmlException | AknotException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } @@ -299,11 +297,8 @@ public class Manifest { Log.debug("PArse <> XML config " + maniPath); ManifestFile tmpManifest = null; try { - tmpManifest = Exml.parseOne(maniPath, ManifestFile.class, "manifest"); - } catch (final ExmlException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (final AknotException e) { + tmpManifest = mapper.parse(maniPath, ManifestFile.class); + } catch (final ExmlException | AknotException e) { // TODO Auto-generated catch block e.printStackTrace(); } diff --git a/src/org/atriasoft/island/actions/DependencySync.java b/src/org/atriasoft/island/actions/DependencySync.java index fc5620f..c4fed0c 100644 --- a/src/org/atriasoft/island/actions/DependencySync.java +++ b/src/org/atriasoft/island/actions/DependencySync.java @@ -26,7 +26,7 @@ import org.atriasoft.ejson.Ejson; import org.atriasoft.ejson.model.JsonArray; import org.atriasoft.ejson.model.JsonObject; import org.atriasoft.ejson.model.JsonString; -import org.atriasoft.exml.Exml; +import org.atriasoft.exml.XmlMapper; import org.atriasoft.exml.exception.ExmlException; import org.atriasoft.island.Config; import org.atriasoft.island.Env; @@ -63,24 +63,24 @@ public class DependencySync { public boolean sources = false; public Path createArtefactLinkFileName(final String fileName) { - Path outPath = Env.getIslandPathArtifactoryLink().resolve(fileName); + final Path outPath = Env.getIslandPathArtifactoryLink().resolve(fileName); return outPath; } public Path createArtefactLinkGLDFile(final String fileName) { - Path outPath = Env.getIslandPathArtifactoryLink().resolve("GLD_" + fileName + ".json"); + final Path outPath = Env.getIslandPathArtifactoryLink().resolve("GLD_" + fileName + ".json"); return outPath; } public Path createBasePkgLocalFileName(final List artefactories, final Dependency dependency, final String version) { - Artifactory artifactory = findArtifactory(artefactories, dependency.remote()); - String out = artifactory.getName() + "/" + dependency.org().replace(".", "/") + "/" + dependency.name() + "/" + version; - Path outPath = Env.getIslandPathArtifactory().resolve(out); + final Artifactory artifactory = findArtifactory(artefactories, dependency.remote()); + final String out = artifactory.getName() + "/" + dependency.org().replace(".", "/") + "/" + dependency.name() + "/" + version; + final Path outPath = Env.getIslandPathArtifactory().resolve(out); return outPath; } public String createBasePkgRemoteFileName(final List artefactories, final Dependency dependency, final String version) { - Artifactory artifactory = findArtifactory(artefactories, dependency.remote()); + final Artifactory artifactory = findArtifactory(artefactories, dependency.remote()); String out = artifactory.getFetch(); while (out.endsWith("/")) { out = out.substring(0, out.length() - 1); @@ -89,31 +89,31 @@ public class DependencySync { return out; } - private void createGLDFile(PomMaven mavenPom, Path createArtefactLinkGLDFile, Path artefactLink) { - GLDDependModel model = new GLDDependModel(); + private void createGLDFile(final PomMaven mavenPom, final Path createArtefactLinkGLDFile, final Path artefactLink) { + final GLDDependModel model = new GLDDependModel(); model.path.java.add(artefactLink.getFileName().toString()); model.groupId = mavenPom.groupId(); model.version = mavenPom.version(); if (mavenPom.dependencies() != null) { - for (PomDependency elem : mavenPom.dependencies()) { + for (final PomDependency elem : mavenPom.dependencies()) { model.dependency.add(elem.artifactId()); } } // TODO: for later : create a generic serializer ... - StringBuilder data = new StringBuilder(); - JsonObject root = new JsonObject(); + final StringBuilder data = new StringBuilder(); + final JsonObject root = new JsonObject(); root.put("type", new JsonString(model.type)); root.put("group-id", new JsonString(model.groupId)); root.put("version", new JsonString(model.version)); - JsonObject path = new JsonObject(); + final JsonObject path = new JsonObject(); root.put("path", path); - JsonArray java = new JsonArray(); - for (String elem : model.path.java) { + final JsonArray java = new JsonArray(); + for (final String elem : model.path.java) { java.add(new JsonString(elem)); } path.put("java", java); - JsonArray dep = new JsonArray(); - for (String elem : model.dependency) { + final JsonArray dep = new JsonArray(); + for (final String elem : model.dependency) { dep.add(new JsonString(elem)); } root.put("dependency", dep); @@ -139,10 +139,10 @@ public class DependencySync { private void downloadPackage(final List artefactories, final Dependency dependency, final List alreadyDone) throws Exception { Log.debug("download : " + dependency); - for (UpdateDependency elem : alreadyDone) { + for (final UpdateDependency elem : alreadyDone) { if (elem.dependency.org().equals(dependency.org()) && elem.dependency.name().equals(dependency.name()) && elem.dependency.remote().equals(dependency.remote())) { // find element ==> check version - for (String version : elem.download) { + for (final String version : elem.download) { if (version.equals(dependency.revision())) { // already download ==> skip ... return; @@ -151,15 +151,16 @@ public class DependencySync { } } - String metadataFile = createMavenMetadataRemoteFileName(artefactories, dependency); + final String metadataFile = createMavenMetadataRemoteFileName(artefactories, dependency); Log.verbose("Metadata position: " + metadataFile); - Path localPath = createMetadataLocalFileName(artefactories, dependency); - String dataAsString = readAllMatadataAndStore(metadataFile, localPath); + final Path localPath = createMetadataLocalFileName(artefactories, dependency); + final String dataAsString = readAllMatadataAndStore(metadataFile, localPath); Log.verbose("dataAsString: " + dataAsString); MavenMetadata metaData = null; + final XmlMapper mapper = new XmlMapper(); try { - metaData = Exml.parseOne(dataAsString, MavenMetadata.class, "metadata"); - } catch (ExmlException e) { + metaData = mapper.parse(dataAsString, MavenMetadata.class); + } catch (final ExmlException e) { // TODO Auto-generated catch block e.printStackTrace(); } @@ -169,17 +170,17 @@ public class DependencySync { // TODO : Check if the version is download - String lastReleaseTag = metaData.versioning().release(); + final String lastReleaseTag = metaData.versioning().release(); - String remotePakageFileName = createBasePkgRemoteFileName(artefactories, dependency, lastReleaseTag); - Path localPackageFileName = createBasePkgLocalFileName(artefactories, dependency, lastReleaseTag); + final String remotePakageFileName = createBasePkgRemoteFileName(artefactories, dependency, lastReleaseTag); + final Path localPackageFileName = createBasePkgLocalFileName(artefactories, dependency, lastReleaseTag); // pom sources String base = getPomFileName(dependency, lastReleaseTag); readRemoteFileAndStore(remotePakageFileName + "/" + base, localPackageFileName.resolve(base), base); PomMaven mavenPom = null; try { - mavenPom = Exml.parseOne(localPackageFileName.resolve(base), PomMaven.class, "project"); - } catch (ExmlException e) { + mavenPom = mapper.parse(localPackageFileName.resolve(base), PomMaven.class); + } catch (final ExmlException e) { // TODO Auto-generated catch block e.printStackTrace(); } @@ -210,7 +211,7 @@ public class DependencySync { readRemoteFileAndStore(remotePakageFileName + "/" + base, localPackageFileName.resolve(base), base); */ boolean find = false; - for (UpdateDependency elem : alreadyDone) { + for (final UpdateDependency elem : alreadyDone) { if (elem.dependency.org().equals(dependency.org()) && elem.dependency.name().equals(dependency.name()) && elem.dependency.remote().equals(dependency.remote())) { // find element ==> check version elem.download.add(dependency.revision()); @@ -219,14 +220,14 @@ public class DependencySync { } } if (!find) { - UpdateDependency tmpp = new UpdateDependency(); + final UpdateDependency tmpp = new UpdateDependency(); tmpp.dependency = dependency; tmpp.download.add(dependency.revision()); alreadyDone.add(tmpp); } if (mavenPom != null && mavenPom.dependencies() != null) { - for (PomDependency value : mavenPom.dependencies()) { - Dependency dependencyTmp = new Dependency(value.groupId(), value.artifactId(), value.version(), dependency.remote()); + for (final PomDependency value : mavenPom.dependencies()) { + final Dependency dependencyTmp = new Dependency(value.groupId(), value.artifactId(), value.version(), dependency.remote()); downloadPackage(artefactories, dependencyTmp, alreadyDone); } } @@ -234,28 +235,28 @@ public class DependencySync { @ArgExecute public void execute() throws ActionException, Exception { - List alreadyDone = new ArrayList<>(); + final List alreadyDone = new ArrayList<>(); // check system is OK Manifest.checkIsInit(); - ConfigManifest configuration = Config.getUniqueConfig(); + final ConfigManifest configuration = Config.getUniqueConfig(); // load the manifest after pulling it (if possible) - Path file_source_manifest = Env.getIslandPathManifest().resolve(configuration.getManifestName()); + final Path file_source_manifest = Env.getIslandPathManifest().resolve(configuration.getManifestName()); if (!Files.exists(file_source_manifest)) { Log.critical("Missing manifest file : '" + file_source_manifest + "'"); } - Manifest mani = new Manifest(file_source_manifest); + final Manifest mani = new Manifest(file_source_manifest); - List artefactories = mani.getAllArtefactories(); - List dependencies = mani.getAllDependencies(); + final List artefactories = mani.getAllArtefactories(); + final List dependencies = mani.getAllDependencies(); Log.info("Synchronize of: " + dependencies.size() + " dependenc" + Tools.getPlural(dependencies)); Log.info("base on: " + artefactories.size() + " artefactor" + Tools.getPlural(artefactories)); int id_element = 0; - for (Dependency elem : dependencies) { + for (final Dependency elem : dependencies) { id_element++; - String base_display = Tools.getListBaseDisplay(id_element, dependencies.size(), elem); + final String base_display = Tools.getListBaseDisplay(id_element, dependencies.size(), elem); Log.info("dep-sync : " + base_display); Log.debug("elem : " + elem); downloadPackage(artefactories, elem, alreadyDone); @@ -265,7 +266,7 @@ public class DependencySync { Artifactory findArtifactory(final List artefactories, final String name) { Artifactory out = null; - for (Artifactory elem : artefactories) { + for (final Artifactory elem : artefactories) { if ("default".equals(elem.getName())) { out = elem; break; @@ -275,8 +276,7 @@ public class DependencySync { if (out == null) { out = new Artifactory("default", "https://repo1.maven.org/maven2", "maven"); } - boolean find = false; - for (Artifactory elem : artefactories) { + for (final Artifactory elem : artefactories) { if (name.equals(elem.getName())) { return elem; } @@ -310,38 +310,38 @@ public class DependencySync { void listFiles(final Path path) { Log.info("List of file in " + path); try { - ZipFile zipFile = new ZipFile(path.toFile()); + final ZipFile zipFile = new ZipFile(path.toFile()); - Enumeration entries = zipFile.entries(); + final Enumeration entries = zipFile.entries(); while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); + final ZipEntry entry = entries.nextElement(); Log.info(" - " + entry.getName() + " (" + entry.getSize() + ")"); //InputStream stream = zipFile.getInputStream(entry); } - } catch (IOException ex) { + } catch (final IOException ex) { Log.error("Catch Exception : " + ex.getLocalizedMessage()); ex.printStackTrace(); } } String readAllMatadataAndStore(final String urlPath, final Path path) throws Exception { - URL url = new URL(urlPath); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); + final URL url = new URL(urlPath); + final HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setConnectTimeout(5000); con.setReadTimeout(5000); con.setInstanceFollowRedirects(false); - StringBuilder result = new StringBuilder(); + final StringBuilder result = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()))) { for (String line; (line = reader.readLine()) != null;) { result.append(line); result.append("\n"); } - } catch (Exception ex) { + } catch (final Exception ex) { Log.error("Can not retrive data from artefactory ..." + ex.getLocalizedMessage()); throw ex; } - String dataAsString = result.toString(); + final String dataAsString = result.toString(); Log.verbose("return=" + dataAsString); Tools.fileWriteData(path, dataAsString); return dataAsString; @@ -354,24 +354,24 @@ public class DependencySync { } try { if (!Files.exists(path.getParent())) { - File file = path.getParent().toFile(); + final File file = path.getParent().toFile(); if (!file.mkdirs()) { Log.critical("Can not create the path:" + path.getParent()); } } - URL url = new URL(urlPath); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); + final URL url = new URL(urlPath); + final HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setConnectTimeout(5000); con.setReadTimeout(5000); con.setInstanceFollowRedirects(false); final int BUFFER_SIZE = 1024 * 1024; - InputStream inputStream = con.getInputStream(); - Path tmpFile = path.getParent().resolve(path.getFileName() + "__tmp"); - FileOutputStream outputStream = new FileOutputStream(tmpFile.toFile()); + final InputStream inputStream = con.getInputStream(); + final Path tmpFile = path.getParent().resolve(path.getFileName() + "__tmp"); + final FileOutputStream outputStream = new FileOutputStream(tmpFile.toFile()); int bytesRead = -1; int totalRead = 0; - byte[] buffer = new byte[BUFFER_SIZE]; + final byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); totalRead += bytesRead; @@ -382,7 +382,7 @@ public class DependencySync { inputStream.close(); // this is the way to have atomic replacement of file and not partial downloaded file ==> move is 99.999999% atomic. Files.move(tmpFile, path); - } catch (FileNotFoundException ex) { + } catch (final FileNotFoundException ex) { Log.warning("File not found: " + urlPath); } finally { diff --git a/src/org/atriasoft/island/model/manifest/ConfigManifest.java b/src/org/atriasoft/island/model/manifest/ConfigManifest.java index 60c6d53..69bc2bd 100644 --- a/src/org/atriasoft/island/model/manifest/ConfigManifest.java +++ b/src/org/atriasoft/island/model/manifest/ConfigManifest.java @@ -9,13 +9,14 @@ import java.util.ListIterator; import org.atriasoft.aknot.annotation.AknotList; import org.atriasoft.aknot.annotation.AknotName; import org.atriasoft.aknot.exception.AknotException; -import org.atriasoft.exml.Exml; +import org.atriasoft.exml.XmlMapper; import org.atriasoft.exml.exception.ExmlBuilderException; import org.atriasoft.exml.exception.ExmlException; import org.atriasoft.island.Env; import org.atriasoft.island.internal.Log; import org.atriasoft.island.model.Volatile; +@AknotName("config-island") public class ConfigManifest { public static ConfigManifest load() { @@ -23,9 +24,10 @@ public class ConfigManifest { } public static ConfigManifest load(final Path path) { - ConfigManifest[] root = null; + ConfigManifest root = null; + final XmlMapper mapper = new XmlMapper(); try { - root = Exml.parse(path, ConfigManifest.class, "config-island"); + root = mapper.parse(path, ConfigManifest.class); } catch (final ExmlException e) { Log.error("Can not parse the file.1. " + path); e.printStackTrace(); @@ -33,10 +35,7 @@ public class ConfigManifest { Log.error("Can not parse the file.2. " + path); e.printStackTrace(); } - if (root.length != 1) { - Log.error("Can not parse the file.3. " + path); - } - return root[0]; + return root; } private String repo = ""; @@ -181,9 +180,10 @@ public class ConfigManifest { } public void store(final Path path) throws ExmlBuilderException { + final XmlMapper mapper = new XmlMapper(); try { - Exml.store(path, this, "config-island"); - } catch (final ExmlException | IOException ex) { + mapper.store(this, path); + } catch (final ExmlException | IOException | AknotException ex) { Log.warning("detect throw: " + ex.getMessage()); ex.printStackTrace(); } diff --git a/src/org/atriasoft/island/model/manifest/ManifestFile.java b/src/org/atriasoft/island/model/manifest/ManifestFile.java index 2569bd8..244c57a 100644 --- a/src/org/atriasoft/island/model/manifest/ManifestFile.java +++ b/src/org/atriasoft/island/model/manifest/ManifestFile.java @@ -10,6 +10,7 @@ import org.atriasoft.aknot.annotation.AknotManaged; import org.atriasoft.aknot.annotation.AknotName; @AknotIgnoreUnknown +@AknotName("manifest") public class ManifestFile { private Path fileRealPath = null; private List projects = new ArrayList<>(); diff --git a/src/org/atriasoft/island/model/maven/MavenMetadata.java b/src/org/atriasoft/island/model/maven/MavenMetadata.java index 38101ff..812835f 100644 --- a/src/org/atriasoft/island/model/maven/MavenMetadata.java +++ b/src/org/atriasoft/island/model/maven/MavenMetadata.java @@ -1,11 +1,12 @@ package org.atriasoft.island.model.maven; +import org.atriasoft.aknot.annotation.AknotDefaultAttribute; import org.atriasoft.aknot.annotation.AknotIgnoreUnknown; import org.atriasoft.aknot.annotation.AknotName; -import org.atriasoft.aknot.annotation.AknotDefaultAttribute; @AknotDefaultAttribute(false) @AknotIgnoreUnknown +@AknotName("metadata") public record MavenMetadata( @AknotName("groupId") String groupId, @AknotName("artifactId") String artifactId, diff --git a/src/org/atriasoft/island/model/maven/PomMaven.java b/src/org/atriasoft/island/model/maven/PomMaven.java index 11e141b..86168e1 100644 --- a/src/org/atriasoft/island/model/maven/PomMaven.java +++ b/src/org/atriasoft/island/model/maven/PomMaven.java @@ -10,6 +10,7 @@ import org.atriasoft.aknot.annotation.AknotOptional; @AknotDefaultNullValue @AknotIgnoreUnknown +@AknotName("project") public record PomMaven( @AknotName("modelVersion") String modelVersion, @AknotName("groupId") String groupId,