@Test public void testEnvironmentResolver(){ EnvironmentImpl environment = new EnvironmentImpl(); environment.setAttribute( "testAtr1", true ); environment.setAttribute( "testAtr2", "33" ); environment.setAttribute( "testAtr3", null ); ELProcessor processor = ElUtil.initNewELProcessor( environment, null ); boolean result = (boolean)processor.eval( "empty testAtr3 && testAtr1" ); // this again fails: "ELResolver cannot handle a null base Object with identifier 'unknownAtr'" //boolean result = (boolean)processor.eval( "empty testAtr3 && testAtr1 && empty unknownAtr" ); Assert.assertTrue( result ); }
@Test public void testEnvironmentResolverDate(){ Date start = new Date(); EnvironmentImpl environment = new EnvironmentImpl(); environment.setAttribute( "currentMillis", System.currentTimeMillis() ); try{ Thread.sleep( 30 ); } catch( InterruptedException e ){ } ELProcessor processor = ElUtil.initNewELProcessor( environment, null ); boolean result = (boolean)processor.eval( "NOW.time > currentMillis" ); Assert.assertTrue( result ); Date now = (Date)processor.eval( "NOW" ); Assert.assertTrue( now.after( start ) ); }
@Test public void testElExpressionGettingMessagesByCaegory() { messages.add(new SimpleMessage("client.id", "will generated", Severity.INFO)); messages.add(new SimpleMessage("client.name", "not null")); messages.add(new SimpleMessage("client.name", "not empty")); ELProcessor el = new ELProcessor(); el.defineBean("messages", messages); String result = el.eval("messages.errors.from('client.name')").toString(); assertThat(result, is("not null, not empty")); result = el.eval("messages.errors.from('client.name').join(' - ')").toString(); assertThat(result, is("not null - not empty")); result = el.eval("messages.errors.from('client.id')").toString(); assertThat(result, isEmptyString()); result = el.eval("messages.info.from('client.id')").toString(); assertThat(result, is("will generated")); }
/** * Creates and prepares a new ELProcessor instance with workflow engine configuration to evaluate expressions on * workflow instance environment together with some additional features (NOW variable, WORKFLOW_INSTANCE_ID variable). * * The ELProcessor instance is meant to be used "quickly" only for current task execution/evaluation and should be discarded after that. */ public static ELProcessor initNewELProcessor( Environment environment, Long externalInstanceId ){ ELProcessor processor = new ELProcessor(); processor.getELManager().addBeanNameResolver( new EnvironmentBeanNameResolver( environment ) ); processor.setValue( ReservedVariables.NOW, new Date() ); processor.setValue( ReservedVariables.WORKFLOW_INSTANCE_ID, externalInstanceId ); return processor; }
@Override public boolean evaluate( GraphInstance instance ){ boolean result = false; if( StringUtils.isNotBlank( condition ) ){ ELProcessor processor = ElUtil.initNewELProcessor( instance.getEnvironment(), instance.getExternalId() ); result = (boolean)processor.eval( condition ); } return result; }
@Override public void execute( GraphEngine engine, Token token ){ GraphInstance instance = token.getInstance(); Environment environment = instance.getEnvironment(); if( !environment.containsAttribute( attribute ) ){ if( isRequired ){ // Throw exception throw new WorkflowException( "Missing required attribute '" + attribute + "'" ); } else{ // Use default value if( defaultValue instanceof String && ElUtil.hasBrackets( (String)defaultValue ) ){ ELProcessor processor = ElUtil.initNewELProcessor( environment, instance.getExternalId() ); Object expressionResult = processor.eval( ElUtil.removeBrackets( (String)defaultValue ) ); environment.setAttribute( attribute, expressionResult ); } else{ environment.setAttribute( attribute, defaultValue ); } } } // Validate type of value Object value = environment.getAttribute( attribute ); if( value != null && !type.isAssignableFrom( value.getClass() ) ){ throw new WorkflowException( "The value of attribute '" + attribute + "' is of type " + value.getClass().getCanonicalName() + " whis is not assignable to the expected type " + type.getCanonicalName() ); } engine.complete( token, null ); }
@Override public void execute( GraphEngine engine, Token token ){ GraphInstance instance = token.getInstance(); Environment environment = instance.getEnvironment(); if( value instanceof String && ElUtil.hasBrackets( (String)value ) ){ ELProcessor processor = ElUtil.initNewELProcessor( environment, instance.getExternalId() ); Object expressionResult = processor.eval( ElUtil.removeBrackets( (String)value ) ); environment.setAttribute( attribute, expressionResult ); } else{ environment.setAttribute( attribute, value ); } engine.complete( token, null ); }
@Override @SuppressWarnings("unchecked") public T evaluate( GraphInstance instance ){ if( StringUtils.isNotBlank( expression ) ){ ELProcessor processor = ElUtil.initNewELProcessor( instance.getEnvironment(), instance.getExternalId() ); Object expressionResult = processor.eval( ElUtil.removeBrackets( expression ) ); return (T)expressionResult; } return null; }
@Test public void testBean(){ ELProcessor processor = ElUtil.initNewELProcessor( new EnvironmentImpl(), null ); processor.defineBean( "client", new Client( 1, "Heli Kopter" ) ); String name = (String)processor.eval( "client.name" ); Assert.assertEquals( name, "Heli Kopter" ); }
@Test public void testConditions(){ ELProcessor processor = ElUtil.initNewELProcessor( new EnvironmentImpl(), null ); processor.defineBean( "client", new Client( 1, "Heli Kopter" ) ); boolean result = (boolean)processor.eval( "not empty client.name and client.id > 0" ); Assert.assertTrue( result ); }
@Test public void testValue(){ ELProcessor processor = ElUtil.initNewELProcessor( new EnvironmentImpl(), null ); processor.setValue( "throwException", false ); boolean success = (boolean)processor.eval( "not throwException" ); Assert.assertTrue( success ); }
@Test public void testEnvironmentBean(){ EnvironmentImpl environment = new EnvironmentImpl(); environment.setAttribute( "testAtr1", true ); environment.setAttribute( "testAtr2", "33" ); environment.setAttribute( "testAtr3", null ); ELProcessor processor = ElUtil.initNewELProcessor( environment, null ); // this way all the parameters must be prefixed with "env.", but we will at least be able to check unmapped attributes without an exception processor.defineBean( "env", environment.getAttributes() ); boolean result = (boolean)processor.eval( "empty env.testAtr3 && env.testAtr1 && empty env.unknownAtr" ); Assert.assertTrue( result ); }
@Test public void testEnvironmentResolverNull(){ EnvironmentImpl environment = new EnvironmentImpl(); environment.setAttribute( "testDate1", null ); ELProcessor processor = ElUtil.initNewELProcessor( environment, null ); // in EL, null.fieldName does not throw a nullpointer, and null in conditions always returns false boolean result = (boolean)processor.eval( "testDate1.time < System.currentTimeMillis()" ); Assert.assertFalse( result ); result = (boolean)processor.eval( "testDate1.whatever > System.currentTimeMillis()" ); Assert.assertFalse( result ); }
@Test public void testEnvironmentResolverInstanceIdNull(){ EnvironmentImpl environment = new EnvironmentImpl(); ELProcessor processor = ElUtil.initNewELProcessor( environment, null ); Long instanceId = (Long)processor.eval( "WORKFLOW_INSTANCE_ID" ); Assert.assertEquals( null, instanceId ); }
@Test public void testEnvironmentResolverInstanceId(){ EnvironmentImpl environment = new EnvironmentImpl(); ELProcessor processor = ElUtil.initNewELProcessor( environment, 55L ); long instanceId = (long)processor.eval( "WORKFLOW_INSTANCE_ID" ); Assert.assertEquals( 55, instanceId ); }
@Before public void setup() { originalMsg = ProtoUtils.newGalaxy(); elp = new ELProcessorEx(); stdElp = new ELProcessor(); protoElp = new ProtoELProcessorEx(); builder = DynamicMessage.newBuilder(originalMsg); }
@Before public void setup() { elProcessor = new ELProcessor(); }
protected ELProcessor newELProcessor() { return new ProtoELProcessorEx(); }
JavaxElMetricStrategy(Object object) { processor = new ELProcessor(); processor.defineBean("this", object); }
JavaxElMetricStrategy(Class<?> clazz) { processor = new ELProcessor(); processor.getELManager().importClass(clazz.getName()); }
@GET public String evaluatePath() { Person person = new Person("Luke", new Address("Tatooine Way", "133", "232423")); ELProcessor processor = new ELProcessor(); ELContext context = processor.getELManager().getELContext(); context.getVariableMapper().setVariable("person", ELManager.getExpressionFactory().createValueExpression(person, Person.class)); Object address = processor.getValue("person.address.street", String.class); return address.toString(); }
/** * Note that we have to use a separate {@link ELProcessor} for each evaluation * as the {@link OptionsELResolver} may not be reused. * * @param expression * @param options * @param configuration * @return the result of the expression evaluation */ static Object eval(String expression, Options options, Configuration configuration) { ELProcessorFactory elpFactory = (ELProcessorFactory) configuration .getPropertyValue(ELProcessorFactory.EL_PROCESSOR_FACTORY_KEY); ELProcessor elp = elpFactory.createELProcessor(configuration); elp.getELManager().addELResolver(new OptionsELResolver(options)); return elp.eval(expression); }
/** * The returned processor is used to evaluate a single EL expression. * * @param configuration * @return a new EL processor instance */ ELProcessor createELProcessor(Configuration configuration);
/** * * @return the default factory */ static ELProcessorFactory defaultFactory() { return c -> new ELProcessor(); }