[Minor] Added further methods for setting up a Strolch Agent
This commit is contained in:
parent
fa136c966c
commit
b30b791460
|
@ -14,6 +14,7 @@ import org.xml.sax.helpers.DefaultHandler;
|
|||
|
||||
import li.strolch.runtime.configuration.ConfigurationParser;
|
||||
import li.strolch.runtime.configuration.StrolchConfigurationException;
|
||||
import li.strolch.runtime.configuration.StrolchEnvironment;
|
||||
import li.strolch.utils.dbc.DBC;
|
||||
import li.strolch.utils.helper.FileHelper;
|
||||
import li.strolch.utils.helper.StringHelper;
|
||||
|
@ -189,6 +190,54 @@ public class StrolchBootstrapper extends DefaultHandler {
|
|||
return setup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up Strolch by evaluating the environment from {@link StrolchEnvironment#getEnvironmentFromResourceEnv(Class)}
|
||||
* and then delegating to {@link #setupByBoostrapFile(String, File)}
|
||||
*
|
||||
* @param clazz
|
||||
* the class from which to load the resource as stream
|
||||
* @param bootstrapFile
|
||||
* the bootstrap file to load
|
||||
*
|
||||
* @return the Agent which is setup
|
||||
*/
|
||||
public StrolchAgent setupByBoostrapFile(Class<?> clazz, File bootstrapFile) {
|
||||
DBC.PRE.assertNotNull("clazz must be set!", clazz);
|
||||
DBC.PRE.assertNotNull("bootstrapFile must be set!", bootstrapFile);
|
||||
this.environment = StrolchEnvironment.getEnvironmentFromResourceEnv(clazz);
|
||||
parseBoostrapFile(bootstrapFile);
|
||||
return setup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up Strolch by evaluating the environment from {@link StrolchEnvironment#getEnvironmentFromResourceEnv(Class)}
|
||||
* and then delegating to {@link #setupByBoostrapFile(String, File)}
|
||||
*
|
||||
* @param clazz
|
||||
* the class from which to load the resource as stream
|
||||
* @param bootstrapFile
|
||||
* the input stream to the bootstrap file to load
|
||||
*
|
||||
* @return the Agent which is setup
|
||||
*/
|
||||
public StrolchAgent setupByBoostrapFile(Class<?> clazz, InputStream bootstrapFile) {
|
||||
DBC.PRE.assertNotNull("clazz must be set!", clazz);
|
||||
DBC.PRE.assertNotNull("bootstrapFile must be set!", bootstrapFile);
|
||||
this.environment = StrolchEnvironment.getEnvironmentFromResourceEnv(clazz);
|
||||
parseBoostrapFile(bootstrapFile);
|
||||
return setup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up Strolch by loading the given bootstrap file for configuration
|
||||
*
|
||||
* @param environment
|
||||
* the environment to load from the boostrap file
|
||||
* @param bootstrapFile
|
||||
* the bootstrap file to load
|
||||
*
|
||||
* @return the Agent which is setup
|
||||
*/
|
||||
public StrolchAgent setupByBoostrapFile(String environment, File bootstrapFile) {
|
||||
DBC.PRE.assertNotEmpty("Environment must be set!", environment);
|
||||
DBC.PRE.assertNotNull("bootstrapFile must be set!", bootstrapFile);
|
||||
|
@ -197,6 +246,24 @@ public class StrolchBootstrapper extends DefaultHandler {
|
|||
return setup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up Strolch by loading the given bootstrap file for configuration
|
||||
*
|
||||
* @param environment
|
||||
* the environment to load from the boostrap file
|
||||
* @param bootstrapFile
|
||||
* the input stream to the bootstrap file to load
|
||||
*
|
||||
* @return the Agent which is setup
|
||||
*/
|
||||
public StrolchAgent setupByBoostrapFile(String environment, InputStream bootstrapFile) {
|
||||
DBC.PRE.assertNotEmpty("Environment must be set!", environment);
|
||||
DBC.PRE.assertNotNull("bootstrapFile must be set!", bootstrapFile);
|
||||
this.environment = environment;
|
||||
parseBoostrapFile(bootstrapFile);
|
||||
return setup();
|
||||
}
|
||||
|
||||
private StrolchAgent setup() {
|
||||
|
||||
DBC.PRE.assertNotEmpty("Environment must be set!", this.environment);
|
||||
|
@ -267,6 +334,24 @@ public class StrolchBootstrapper extends DefaultHandler {
|
|||
+ " not configured in bootstrap configuration " + bootstrapFile.getAbsolutePath());
|
||||
}
|
||||
|
||||
evaluatePaths();
|
||||
}
|
||||
|
||||
private void parseBoostrapFile(InputStream bootstrapStream) {
|
||||
|
||||
// parse the document using ourselves as the DefaultHandler
|
||||
XmlHelper.parseDocument(bootstrapStream, this);
|
||||
|
||||
if (!this.envFound) {
|
||||
throw new StrolchConfigurationException("Environment " + this.environment
|
||||
+ " not configured in bootstrap configuration from given stream!");
|
||||
}
|
||||
|
||||
evaluatePaths();
|
||||
}
|
||||
|
||||
private void evaluatePaths() {
|
||||
|
||||
// validate the parsed data
|
||||
if (!this.defaultAllowed) {
|
||||
if (StringHelper.isEmpty(this.configS) || StringHelper.isEmpty(this.dataS)
|
||||
|
|
|
@ -17,9 +17,14 @@ package li.strolch.runtime.configuration;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import li.strolch.runtime.StrolchConstants;
|
||||
import li.strolch.utils.dbc.DBC;
|
||||
import li.strolch.utils.helper.StringHelper;
|
||||
|
@ -31,6 +36,8 @@ public class StrolchEnvironment {
|
|||
|
||||
public static final String ENV_PROPERTIES_FILE = "ENV.properties"; //$NON-NLS-1$
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(StrolchEnvironment.class);
|
||||
|
||||
public static String getEnvironmentFromSystemProperties() {
|
||||
String environment = System.getProperties().getProperty(StrolchConstants.ENV_STROLCH);
|
||||
if (StringHelper.isEmpty(environment)) {
|
||||
|
@ -50,8 +57,8 @@ public class StrolchEnvironment {
|
|||
try (FileInputStream fin = new FileInputStream(envF)) {
|
||||
envP.load(fin);
|
||||
} catch (Exception e) {
|
||||
throw new StrolchConfigurationException(MessageFormat.format(
|
||||
"Failed to load {0} in {1}", ENV_PROPERTIES_FILE, rootPath), e); //$NON-NLS-1$
|
||||
throw new StrolchConfigurationException(
|
||||
MessageFormat.format("Failed to load {0} in {1}", ENV_PROPERTIES_FILE, rootPath), e); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
String environment = envP.getProperty(StrolchConstants.ENV_STROLCH);
|
||||
|
@ -63,4 +70,32 @@ public class StrolchEnvironment {
|
|||
|
||||
return environment;
|
||||
}
|
||||
|
||||
public static String getEnvironmentFromResourceEnv(Class<?> clazz) {
|
||||
InputStream stream = clazz.getResourceAsStream("/" + ENV_PROPERTIES_FILE);
|
||||
DBC.PRE.assertNotNull(
|
||||
MessageFormat.format("{0} does not exist as root resource for class {1}", ENV_PROPERTIES_FILE, clazz),
|
||||
stream);
|
||||
Properties envP = new Properties();
|
||||
try {
|
||||
envP.load(stream);
|
||||
} catch (Exception e) {
|
||||
throw new StrolchConfigurationException(MessageFormat.format("Failed to load {0}", ENV_PROPERTIES_FILE), e); //$NON-NLS-1$
|
||||
} finally {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to close InputStream!", e);
|
||||
}
|
||||
}
|
||||
|
||||
String environment = envP.getProperty(StrolchConstants.ENV_STROLCH);
|
||||
if (StringHelper.isEmpty(environment)) {
|
||||
String msg = "The property {0} does not exist in {1}"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, StrolchConstants.ENV_STROLCH, ENV_PROPERTIES_FILE);
|
||||
throw new StrolchConfigurationException(msg);
|
||||
}
|
||||
|
||||
return environment;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue