public ExpressionHandler(String scriptBaseClass) { super(); groovyPool = new GroovyPool(scriptBaseClass); }
@Override public GroovyPoolEntry makeObject(GroovyPoolKey arg0) throws Exception { logger.debug("Creating new script: " + arg0.getScript()); GroovyShell shell = new GroovyShell(groovyCompilerConfiguration); Script script = shell.parse(arg0.getScript()); GroovyPoolEntry result = new GroovyPoolEntry(arg0, script); return result; }
protected Context(Configuration configuration, AbstractFunctionType<?> functionType, Map<String, String> properties) { super(); token = new AgentTokenWrapper(); if(properties!=null) { token.setProperties(properties); } Grid grid = new Grid(0); GridClientImpl client = new GridClientImpl(grid, grid); FunctionTypeRegistry functionTypeRegistry = new FunctionTypeRegistryImpl(new FileResolver(new AttachmentManager(configuration)), grid); functionTypeRegistry.registerFunctionType(functionType); functionExecutionService = new FunctionExecutionServiceImpl(client, functionAccessor, functionTypeRegistry, new DynamicBeanResolver(new DynamicValueResolver(new ExpressionHandler()))); }
GroovyPoolEntry entry = groovyPool.borrowShell(expression); try { Script script = entry.getScript(); script.setBinding(binding); result = script.run(); } finally { if(entry!=null && entry.getScript()!=null) { entry.getScript().setBinding(new Binding()); groovyPool.returnShell(entry);
public Object evaluate(JsonObject dynamicValueAsJson, Map<String, Object> bindings) { boolean isDynamic = dynamicValueAsJson.getBoolean("dynamic"); if(isDynamic) { String exprType = dynamicValueAsJson.containsKey("expressionType")?dynamicValueAsJson.getString("expressionType"):null; String expression = dynamicValueAsJson.getString("expression"); try { return expressionHandler.evaluateGroovyExpression(expression, bindings); } catch (Exception e) { throw new RuntimeException("Error evaluating "+expression, e); } } else { return dynamicValueAsJson.get("value"); } }
public GroovyPoolEntry borrowShell(String script) throws Exception { GroovyPoolKey key = new GroovyPoolKey(script); GroovyPoolEntry entry; try { entry = pool.borrowObject(key); return entry; } catch (Exception e) { logger.error("An error occurred while borrowing script: " + script, e); throw e; } }
public GroovyPool(String scriptBaseClass) { super(); Configuration conf = Configuration.getInstance(); try { pool = new GenericKeyedObjectPool<>(new GroovyPoolFactory(scriptBaseClass)); pool.setTestOnBorrow(true); pool.setMaxTotal(conf.getPropertyAsInteger("tec.expressions.pool.maxtotal",1000)); pool.setMaxActive(-1); pool.setMaxIdle(conf.getPropertyAsInteger("tec.expressions.pool.maxidle",-1)); pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK); pool.setTimeBetweenEvictionRunsMillis(30000); pool.setMinEvictableIdleTimeMillis(-1); } catch(Exception e) { logger.error("An error occurred while starting GroovyPool.", e); } }
protected Context(Configuration configuration, AbstractFunctionType<?> functionType, Map<String, String> properties) { super(); this.properties = properties; this.fileManagerDirectory = FileHelper.createTempFolder(); grid = new Grid(fileManagerDirectory, 0); client = new GridClientImpl(grid); FunctionTypeRegistry functionTypeRegistry = new FunctionTypeRegistryImpl(new FileResolver(new AttachmentManager(configuration)), client); functionTypeRegistry.registerFunctionType(functionType); try { functionExecutionService = new FunctionExecutionServiceImpl(client, functionTypeRegistry, new DynamicBeanResolver(new DynamicValueResolver(new ExpressionHandler()))); } catch (FunctionExecutionServiceException e) { throw new RuntimeException("Error while creating function execution service", e); } }
public void evaluate(DynamicValue<?> dynamicValue, Map<String, Object> bindings) { if(dynamicValue.isDynamic()) { // TODO support different expression types String exprType = dynamicValue.expressionType; EvaluationResult result = new EvaluationResult(); try { Object evaluationResult = expressionHandler.evaluateGroovyExpression(dynamicValue.expression, bindings); // When using placeholders in strings, groovy returns an object of type GString. // Curiously the class GSting doesn't extend String. For this reason we call the toString() method here // to avoid later casting issues when DynamicValue.get() is called if(evaluationResult instanceof GString) { evaluationResult = evaluationResult.toString(); } result.setResultValue(evaluationResult); } catch (Exception e) { result.setEvaluationException(e); } dynamicValue.evalutationResult = result; } }
private void initContext() { context = new GlobalContext(); context.setPluginManager(pluginManager); Configuration configuration = Configuration.getInstance(); mongoClientSession = new MongoClientSession(configuration); context.setConfiguration(configuration); context.setMongoClientSession(mongoClientSession); context.put(CollectionRegistry.class, new CollectionRegistry()); context.setExecutionAccessor(new ExecutionAccessorImpl(mongoClientSession)); context.setArtefactAccessor(new ArtefactAccessorImpl(mongoClientSession)); context.setReportAccessor(new ReportNodeAccessorImpl(mongoClientSession)); context.setScheduleAccessor(new ExecutionTaskAccessorImpl(mongoClientSession)); context.setUserAccessor(new UserAccessorImpl(mongoClientSession)); context.setArtefactManager(new ArtefactManager(context.getArtefactAccessor())); context.setRepositoryObjectManager(new RepositoryObjectManager(context.getArtefactAccessor())); context.setExpressionHandler(new ExpressionHandler(configuration.getProperty("tec.expressions.scriptbaseclass"))); context.setDynamicBeanResolver(new DynamicBeanResolver(new DynamicValueResolver(context.getExpressionHandler()))); context.setEventManager(new EventManager()); context.setAttachmentManager(new AttachmentManager(configuration)); createOrUpdateIndexes(); }
context.setExecutionCallbacks(pluginManager.getProxy()); context.setExpressionHandler(new ExpressionHandler()); context.setDynamicBeanResolver(new DynamicBeanResolver(new DynamicValueResolver(context.getExpressionHandler())));