Example usage for org.springframework.expression.spel.support StandardEvaluationContext setVariable

List of usage examples for org.springframework.expression.spel.support StandardEvaluationContext setVariable

Introduction

In this page you can find the example usage for org.springframework.expression.spel.support StandardEvaluationContext setVariable.

Prototype

@Override
    public void setVariable(@Nullable String name, @Nullable Object value) 

Source Link

Usage

From source file:com.oracle.coherence.spring.CoherenceBeanExpressionResolver.java

/**
 * {@inheritDoc}/*from   w  w  w .j a  v a 2 s .  c o  m*/
 */
@Override
protected void customizeEvaluationContext(StandardEvaluationContext evalContext) {
    evalContext.setVariable("resolver", getResolver());
}

From source file:com.oembedler.moon.graphql.engine.dfs.GraphQLSchemaDfsTraversal.java

public Object evaluateSpElExpression(DfsContext dfsContext, Class<?> implClass, Object instance,
        String spElExpression, Object defaultIfNone) {
    Object defaultValue = defaultIfNone;
    if (StringUtils.hasText(spElExpression)) {
        Expression expression = SPEL_EXPRESSION_PARSER.parseExpression(spElExpression);

        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setVariable(GraphQLConstants.DFS_IMPLEMENTATION_CLASS, implClass);
        context.setVariable(GraphQLConstants.DFS_OBJECT_INSTANCE, instance);
        defaultValue = expression.getValue(context);
    }/*from   w  ww. j  a v a2s .  c  o  m*/
    return defaultValue;
}

From source file:net.abhinavsarkar.spelhelper.SpelHelper.java

private EvaluationContext getEvaluationContext(final Object rootObject) {
    StandardEvaluationContext newContext = new StandardEvaluationContext(rootObject);
    newContext.getMethodResolvers().add(new ImplicitMethodResolver());
    newContext.getPropertyAccessors().add(new ImplicitPropertyAccessor());
    newContext.setConstructorResolvers(asList((ConstructorResolver) new ImplicitConstructorResolver()));
    for (Method method : registeredFunctions) {
        newContext.setVariable(method.getName(), method);
    }// w ww . ja  va2s. c  om
    newContext.setVariable(CONTEXT_LOOKUP_KEY, this);
    return newContext;
}

From source file:org.flockdata.transform.ExpressionHelper.java

private static void setContextVariables(ContentModel contentModel, Map<String, Object> row,
        StandardEvaluationContext context) {
    if (contentModel != null)
        context.setVariable("model", contentModel);
    //        for (String s : row.keySet()) {
    //            context.setVariable(s, row.get(s));
    //        }/*from  w  w  w  .j a v a  2s  .  c o m*/
}

From source file:org.kaaproject.kaa.server.admin.services.KaaAdminServiceImpl.java

@Override
public boolean testProfileFilter(RecordField endpointProfile, RecordField serverProfile, String filterBody)
        throws KaaAdminServiceException {
    checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER);
    try {//from  w  w w  .j  a v a2s .c  om
        GenericRecord endpointProfileRecord = null;
        GenericRecord serverProfileRecord = null;
        try {
            if (endpointProfile != null) {
                endpointProfileRecord = FormAvroConverter.createGenericRecordFromRecordField(endpointProfile);
            }
            if (serverProfile != null) {
                serverProfileRecord = FormAvroConverter.createGenericRecordFromRecordField(serverProfile);
            }
        } catch (Exception e) {
            throw Utils.handleException(e);
        }
        try {
            Expression expression = new SpelExpressionParser().parseExpression(filterBody);
            StandardEvaluationContext evaluationContext;
            if (endpointProfileRecord != null) {
                evaluationContext = new StandardEvaluationContext(endpointProfileRecord);
                evaluationContext.setVariable(DefaultFilterEvaluator.CLIENT_PROFILE_VARIABLE_NAME,
                        endpointProfileRecord);
            } else {
                evaluationContext = new StandardEvaluationContext();
            }
            evaluationContext.addPropertyAccessor(new GenericRecordPropertyAccessor());
            evaluationContext.setVariable(DefaultFilterEvaluator.EP_KEYHASH_VARIABLE_NAME, "test");
            if (serverProfileRecord != null) {
                evaluationContext.setVariable(DefaultFilterEvaluator.SERVER_PROFILE_VARIABLE_NAME,
                        serverProfileRecord);
            }
            return expression.getValue(evaluationContext, Boolean.class);
        } catch (Exception e) {
            throw new KaaAdminServiceException("Invalid profile filter: " + e.getMessage(), e,
                    ServiceErrorCode.BAD_REQUEST_PARAMS);
        }
    } catch (Exception e) {
        throw Utils.handleException(e);
    }
}

From source file:org.kaaproject.kaa.server.admin.services.KaaAdminServiceImpl.java

