Skip to content

Commit

Permalink
Merge pull request strongbox#1919 from luan-cestari/master
Browse files Browse the repository at this point in the history
Change MavenArtifactGenerator to reduce File and related usage
  • Loading branch information
carlspring committed Oct 25, 2020
2 parents c9c72a5 + 00835ef commit 545d049
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 111 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,4 @@ To accept, please:
| Ankit Tomar | | Gurgaon, India | 2019-11-10 |
| Valentin Bojinov | | Sofia, Bulgaria | 2020-04-01 |
| Georg Leber | CGH IT-Solutions | Aachen, Germany | 2020-10-15 |

| Luan Cestari | | São Paulo, Brazil | 2020-04-01 |
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.digest.MessageDigestAlgorithms;
import org.carlspring.commons.io.RandomInputStream;
import org.carlspring.strongbox.io.LayoutOutputStream;
import org.carlspring.strongbox.util.MessageDigestUtils;
import org.carlspring.strongbox.util.TestFileUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,20 @@ private void deployMetadata(Metadata metadata,
{
File metadataFile = new File(getBasedir(), metadataPath);

InputStream is = new FileInputStream(metadataFile);
MultipleDigestInputStream mdis = new MultipleDigestInputStream(is);

String url = client.getContextBaseUrl() + "/storages/" + storageId + "/" + repositoryId + "/" + metadataPath;
try (InputStream is = new FileInputStream(metadataFile);
MultipleDigestInputStream mdis = new MultipleDigestInputStream(is))
{
String url = client.getContextBaseUrl() + "/storages/" + storageId + "/" + repositoryId + "/" + metadataPath;

logger.debug("Deploying {}...", url);
logger.debug("Deploying {}...", url);

client.deployMetadata(is, url, metadataPath.substring(metadataPath.lastIndexOf("/")));
client.deployMetadata(is, url, metadataPath.substring(metadataPath.lastIndexOf("/")));

deployChecksum(mdis,
storageId,
repositoryId,
metadataPath.substring(0, metadataPath.lastIndexOf('/') + 1), "maven-metadata.xml");
deployChecksum(mdis,
storageId,
repositoryId,
metadataPath.substring(0, metadataPath.lastIndexOf('/') + 1), "maven-metadata.xml");
}
}

private void deployChecksum(MultipleDigestInputStream mdis,
Expand Down Expand Up @@ -196,10 +197,12 @@ public Metadata retrieveMetadata(String path)
{
if (client.pathExists(path))
{
InputStream is = client.getResource(path);
MetadataXpp3Reader reader = new MetadataXpp3Reader();
try (InputStream is = client.getResource(path))
{
MetadataXpp3Reader reader = new MetadataXpp3Reader();

return reader.read(is);
return reader.read(is);
}
}

return null;
Expand All @@ -215,7 +218,6 @@ public void deploy(Artifact artifact,
try (InputStream is = new FileInputStream(artifactFile);
MultipleDigestInputStream ais = new MultipleDigestInputStream(is))
{

String url = client.getContextBaseUrl() + "/storages/" + storageId + "/" + repositoryId + "/" + artifactToPath;

logger.debug("Deploying {}...", url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.*;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -116,7 +117,7 @@ public void generate(String ga, String packaging, String... versions)
for (String version : versions)
{
Artifact artifact = MavenArtifactTestUtils.getArtifactFromGAVTC(ga + ":" + version);
artifact.setFile(new File(getBasedir() + "/" + MavenArtifactUtils.convertArtifactToPath(artifact)));
artifact.setFile(basedir.resolve("/" + MavenArtifactUtils.convertArtifactToPath(artifact)).toFile());

generate(artifact, packaging);
}
Expand All @@ -129,7 +130,7 @@ public void generate(String ga, String... versions)
for (String version : versions)
{
Artifact artifact = MavenArtifactTestUtils.getArtifactFromGAVTC(ga + ":" + version);
artifact.setFile(new File(getBasedir() + "/" + MavenArtifactUtils.convertArtifactToPath(artifact)));
artifact.setFile(basedir.resolve("/" + MavenArtifactUtils.convertArtifactToPath(artifact)).toFile());

generate(artifact);
}
Expand Down Expand Up @@ -164,17 +165,17 @@ public void createArchive(Artifact artifact)
createArchive(artifact, new Random().nextInt(ArtifactGenerator.DEFAULT_BYTES_SIZE));
}

public void createArchive(Artifact artifact, long bytesSize)
public void createArchive(Artifact artifact,
long bytesSize)
throws NoSuchAlgorithmException,
IOException
{
File artifactFile = basedir.resolve(MavenArtifactUtils.convertArtifactToPath(artifact)).toFile();
Path artifactPath = basedir.resolve(MavenArtifactUtils.convertArtifactToPath(artifact));

// Make sure the artifact's parent directory exists before writing the model.
//noinspection ResultOfMethodCallIgnored
artifactFile.getParentFile().mkdirs();
Files.createDirectories(artifactPath.getParent());

try (JarOutputStream zos = new JarOutputStream(newOutputStream(artifactFile), createManifest()))
try (JarOutputStream zos = new JarOutputStream(Files.newOutputStream(artifactPath), createManifest()))
{
createMavenPropertiesFile(artifact, zos);
addMavenPomFile(artifact, zos);
Expand All @@ -183,7 +184,7 @@ public void createArchive(Artifact artifact, long bytesSize)
zos.flush();
}

generateChecksumsForArtifact(artifactFile);
generateChecksumsForArtifact(artifactPath);
}

public Manifest createManifest()
Expand Down Expand Up @@ -234,31 +235,25 @@ private void copyLicenseFiles(JarOutputStream jos)
}

protected OutputStream newOutputStream(File artifactFile)
throws IOException
throws IOException
{
return new FileOutputStream(artifactFile);
return Files.newOutputStream(artifactFile.toPath());
}

public void createMetadata(Metadata metadata, String metadataPath)
public void createMetadata(Metadata metadata,
String metadataPathStr)
throws NoSuchAlgorithmException, IOException
{
File metadataFile = null;

try
{
metadataFile = basedir.resolve(metadataPath).toFile();
Path metadataPath = basedir.resolve(metadataPathStr);

if (metadataFile.exists())
if (Files.exists(metadataPath))
{
metadataFile.delete();
Files.delete(metadataPath);
}

// Make sure the artifact's parent directory exists before writing
// the model.
// noinspection ResultOfMethodCallIgnored
metadataFile.getParentFile().mkdirs();

try (OutputStream os = new MultipleDigestOutputStream(metadataFile, newOutputStream(metadataFile)))
try (OutputStream os = new MultipleDigestOutputStream(metadataPath, Files.newOutputStream(metadataPath)))
{
Writer writer = WriterFactory.newXmlWriter(os);
MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
Expand All @@ -269,16 +264,18 @@ public void createMetadata(Metadata metadata, String metadataPath)
}
finally
{
generateChecksumsForArtifact(metadataFile);
generateChecksumsForArtifact(basedir.resolve(metadataPathStr));
}
}

private void addMavenPomFile(Artifact artifact, JarOutputStream zos) throws IOException
private void addMavenPomFile(Artifact artifact,
JarOutputStream zos)
throws IOException
{
final Artifact pomArtifact = MavenArtifactTestUtils.getPOMArtifact(artifact);
File pomFile = basedir.resolve(MavenArtifactUtils.convertArtifactToPath(pomArtifact)).toFile();
Path pomPath = basedir.resolve(MavenArtifactUtils.convertArtifactToPath(pomArtifact));

try (FileInputStream fis = new FileInputStream(pomFile))
try (InputStream fis = Files.newInputStream(pomPath))
{
JarEntry ze = new JarEntry("META-INF/maven/" +
artifact.getGroupId() + "/" +
Expand All @@ -299,7 +296,8 @@ private void addMavenPomFile(Artifact artifact, JarOutputStream zos) throws IOEx
}
}

public static void createMavenPropertiesFile(Artifact artifact, JarOutputStream jos)
public static void createMavenPropertiesFile(Artifact artifact,
JarOutputStream jos)
throws IOException
{
JarEntry ze = new JarEntry("META-INF/maven/" +
Expand Down Expand Up @@ -329,16 +327,16 @@ public static void createMavenPropertiesFile(Artifact artifact, JarOutputStream
jos.closeEntry();
}

public void generatePom(Artifact artifact, String packaging)
public void generatePom(Artifact artifact,
String packaging)
throws IOException,
NoSuchAlgorithmException
{
final Artifact pomArtifact = MavenArtifactTestUtils.getPOMArtifact(artifact);
File pomFile = basedir.resolve(MavenArtifactUtils.convertArtifactToPath(pomArtifact)).toFile();
Path artifactPath = basedir.resolve(MavenArtifactUtils.convertArtifactToPath(pomArtifact));

// Make sure the artifact's parent directory exists before writing the model.
//noinspection ResultOfMethodCallIgnored
pomFile.getParentFile().mkdirs();
Files.createDirectories(artifactPath.getParent());

Model model = new Model();
model.setGroupId(artifact.getGroupId());
Expand All @@ -350,13 +348,13 @@ public void generatePom(Artifact artifact, String packaging)

logger.debug("Generating pom file for {}...", artifact);

try (OutputStreamWriter pomFileWriter = new OutputStreamWriter(newOutputStream(pomFile)))
try (OutputStreamWriter pomFileWriter = new OutputStreamWriter(Files.newOutputStream(artifactPath)))
{
MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
xpp3Writer.write(pomFileWriter, model);
}

generateChecksumsForArtifact(pomFile);
generateChecksumsForArtifact(artifactPath);
}

private void setLicensesInPom(Model model)
Expand All @@ -378,10 +376,10 @@ private void setLicensesInPom(Model model)
}
}

private void generateChecksumsForArtifact(File artifactFile)
private void generateChecksumsForArtifact(Path artifactPath)
throws NoSuchAlgorithmException, IOException
{
try (InputStream is = new FileInputStream(artifactFile);
try (InputStream is = Files.newInputStream(artifactPath);
MultipleDigestInputStream mdis = new MultipleDigestInputStream(is))
{
int size = 4096;
Expand All @@ -395,17 +393,15 @@ private void generateChecksumsForArtifact(File artifactFile)
String md5 = mdis.getMessageDigestAsHexadecimalString(EncryptionAlgorithmsEnum.MD5.getAlgorithm());
String sha1 = mdis.getMessageDigestAsHexadecimalString(EncryptionAlgorithmsEnum.SHA1.getAlgorithm());

Path artifactPath = artifactFile.toPath();

Path checksumPath = artifactPath.resolveSibling(artifactPath.getFileName() + EncryptionAlgorithmsEnum.MD5.getExtension());
try (OutputStream os = newOutputStream(checksumPath.toFile()))
try (OutputStream os = Files.newOutputStream(checksumPath))
{
IOUtils.write(md5, os, StandardCharsets.UTF_8);
os.flush();
}

checksumPath = artifactPath.resolveSibling(artifactPath.getFileName() + EncryptionAlgorithmsEnum.SHA1.getExtension());
try (OutputStream os = newOutputStream(checksumPath.toFile()))
try (OutputStream os = Files.newOutputStream(checksumPath))
{
IOUtils.write(sha1, os, StandardCharsets.UTF_8);
os.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,12 @@ public Path buildPackage(long bytesSize)

Files.createDirectories(packagePath.getParent());

try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(packagePath, StandardOpenOption.CREATE)))
try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(packagePath, StandardOpenOption.CREATE));
GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(out);
TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gzipOut))
{
GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(out);
TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gzipOut);

writeContent(tarOut, bytesSize);
writePackageJson(tarOut);

tarOut.close();
gzipOut.close();
}

calculateChecksum();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.carlspring.strongbox.artifact.generator;

import org.carlspring.commons.io.RandomInputStream;
import org.carlspring.strongbox.artifact.coordinates.NugetArtifactCoordinates;
import org.carlspring.strongbox.artifact.coordinates.versioning.SemanticVersion;
import org.carlspring.strongbox.io.LayoutOutputStream;
Expand Down Expand Up @@ -181,29 +180,26 @@ public void createArchive(Nuspec nuspec,

try (OutputStream fileOutputStream = Files.newOutputStream(packagePath))
{
LayoutOutputStream layoutOutputStream = new LayoutOutputStream(fileOutputStream);
layoutOutputStream.addAlgorithm(MessageDigestAlgorithms.SHA_512);
layoutOutputStream.setDigestStringifier(this::toBase64);

try (ZipOutputStream zos = new ZipOutputStream(layoutOutputStream))
try (LayoutOutputStream layoutOutputStream = new LayoutOutputStream(fileOutputStream))
{
addNugetNuspecFile(nuspec, zos);
TestFileUtils.generateFile(zos, bytesSize, "file-with-given-size");
layoutOutputStream.addAlgorithm(MessageDigestAlgorithms.SHA_512);
layoutOutputStream.setDigestStringifier(this::toBase64);

String id = nuspec.getId();
try (ZipOutputStream zos = new ZipOutputStream(layoutOutputStream))
{
addNugetNuspecFile(nuspec, zos);
TestFileUtils.generateFile(zos, bytesSize, "file-with-given-size");

SemanticVersion version = nuspec.getVersion();
createMetadata(id, version.toString(), zos);
String id = nuspec.getId();

createContentType(zos);
createRels(id, zos);
}
finally
{
layoutOutputStream.close();
}
SemanticVersion version = nuspec.getVersion();
createMetadata(id, version.toString(), zos);

generateChecksum(packagePath, layoutOutputStream);
createContentType(zos);
createRels(id, zos);
}
generateChecksum(packagePath, layoutOutputStream);
}
}
}

Expand Down Expand Up @@ -232,17 +228,16 @@ private void createContentType(ZipOutputStream zos)
ZipEntry ze = new ZipEntry("[Content_Types].xml");
zos.putNextEntry(ze);

InputStream is = getClass().getResourceAsStream("[Content_Types].xml");
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0)
try (InputStream is = getClass().getResourceAsStream("[Content_Types].xml"))
{
zos.write(buffer, 0, len);
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0)
{
zos.write(buffer, 0, len);
}
}

is.close();
zos.closeEntry();

}

