use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.
the class RulesCache method createMachine.
private PrologMachineCopy createMachine(Project.NameKey project, ObjectId rulesId) throws CompileException {
//
if (rulesDir != null) {
Path jarPath = rulesDir.resolve("rules-" + rulesId.getName() + ".jar");
if (Files.isRegularFile(jarPath)) {
URL[] cp = new URL[] { toURL(jarPath) };
return save(newEmptyMachine(new URLClassLoader(cp, systemLoader)));
}
}
// Dynamically consult the rules into the machine's internal database.
//
String rules = read(project, rulesId);
PrologMachineCopy pmc = consultRules("rules.pl", new StringReader(rules));
if (pmc == null) {
throw new CompileException("Cannot consult rules of " + project);
}
return pmc;
}
use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.
the class RulesCache method read.
private String read(Project.NameKey project, ObjectId rulesId) throws CompileException {
try (Repository git = gitMgr.openRepository(project)) {
try {
ObjectLoader ldr = git.open(rulesId, Constants.OBJ_BLOB);
byte[] raw = ldr.getCachedBytes(maxSrcBytes);
return RawParseUtils.decode(raw);
} catch (LargeObjectException e) {
throw new CompileException("rules of " + project + " are too large", e);
} catch (RuntimeException | IOException e) {
throw new CompileException("Cannot load rules of " + project, e);
}
} catch (IOException e) {
throw new CompileException("Cannot open repository " + project, e);
}
}
use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.
the class SubmitRuleEvaluator method runSubmitFilters.
private Term runSubmitFilters(Term results, PrologEnvironment env, String filterRuleLocatorName, String filterRuleWrapperName) throws RuleEvalException {
ProjectState projectState = control.getProjectControl().getProjectState();
PrologEnvironment childEnv = env;
for (ProjectState parentState : projectState.parents()) {
PrologEnvironment parentEnv;
try {
parentEnv = parentState.newPrologEnvironment();
} catch (CompileException err) {
throw new RuleEvalException("Cannot consult rules.pl for " + parentState.getProject().getName(), err);
}
parentEnv.copyStoredValues(childEnv);
Term filterRule = parentEnv.once("gerrit", filterRuleLocatorName, new VariableTerm());
try {
if (opts.fastEvalLabels()) {
env.once("gerrit", "assume_range_from_label");
}
Term[] template = parentEnv.once("gerrit", filterRuleWrapperName, filterRule, results, new VariableTerm());
results = template[2];
} catch (ReductionLimitException err) {
throw new RuleEvalException(String.format("%s on change %d of %s", err.getMessage(), cd.getId().get(), parentState.getProject().getName()));
} catch (RuntimeException err) {
throw new RuleEvalException(String.format("Exception calling %s on change %d of %s", filterRule, cd.getId().get(), parentState.getProject().getName()), err);
} finally {
reductionsConsumed += env.getReductions();
}
childEnv = parentEnv;
}
return results;
}
use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.
the class SubmitRuleEvaluator method getPrologEnvironment.
private PrologEnvironment getPrologEnvironment(CurrentUser user) throws RuleEvalException {
ProjectState projectState = control.getProjectControl().getProjectState();
PrologEnvironment env;
try {
if (opts.rule() == null) {
env = projectState.newPrologEnvironment();
} else {
env = projectState.newPrologEnvironment("stdin", new StringReader(opts.rule()));
}
} catch (CompileException err) {
String msg;
if (opts.rule() == null && control.getProjectControl().isOwner()) {
msg = String.format("Cannot load rules.pl for %s: %s", getProjectName(), err.getMessage());
} else if (opts.rule() != null) {
msg = err.getMessage();
} else {
msg = String.format("Cannot load rules.pl for %s", getProjectName());
}
throw new RuleEvalException(msg, err);
}
env.set(StoredValues.REVIEW_DB, cd.db());
env.set(StoredValues.CHANGE_DATA, cd);
env.set(StoredValues.CHANGE_CONTROL, control);
if (user != null) {
env.set(StoredValues.CURRENT_USER, user);
}
return env;
}
use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.
the class PrologCompiler method compileJava.
/** Compile java src into java .class files */
private void compileJava(File tempDir) throws IOException, CompileException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new CompileException("JDK required (running inside of JRE)");
}
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(getAllFiles(tempDir, ".java"));
ArrayList<String> options = new ArrayList<>();
String classpath = getMyClasspath();
if (classpath != null) {
options.add("-classpath");
options.add(classpath);
}
options.add("-d");
options.add(tempDir.getPath());
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
if (!task.call()) {
Locale myLocale = Locale.getDefault();
StringBuilder msg = new StringBuilder();
msg.append("Cannot compile to Java bytecode:");
for (Diagnostic<? extends JavaFileObject> err : diagnostics.getDiagnostics()) {
msg.append('\n');
msg.append(err.getKind());
msg.append(": ");
if (err.getSource() != null) {
msg.append(err.getSource().getName());
}
msg.append(':');
msg.append(err.getLineNumber());
msg.append(": ");
msg.append(err.getMessage(myLocale));
}
throw new CompileException(msg.toString());
}
}
}
Aggregations