Skip to content

Commit

Permalink
--forceLaunch flag will launch even if pre-install validations fail
Browse files Browse the repository at this point in the history
  • Loading branch information
eostermueller committed Jul 19, 2020
1 parent 4b5aa8e commit 1621031
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// }

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public ProcessModelBuilder createProcessModelBuilder() throws Snail4jException {
}
@Override
public
Snail4jInstaller createNewInstaller() {
Snail4jInstaller createNewInstaller() throws CannotFindSnail4jFactoryClass {
return new Snail4jInstaller();
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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();
Expand All @@ -64,7 +63,7 @@ public String toString() {
}
}

public void sutPortsAreAvailable(Configuration cfg) throws CannotFindSnail4jFactoryClass, Snail4jMultiException {
public int sutPortsAreAvailable(Configuration cfg) throws CannotFindSnail4jFactoryClass, Snail4jMultiException {
List<String> errors = new ArrayList<String>();
int timeoutMs = 1000;
List<TcpTarget> targets = new ArrayList<TcpTarget>();
Expand All @@ -78,26 +77,28 @@ 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);

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();

Expand All @@ -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)) {
Expand All @@ -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,
Expand All @@ -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() );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
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;
import javax.management.MBeanServer;
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");
Expand All @@ -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
*
Expand All @@ -65,9 +71,9 @@ static boolean JAVA_HOME_pointsToCurrentJava() throws Snail4jException {
└───webstart
</pre>
*/
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")) {
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 1621031

Please sign in to comment.