[Major] add subpath to StrolchBootstrapper.setupByUserDir()

This commit is contained in:
Robert von Burg 2019-09-10 13:21:04 +02:00
parent 55e6121633
commit 5360d4c0fb
2 changed files with 39 additions and 37 deletions

View File

@ -72,7 +72,7 @@ public class StrolchBootstrapper extends DefaultHandler {
* Bootstrap Strolch using the given app {@link StrolchVersion}. This version is used for information on the code * Bootstrap Strolch using the given app {@link StrolchVersion}. This version is used for information on the code
* base from which the agent is instantiated. * base from which the agent is instantiated.
* </p> * </p>
* *
* @param appVersion * @param appVersion
*/ */
public StrolchBootstrapper(StrolchVersion appVersion) { public StrolchBootstrapper(StrolchVersion appVersion) {
@ -85,9 +85,9 @@ public class StrolchBootstrapper extends DefaultHandler {
* Bootstrap Strolch using the given {@link Class} from which to get the {@link #APP_VERSION_PROPERTIES} resource * Bootstrap Strolch using the given {@link Class} from which to get the {@link #APP_VERSION_PROPERTIES} resource
* stream. The version is used for information on the code base from which the agent is instantiated. * stream. The version is used for information on the code base from which the agent is instantiated.
* </p> * </p>
* *
* @param appClass * @param appClass
* the class where the {@link #APP_VERSION_PROPERTIES} resource resides * the class where the {@link #APP_VERSION_PROPERTIES} resource resides
*/ */
public StrolchBootstrapper(Class<?> appClass) { public StrolchBootstrapper(Class<?> appClass) {
DBC.PRE.assertNotNull("appClass must be set!", appClass); DBC.PRE.assertNotNull("appClass must be set!", appClass);
@ -108,11 +108,12 @@ public class StrolchBootstrapper extends DefaultHandler {
this.environmentOverride = environmentOverride; this.environmentOverride = environmentOverride;
} }
public StrolchAgent setupByUserDir(String environment) { public StrolchAgent setupByUserDir(String environment, String subPath) {
DBC.PRE.assertNotEmpty("Environment must be set!", environment); DBC.PRE.assertNotEmpty("Environment must be set!", environment);
DBC.PRE.assertNotEmpty("Sub Path must be set!", subPath);
this.environment = environment; this.environment = environment;
File rootPathF = new File(System.getProperty(SYS_PROP_USER_DIR)); File rootPathF = new File(System.getProperty(SYS_PROP_USER_DIR), subPath);
this.configPathF = new File(rootPathF, PATH_CONFIG); this.configPathF = new File(rootPathF, PATH_CONFIG);
this.dataPathF = new File(rootPathF, PATH_DATA); this.dataPathF = new File(rootPathF, PATH_DATA);
this.tempPathF = new File(rootPathF, PATH_TEMP); this.tempPathF = new File(rootPathF, PATH_TEMP);
@ -193,12 +194,12 @@ public class StrolchBootstrapper extends DefaultHandler {
/** /**
* Set up Strolch by evaluating the environment from {@link StrolchEnvironment#getEnvironmentFromResourceEnv(Class)} * Set up Strolch by evaluating the environment from {@link StrolchEnvironment#getEnvironmentFromResourceEnv(Class)}
* and then delegating to {@link #setupByBootstrapFile(String, File)} * and then delegating to {@link #setupByBootstrapFile(String, File)}
* *
* @param clazz * @param clazz
* the class from which to load the resource as stream * the class from which to load the resource as stream
* @param bootstrapFile * @param bootstrapFile
* the bootstrap file to load * the bootstrap file to load
* *
* @return the Agent which is setup * @return the Agent which is setup
*/ */
public StrolchAgent setupByBootstrapFile(Class<?> clazz, File bootstrapFile) { public StrolchAgent setupByBootstrapFile(Class<?> clazz, File bootstrapFile) {
@ -212,12 +213,12 @@ public class StrolchBootstrapper extends DefaultHandler {
/** /**
* Set up Strolch by evaluating the environment from {@link StrolchEnvironment#getEnvironmentFromResourceEnv(Class)} * Set up Strolch by evaluating the environment from {@link StrolchEnvironment#getEnvironmentFromResourceEnv(Class)}
* and then delegating to {@link #setupByBootstrapFile(String, File)} * and then delegating to {@link #setupByBootstrapFile(String, File)}
* *
* @param clazz * @param clazz
* the class from which to load the resource as stream * the class from which to load the resource as stream
* @param bootstrapFile * @param bootstrapFile
* the input stream to the bootstrap file to load * the input stream to the bootstrap file to load
* *
* @return the Agent which is setup * @return the Agent which is setup
*/ */
public StrolchAgent setupByBootstrapFile(Class<?> clazz, InputStream bootstrapFile) { public StrolchAgent setupByBootstrapFile(Class<?> clazz, InputStream bootstrapFile) {
@ -230,12 +231,12 @@ public class StrolchBootstrapper extends DefaultHandler {
/** /**
* Set up Strolch by loading the given bootstrap file for configuration * Set up Strolch by loading the given bootstrap file for configuration
* *
* @param environment * @param environment
* the environment to load from the boostrap file * the environment to load from the boostrap file
* @param bootstrapFile * @param bootstrapFile
* the bootstrap file to load * the bootstrap file to load
* *
* @return the Agent which is setup * @return the Agent which is setup
*/ */
public StrolchAgent setupByBootstrapFile(String environment, File bootstrapFile) { public StrolchAgent setupByBootstrapFile(String environment, File bootstrapFile) {
@ -248,12 +249,12 @@ public class StrolchBootstrapper extends DefaultHandler {
/** /**
* Set up Strolch by loading the given bootstrap file for configuration * Set up Strolch by loading the given bootstrap file for configuration
* *
* @param environment * @param environment
* the environment to load from the boostrap file * the environment to load from the boostrap file
* @param bootstrapFile * @param bootstrapFile
* the input stream to the bootstrap file to load * the input stream to the bootstrap file to load
* *
* @return the Agent which is setup * @return the Agent which is setup
*/ */
public StrolchAgent setupByBootstrapFile(String environment, InputStream bootstrapFile) { public StrolchAgent setupByBootstrapFile(String environment, InputStream bootstrapFile) {
@ -332,8 +333,9 @@ public class StrolchBootstrapper extends DefaultHandler {
XmlHelper.parseDocument(bootstrapFile, this); XmlHelper.parseDocument(bootstrapFile, this);
if (!this.envFound) { if (!this.envFound) {
throw new StrolchConfigurationException("Environment " + this.environment throw new StrolchConfigurationException(
+ " not configured in bootstrap configuration " + bootstrapFile.getAbsolutePath()); "Environment " + this.environment + " not configured in bootstrap configuration " + bootstrapFile
.getAbsolutePath());
} }
evaluatePaths(); evaluatePaths();
@ -356,8 +358,8 @@ public class StrolchBootstrapper extends DefaultHandler {
// validate the parsed data // validate the parsed data
if (!this.defaultAllowed) { if (!this.defaultAllowed) {
if (StringHelper.isEmpty(this.configS) || StringHelper.isEmpty(this.dataS) if (StringHelper.isEmpty(this.configS) || StringHelper.isEmpty(this.dataS) || StringHelper
|| StringHelper.isEmpty(this.tempS)) { .isEmpty(this.tempS)) {
String msg = "One element of " + Arrays.toString(new String[] { CONFIG, DATA, TEMP }) String msg = "One element of " + Arrays.toString(new String[] { CONFIG, DATA, TEMP })
+ " is not set and environment " + this.environment + " does not have attribute " + DEFAULT + " is not set and environment " + this.environment + " does not have attribute " + DEFAULT
+ "=\"true\". Either set the value or allow using default values!"; + "=\"true\". Either set the value or allow using default values!";
@ -365,9 +367,9 @@ public class StrolchBootstrapper extends DefaultHandler {
} }
} }
String root = StringHelper.isEmpty(this.rootS) String root = StringHelper.isEmpty(this.rootS) ?
? new File(System.getProperty(SYS_PROP_USER_DIR)).getAbsolutePath() new File(System.getProperty(SYS_PROP_USER_DIR)).getAbsolutePath() :
: this.rootS; this.rootS;
String config = StringHelper.isEmpty(this.configS) ? PATH_CONFIG : this.configS; String config = StringHelper.isEmpty(this.configS) ? PATH_CONFIG : this.configS;
String data = StringHelper.isEmpty(this.dataS) ? PATH_DATA : this.dataS; String data = StringHelper.isEmpty(this.dataS) ? PATH_DATA : this.dataS;
String temp = StringHelper.isEmpty(this.tempS) ? PATH_TEMP : this.tempS; String temp = StringHelper.isEmpty(this.tempS) ? PATH_TEMP : this.tempS;

View File

@ -17,7 +17,7 @@ public class BootstrapperTest {
public void shouldSetupByUserDir() { public void shouldSetupByUserDir() {
File rootSrcPath = new File("src/test/resources/configtest"); File rootSrcPath = new File("src/test/resources/configtest");
File rootDstPath = new File("target/shouldSetupByUserDir"); File rootDstPath = new File("target/shouldSetupByUserDir/runtime");
if (rootDstPath.exists() && !FileHelper.deleteFile(rootDstPath, true)) { if (rootDstPath.exists() && !FileHelper.deleteFile(rootDstPath, true)) {
throw new RuntimeException("Could not delete target " + rootDstPath); throw new RuntimeException("Could not delete target " + rootDstPath);
} }
@ -30,9 +30,9 @@ public class BootstrapperTest {
} }
String userDir = System.getProperty("user.dir"); String userDir = System.getProperty("user.dir");
System.setProperty("user.dir", rootDstPath.getAbsolutePath()); System.setProperty("user.dir", rootDstPath.getParentFile().getAbsolutePath());
try { try {
StrolchAgent agent = new StrolchBootstrapper(RuntimeMock.getAppVersion()).setupByUserDir("dev"); StrolchAgent agent = new StrolchBootstrapper(RuntimeMock.getAppVersion()).setupByUserDir("dev", "runtime");
assertEquals("dev", agent.getStrolchConfiguration().getRuntimeConfiguration().getEnvironment()); assertEquals("dev", agent.getStrolchConfiguration().getRuntimeConfiguration().getEnvironment());
} finally { } finally {
System.setProperty("user.dir", userDir); System.setProperty("user.dir", userDir);
@ -68,8 +68,8 @@ public class BootstrapperTest {
throw new RuntimeException("Could not delete target " + rootDstPath); throw new RuntimeException("Could not delete target " + rootDstPath);
} }
StrolchAgent agent = new StrolchBootstrapper(RuntimeMock.getAppVersion()).setupByCopyingRoot("dev", rootSrcPath, StrolchAgent agent = new StrolchBootstrapper(RuntimeMock.getAppVersion())
rootDstPath); .setupByCopyingRoot("dev", rootSrcPath, rootDstPath);
assertEquals("dev", agent.getStrolchConfiguration().getRuntimeConfiguration().getEnvironment()); assertEquals("dev", agent.getStrolchConfiguration().getRuntimeConfiguration().getEnvironment());
} }
@ -90,8 +90,8 @@ public class BootstrapperTest {
} }
File bootstrapFile = new File("src/test/resources/bootstraptest/StrolchBootstrap.xml"); File bootstrapFile = new File("src/test/resources/bootstraptest/StrolchBootstrap.xml");
StrolchAgent agent = new StrolchBootstrapper(RuntimeMock.getAppVersion()).setupByBootstrapFile("dev", StrolchAgent agent = new StrolchBootstrapper(RuntimeMock.getAppVersion())
bootstrapFile); .setupByBootstrapFile("dev", bootstrapFile);
assertEquals("dev", agent.getStrolchConfiguration().getRuntimeConfiguration().getEnvironment()); assertEquals("dev", agent.getStrolchConfiguration().getRuntimeConfiguration().getEnvironment());
agent.destroy(); agent.destroy();
} }
@ -120,8 +120,8 @@ public class BootstrapperTest {
File bootstrapFile = new File("src/test/resources/bootstraptest/StrolchBootstrap.xml"); File bootstrapFile = new File("src/test/resources/bootstraptest/StrolchBootstrap.xml");
StrolchAgent agent = new StrolchBootstrapper(RuntimeMock.getAppVersion()).setupByBootstrapFile("test.next", StrolchAgent agent = new StrolchBootstrapper(RuntimeMock.getAppVersion())
bootstrapFile); .setupByBootstrapFile("test.next", bootstrapFile);
assertEquals("test", agent.getStrolchConfiguration().getRuntimeConfiguration().getEnvironment()); assertEquals("test", agent.getStrolchConfiguration().getRuntimeConfiguration().getEnvironment());
agent.destroy(); agent.destroy();
} }