private void validateProfileFilterBody(String endpointProfileSchemaId, String serverProfileSchemaId,
        String filterBody) throws KaaAdminServiceException {
    GenericRecord endpointProfileRecord = null;
    GenericRecord serverProfileRecord = null;
    try {/*from w  w  w.ja va 2s  .c o  m*/
        if (endpointProfileSchemaId != null) {
            EndpointProfileSchemaDto endpointProfileSchema = getProfileSchema(endpointProfileSchemaId);
            endpointProfileRecord = getDefaultRecordFromCtlSchema(endpointProfileSchema.getCtlSchemaId());
        }
        if (serverProfileSchemaId != null) {
            ServerProfileSchemaDto serverProfileSchema = getServerProfileSchema(serverProfileSchemaId);
            serverProfileRecord = getDefaultRecordFromCtlSchema(serverProfileSchema.getCtlSchemaId());
        }
    } catch (Exception e) {
        throw Utils.handleException(e);
    }
    try {
        Expression expression = new SpelExpressionParser().parseExpression(filterBody);
        StandardEvaluationContext evaluationContext;
        if (endpointProfileRecord != null) {
            evaluationContext = new StandardEvaluationContext(endpointProfileRecord);
            evaluationContext.setVariable(DefaultFilterEvaluator.CLIENT_PROFILE_VARIABLE_NAME,
                    endpointProfileRecord);
        } else {
            evaluationContext = new StandardEvaluationContext();
        }
        evaluationContext.addPropertyAccessor(new GenericRecordPropertyAccessor());
        evaluationContext.setVariable(DefaultFilterEvaluator.EP_KEYHASH_VARIABLE_NAME, "test");
        if (serverProfileRecord != null) {
            evaluationContext.setVariable(DefaultFilterEvaluator.SERVER_PROFILE_VARIABLE_NAME,
                    serverProfileRecord);
        }
        expression.getValue(evaluationContext, Boolean.class);
    } catch (Exception e) {
        throw new KaaAdminServiceException("Invalid profile filter body!", e,
                ServiceErrorCode.BAD_REQUEST_PARAMS);
    }
}

From source file:org.kaaproject.kaa.server.admin.services.ProfileServiceImpl.java

@Override
public boolean testProfileFilter(RecordField endpointProfile, RecordField serverProfile, String filterBody)
        throws KaaAdminServiceException {
    checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER);
    try {/* w w w  . j  ava  2  s  .c o m*/
        GenericRecord endpointProfileRecord = null;
        GenericRecord serverProfileRecord = null;
        try {
            if (endpointProfile != null) {
                endpointProfileRecord = FormAvroConverter.createGenericRecordFromRecordField(endpointProfile);
            }
            if (serverProfile != null) {
                serverProfileRecord = FormAvroConverter.createGenericRecordFromRecordField(serverProfile);
            }
        } catch (Exception ex) {
            throw Utils.handleException(ex);
        }
        try {
            final Expression expression = new SpelExpressionParser().parseExpression(filterBody);
            StandardEvaluationContext evaluationContext;
            if (endpointProfileRecord != null) {
                evaluationContext = new StandardEvaluationContext(endpointProfileRecord);
                evaluationContext.setVariable(DefaultFilterEvaluator.CLIENT_PROFILE_VARIABLE_NAME,
                        endpointProfileRecord);
            } else {
                evaluationContext = new StandardEvaluationContext();
            }
            evaluationContext.addPropertyAccessor(new GenericRecordPropertyAccessor());
            evaluationContext.setVariable(DefaultFilterEvaluator.EP_KEYHASH_VARIABLE_NAME, "test");
            if (serverProfileRecord != null) {
                evaluationContext.setVariable(DefaultFilterEvaluator.SERVER_PROFILE_VARIABLE_NAME,
                        serverProfileRecord);
            }
            return expression.getValue(evaluationContext, Boolean.class);
        } catch (Exception ex) {
            throw new KaaAdminServiceException("Invalid profile filter: " + ex.getMessage(), ex,
                    ServiceErrorCode.BAD_REQUEST_PARAMS);
        }
    } catch (Exception ex) {
        throw Utils.handleException(ex);
    }
}

From source file:org.metaeffekt.dcc.commons.mapping.AbstractPropertyExpression.java

@SuppressWarnings("unchecked")
protected <T> T evaluateExpression(String originalExpression, String value, Class<T> type) {
    final ExpressionParser parser = new SpelExpressionParser();

    try {/*  w  w w .j  a  va  2s.c  om*/
        final StandardEvaluationContext context = new StandardEvaluationContext();
        context.setRootObject(propertiesHolder);
        context.addPropertyAccessor(new EnvironmentAccessor());
        context.setVariable(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, STANDARD_ENVIRONMENT);
        context.setVariable(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, STANDARD_ENVIRONMENT);

        final Expression expression = parser.parseExpression(value, PARSER_CONTEXT);

        return expression.getValue(context, type);
    } catch (ParseException | EvaluationException e) {
        LOG.debug(String.format("Failed to evaluate expression %s, and with replaced properties %s",
                originalExpression, value), e);
    }

    if (type == String.class) {
        return (T) value;
    }

    return null;
}

From source file:org.springframework.cloud.function.deployer.ApplicationRunner.java

public Object evaluate(String expression, Object root, Object... attrs) {
    Expression parsed = new SpelExpressionParser(this.config).parseExpression(expression);
    StandardEvaluationContext context = new StandardEvaluationContext(root);
    context.setTypeLocator(this.typeLocator);
    if (attrs.length % 2 != 0) {
        throw new IllegalArgumentException("Context attributes must be name, value pairs");
    }// ww w  .  j  a  va  2 s . c  om
    for (int i = 0; i < attrs.length / 2; i++) {
        String name = (String) attrs[2 * i];
        Object value = attrs[2 * i + 1];
        context.setVariable(name, value);
    }
    return parsed.getValue(context);
}

From source file:org.springframework.cloud.function.deployer.ApplicationRunner.java

private void runContext(String mainClass, Map<String, String> properties, String... args) {
    Expression parsed = new SpelExpressionParser().parseExpression("run(#main,#properties,#args)");
    StandardEvaluationContext context = this.app;
    context.setVariable("main", mainClass);
    context.setVariable("properties", properties);
    context.setVariable("args", args);
    parsed.getValue(context);//from   w w  w .  j  a  va 2  s .c om
}