use of bsh.Interpreter in project symmetric-ds by JumpMind.
the class ExtensionService method registerExtension.
protected void registerExtension(Extension extension) {
if (extension.getExtensionText() != null) {
if (extension.getExtensionType().equalsIgnoreCase(Extension.EXTENSION_TYPE_JAVA)) {
try {
Object ext = simpleClassCompiler.getCompiledClass(extension.getExtensionText());
registerExtension(extension.getExtensionId(), (IExtensionPoint) ext);
} catch (Exception e) {
log.error("Error while compiling Java extension " + extension.getExtensionId(), e);
}
} else if (extension.getExtensionType().equalsIgnoreCase(Extension.EXTENSION_TYPE_BSH)) {
try {
Interpreter interpreter = new Interpreter();
interpreter.eval(extension.getExtensionText());
interpreter.set("engine", engine);
interpreter.set("sqlTemplate", engine.getDatabasePlatform().getSqlTemplate());
interpreter.set("log", log);
Object ext = interpreter.getInterface(Class.forName(extension.getInterfaceName()));
registerExtension(extension.getExtensionId(), (IExtensionPoint) ext);
} catch (EvalError e) {
log.error("Error while parsing BSH extension " + extension.getExtensionId(), e);
} catch (ClassNotFoundException e) {
log.error("Interface class not found for BSH extension " + extension.getExtensionId(), e);
}
} else {
log.error("Skipping extension " + extension.getExtensionId() + ", unknown extension type " + extension.getExtensionType());
}
}
}
use of bsh.Interpreter in project symmetric-ds by JumpMind.
the class FileSyncService method processZip.
protected List<IncomingBatch> processZip(InputStream is, String sourceNodeId, ProcessInfo processInfo) throws IOException {
File unzipDir = new File(parameterService.getTempDirectory(), String.format("filesync_incoming/%s/%s", engine.getNodeService().findIdentityNodeId(), sourceNodeId));
FileUtils.deleteDirectory(unzipDir);
unzipDir.mkdirs();
try {
AppUtils.unzip(is, unzipDir);
} catch (IoException ex) {
if (ex.toString().contains("EOFException")) {
// This happens on Android, when there is an empty zip.
//log.debug("Caught exception while unzipping.", ex);
} else {
throw ex;
}
}
Set<Long> batchIds = new TreeSet<Long>();
String[] files = unzipDir.list(DirectoryFileFilter.INSTANCE);
if (files != null) {
for (int i = 0; i < files.length; i++) {
try {
batchIds.add(Long.parseLong(files[i]));
} catch (NumberFormatException e) {
log.error("Unexpected directory name. Expected a number representing a batch id. Instead the directory was named '{}'", files[i]);
}
}
}
List<IncomingBatch> batchesProcessed = new ArrayList<IncomingBatch>();
IIncomingBatchService incomingBatchService = engine.getIncomingBatchService();
processInfo.setStatus(ProcessInfo.Status.LOADING);
for (Long batchId : batchIds) {
processInfo.setCurrentBatchId(batchId);
processInfo.incrementBatchCount();
File batchDir = new File(unzipDir, Long.toString(batchId));
IncomingBatch incomingBatch = new IncomingBatch();
File batchInfo = new File(batchDir, "batch-info.txt");
if (batchInfo.exists()) {
List<String> info = FileUtils.readLines(batchInfo);
if (info != null && info.size() > 0) {
incomingBatch.setChannelId(info.get(0).trim());
} else {
incomingBatch.setChannelId(Constants.CHANNEL_FILESYNC);
}
} else {
incomingBatch.setChannelId(Constants.CHANNEL_FILESYNC);
}
incomingBatch.setBatchId(batchId);
incomingBatch.setStatus(IncomingBatch.Status.LD);
incomingBatch.setNodeId(sourceNodeId);
incomingBatch.setByteCount(FileUtils.sizeOfDirectory(batchDir));
batchesProcessed.add(incomingBatch);
if (incomingBatchService.acquireIncomingBatch(incomingBatch)) {
File syncScript = new File(batchDir, "sync.bsh");
if (syncScript.exists()) {
String script = FileUtils.readFileToString(syncScript);
Interpreter interpreter = new Interpreter();
boolean isLocked = false;
try {
setInterpreterVariables(engine, sourceNodeId, batchDir, interpreter);
long waitMillis = getParameterService().getLong(ParameterConstants.FILE_SYNC_LOCK_WAIT_MS);
log.debug("The {} node is attempting to get shared lock for to update incoming status", sourceNodeId);
isLocked = engine.getClusterService().lock(ClusterConstants.FILE_SYNC_SHARED, ClusterConstants.TYPE_SHARED, waitMillis);
if (isLocked) {
log.debug("The {} node got a shared file sync lock", sourceNodeId);
@SuppressWarnings("unchecked") Map<String, String> filesToEventType = (Map<String, String>) interpreter.eval(script);
if (engine.getParameterService().is(ParameterConstants.FILE_SYNC_PREVENT_PING_BACK)) {
updateFileIncoming(sourceNodeId, filesToEventType);
}
incomingBatch.setStatementCount(filesToEventType != null ? filesToEventType.size() : 0);
} else {
throw new RuntimeException("Could not obtain file sync shared lock within " + waitMillis + " millis");
}
incomingBatch.setStatus(IncomingBatch.Status.OK);
if (incomingBatchService.isRecordOkBatchesEnabled()) {
incomingBatchService.updateIncomingBatch(incomingBatch);
} else if (incomingBatch.isRetry()) {
incomingBatchService.deleteIncomingBatch(incomingBatch);
}
} catch (Throwable ex) {
if (ex instanceof TargetError) {
Throwable target = ((TargetError) ex).getTarget();
if (target != null) {
ex = target;
}
} else if (ex instanceof EvalError) {
log.error("Failed to evalulate the script:\n{}", script);
}
if (ex instanceof FileConflictException) {
log.error(ex.getMessage() + ". Failed to process file sync batch " + batchId);
} else {
log.error("Failed to process file sync batch " + batchId, ex);
}
incomingBatch.setErrorFlag(true);
incomingBatch.setStatus(IncomingBatch.Status.ER);
incomingBatch.setSqlMessage(ex.getMessage());
if (incomingBatchService.isRecordOkBatchesEnabled() || incomingBatch.isRetry()) {
incomingBatchService.updateIncomingBatch(incomingBatch);
} else {
incomingBatchService.insertIncomingBatch(incomingBatch);
}
processInfo.setStatus(ProcessInfo.Status.ERROR);
break;
} finally {
log.debug("The {} node is done processing file sync files", sourceNodeId);
if (isLocked) {
engine.getClusterService().unlock(ClusterConstants.FILE_SYNC_SHARED, ClusterConstants.TYPE_SHARED);
}
}
} else {
log.error("Could not find the sync.bsh script for batch {}", batchId);
}
}
}
return batchesProcessed;
}
use of bsh.Interpreter in project adempiere by adempiere.
the class Scriptlet method execute.
/*************************************************************************/
/**
* Execute Script
* Loads environment and saves result
* @return null or Exception
*/
public Exception execute() {
m_result = null;
m_description = null;
if (m_variable == null || m_variable.length() == 0 || m_script == null || m_script.length() == 0) {
IllegalArgumentException e = new IllegalArgumentException("No variable/script");
log.warning(e.toString());
return e;
}
Interpreter i = new Interpreter();
loadEnvironment(i);
try {
log.config(m_script);
i.eval(m_script);
} catch (Exception e) {
log.warning(e.toString());
return e;
}
try {
m_result = i.get(m_variable);
log.config("Result (" + m_result.getClass().getName() + ") " + m_result);
} catch (Exception e) {
log.warning("Result - " + e);
if (e instanceof NullPointerException)
e = new IllegalArgumentException("Result Variable not found - " + m_variable);
return e;
}
try {
m_description = i.get(DESCRIPTION_VARIABLE);
} catch (Exception e) {
log.warning("Description - " + e);
return e;
}
return null;
}
use of bsh.Interpreter in project bamboobsc by billchen198318.
the class ScriptExpressionUtils method executeBsh.
private static void executeBsh(String scriptExpression, Map<String, Object> results, Map<String, Object> parameters) throws Exception {
//Interpreter bshInterpreter = new Interpreter();
Interpreter bshInterpreter = buildBshInterpreter(true);
if (parameters != null) {
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
bshInterpreter.set(entry.getKey(), entry.getValue());
}
}
bshInterpreter.eval(scriptExpression);
if (results != null) {
for (Map.Entry<String, Object> entry : results.entrySet()) {
entry.setValue(bshInterpreter.get(entry.getKey()));
}
}
}
use of bsh.Interpreter in project cw-advandroid by commonsguy.
the class BshInterpreter method executeScript.
public Bundle executeScript(Bundle input) {
Interpreter i = new Interpreter();
Bundle output = new Bundle(input);
String script = input.getString(InterpreterService.SCRIPT);
if (script != null) {
try {
i.set(InterpreterService.BUNDLE, input);
i.set(InterpreterService.RESULT, output);
Object eval_result = i.eval(script);
output.putString("result", eval_result.toString());
} catch (Throwable t) {
output.putString("error", t.getMessage());
}
}
return (output);
}
Aggregations