From 16210319279a689cc60291edc0681963cfe5a5b4 Mon Sep 17 00:00:00 2001 From: eostermueller Date: Sun, 19 Jul 2020 09:54:00 -0500 Subject: [PATCH] --forceLaunch flag will launch even if pre-install validations fail --- .../eostermueller/snail4j/Application.java | 24 +++++ .../snail4j/CommandLineArgs.java | 33 +++++++ .../eostermueller/snail4j/DefaultFactory.java | 2 +- .../eostermueller/snail4j/InstallAdvice.java | 88 ++++++++----------- .../eostermueller/snail4j/JdkUtils.java | 65 ++++++++++---- .../snail4j/Snail4jInstaller.java | 70 +++++++++++---- .../snail4j/SpringBootSnail4J.java | 60 +++++++++---- .../snail4j/launcher/Factory.java | 3 +- .../snail4j/launcher/Messages.java | 2 + .../snail4j/launcher/Messages_en_US.java | 7 +- .../snail4j/CommandLineArgsTest.java | 24 +++++ .../snail4j/TestInstallAdvice.java | 53 ++++++++--- 12 files changed, 306 insertions(+), 125 deletions(-) create mode 100644 backend/src/main/java/com/github/eostermueller/snail4j/CommandLineArgs.java create mode 100644 backend/src/test/java/com/github/eostermueller/snail4j/CommandLineArgsTest.java diff --git a/backend/src/main/java/com/github/eostermueller/snail4j/Application.java b/backend/src/main/java/com/github/eostermueller/snail4j/Application.java index 7a59827..af54145 100644 --- a/backend/src/main/java/com/github/eostermueller/snail4j/Application.java +++ b/backend/src/main/java/com/github/eostermueller/snail4j/Application.java @@ -8,18 +8,42 @@ import org.springframework.boot.context.ApplicationPidFileWriter; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import java.util.Arrays; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; /** * @author eostermueller * */ @SpringBootApplication public class Application extends SpringBootServletInitializer { + public static CommandLineArgs commandLineArguments = null; +//public class Application implements ApplicationRunner { + private final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); public static void main(String[] args) { SpringApplication application = new SpringApplication(Application.class); + Application.commandLineArguments = CommandLineArgs.create(args); application.addListeners(new ApplicationPidFileWriter("snail4j.pid")); application.run(args); } +// @Override +// public void run(ApplicationArguments args) throws Exception { +// LOGGER.info("Application started with command-line arguments: {}", Arrays.toString(args.getSourceArgs())); +// LOGGER.info("NonOptionArgs: {}", args.getNonOptionArgs()); +// LOGGER.info("OptionNames: {}", args.getOptionNames()); +// +// for (String name : args.getOptionNames()){ +// LOGGER.info("arg-" + name + "=" + args.getOptionValues(name)); +// } +// +// boolean containsOption = args.containsOption("person.name"); +// LOGGER.info("Contains person.name: " + containsOption); +// } + } diff --git a/backend/src/main/java/com/github/eostermueller/snail4j/CommandLineArgs.java b/backend/src/main/java/com/github/eostermueller/snail4j/CommandLineArgs.java new file mode 100644 index 0000000..3625ed6 --- /dev/null +++ b/backend/src/main/java/com/github/eostermueller/snail4j/CommandLineArgs.java @@ -0,0 +1,33 @@ +package com.github.eostermueller.snail4j; + + +public class CommandLineArgs { + public static final String FORCE_LAUNCH = "--forceLaunch"; + public CommandLineArgs(String[] stringArgs) { + args = stringArgs; + } + + + static CommandLineArgs create(String[] stringArgs) { + CommandLineArgs args = new CommandLineArgs(stringArgs); + return args; + } + String args[] = null; + + /** + * This is a backup plan, in case snal4j has a bug/issue with prechecks/validation that unnecessarily aborts install/launch. + * @return + */ + public boolean isForceLaunch() { + boolean found = false; + for (String oneArg : args ) { + if (FORCE_LAUNCH.toLowerCase().equals(oneArg.toLowerCase())) { + found = true; + break; + } + } + + return found; + } + +} diff --git a/backend/src/main/java/com/github/eostermueller/snail4j/DefaultFactory.java b/backend/src/main/java/com/github/eostermueller/snail4j/DefaultFactory.java index 456289e..692104d 100644 --- a/backend/src/main/java/com/github/eostermueller/snail4j/DefaultFactory.java +++ b/backend/src/main/java/com/github/eostermueller/snail4j/DefaultFactory.java @@ -235,7 +235,7 @@ public ProcessModelBuilder createProcessModelBuilder() throws Snail4jException { } @Override public - Snail4jInstaller createNewInstaller() { + Snail4jInstaller createNewInstaller() throws CannotFindSnail4jFactoryClass { return new Snail4jInstaller(); } @Override diff --git a/backend/src/main/java/com/github/eostermueller/snail4j/InstallAdvice.java b/backend/src/main/java/com/github/eostermueller/snail4j/InstallAdvice.java index 478900c..54703c8 100644 --- a/backend/src/main/java/com/github/eostermueller/snail4j/InstallAdvice.java +++ b/backend/src/main/java/com/github/eostermueller/snail4j/InstallAdvice.java @@ -18,15 +18,17 @@ * */ public class InstallAdvice { - private StartupErrorLogger startupErrorLogger; - public InstallAdvice(StartupErrorLogger val) throws Snail4jException { - this.startupErrorLogger = val; - if (this.startupErrorLogger==null) - throw new Snail4jException("Bug. expectied logger object"); + private StartupLogger startupLogger; + public InstallAdvice(StartupLogger val) throws Snail4jException { + messages = DefaultFactory.getFactory().getMessages(); + this.startupLogger = val; + if (this.startupLogger==null) + throw new Snail4jException("Bug. expectied StartupErrorLogger object"); } - interface StartupErrorLogger { + interface StartupLogger { public void error(String msg); public void info(String msg); + public void debug(String msg); } private static final String WIREMOCK_PORT_PROPERTY = "wiremockPort"; @@ -38,9 +40,6 @@ interface StartupErrorLogger { Messages messages = null; private static String[] UNSUPPORTED_JAVA_SPECIFICATION_VERSIONS = new String[]{ "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7" }; - public InstallAdvice() throws CannotFindSnail4jFactoryClass { - messages = DefaultFactory.getFactory().getMessages(); - } static class TcpTarget { static TcpTarget create(String hostname, int port, String name, String snail4jPropertyName) { TcpTarget t = new TcpTarget(); @@ -64,7 +63,7 @@ public String toString() { } } - public void sutPortsAreAvailable(Configuration cfg) throws CannotFindSnail4jFactoryClass, Snail4jMultiException { + public int sutPortsAreAvailable(Configuration cfg) throws CannotFindSnail4jFactoryClass, Snail4jMultiException { List errors = new ArrayList(); int timeoutMs = 1000; List targets = new ArrayList(); @@ -78,9 +77,10 @@ public void sutPortsAreAvailable(Configuration cfg) throws CannotFindSnail4jFact t.active = true; String error = DefaultFactory.getFactory().getMessages() .tcpPortConflict(t.name,t.hostname,t.port,t.snail4jPropertyName); - errors.add( error ); + this.startupLogger.error(error); + errors.add(error); } - startupErrorLogger.info( "Snail4j Port status: " + t.toString() ); + startupLogger.debug( "Snail4j Port status: " + t.toString() ); } String portInitStatus = DefaultFactory.getFactory().getMessages() .portInitStatus(errors); @@ -88,16 +88,17 @@ public void sutPortsAreAvailable(Configuration cfg) throws CannotFindSnail4jFact DefaultFactory.getFactory().getEventHistory().getEvents().add( Event.create(portInitStatus) ); - if (errors.size() > 0) { - throw new Snail4jMultiException(errors); - } - + if (errors.size() > 0) + this.startupLogger.error(portInitStatus); + else + this.startupLogger.debug(portInitStatus); + + return errors.size(); } public boolean isJdk() throws Snail4jException { boolean rc = true; - rc = JdkUtils.isJdk(); @@ -112,28 +113,12 @@ public boolean isJdk() throws Snail4jException { throw new Snail4jException("Bug: could not create error message showing location of java."); } if (!rc) - startupErrorLogger.error( LOG_PREFIX+errorMsg ); + startupLogger.error( errorMsg ); return rc; } - public boolean JAVA_HOME_pointsToCurrentJava() throws Snail4jException { - - - boolean rc = false; - rc = JdkUtils.JAVA_HOME_pointsToCurrentJava(); - - if (!rc) - startupErrorLogger.error(LOG_PREFIX+ - messages.JAVA_HOME_mustPointToCurrentJava( - JdkUtils.get_JAVA_HOME(), - JdkUtils.getCurrentJavaPath() - )); - return rc; - } public boolean isJavaSpecificationVersionOk() throws Snail4jException { - boolean rc = true; - String currentJavaSpecificationVersion = System.getProperty("java.specification.version"); if (currentJavaSpecificationVersion==null || "".equals(currentJavaSpecificationVersion)) { @@ -146,8 +131,8 @@ public boolean isJavaSpecificationVersionOk() throws Snail4jException { Path p = JdkUtils.getCurrentJavaPath(); if (ynOnTheUnsupportedList) - startupErrorLogger.error( - LOG_PREFIX+ + startupLogger.error( + messages.unsupportedJavaVersion ( currentJavaSpecificationVersion, p, @@ -157,35 +142,34 @@ public boolean isJavaSpecificationVersionOk() throws Snail4jException { return !ynOnTheUnsupportedList; } + public Path get_JAVA_HOME() { + Path java_home = null; + String javaHomeString = System.getenv("JAVA_HOME"); + if (javaHomeString != null) + java_home = Paths.get(javaHomeString); + + return java_home; + } /** * * @return * @throws CannotFindSnail4jFactoryClass * @throws MalformedURLException */ - public boolean isJavaHomeEnvVarOk() throws CannotFindSnail4jFactoryClass, MalformedURLException { + public boolean isJavaHomeDirExists(Path java_home) throws CannotFindSnail4jFactoryClass, MalformedURLException { boolean rc = false; - String javaHome = System.getenv("JAVA_HOME"); - if (javaHome == null) { - startupErrorLogger.error( - LOG_PREFIX+ - messages.javaHomeEnvVarNotSet() ); + File javaHomeFolder = java_home.toFile(); + if (!javaHomeFolder.exists() || !javaHomeFolder.isDirectory()) { + startupLogger.error( + messages.javaHomeFolderDoesNotExistOrLackingPermissions(javaHomeFolder) ); } else { - File javaHomeFolder = Paths.get(javaHome).toFile(); - if (!javaHomeFolder.exists() || !javaHomeFolder.isDirectory()) { - startupErrorLogger.error( - LOG_PREFIX+ - messages.javaHomeFolderDoesNotExistOrLackingPermissions(javaHomeFolder) ); - } else { - rc = true; - } + rc = true; } if (!rc) { - startupErrorLogger.error( - LOG_PREFIX+ + startupLogger.error( new DocumentationLinks().getJdkInstallAdvice().toString() ); } diff --git a/backend/src/main/java/com/github/eostermueller/snail4j/JdkUtils.java b/backend/src/main/java/com/github/eostermueller/snail4j/JdkUtils.java index 0d5cf98..ef45684 100644 --- a/backend/src/main/java/com/github/eostermueller/snail4j/JdkUtils.java +++ b/backend/src/main/java/com/github/eostermueller/snail4j/JdkUtils.java @@ -6,7 +6,6 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; -import java.util.function.BooleanSupplier; import javax.management.InstanceNotFoundException; import javax.management.MBeanException; @@ -14,11 +13,14 @@ import javax.management.ObjectName; import javax.management.ReflectionException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.github.eostermueller.snail4j.OsUtils.OsResult; -import com.github.eostermueller.snail4j.DefaultFactory; import com.github.eostermueller.snail4j.launcher.Messages; public class JdkUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(JdkUtils.class); static Path getCurrentJavaPath() throws Snail4jException { String pathOfRunningJava = System.getProperty("sun.boot.library.path"); @@ -30,18 +32,22 @@ static Path getCurrentJavaPath() throws Snail4jException { return Paths.get(pathOfRunningJava); } - static boolean JAVA_HOME_pointsToCurrentJava() throws Snail4jException { + static boolean pointsToCurrentJava(Path javaHome) throws Snail4jException { + + javaHome = javaHome.normalize(); Path currentJava = getCurrentJavaPath().normalize(); - - Path javaHome = get_JAVA_HOME().normalize(); - + if (currentJava.endsWith( Paths.get("bin") )) currentJava = currentJava.getParent(); return currentJava.startsWith(javaHome); } + public static boolean isJdk() { + return !(getDirectoryOfJavaCompiler() == null); + + } /* * @stolenFrom: https://stackoverflow.com/a/58737549/2377579 * @@ -65,9 +71,9 @@ static boolean JAVA_HOME_pointsToCurrentJava() throws Snail4jException { └───webstart */ - public static boolean isJdk() { - boolean rc = false; - String path = System.getProperty("sun.boot.library.path"); + public static Path getDirectoryOfJavaCompiler() { + Path pathToJavaCompiler = null; + String path = getInstallPathOfThisJvm(); if(path != null) { String javacPath = ""; if(path.endsWith(File.separator + "bin")) { @@ -78,22 +84,43 @@ public static boolean isJdk() { javacPath = path.substring(0, libIndex) + File.separator + "bin"; } } - if(!javacPath.isEmpty()) { - rc = new File(javacPath, "javac").exists() || new File(javacPath, "javac.exe").exists(); + pathToJavaCompiler = getDirectoryOfJavaCompiler( Paths.get(javacPath) ); + } + if (pathToJavaCompiler==null) + LOGGER.debug(String.format("Failed to locate java compiler executable. The sun.boot.library.path is %s", path)); + else + LOGGER.debug(String.format("Found javac executable in %s", pathToJavaCompiler.toAbsolutePath().toString() )); + + return pathToJavaCompiler; + } + + public static String getInstallPathOfThisJvm() { + String path = System.getProperty("sun.boot.library.path"); + LOGGER.debug(String.format("sun.boot.library.path is %s", path)); + return path; + } + public static Path getDirectoryOfJavaCompiler(Path path) { + Path pathToJavaCompiler = null; + String possiblePathToCompiler = path.toAbsolutePath().toString(); + if(!possiblePathToCompiler.isEmpty()) { + LOGGER.debug(String.format("looking for javac executable in %s", possiblePathToCompiler)); + + if ( new File(possiblePathToCompiler, "javac").exists() || new File(possiblePathToCompiler, "javac.exe").exists() ) + pathToJavaCompiler = Paths.get(possiblePathToCompiler); + else { /** The above will work most of the time. * The following addresses the OpenJDK "JRE+JDK" Packaging Scenario, detailed in method comments. */ - if (!rc) { - javacPath = javacPath + "/../../bin"; - rc = new File(javacPath, "javac").exists() || new File(javacPath, "javac.exe").exists(); - } - } - } - return rc; + possiblePathToCompiler = possiblePathToCompiler + "/../../bin"; + LOGGER.debug(String.format("looking for javac executable in %s", possiblePathToCompiler)); + if ( new File(possiblePathToCompiler, "javac").exists() || new File(possiblePathToCompiler, "javac.exe").exists() ) + pathToJavaCompiler = Paths.get(possiblePathToCompiler); + } + } + return pathToJavaCompiler; } public static Path get_JAVA_HOME() throws Snail4jException { String javaHomeEnvVar = System.getenv("JAVA_HOME"); - //String javaHomeEnvVar = "C:\\java\\java-1.8.0-openjdk-1.8.0.252-2.b09.ojdkbuild.windows.x86_64\\java-1.8.0-openjdk-1.8.0.252-2.b09.ojdkbuild.windows.x86_64"; Messages m = DefaultFactory.getFactory().getMessages(); Path p = null; if (javaHomeEnvVar!=null && javaHomeEnvVar.trim().length()>0) { diff --git a/backend/src/main/java/com/github/eostermueller/snail4j/Snail4jInstaller.java b/backend/src/main/java/com/github/eostermueller/snail4j/Snail4jInstaller.java index c16dc95..6dafabb 100644 --- a/backend/src/main/java/com/github/eostermueller/snail4j/Snail4jInstaller.java +++ b/backend/src/main/java/com/github/eostermueller/snail4j/Snail4jInstaller.java @@ -1,8 +1,6 @@ package com.github.eostermueller.snail4j; -import java.io.BufferedReader; import java.io.File; -import java.io.InputStreamReader; import java.net.MalformedURLException; import java.nio.file.Path; import java.nio.file.Paths; @@ -14,6 +12,7 @@ import com.github.eostermueller.snail4j.launcher.CannotFindSnail4jFactoryClass; import com.github.eostermueller.snail4j.launcher.Configuration; +import com.github.eostermueller.snail4j.launcher.Messages; /** * The SpringBootStartupInstaller will handle progress meter @@ -33,8 +32,14 @@ * @author erikostermueller * */ -public class Snail4jInstaller implements InstallAdvice.StartupErrorLogger { +public class Snail4jInstaller implements InstallAdvice.StartupLogger { public static final String LOG_PREFIX = "#### "; + + Messages messages = null; + public Snail4jInstaller() throws CannotFindSnail4jFactoryClass { + messages = DefaultFactory.getFactory().getMessages(); + + } /** * Here is some research from the JDKs installed on my machine: @@ -51,25 +56,49 @@ public class Snail4jInstaller implements InstallAdvice.StartupErrorLogger { * @throws Snail4jException */ - public int preinstallCheck(Configuration cfg) throws MalformedURLException, Snail4jException { + public int preInstallJavaValidation(Configuration cfg) throws MalformedURLException, Snail4jException { int errorCount = 0; - InstallAdvice ia = new InstallAdvice(); + InstallAdvice ia = new InstallAdvice((InstallAdvice.StartupLogger)this); if (!ia.isJavaSpecificationVersionOk() ) - errorCount++; - - if (!ia.isJavaHomeEnvVarOk() ) - errorCount++; - - if (!ia.JAVA_HOME_pointsToCurrentJava()) - errorCount++; - - if (!ia.isJdk() ) - errorCount++; + errorCount++; //Unlikely this will happen, because 1.7 or before JDK won't run a 1.8 or higher jar file. + + Path javac_dir = JdkUtils.getDirectoryOfJavaCompiler(); + if (javac_dir!=null) { + cfg.setJavaHome(javac_dir); + } else { + info( messages.jreIsNotEnough( Paths.get(JdkUtils.getInstallPathOfThisJvm() ) ) ); + Path java_home_from_env = ia.get_JAVA_HOME(); + info( messages.attemptingToUseJavaHomeToFindJavaCompiler( java_home_from_env ) ); + + if (!ia.isJavaHomeDirExists(java_home_from_env) ) + errorCount++; + else { + if( JdkUtils.pointsToCurrentJava(java_home_from_env) ) { + cfg.setJavaHome(java_home_from_env); + if (JdkUtils.getDirectoryOfJavaCompiler(java_home_from_env)==null ) { + info( messages.jreIsNotEnough( java_home_from_env ) ); + errorCount++; + } + } else { + errorCount++; + } + } + } + + LOGGER.debug(LOG_PREFIX+String.format("Detected [%d] JDK issues",errorCount)); + return errorCount; + } + public int preInstallValidation(Configuration cfg) throws MalformedURLException, Snail4jException { + InstallAdvice ia = new InstallAdvice((InstallAdvice.StartupLogger)this); + int errorCount = 0; - LOGGER.debug(LOG_PREFIX+String.format("Detected [%d] install issues.",errorCount)); + errorCount += preInstallJavaValidation(cfg); + errorCount += ia.sutPortsAreAvailable(cfg); + info("Number if install issues: " + errorCount ); + return errorCount; } Configuration getConfiguration() throws Snail4jException { @@ -593,11 +622,16 @@ protected void installSutApp() throws Snail4jException { } @Override public void error(String msg) { - LOGGER.error(LOG_PREFIX + msg); + System.out.println(LOG_PREFIX + "ERROR: " + msg); } @Override public void info(String msg) { - LOGGER.info(LOG_PREFIX+msg); + System.out.println(LOG_PREFIX+ "INFO: " + msg); + } + @Override + public void debug(String msg) { + if (LOGGER.isDebugEnabled()) + System.out.println(LOG_PREFIX+"DEBUG: "+msg); } } diff --git a/backend/src/main/java/com/github/eostermueller/snail4j/SpringBootSnail4J.java b/backend/src/main/java/com/github/eostermueller/snail4j/SpringBootSnail4J.java index 2c6fc46..9f60a80 100644 --- a/backend/src/main/java/com/github/eostermueller/snail4j/SpringBootSnail4J.java +++ b/backend/src/main/java/com/github/eostermueller/snail4j/SpringBootSnail4J.java @@ -29,12 +29,14 @@ */ @Component public class SpringBootSnail4J implements ApplicationListener { + private String BLANK_SPACE = "\n\n\n"; @Autowired private ConfigurableApplicationContext ctx; private final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); private static ProcessModelSingleton PROCESS_MODEL_SINGLETON = null; + public static ProcessModelSingleton getProcessModelSingleton() throws ConfigVariableNotFoundException, Snail4jException { return PROCESS_MODEL_SINGLETON; @@ -92,23 +94,24 @@ private void install(Configuration cfg) throws CannotFindSnail4jFactoryClass, Ma Event.create(m.startInstallMessage()) ); Snail4jInstaller snail4jInstaller = DefaultFactory.getFactory().createNewInstaller(); - LOGGER.info(InstallAdvice.LOG_PREFIX+"Maven online mode: " + cfg.isMavenOnline() ); - LOGGER.info(InstallAdvice.LOG_PREFIX+"snail4j maven repo location: " + cfg.isSnail4jMavenRepo() ); + LOGGER.debug(Snail4jInstaller.LOG_PREFIX+"Maven online mode: " + cfg.isMavenOnline() ); + LOGGER.debug(Snail4jInstaller.LOG_PREFIX+"snail4j maven repo location: " + cfg.isSnail4jMavenRepo() ); - int countOfFailedPreChecks = snail4jInstaller.preinstallCheck(cfg); - LOGGER.info(InstallAdvice.LOG_PREFIX+"Number if install issues: " + countOfFailedPreChecks ); + int countOfFailedPreChecks = snail4jInstaller.preInstallValidation(cfg); - if (countOfFailedPreChecks==0) + if (countOfFailedPreChecks==0 || Application.commandLineArguments.isForceLaunch() ) snail4jInstaller.install(); else throw new Snail4jException(m.startupAborted(countOfFailedPreChecks) ); - LOGGER.info(InstallAdvice.LOG_PREFIX+"Install finished. Ready to load test!"); + LOGGER.info(Snail4jInstaller.LOG_PREFIX+"Install finished. Ready to load test!"); dispEndInstallBanner( m.successfulInstallation() ); + dispSuccessLargeLetters(); DefaultFactory.getFactory().getEventHistory().getEvents().add( Event.create(m.successfulInstallation()) ); } catch (Snail4jException e) { dispEndInstallBanner( m.failedInstallation() ); + dispFailLargeLetters(); DefaultFactory.getFactory().getEventHistory().getEvents().add( Event.create(m.failedInstallation()) ); LOGGER.error( e.getMessage() ); @@ -123,22 +126,41 @@ private void install(Configuration cfg) throws CannotFindSnail4jFactoryClass, Ma this.LOGGER.error("Install has been skipped! It only runs when WindTunnel is launched with 'java -jar'. Startup: [" + path + "]" ); } } - + private void dispSuccessLargeLetters() { + System.out.println(" _______. __ __ ______ ______ _______ _______. _______."); + System.out.println(" / || | | | / | / || ____| / | / |"); + System.out.println(" | (----`| | | | | ,----'| ,----'| |__ | (----` | (----`"); + System.out.println(" \\ \\ | | | | | | | | | __| \\ \\ \\ \\ "); + System.out.println(".----) | | `--' | | `----.| `----.| |____.----) | .----) | "); + System.out.println("|_______/ \\______/ \\______| \\______||_______|_______/ |_______/ "); + } + private void dispFailLargeLetters() { + System.out.println(" _______ ___ __ __ "); + System.out.println("| ____| / \\ | | | | "); + System.out.println("| |__ / ^ \\ | | | | "); + System.out.println("| __| / /_\\ \\ | | | | "); + System.out.println("| | / _____ \\ | | | `----."); + System.out.println("|__| /__/ \\__\\ |__| |_______|"); + } private void dispStartInstallBanner(String msg) { - this.LOGGER.info("########################################" ); - this.LOGGER.info("########################################" ); - this.LOGGER.info("#### #####" ); - this.LOGGER.info("#### " + msg); - this.LOGGER.info("#### #####" ); - this.LOGGER.info("#### #####" ); + + System.out.println(BLANK_SPACE); + System.out.println("########################################" ); + System.out.println("########################################" ); + System.out.println(Snail4jInstaller.LOG_PREFIX ); + System.out.println(Snail4jInstaller.LOG_PREFIX + msg); + System.out.println(Snail4jInstaller.LOG_PREFIX ); + System.out.println(Snail4jInstaller.LOG_PREFIX ); } private void dispEndInstallBanner(String msg) { - this.LOGGER.info("#### #####" ); - this.LOGGER.info("#### #####" ); - this.LOGGER.info("#### " + msg); - this.LOGGER.info("#### #####" ); - this.LOGGER.info("########################################" ); - this.LOGGER.info("########################################" ); + System.out.println(Snail4jInstaller.LOG_PREFIX ); + System.out.println(Snail4jInstaller.LOG_PREFIX ); + System.out.println(Snail4jInstaller.LOG_PREFIX + msg); + System.out.println(Snail4jInstaller.LOG_PREFIX ); + System.out.println(Snail4jInstaller.LOG_PREFIX ); + System.out.println("########################################" ); + System.out.println("########################################" ); + System.out.println(BLANK_SPACE); } } diff --git a/backend/src/main/java/com/github/eostermueller/snail4j/launcher/Factory.java b/backend/src/main/java/com/github/eostermueller/snail4j/launcher/Factory.java index f044958..11740a2 100644 --- a/backend/src/main/java/com/github/eostermueller/snail4j/launcher/Factory.java +++ b/backend/src/main/java/com/github/eostermueller/snail4j/launcher/Factory.java @@ -1,6 +1,5 @@ package com.github.eostermueller.snail4j.launcher; -import java.io.File; import com.github.eostermueller.snail4j.Snail4jException; import com.github.eostermueller.snail4j.Snail4jInstaller; @@ -31,7 +30,7 @@ public interface Factory { ProcessModelBuilder createProcessModelBuilder() throws Snail4jException; - Snail4jInstaller createNewInstaller(); + Snail4jInstaller createNewInstaller() throws CannotFindSnail4jFactoryClass; ConfigLookup createConfigLookup() throws Snail4jException; diff --git a/backend/src/main/java/com/github/eostermueller/snail4j/launcher/Messages.java b/backend/src/main/java/com/github/eostermueller/snail4j/launcher/Messages.java index 27151de..9e9e4cb 100644 --- a/backend/src/main/java/com/github/eostermueller/snail4j/launcher/Messages.java +++ b/backend/src/main/java/com/github/eostermueller/snail4j/launcher/Messages.java @@ -63,4 +63,6 @@ String getSutStopMessage(String humanReadableString, String absolutePath, boolea String JAVA_HOME_mustPointToCurrentJava(Path get_JAVA_HOME, Path currentJavaPath); + String attemptingToUseJavaHomeToFindJavaCompiler(Path java_home_from_env); + } diff --git a/backend/src/main/java/com/github/eostermueller/snail4j/launcher/Messages_en_US.java b/backend/src/main/java/com/github/eostermueller/snail4j/launcher/Messages_en_US.java index 8e570fe..8b065e5 100644 --- a/backend/src/main/java/com/github/eostermueller/snail4j/launcher/Messages_en_US.java +++ b/backend/src/main/java/com/github/eostermueller/snail4j/launcher/Messages_en_US.java @@ -156,7 +156,7 @@ public String unsupportedJavaVersion(String currentJavaSpecificationVersion, Pat public String jreIsNotEnough(Path currentJavaPath) { String path = currentJavaPath.toFile().getAbsolutePath(); - return String.format("JRE was used to launch snail4j uber jar. Snail4j instead requires a JDK. Path to JRE that was used:: %s", path ); + return String.format("Is this a JRE? Snail4j instead requires a JDK. Path to JRE that was used:: \n#### \t\t\t%s", path ); } @Override @@ -172,4 +172,9 @@ public String JAVA_HOME_mustPointToCurrentJava(Path JAVA_HOME, Path currentJavaP ); } + @Override + public String attemptingToUseJavaHomeToFindJavaCompiler(Path java_home_from_env) { + return String.format("Attempting to use JAVA_HOME value to locate Java Compiler executable. \n####\t\t\t JAVA_HOME=%s", java_home_from_env.toAbsolutePath().toString() ); + } + } diff --git a/backend/src/test/java/com/github/eostermueller/snail4j/CommandLineArgsTest.java b/backend/src/test/java/com/github/eostermueller/snail4j/CommandLineArgsTest.java new file mode 100644 index 0000000..2c31bc2 --- /dev/null +++ b/backend/src/test/java/com/github/eostermueller/snail4j/CommandLineArgsTest.java @@ -0,0 +1,24 @@ +package com.github.eostermueller.snail4j; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class CommandLineArgsTest { + + @Test + void canDetectForceLaunchArg() { + CommandLineArgs args = CommandLineArgs.create( new String[] { CommandLineArgs.FORCE_LAUNCH} ); + assertTrue (args.isForceLaunch()); + + args = CommandLineArgs.create(new String[]{ }); + assertFalse (args.isForceLaunch() ); + + args = CommandLineArgs.create(new String[] { "foo","bar"}); + assertFalse (args.isForceLaunch()); + + args = CommandLineArgs.create( new String[] { "--ForceLAUNCH" } ); + assertTrue (args.isForceLaunch()); + } + +} diff --git a/backend/src/test/java/com/github/eostermueller/snail4j/TestInstallAdvice.java b/backend/src/test/java/com/github/eostermueller/snail4j/TestInstallAdvice.java index 6c83c15..90046a9 100644 --- a/backend/src/test/java/com/github/eostermueller/snail4j/TestInstallAdvice.java +++ b/backend/src/test/java/com/github/eostermueller/snail4j/TestInstallAdvice.java @@ -4,6 +4,8 @@ import java.io.File; import java.net.MalformedURLException; +import java.nio.file.Path; +import java.nio.file.Paths; import org.junit.Rule; import org.junit.contrib.java.lang.system.EnvironmentVariables; @@ -13,9 +15,12 @@ import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.condition.OS; +import com.github.eostermueller.snail4j.InstallAdvice.StartupLogger; import com.github.eostermueller.snail4j.launcher.CannotFindSnail4jFactoryClass; +import com.github.eostermueller.snail4j.launcher.Configuration; +import com.github.eostermueller.snail4j.launcher.agent.TestConfiguration; -class TestInstallAdvice { +class TestInstallAdvice implements InstallAdvice.StartupLogger { @TempDir static File sharedTempDir; @@ -30,18 +35,36 @@ public void canChangeEnvironmentVariable() { environmentVariables.clear("JAVA_HOME"); assertNull(System.getenv("JAVA_HOME")); } + @Override + public void info(String msg) { + System.out.println(msg); + } + + @Override + public void error(String msg) { + System.out.println(msg); + } + @Override + public void debug(String msg) { + System.out.println(msg); + } + @Test @EnabledOnOs({OS.LINUX, OS.MAC}) - public void canDetectWhen_JAVA_HOME_pointsToNonExistentFolder_nix() throws CannotFindSnail4jFactoryClass, MalformedURLException { + public void canDetectWhen_JAVA_HOME_pointsToNonExistentFolder_nix() throws MalformedURLException, Snail4jException { //set JAVA_HOME to a folder that does not exist environmentVariables.set("JAVA_HOME", File.separator + java.util.UUID.randomUUID().toString() ); - InstallAdvice installAdvice = new InstallAdvice(); - assertFalse( installAdvice.isJavaHomeEnvVarOk() );//detect that it doesn't exist + Configuration cfg = new TestConfiguration(); + + InstallAdvice installAdvice = new InstallAdvice( this ); + + Path p = JdkUtils.getCurrentJavaPath(); + assertFalse( installAdvice.isJavaHomeDirExists(p) );//detect that it doesn't exist } @Test @EnabledOnOs({OS.WINDOWS}) - public void canDetectWhen_JAVA_HOME_pointsToNonExistentFolder() throws CannotFindSnail4jFactoryClass, MalformedURLException { + public void canDetectWhen_JAVA_HOME_pointsToNonExistentFolder() throws MalformedURLException, Snail4jException { //set JAVA_HOME to a folder that does not exist String folderThatDoesNotExist = "C:" + File.separator + java.util.UUID.randomUUID().toString(); @@ -49,26 +72,30 @@ public void canDetectWhen_JAVA_HOME_pointsToNonExistentFolder() throws CannotFin String javaHome = System.getenv("JAVA_HOME"); assertEquals(folderThatDoesNotExist,javaHome); - InstallAdvice installAdvice = new InstallAdvice(); - assertFalse( installAdvice.isJavaHomeEnvVarOk() );//Detect that that folder does not exist. + Path p = Paths.get(javaHome); + InstallAdvice installAdvice = new InstallAdvice(this); + assertFalse( installAdvice.isJavaHomeDirExists(p) );//Detect that that folder does not exist. } @Test - public void canDetectWhen_JAVA_HOME_pointsToExistingFolder() throws CannotFindSnail4jFactoryClass, MalformedURLException { + public void canDetectWhen_JAVA_HOME_pointsToExistingFolder() throws MalformedURLException, Snail4jException { environmentVariables.set("JAVA_HOME",sharedTempDir.getAbsolutePath()); - InstallAdvice installAdvice = new InstallAdvice(); - assertTrue( installAdvice.isJavaHomeEnvVarOk() ); + Path p = JdkUtils.get_JAVA_HOME(); + + InstallAdvice installAdvice = new InstallAdvice(this); + assertTrue( installAdvice.isJavaHomeDirExists(p) ); } @Test - public void canDetectWhen_JAVA_HOME_isNotSet() throws CannotFindSnail4jFactoryClass, MalformedURLException { + public void canDetectWhen_JAVA_HOME_isNotSet() throws MalformedURLException, Snail4jException { environmentVariables.clear("JAVA_HOME"); + Path p = JdkUtils.get_JAVA_HOME(); - InstallAdvice installAdvice = new InstallAdvice(); - assertFalse( installAdvice.isJavaHomeEnvVarOk() ); + InstallAdvice installAdvice = new InstallAdvice(this); + assertFalse( installAdvice.isJavaHomeDirExists(p) ); }