Skip to content

Commit

Permalink
aborting startup when JAVA_HOME is not set. addresses issue #44
Browse files Browse the repository at this point in the history
  • Loading branch information
eostermueller committed Jun 7, 2020
1 parent 566c8c0 commit 4d4d412
Show file tree
Hide file tree
Showing 22 changed files with 349 additions and 65 deletions.
8 changes: 8 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
<systemPath>${toolsjar}</systemPath>
</dependency>

<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<!-- <version>1.19.0</version> newer, but broken!!! -->
<version>1.17.2</version>
<scope>test</scope>
</dependency>



<!-- end of launcher dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**
Expand All @@ -16,8 +17,9 @@ public class Application extends SpringBootServletInitializer {


public static void main(String[] args) {
SpringApplication.run(Application.class, args);

SpringApplication application = new SpringApplication(Application.class);
application.addListeners(new ApplicationPidFileWriter("snail4j.pid"));
application.run(args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.eostermueller.snail4j;

public class DefaultJdkTools implements JdkTools {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.eostermueller.snail4j;

import java.net.MalformedURLException;
import java.net.URL;

public class DocumentationLinks {
public URL getJdkInstallAdvice() throws MalformedURLException {
return new URL("https://github.com/eostermueller/snail4j/wiki/JDK-Setup");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.eostermueller.snail4j;

import java.io.File;
import java.net.MalformedURLException;
import java.nio.file.Paths;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.eostermueller.snail4j.launcher.CannotFindSnail4jFactoryClass;
import com.github.eostermueller.snail4j.launcher.Messages;

public class InstallAdvice {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

/**
*
* @return
* @throws CannotFindSnail4jFactoryClass
* @throws MalformedURLException
*/
public boolean isJavaHomeEnvVarOk() throws CannotFindSnail4jFactoryClass, MalformedURLException {
boolean rc = false;

Messages m = DefaultFactory.getFactory().getMessages();

String javaHome = System.getenv("JAVA_HOME");
if (javaHome == null) {
LOGGER.error( m.javaHomeEnvVarNotSet() );
} else {
File javaHomeFolder = Paths.get(javaHome).toFile();
if (!javaHomeFolder.exists() || !javaHomeFolder.isDirectory()) {
LOGGER.error( m.javaHomeFolderDoesNotExist(javaHomeFolder) );
} else {
rc = true;
}
}

if (!rc) {
LOGGER.error( new DocumentationLinks().getJdkInstallAdvice().toString() );
}

return rc;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.eostermueller.snail4j;

public interface JdkTools {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
import java.util.ArrayList;
Expand All @@ -11,6 +12,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.eostermueller.snail4j.launcher.CannotFindSnail4jFactoryClass;
import com.github.eostermueller.snail4j.launcher.Configuration;

/**
Expand All @@ -32,6 +34,16 @@
*
*/
public class Snail4jInstaller {
public int preinstallCheck() throws CannotFindSnail4jFactoryClass, MalformedURLException {
int zeroErrorsMeansSuccess = 0;

InstallAdvice ia = new InstallAdvice();

if (!ia.isJavaHomeEnvVarOk() )
zeroErrorsMeansSuccess++;

return zeroErrorsMeansSuccess;
}
Configuration getConfiguration() throws Snail4jException {
return DefaultFactory.getFactory().getConfiguration();
}
Expand Down Expand Up @@ -62,6 +74,7 @@ public void install() throws Snail4jException {
}

}

private void installJMeterDistribution() throws Snail4jException {
PathUtil pathUtil = new PathUtil();
String path = pathUtil.getBaseClassspath();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.github.eostermueller.snail4j;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import com.github.eostermueller.snail4j.launcher.CannotFindSnail4jFactoryClass;
import com.github.eostermueller.snail4j.launcher.ConfigVariableNotFoundException;
import com.github.eostermueller.snail4j.launcher.Configuration;
import com.github.eostermueller.snail4j.launcher.Event;
import com.github.eostermueller.snail4j.launcher.Suite;
import com.github.eostermueller.snail4j.processmodel.ProcessModelBuilder;
import com.github.eostermueller.snail4j.launcher.Messages;
import com.github.eostermueller.snail4j.processmodel.ProcessModelSingleton;

/**
Expand All @@ -27,6 +29,8 @@
*/
@Component
public class SpringBootSnail4J implements ApplicationListener<ApplicationReadyEvent> {
@Autowired
private ConfigurableApplicationContext ctx;
private static final String WIREMOCK_PORT_PROPERTY = "wiremockPort";

private static final String H2_PORT_PROPERTY = "h2Port";
Expand All @@ -50,15 +54,13 @@ public static void setProcessModelSingleton(ProcessModelSingleton val){
public void onApplicationEvent(ApplicationReadyEvent event) {

try {
Configuration cfg = DefaultFactory.getFactory().getConfiguration();
Configuration cfg = DefaultFactory.getFactory().getConfiguration();


install(cfg);
DefaultFactory.getFactory().getEventHistory().getEvents().add(
Event.create("snail4j install complete.") );
initProcessModel();
initPorts(cfg);
DefaultFactory.getFactory().getEventHistory().getEvents().add(
Event.create("snail4j startup complete.") );
} catch (Snail4jException e) {
} catch (Snail4jException | MalformedURLException e) {
try {
ProcessModelSingleton.getInstance().setCauseOfSystemFailure(e);
DefaultFactory.getFactory().getEventHistory().addException("Exception during snail4j startup.", e);
Expand Down Expand Up @@ -132,36 +134,52 @@ private void initPorts(Configuration cfg) throws Snail4jException {
private void initProcessModel() throws ConfigVariableNotFoundException, Snail4jException {
ProcessModelSingleton.getInstance();
}
private void install(Configuration cfg) {
private void install(Configuration cfg) throws CannotFindSnail4jFactoryClass, MalformedURLException {
String path = new PathUtil().getBaseClassspath();
Messages m = DefaultFactory.getFactory().getMessages();
if (path.contains(PathUtil.JAR_SUFFIX)) { //only install if launched using "java -jar". Elsewise, installs happen with every "backend" build, because Spring Boot is launched during integration testing.
dispInstallBanner();
try {

dispInstallBanner(m.startInstallMessage() );
DefaultFactory.getFactory().getEventHistory().getEvents().add(
Event.create(m.startInstallMessage()) );

Snail4jInstaller snail4jInstaller = DefaultFactory.getFactory().createNewInstaller();
LOGGER.info("Maven online mode: " + cfg.isMavenOnline() );
LOGGER.info("snail4j maven repo location: " + cfg.isSnail4jMavenRepo() );

LOGGER.info("online: " + cfg.isMavenOnline() );
LOGGER.info("snail4j maven repo: " + cfg.isSnail4jMavenRepo() );
snail4jInstaller.install();
int countOfFailedPreChecks = snail4jInstaller.preinstallCheck();
if (countOfFailedPreChecks==0)
snail4jInstaller.install();
else
throw new Snail4jException(String.format("[%d] install prechecks failed.", countOfFailedPreChecks));

LOGGER.info("Install finished. Ready to load test!");

dispInstallBanner( m.successfulInstallation() );
DefaultFactory.getFactory().getEventHistory().getEvents().add(
Event.create(m.successfulInstallation()) );
} catch (Snail4jException e) {
// TODO Auto-generated catch block
dispInstallBanner( m.failedInstallation() );
DefaultFactory.getFactory().getEventHistory().getEvents().add(
Event.create(m.failedInstallation()) );
e.printStackTrace();
}
if (ctx !=null)
ctx.close();//Shut down spring boot
}

} else {
this.LOGGER.info("Install has been skipped! It only runs when WindTunnel is launched with 'java -jar'. Startup: [" + path + "]" );
this.LOGGER.error("Install has been skipped! It only runs when WindTunnel is launched with 'java -jar'. Startup: [" + path + "]" );
}
}

private void dispInstallBanner() {
this.LOGGER.info("##########################" );
this.LOGGER.info("##########################" );
this.LOGGER.info("#### #####" );
this.LOGGER.info("#### I N S T A L L #####" );
this.LOGGER.info("#### #####" );
this.LOGGER.info("##########################" );
this.LOGGER.info("##########################" );
private void dispInstallBanner(String msg) {
this.LOGGER.info("########################################" );
this.LOGGER.info("########################################" );
this.LOGGER.info("#### #####" );
this.LOGGER.info("#### " + msg);
this.LOGGER.info("#### #####" );
this.LOGGER.info("########################################" );
this.LOGGER.info("########################################" );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.nio.file.Path;

import com.github.eostermueller.snail4j.Snail4jException;

public interface Configuration {
/**
* Path with
Expand Down Expand Up @@ -123,6 +125,7 @@ public interface Configuration {
String getUseCaseSearchCriteria();
void setSutJvmArguments(String val);
String getSutJvmArguments();
void setDefaultHostname(String hostname) throws Snail4jException;



Expand Down
Loading

0 comments on commit 4d4d412

Please sign in to comment.