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

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

Introduction

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

Prototype

public void addPropertyAccessor(PropertyAccessor accessor) 

Source Link

Usage

From source file:com.qpark.eip.core.spring.PayloadLogger.java

/** Creates the {@link PayloadLogger}. */
public PayloadLogger() {
    final StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
    standardEvaluationContext.addPropertyAccessor(new MapAccessor());
    this.evaluationContext = standardEvaluationContext;
    this.expression = EXPRESSION_PARSER.parseExpression("payload");
}

From source file:com.google.code.activetemplates.impl.TemplateCompilerImpl.java

@Override
public void compile(final Template t, final TemplateModel model, XmlResult out)
        throws TemplateCompileException {

    final XmlSource s = t.createSource();
    XMLEventReader r = null;//from   w  w w  . j av  a2 s  . co  m
    XMLEventWriter w = null;
    try {
        r = inFactory.createXMLEventReader(s.getSource());
        w = outFactory.createXMLEventWriter(out.getResult());
        final XMLStreamException[] xe = new XMLStreamException[1];

        final XMLEventReader fr = r;
        final XMLEventWriter fw = w;

        StandardEvaluationContext eContext = new StandardEvaluationContext(model);
        eContext.addPropertyAccessor(new TemplateModelPropertyAccessor());

        CompileContext ctx = new CompileContext(fr, fw, eFactory, eComponentFactory, expressionParser,
                eContext);
        doCompile(t.getName(), ctx);

        if (xe[0] != null) {
            throw xe[0];
        }

    } catch (XMLStreamException e) {
        throw new TemplateCompileException(e);
    } finally {
        s.close();
        if (r != null)
            try {
                r.close();
            } catch (XMLStreamException e) {
            }
        if (w != null)
            try {
                w.close();
            } catch (XMLStreamException e) {
            }
    }
}

From source file:org.focusns.common.web.page.engine.widget.WidgetPageEngine.java

private EvaluationContext createEvaluationContext() {
    StandardEvaluationContext evaluationContext = (StandardEvaluationContext) getServletContext()
            .getAttribute(EVALUATION_CONTEXT_KEY);
    if (evaluationContext == null) {
        evaluationContext = new StandardEvaluationContext();
        evaluationContext.addPropertyAccessor(new NavigatorPropertyAccessor());
        evaluationContext.addPropertyAccessor(new ServletPropertyAccessor());
        evaluationContext.addPropertyAccessor(new MapAccessor());
        ////from   w  ww .j  av  a  2 s  .  co m
        getServletContext().setAttribute(EVALUATION_CONTEXT_KEY, evaluationContext);
    }
    return evaluationContext;
}

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 .ja v  a 2  s  . 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   ww  w  . j a  va 2s. com
        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 {/*from ww  w .j  ava 2  s .co 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 {//from  w  w w . j ava2  s .  co  m
        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.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport.java

/**
 * Constructs, configures and initializes a new instance of an {@link EvaluationContext}.
 *
 * @param beanFactory reference to the Spring {@link BeanFactory}.
 * @return a new {@link EvaluationContext}.
 * @see org.springframework.beans.factory.BeanFactory
 * @see org.springframework.expression.EvaluationContext
 * @see #getBeanFactory()/*from   www .j ava2  s  .c o m*/
 */
protected EvaluationContext newEvaluationContext(BeanFactory beanFactory) {

    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();

    evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());
    evaluationContext.addPropertyAccessor(new EnvironmentAccessor());
    evaluationContext.addPropertyAccessor(new MapAccessor());
    evaluationContext.setTypeLocator(new StandardTypeLocator(getBeanClassLoader()));

    configureTypeConverter(evaluationContext, beanFactory);

    return evaluationContext;
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

private <S extends Object> S read(final MongoPersistentEntity<S> entity, final DBObject dbo) {

    final StandardEvaluationContext spelCtx = new StandardEvaluationContext(dbo);
    spelCtx.addPropertyAccessor(DBObjectPropertyAccessor.INSTANCE);

    if (applicationContext != null) {
        spelCtx.setBeanResolver(new BeanFactoryResolver(applicationContext));
    }/*www .j a  v  a  2  s.com*/

    final MappedConstructor constructor = new MappedConstructor(entity, mappingContext);

    SpELAwareParameterValueProvider delegate = new SpELAwareParameterValueProvider(spelExpressionParser,
            spelCtx);
    ParameterValueProvider provider = new DelegatingParameterValueProvider(constructor, dbo, delegate);

    final BeanWrapper<MongoPersistentEntity<S>, S> wrapper = BeanWrapper.create(entity, provider,
            conversionService);

    // Set properties not already set in the constructor
    entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
        public void doWithPersistentProperty(MongoPersistentProperty prop) {

            boolean isConstructorProperty = constructor.isConstructorParameter(prop);
            boolean hasValueForProperty = dbo.containsField(prop.getFieldName());

            if (!hasValueForProperty || isConstructorProperty) {
                return;
            }

            Object obj = getValueInternal(prop, dbo, spelCtx, prop.getSpelExpression());
            wrapper.setProperty(prop, obj, useFieldAccessOnly);
        }
    });

    // Handle associations
    entity.doWithAssociations(new AssociationHandler<MongoPersistentProperty>() {
        public void doWithAssociation(Association<MongoPersistentProperty> association) {
            MongoPersistentProperty inverseProp = association.getInverse();
            Object obj = getValueInternal(inverseProp, dbo, spelCtx, inverseProp.getSpelExpression());
            try {
                wrapper.setProperty(inverseProp, obj);
            } catch (IllegalAccessException e) {
                throw new MappingException(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                throw new MappingException(e.getMessage(), e);
            }
        }
    });

    return wrapper.getBean();
}

From source file:org.springframework.integration.expression.ExpressionUtils.java

/**
 * Create a {@link StandardEvaluationContext} with a {@link MapAccessor} in its
 * property accessor property and the supplied {@link ConversionService} in its
 * conversionService property./*from w  w  w  .  java 2 s  .c  om*/
 * @param conversionService the conversion service.
 * @return the evaluation context.
 */
private static StandardEvaluationContext createStandardEvaluationContext(ConversionService conversionService,
        BeanFactory beanFactory) {
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    evaluationContext.addPropertyAccessor(new MapAccessor());
    if (conversionService != null) {
        evaluationContext.setTypeConverter(new StandardTypeConverter(conversionService));
    }
    if (beanFactory != null) {
        evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
    }
    return evaluationContext;
}