if (scriptEngineName != null)
{
scriptEngine = createScriptEngineByName(scriptEngineName);
if (scriptEngine == null)
{
throw new InitialisationException(MessageFactory.createStaticMessage("Scripting engine '" + scriptEngineName + "' not found. Available engines are: " + listAvailableEngines()), this);
}
}
// Determine scripting engine to use by file extension
else if (scriptFile != null)
{
int i = scriptFile.lastIndexOf(".");
if (i > -1)
{
logger.info("Script Engine name not set. Guessing by file extension.");
String ext = scriptFile.substring(i + 1);
scriptEngine = createScriptEngineByExtension(ext);
if (scriptEngine == null)
{
throw new InitialisationException(MessageFactory.createStaticMessage("File extension '" + ext + "' does not map to a scripting engine. Available engines are: " + listAvailableEngines()), this);
}
else
{
setScriptEngineName(scriptEngine.getFactory().getEngineName());
}
}
}
Reader script;
// Load script from variable
if (StringUtils.isNotBlank(scriptText))
{
script = new StringReader(scriptText);
}
// Load script from file
else if (scriptFile != null)
{
InputStream is;
try
{
is = IOUtils.getResourceAsStream(scriptFile, getClass());
}
catch (IOException e)
{
throw new InitialisationException(CoreMessages.cannotLoadFromClasspath(scriptFile), e, this);
}
if (is == null)
{
throw new InitialisationException(CoreMessages.cannotLoadFromClasspath(scriptFile), this);
}
script = new InputStreamReader(is);
}
else
{
throw new InitialisationException(CoreMessages.propertiesNotSet("scriptText, scriptFile"), this);
}
// Pre-compile script if scripting engine supports compilation.
if (scriptEngine instanceof Compilable)
{
try
{
compiledScript = ((Compilable) scriptEngine).compile(script);
}
catch (ScriptException e)
{
throw new InitialisationException(e, this);
}
}
}