use of com.dexels.navajo.script.api.CompilationException in project navajo by Dexels.
the class BundleCreatorComponent method compileAndCreateBundle.
private void compileAndCreateBundle(String script, File scriptPath, final String scriptTenant, boolean hasTenantSpecificFile, boolean forceTenant, boolean keepIntermediate, List<String> success, List<String> skipped, List<String> failures) throws CompilationException {
String myScript = script;
if (forceTenant) {
myScript = script + "_" + scriptTenant;
}
try {
getScriptCompiler(scriptPath).compile(scriptPath, scriptTenant, hasTenantSpecificFile, forceTenant);
if (getScriptCompiler(scriptPath).scriptNeedsCompilation()) {
javaCompiler.compileJava(myScript);
}
javaCompiler.compileJava(myScript + "Factory");
createBundleJar(myScript, scriptPath, scriptTenant, keepIntermediate, hasTenantSpecificFile);
success.add(myScript);
} catch (SkipCompilationException e) {
logger.debug("Script fragment: {} ", script);
skipped.add(script);
return;
} catch (Exception e) {
failures.add(script);
throw new CompilationException("Error compiling script", e);
}
if (forceTenant) {
logger.info("Finished compiling and bundling {} for {}", script, scriptTenant);
} else {
logger.info("Finished compiling and bundling {}", script);
}
}
use of com.dexels.navajo.script.api.CompilationException in project navajo by Dexels.
the class ScalaCompiler method compileScript.
@Override
protected Set<String> compileScript(File scriptFile, String script, String packagePath, List<Dependency> dependencies, String tenant, boolean hasTenantSpecificFile, boolean forceTenant) throws CompilationException {
final Set<String> packages = new HashSet<>();
for (String pkg : standardPackages) {
packages.add(pkg);
}
PackageReportingClassLoader prc = new PackageReportingClassLoader(navajoScriptClassLoader);
prc.addPackageListener(name -> {
if (whitelist.contains(name)) {
packages.add(name);
}
});
File targetDir = new File(navajoIOConfig.getCompiledScriptPath(), packagePath + File.separator + script);
targetDir.mkdirs();
Settings settings = new Settings();
settings.outputDirs().setSingleOutput(targetDir.getAbsolutePath());
ConsoleReporter reporter = new ConsoleReporter(settings);
ReflectGlobal g = new ReflectGlobal(settings, reporter, prc);
Global.Run compiler = g.new Run();
ListBuffer<String> files = new ListBuffer<>();
String file = scriptFile.getAbsolutePath();
addScalaCommon(files);
files.$plus$eq(file);
try {
compiler.compile(files.toList());
} catch (Exception e) {
logger.error("Exception on getting scala code! {}", e);
}
logger.debug("finished compiling scala for the following files: {}", compiler.compiledFiles());
return packages;
}
use of com.dexels.navajo.script.api.CompilationException in project navajo by Dexels.
the class TslCompiler method compileToJava.
/**
* Only used by OSGi now
*
* @param script
* @param input
* @param output
* @param packagePath
* The package in the Java file. OSGi doesn't handle default
* packages well, so default package will be translated to
* 'defaultPackage'
* @param scriptPackagePath
* the path of the scriptFile from the scriptRoot
* @param classLoader
* @param navajoIOConfig
* @param forceTenant TODO
* @return
* @throws Exception
*/
public String compileToJava(String script, String input, String output, String packagePath, String scriptPackagePath, ClassLoader classLoader, NavajoIOConfig navajoIOConfig, List<Dependency> deps, String tenant, boolean hasTenantSpecificScript, boolean forceTenant) throws CompilationException, SkipCompilationException {
String tenantScript = script;
if (forceTenant) {
tenantScript = script + "_" + tenant;
}
String javaFile = output + "/" + tenantScript + ".java";
TslCompiler tslCompiler = new TslCompiler(classLoader, navajoIOConfig);
try {
String bareScript;
if (script.indexOf('/') >= 0) {
bareScript = script.substring(script.lastIndexOf('/') + 1, script.length());
} else {
bareScript = script;
}
tslCompiler.compileScript(bareScript, input, output, scriptPackagePath, navajoIOConfig.getOutputWriter(output, packagePath, tenantScript, ".java"), deps, tenant, hasTenantSpecificScript, forceTenant);
return javaFile;
} catch (SkipCompilationException ex) {
throw new SkipCompilationException("Skip compilation exception, script: " + script + " does not need compiling");
} catch (Throwable ex) {
logger.error("Error compiling script: " + script, ex);
// Isn't this what 'finally' is for?
File f = new File(javaFile);
if (f.exists()) {
f.delete();
}
if (ex instanceof Exception) {
throw new CompilationException("Other compilation exception:", ex);
}
return null;
}
}
use of com.dexels.navajo.script.api.CompilationException in project navajo by Dexels.
the class BundleCreatorComponent method createBundleNoLocking.
private void createBundleNoLocking(String rpcName, List<String> failures, List<String> success, List<String> skipped, boolean force, boolean keepIntermediate) throws CompilationException {
boolean matchedScript = false;
removeOldCompiledScriptFiles(rpcName);
for (ScriptCompiler compiler : compilers.values()) {
System.err.println("Checking compiler " + compiler + " to compile service " + rpcName);
File scriptFolder = new File(navajoIOConfig.getRootPath(), compiler.getRelativeScriptPath());
File f = new File(scriptFolder, rpcName + compiler.getScriptExtension());
// Look for other tenant-specific files
AbstractFileFilter fileFilter = new WildcardFileFilter(FilenameUtils.getBaseName(rpcName) + "_*" + compiler.getScriptExtension());
File dir = new File(scriptFolder, FilenameUtils.getPath(rpcName));
Collection<File> files = Collections.<File>emptySet();
if (dir.exists()) {
files = FileUtils.listFiles(dir, fileFilter, null);
}
Map<String, File> tenantSpecificFiles = new HashMap<>();
Collection<String> tenantsToIgnore = new ArrayList<>();
for (File ascript : files) {
matchedScript = true;
String pathRelative = getRelative(scriptFolder, ascript);
String[] splitted = pathRelative.split("\\.");
String tenantScriptName = splitted[0].replace('\\', '/');
tenantSpecificFiles.put(tenantScriptName, ascript);
// Get the tenant out of the name and put it in the tenantsToIgnore collection
tenantsToIgnore.add(tenantScriptName.split("_")[1]);
}
if (f.exists()) {
matchedScript = true;
createBundleForScript(rpcName, rpcName, f, tenantsToIgnore, true, failures, success, skipped, keepIntermediate);
}
for (Map.Entry<String, File> entry : tenantSpecificFiles.entrySet()) {
createBundleForScript(entry.getKey(), rpcName, entry.getValue(), tenantsToIgnore, false, failures, success, skipped, keepIntermediate);
}
if (matchedScript) {
break;
}
}
if (!matchedScript) {
throw new CompilationException("Unable to find script for " + rpcName);
}
}
use of com.dexels.navajo.script.api.CompilationException in project navajo by Dexels.
the class BundleCreatorComponent method getCompiledScript.
@SuppressWarnings("unchecked")
private CompiledScriptInterface getCompiledScript(String rpcName, String tenant) throws CompilationException {
String scriptName = rpcName.replaceAll("/", ".");
String realTenant = "default";
if (tenant != null) {
realTenant = tenant;
}
if (scriptsMap.containsKey(rpcName)) {
Map<String, CompiledScriptFactory> myScripts = scriptsMap.get(rpcName);
CompiledScriptFactory csf = myScripts.get(realTenant);
if (csf != null) {
try {
return csf.getCompiledScript();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
logger.error("Exception on retrieving cached CompiledScriptFactory for {} {} - going to try non-cached one", rpcName, realTenant);
}
}
}
String filter = "(&(navajo.scriptName=" + scriptName + ") (|(navajo.tenant=" + tenant + ") (navajo.tenant=default)))";
ServiceReference<CompiledScriptFactory>[] servicereferences;
try {
servicereferences = (ServiceReference<CompiledScriptFactory>[]) bundleContext.getServiceReferences(CompiledScriptFactory.class.getName(), filter);
if (servicereferences != null) {
// First try to find one that matches our tenant
for (ServiceReference<CompiledScriptFactory> srinstance : servicereferences) {
if (srinstance.getProperty("navajo.tenant").equals(tenant)) {
CompiledScriptFactory csf = bundleContext.getService(srinstance);
if (csf == null) {
logger.warn("Script with filter: {} found, but could not be resolved.", filter);
return null;
}
updateCachedCompiledScript(rpcName, realTenant, csf);
return csf.getCompiledScript();
}
}
// if that fails, simply return first one (probably "default")
if (servicereferences.length > 0) {
ServiceReference<CompiledScriptFactory> srinstance = servicereferences[0];
CompiledScriptFactory csf = bundleContext.getService(srinstance);
if (csf == null) {
logger.warn("Script with filter: {} found, but could not be resolved.", filter);
return null;
}
updateCachedCompiledScript(rpcName, realTenant, csf);
return csf.getCompiledScript();
}
}
} catch (InvalidSyntaxException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new CompilationException("Error resolving script service for: " + rpcName, e);
}
return null;
}
Aggregations