Search in sources :

Example 6 with EvalError

use of bsh.EvalError 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());
        }
    }
}
Also used : Interpreter(bsh.Interpreter) IExtensionPoint(org.jumpmind.extension.IExtensionPoint) EvalError(bsh.EvalError)

Example 7 with EvalError

use of bsh.EvalError 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;
}
Also used : ArrayList(java.util.ArrayList) TreeSet(java.util.TreeSet) IIncomingBatchService(org.jumpmind.symmetric.service.IIncomingBatchService) Interpreter(bsh.Interpreter) EvalError(bsh.EvalError) TargetError(bsh.TargetError) IncomingBatch(org.jumpmind.symmetric.model.IncomingBatch) IoException(org.jumpmind.exception.IoException) FileConflictException(org.jumpmind.symmetric.file.FileConflictException) File(java.io.File) Map(java.util.Map)

Example 8 with EvalError

use of bsh.EvalError in project qi4j-sdk by Qi4j.

the class BeanShellMixin method buildNamespace.

private static NameSpace buildNamespace(Class compositeType, Interpreter runtime) throws IOException {
    ClassLoader loader = compositeType.getClassLoader();
    BshClassManager classManager = BshClassManager.createClassManager(runtime);
    classManager.setClassLoader(loader);
    NameSpace namespace = new NameSpace(classManager, compositeType.getName());
    URL scriptUrl = getFunctionResource(compositeType);
    if (scriptUrl == null) {
        return null;
    }
    Reader source = getSource(compositeType, scriptUrl);
    try {
        runtime.eval(source, namespace, scriptUrl.toString());
    } catch (EvalError evalError) {
        //TODO: Auto-generated, need attention.
        evalError.printStackTrace();
    }
    return namespace;
}
Also used : NameSpace(bsh.NameSpace) EvalError(bsh.EvalError) BshClassManager(bsh.BshClassManager) URL(java.net.URL)

Example 9 with EvalError

use of bsh.EvalError in project symmetric-ds by JumpMind.

the class BshDatabaseWriterFilter method executeScripts.

@Override
protected void executeScripts(DataContext context, String key, Set<String> scripts, boolean isFailOnError) {
    Interpreter interpreter = getInterpreter(context);
    String currentScript = null;
    try {
        bind(interpreter, context, null, null, null);
        if (scripts != null) {
            for (String script : scripts) {
                currentScript = script;
                interpreter.eval(script);
            }
        }
    } catch (EvalError e) {
        if (e instanceof ParseException) {
            String errorMsg = String.format("Evaluation error while parsing the following beanshell script:\n\n%s\n\nThe error was on line %d and the error message was: %s", currentScript, e.getErrorLineNumber(), e.getMessage());
            log.error(errorMsg, e);
            if (isFailOnError) {
                throw new SymmetricException(errorMsg);
            }
        } else if (e instanceof TargetError) {
            Throwable target = ((TargetError) e).getTarget();
            String errorMsg = String.format("Evaluation error occured in the following beanshell script:\n\n%s\n\nThe error was on line %d", currentScript, e.getErrorLineNumber());
            log.error(errorMsg, target);
            if (isFailOnError) {
                if (target instanceof RuntimeException) {
                    throw (RuntimeException) target;
                } else {
                    throw new SymmetricException(target);
                }
            } else {
                log.error("Failed while evaluating script", target);
            }
        }
    }
}
Also used : Interpreter(bsh.Interpreter) SymmetricException(org.jumpmind.symmetric.SymmetricException) EvalError(bsh.EvalError) ParseException(bsh.ParseException) TargetError(bsh.TargetError)

Example 10 with EvalError

use of bsh.EvalError in project symmetric-ds by JumpMind.

the class BshDatabaseWriterFilter method processLoadFilters.

@Override
protected boolean processLoadFilters(DataContext context, Table table, CsvData data, Exception error, WriteMethod writeMethod, List<LoadFilter> loadFiltersForTable) {
    boolean writeRow = true;
    LoadFilter currentFilter = null;
    try {
        Interpreter interpreter = getInterpreter(context);
        bind(interpreter, context, table, data, error);
        for (LoadFilter filter : loadFiltersForTable) {
            currentFilter = filter;
            if (filter.isFilterOnDelete() && data.getDataEventType().equals(DataEventType.DELETE) || filter.isFilterOnInsert() && data.getDataEventType().equals(DataEventType.INSERT) || filter.isFilterOnUpdate() && data.getDataEventType().equals(DataEventType.UPDATE)) {
                Object result = null;
                if (writeMethod.equals(WriteMethod.BEFORE_WRITE) && filter.getBeforeWriteScript() != null) {
                    result = interpreter.eval(filter.getBeforeWriteScript());
                } else if (writeMethod.equals(WriteMethod.AFTER_WRITE) && filter.getAfterWriteScript() != null) {
                    result = interpreter.eval(filter.getAfterWriteScript());
                } else if (writeMethod.equals(WriteMethod.HANDLE_ERROR) && filter.getHandleErrorScript() != null) {
                    result = interpreter.eval(filter.getHandleErrorScript());
                }
                if (result != null && result.equals(Boolean.FALSE)) {
                    writeRow = false;
                }
            }
        }
    } catch (EvalError ex) {
        processError(currentFilter, table, ex);
    }
    return writeRow;
}
Also used : Interpreter(bsh.Interpreter) LoadFilter(org.jumpmind.symmetric.model.LoadFilter) EvalError(bsh.EvalError)

Aggregations

EvalError (bsh.EvalError)12 Interpreter (bsh.Interpreter)9 TargetError (bsh.TargetError)4 ScriptCompilationException (org.springframework.scripting.ScriptCompilationException)3 Map (java.util.Map)2 BshClassManager (bsh.BshClassManager)1 NameSpace (bsh.NameSpace)1 ParseException (bsh.ParseException)1 File (java.io.File)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 TreeSet (java.util.TreeSet)1 IoException (org.jumpmind.exception.IoException)1 IExtensionPoint (org.jumpmind.extension.IExtensionPoint)1 SymmetricException (org.jumpmind.symmetric.SymmetricException)1