Skip to content

Commit

Permalink
Convert from java.io to java.nio
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsuter committed Jul 22, 2024
1 parent 87c5425 commit 14654f3
Show file tree
Hide file tree
Showing 34 changed files with 238 additions and 233 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ch/ivyteam/ivy/maven/AbstractEngineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected final File findMatchingEngineInCacheDirectory() throws MojoExecutionEx

protected final ArtifactVersion getInstalledEngineVersion(File engineDir) throws MojoExecutionException {
try {
return new EngineVersionEvaluator(getLog(), engineDir).evaluateVersion();
return new EngineVersionEvaluator(getLog(), engineDir.toPath()).evaluateVersion();
} catch (Exception ex) {
throw new MojoExecutionException("Cannot evaluate engine version", ex);
}
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/ch/ivyteam/ivy/maven/InstallEngineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -187,7 +189,7 @@ private void downloadAndInstallEngine(boolean cleanEngineDir) throws MojoExecuti
if (autoInstallEngine) {
getLog().info("Will automatically download Engine now.");
final EngineDownloader engineDownloader = getDownloader();
File downloadZip = engineDownloader.downloadEngine();
var downloadZip = engineDownloader.downloadEngine();

if (cleanEngineDir) {
removeOldEngineContent();
Expand All @@ -202,7 +204,11 @@ private void downloadAndInstallEngine(boolean cleanEngineDir) throws MojoExecuti
unpackEngine(downloadZip);

if (!downloadUsingMaven) {
downloadZip.delete();
try {
Files.delete(downloadZip);
} catch (IOException ex) {
throw new MojoExecutionException("Could not delete file " + downloadZip.toAbsolutePath(), ex);
}
}

ArtifactVersion installedEngineVersion = getInstalledEngineVersion(getRawEngineDirectory());
Expand Down Expand Up @@ -231,7 +237,7 @@ public EngineDownloader getDownloader() throws MojoExecutionException {
@SuppressWarnings("deprecation")
ProxyInfoProvider proxies = wagonManager::getProxy;
return new URLEngineDownloader(engineDownloadUrl, engineListPageUrl, osArchitecture, ivyVersion,
getIvyVersionRange(), getLog(), getDownloadDirectory(), proxies);
getIvyVersionRange(), getLog(), getDownloadDirectory().toPath(), proxies);
}

static String ivyEngineVersionOfZip(String engineZipFileName) {
Expand All @@ -258,10 +264,10 @@ private boolean engineDirectoryIsEmpty() {
return !getRawEngineDirectory().isDirectory() || ArrayUtils.isEmpty(getRawEngineDirectory().listFiles());
}

private void unpackEngine(File downloadZip) throws MojoExecutionException {
private void unpackEngine(Path downloadZip) throws MojoExecutionException {
String targetLocation = getRawEngineDirectory().getAbsolutePath();
getLog().info("Unpacking engine " + downloadZip.getAbsolutePath() + " to " + targetLocation);
try (var engineZip = new ZipFile(downloadZip)) {
getLog().info("Unpacking engine " + downloadZip.toAbsolutePath() + " to " + targetLocation);
try (var engineZip = new ZipFile(downloadZip.toFile())) {
engineZip.extractAll(targetLocation);
} catch (IOException ex) {
throw new MojoExecutionException("Failed to unpack downloaded engine '" + downloadZip + "'.", ex);
Expand All @@ -271,5 +277,4 @@ private void unpackEngine(File downloadZip) throws MojoExecutionException {
File getDownloadDirectory() {
return SystemUtils.getJavaIoTmpDir();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

/**
* Compiles an ivy Project with an ivyEngine.
*
*
* @author Reguel Wermelinger
* @since 6.0.0
*/
Expand Down Expand Up @@ -82,7 +82,7 @@ private void writeDependencyIarJar(Collection<File> iarJarDepenencies) throws IO
if (iarJarDepenencies == null) { // no dependencies
return;
}
File jar = new SharedFile(project).getIarDependencyClasspathJar();
var jar = new SharedFile(project).getIarDependencyClasspathJar().toFile();
new ClasspathJar(jar).createFileEntries(iarJarDepenencies);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

/**
* Compiles the test sources.
*
*
* @author Reguel Wermelinger
* @since 6.1.0
*/
Expand Down Expand Up @@ -60,7 +60,7 @@ protected void engineExec(MavenProjectBuilderProxy projectBuilder) throws Except
* @return persistent IAR-JARs from {@link CompileProjectMojo}.
*/
private List<File> getDependencyIarJars() {
File iarJarClasspath = new SharedFile(project).getIarDependencyClasspathJar();
var iarJarClasspath = new SharedFile(project).getIarDependencyClasspathJar().toFile();
if (!iarJarClasspath.exists()) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package ch.ivyteam.ivy.maven.deploy;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -81,7 +82,7 @@ public class DeployToEngineMojo extends AbstractDeployMojo {
* <code>\\myRemoteHost\myEngineShare</code>
*/
@Parameter(property = "ivy.deploy.engine.dir", defaultValue = DEPLOY_ENGINE_DIR_DEFAULT)
File deployEngineDirectory;
Path deployEngineDirectory;

/**
* The auto deployment directory of the engine. Must match the ivy engine
Expand Down Expand Up @@ -195,8 +196,8 @@ private void checkHttpParams() {
}
Object defaultDeployEngineDirectory = project.getProperties().get(ENGINE_DIRECTORY_PROPERTY);
if (deployEngineDirectory != null
&& !deployEngineDirectory.getPath().equals(defaultDeployEngineDirectory)) {
logParameterIgnoredByMethod("deployEngineDirectory", deployEngineDirectory.getPath(),
&& !deployEngineDirectory.toFile().getPath().equals(defaultDeployEngineDirectory)) {
logParameterIgnoredByMethod("deployEngineDirectory", deployEngineDirectory.toFile().getPath(),
DeployMethod.HTTP);
}
}
Expand All @@ -216,14 +217,13 @@ private void logParameterIgnoredByMethod(String parameter, String value, String
}

private File getDeployDirectory() throws MojoExecutionException {
if (deployEngineDirectory == null || engineToTarget()) { // re-use engine
// used to build
deployEngineDirectory = getEngineDir(project);
if (deployEngineDirectory == null || engineToTarget()) { // re-use engine used to build
deployEngineDirectory = getEngineDir(project).toPath();
}
if (Paths.get(deployDirectory).isAbsolute()) {
return new File(deployDirectory);
}
return new File(deployEngineDirectory, deployDirectory);
return new File(deployEngineDirectory.toFile(), deployDirectory);
}

public static interface DefaultDeployOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void writeEngineClasspathJar(File engineDirectory) throws IOException {
}

private void writeEngineClasspathJar(List<File> ivyEngineClassPathFiles) throws IOException {
File classPathJar = new SharedFile(maven.project).getEngineClasspathJar();
var classPathJar = new SharedFile(maven.project).getEngineClasspathJar().toFile();
ClasspathJar jar = new ClasspathJar(classPathJar);
jar.setMainClass("ch.ivyteam.ivy.server.ServerLauncher");
jar.createFileEntries(ivyEngineClassPathFiles);
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/ch/ivyteam/ivy/maven/engine/EngineControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
Expand All @@ -28,6 +26,7 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.file.Files;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -113,13 +112,13 @@ private CommandLine toEngineCommand(Command command) {
classpath += File.pathSeparator + context.vmOptions.additionalClasspath;
}

File osgiDir = new File(context.engineDirectory, OsgiDir.INSTALL_AREA);
var osgiDir = context.engineDirectory.resolve(OsgiDir.INSTALL_AREA);

CommandLine cli = new CommandLine(new File(getJavaExec()))
.addArgument("-classpath").addArgument(classpath)
.addArgument("-Divy.engine.testheadless=true")
.addArgument("-Djava.awt.headless=true")
.addArgument("-Dosgi.install.area=" + osgiDir.getAbsolutePath());
.addArgument("-Dosgi.install.area=" + osgiDir.toAbsolutePath());

if (StringUtils.isNotBlank(context.vmOptions.additionalVmOptions)) {
cli.addArguments(context.vmOptions.additionalVmOptions, false);
Expand All @@ -134,12 +133,12 @@ private CommandLine toEngineCommand(Command command) {

private Executor createEngineExecutor() {
return DefaultExecutor.builder()
.setWorkingDirectory(context.engineDirectory)
.setWorkingDirectory(context.engineDirectory.toFile())
.get();
}

private PumpStreamHandler createEngineLogStreamForwarder(Consumer<String> logLineHandler)
throws FileNotFoundException {
throws IOException {
OutputStream output = getEngineLogTarget();
OutputStream engineLogStream = new LineOrientedOutputStreamRedirector(output) {
@Override
Expand All @@ -163,15 +162,17 @@ public void stop() throws IOException {
return streamHandler;
}

private OutputStream getEngineLogTarget() throws FileNotFoundException {
private OutputStream getEngineLogTarget() throws IOException {
if (context.engineLogFile == null) {
context.log.info("Do not forward engine output to a persistent location");
return new ByteArrayOutputStream();
}

context.properties.setMavenProperty(Property.TEST_ENGINE_LOG, context.engineLogFile.getAbsolutePath());
context.log.info("Forwarding engine logs to: " + context.engineLogFile.getAbsolutePath());
return new FileOutputStream(context.engineLogFile.getAbsolutePath());
var logFile = context.engineLogFile;
var logFilePath = logFile.toAbsolutePath().toString();
context.properties.setMavenProperty(Property.TEST_ENGINE_LOG, logFilePath);
context.log.info("Forwarding engine logs to: " + logFilePath);
return Files.newOutputStream(logFile);
}

private String getJavaExec() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ch.ivyteam.ivy.maven.engine;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -15,12 +15,12 @@ public static void loadFromJvmOptionsFile(EngineMojoContext context, CommandLine
loadJvmOptions(context.engineDirectory, context.log).stream().forEach(option -> cli.addArgument(option));
}

public static String loadFromJvmOptionsFile(File identifyAndGetEngineDirectory, Log log) {
public static String loadFromJvmOptionsFile(Path identifyAndGetEngineDirectory, Log log) {
return loadJvmOptions(identifyAndGetEngineDirectory, log).stream().collect(Collectors.joining(" ", " ", " "));
}

private static List<String> loadJvmOptions(File engineDir, Log log) {
var jvmOptionsFile = engineDir.toPath().resolve("bin").resolve("jvm-module.options");
private static List<String> loadJvmOptions(Path engineDir, Log log) {
var jvmOptionsFile = engineDir.resolve("bin").resolve("jvm-module.options");
if (!Files.exists(jvmOptionsFile)) {
log.warn("Couldn't load jvm module options from '" + jvmOptionsFile + "' file.");
return List.of();
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/ch/ivyteam/ivy/maven/engine/EngineMojoContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package ch.ivyteam.ivy.maven.engine;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
Expand All @@ -26,16 +28,16 @@
import ch.ivyteam.ivy.maven.util.SharedFile;

public class EngineMojoContext {
public final File engineDirectory;
public final Path engineDirectory;
public final MavenProject project;
public final Log log;
public final EngineVmOptions vmOptions;
public final String engineClasspathJarPath;
public final MavenProperties properties;
public final File engineLogFile;
public final Path engineLogFile;
public final Integer timeoutInSeconds;

public EngineMojoContext(File engineDirectory, MavenProject project, Log log, File engineLogFile,
public EngineMojoContext(Path engineDirectory, MavenProject project, Log log, Path engineLogFile,
EngineVmOptions vmOptions, Integer timeoutInSeconds) {
this.engineDirectory = engineDirectory;
this.project = project;
Expand All @@ -50,25 +52,23 @@ public EngineMojoContext(File engineDirectory, MavenProject project, Log log, Fi
if (!(new File(engineClasspathJarPath).exists())) {
throw new RuntimeException("Engine ClasspathJar " + engineClasspathJarPath + " does not exist.");
}
if (!(engineDirectory.exists())) {
if (!Files.exists(engineDirectory)) {
throw new RuntimeException("Engine Directory " + engineDirectory + " does not exist.");
}
}

private String setupEngineClasspathJarIfNotExists() {
File classpathJar = new SharedFile(project).getEngineOSGiBootClasspathJar();

var classpathJar = new SharedFile(project).getEngineOSGiBootClasspathJar().toFile();
if (!classpathJar.exists()) {
try {
log.info("Creating a classpath jar for starting the engine");
new ClasspathJar(classpathJar)
.createFileEntries(EngineClassLoaderFactory.getOsgiBootstrapClasspath(engineDirectory));
.createFileEntries(EngineClassLoaderFactory.getOsgiBootstrapClasspath(engineDirectory.toFile()));
} catch (Exception ex) {
throw new RuntimeException(
"Could not create engine classpath jar: '" + classpathJar.getAbsolutePath() + "'", ex);
}
}

return classpathJar.getAbsolutePath();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.ivyteam.ivy.maven.engine;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -11,19 +12,20 @@
import ch.ivyteam.ivy.maven.engine.EngineClassLoaderFactory.OsgiDir;

public class EngineVersionEvaluator {

public static final String LIBRARY_ID = "ch.ivyteam.util";

private Log log;
private File engineDir;
private Path engineDir;

public EngineVersionEvaluator(Log log, File engineDir) {
public EngineVersionEvaluator(Log log, Path engineDir) {
this.log = log;
this.engineDir = engineDir;
}

public ArtifactVersion evaluateVersion() {
if (!isOSGiEngine(engineDir)) {
String absolutePath = engineDir == null ? "" : engineDir.getAbsolutePath();
String absolutePath = engineDir == null ? "" : engineDir.toAbsolutePath().toString();
log.info("Can not evaluate version of a non-OSGi engine in directory '" + absolutePath + "'");
return null;
}
Expand All @@ -37,9 +39,9 @@ public ArtifactVersion evaluateVersion() {
return new DefaultArtifactVersion(toReleaseVersion(version));
}

public static boolean isOSGiEngine(File engineDir) {
File folder = new File(engineDir, OsgiDir.INSTALL_AREA);
return folder.exists();
public static boolean isOSGiEngine(Path engineDir) {
var folder = engineDir.resolve(OsgiDir.INSTALL_AREA);
return Files.exists(folder);
}

public static String toReleaseVersion(String version) { // 6.1.0.51869 ->
Expand All @@ -52,12 +54,12 @@ public static String toReleaseVersion(String version) { // 6.1.0.51869 ->
}

private String getLibraryFileName(String libraryId) {
File ivyLibs = new File(engineDir, OsgiDir.PLUGINS);
if (!ivyLibs.exists()) {
var ivyLibs = engineDir.resolve(OsgiDir.PLUGINS);
if (!Files.exists(ivyLibs)) {
return null;
}

String[] libraryNames = ivyLibs.list();
String[] libraryNames = ivyLibs.toFile().list();
if (libraryNames == null) {
return null;
}
Expand All @@ -69,5 +71,4 @@ private String getLibraryFileName(String libraryId) {
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
/**
* Provides project build functionality that can only be accessed trough
* reflection on an ivy Engine classloader.
*
*
* @author Reguel Wermelinger
* @since 6.0.0
*/
Expand Down Expand Up @@ -81,7 +81,7 @@ private void logEngine()

private Class<?> getOsgiBundledDelegate(URLClassLoader ivyEngineClassLoader,
int timeoutEngineStartInSeconds) throws Exception {
Object bundleContext = new OsgiRuntime(baseDirToBuildIn, log).startEclipseOsgiImpl(ivyEngineClassLoader,
Object bundleContext = new OsgiRuntime(baseDirToBuildIn.toPath(), log).startEclipseOsgiImpl(ivyEngineClassLoader,
timeoutEngineStartInSeconds);
hackProvokeEagerStartOfJdt(bundleContext);
Object buildBundle = findBundle(bundleContext, "ch.ivyteam.ivy.dataclasses.build");
Expand Down
Loading

0 comments on commit 14654f3

Please sign in to comment.