private void createMetadata(String id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public Path generateArtifact(PypiArtifactCoordinates coordinates,

Path fullPathParent = fullPath.getParent();
Files.createDirectories(fullPathParent);
OutputStream outputStream = Files.newOutputStream(fullPath, CREATE);

try(ZipOutputStream zos = new ZipOutputStream(outputStream))
try (OutputStream outputStream = Files.newOutputStream(fullPath, CREATE);
ZipOutputStream zos = new ZipOutputStream(outputStream))
{
if (coordinates.isSourcePackage())
{
Expand Down Expand Up @@ -224,7 +224,6 @@ private void createWheelPackageFiles(ZipOutputStream zos,
"Generator: bdist_wheel (0.33.4)\n" +
"Root-Is-Purelib: true\n" +
"Tag: py2-none-any").getBytes();

createZipEntry(zos, wheelPath, wheelContent);

//create top_level.txt zip entry
Expand Down Expand Up @@ -258,7 +257,6 @@ private void createWheelPackageFiles(ZipOutputStream zos,
topLevelContent.length))
.append(recordPath)
.append(",,");

createZipEntry(zos, recordPath, recordContent.toString().getBytes());

String randomPath = dirPath + "/RANDOM";
Expand Down
Loading

0 comments on commit 545d049

Please sign in to comment.