Example usage for org.springframework.beans MutablePropertyValues getPropertyValueList

List of usage examples for org.springframework.beans MutablePropertyValues getPropertyValueList

Introduction

In this page you can find the example usage for org.springframework.beans MutablePropertyValues getPropertyValueList.

Prototype

public List<PropertyValue> getPropertyValueList() 

Source Link

Document

Return the underlying List of PropertyValue objects in its raw form.

Usage

From source file:com.himanshu.spring.context.loader.sameconfigallcontext.bean.visitor.BeanVisitor.java

private void visitProperties(MutablePropertyValues propertyValues) {
    for (PropertyValue propertyValue : propertyValues.getPropertyValueList()) {
        Object resolvedValue = resolveValue(propertyValue.getValue());
        propertyValues.add(propertyValue.getName(), resolvedValue);
    }/*from   w w w .ja v a  2s  . c om*/
}

From source file:com.apporiented.spring.override.BeanOverrideProcessor.java

private void overwriteBeanDefinition(BeanDefinition target, BeanDefinition source) {
    log.debug("Replacing bean [" + ref + "] with a [" + source.getBeanClassName() + "]");

    target.setBeanClassName(source.getBeanClassName());
    ConstructorArgumentValues cas = target.getConstructorArgumentValues();
    cas.clear();/* w  w w.  j ava2s .c  o  m*/
    cas.addArgumentValues(source.getConstructorArgumentValues());

    MutablePropertyValues pvs = target.getPropertyValues();
    if (!merge) {
        pvs.getPropertyValueList().clear();
    }
    pvs.addPropertyValues(source.getPropertyValues());
}

From source file:org.wallride.web.controller.admin.user.UserDescribeController.java

@RequestMapping
public String describe(@PathVariable String language, @RequestParam long id, String query, Model model) {
    User user = userService.getUserById(id);
    if (user == null) {
        throw new HttpNotFoundException();
    }//  w w w.  ja  v  a2 s  .  c  om

    MutablePropertyValues mpvs = new MutablePropertyValues(
            UriComponentsBuilder.newInstance().query(query).build().getQueryParams());
    for (Iterator<PropertyValue> i = mpvs.getPropertyValueList().iterator(); i.hasNext();) {
        PropertyValue pv = i.next();
        boolean hasValue = false;
        for (String value : (List<String>) pv.getValue()) {
            if (StringUtils.hasText(value)) {
                hasValue = true;
                break;
            }
        }
        if (!hasValue) {
            i.remove();
        }
    }
    BeanWrapperImpl beanWrapper = new BeanWrapperImpl(new UserSearchForm());
    beanWrapper.setConversionService(conversionService);
    beanWrapper.setPropertyValues(mpvs, true, true);
    UserSearchForm form = (UserSearchForm) beanWrapper.getWrappedInstance();
    List<Long> ids = userService.getUserIds(form.toUserSearchRequest());
    if (!CollectionUtils.isEmpty(ids)) {
        int index = ids.indexOf(user.getId());
        if (index < ids.size() - 1) {
            Long next = ids.get(index + 1);
            model.addAttribute("next", next);
        }
        if (index > 0) {
            Long prev = ids.get(index - 1);
            model.addAttribute("prev", prev);
        }
    }

    model.addAttribute("user", user);
    model.addAttribute("query", query);
    return "user/describe";
}

From source file:org.wallride.web.controller.admin.article.ArticleDescribeController.java

@RequestMapping
public String describe(@PathVariable String language, @RequestParam long id, String query, Model model,
        RedirectAttributes redirectAttributes) {
    Article article = articleService.getArticleById(id);
    if (article == null) {
        throw new HttpNotFoundException();
    }// w  ww. j  av a  2 s.c  o  m

    if (!article.getLanguage().equals(language)) {
        Article target = articleService.getArticleByCode(article.getCode(), language);
        if (target != null) {
            redirectAttributes.addAttribute("id", target.getId());
            return "redirect:/_admin/{language}/articles/describe?id={id}";
        } else {
            redirectAttributes.addFlashAttribute("original", article);
            redirectAttributes.addAttribute("code", article.getCode());
            return "redirect:/_admin/{language}/articles/create?code={code}";
        }
    }

    MutablePropertyValues mpvs = new MutablePropertyValues(
            UriComponentsBuilder.newInstance().query(query).build().getQueryParams());
    for (Iterator<PropertyValue> i = mpvs.getPropertyValueList().iterator(); i.hasNext();) {
        PropertyValue pv = i.next();
        boolean hasValue = false;
        for (String value : (List<String>) pv.getValue()) {
            if (StringUtils.hasText(value)) {
                hasValue = true;
                break;
            }
        }
        if (!hasValue) {
            i.remove();
        }
    }
    BeanWrapperImpl beanWrapper = new BeanWrapperImpl(new ArticleSearchForm());
    beanWrapper.setConversionService(conversionService);
    beanWrapper.setPropertyValues(mpvs, true, true);
    ArticleSearchForm form = (ArticleSearchForm) beanWrapper.getWrappedInstance();
    List<Long> ids = articleService.getArticleIds(form.toArticleSearchRequest());
    if (!CollectionUtils.isEmpty(ids)) {
        int index = ids.indexOf(article.getId());
        if (index < ids.size() - 1) {
            Long next = ids.get(index + 1);
            model.addAttribute("next", next);
        }
        if (index > 0) {
            Long prev = ids.get(index - 1);
            model.addAttribute("prev", prev);
        }
    }

    model.addAttribute("article", article);
    model.addAttribute("query", query);
    return "article/describe";
}

From source file:org.wallride.web.controller.admin.page.PageDescribeController.java

@RequestMapping
public String describe(@PathVariable String language, @RequestParam long id, String query, Model model,
        RedirectAttributes redirectAttributes) {
    Page page = pageService.getPageById(id);
    if (page == null) {
        throw new HttpNotFoundException();
    }/*from w w  w.  j  av a2 s .co  m*/

    if (!page.getLanguage().equals(language)) {
        Page target = pageService.getPageByCode(page.getCode(), language);
        if (target != null) {
            redirectAttributes.addAttribute("id", target.getId());
            return "redirect:/_admin/{language}/pages/describe?id={id}";
        } else {
            redirectAttributes.addFlashAttribute("original", page);
            redirectAttributes.addAttribute("code", page.getCode());
            return "redirect:/_admin/{language}/pages/create?code={code}";
        }
    }

    MutablePropertyValues mpvs = new MutablePropertyValues(
            UriComponentsBuilder.newInstance().query(query).build().getQueryParams());
    for (Iterator<PropertyValue> i = mpvs.getPropertyValueList().iterator(); i.hasNext();) {
        PropertyValue pv = i.next();
        boolean hasValue = false;
        for (String value : (List<String>) pv.getValue()) {
            if (StringUtils.hasText(value)) {
                hasValue = true;
                break;
            }
        }
        if (!hasValue) {
            i.remove();
        }
    }
    BeanWrapperImpl beanWrapper = new BeanWrapperImpl(new PageSearchForm());
    beanWrapper.setConversionService(conversionService);
    beanWrapper.setPropertyValues(mpvs, true, true);
    PageSearchForm form = (PageSearchForm) beanWrapper.getWrappedInstance();
    List<Long> ids = pageService.getPageIds(form.toPageSearchRequest());
    if (!CollectionUtils.isEmpty(ids)) {
        int index = ids.indexOf(page.getId());
        if (index < ids.size() - 1) {
            Long next = ids.get(index + 1);
            model.addAttribute("next", next);
        }
        if (index > 0) {
            Long prev = ids.get(index - 1);
            model.addAttribute("prev", prev);
        }
    }

    model.addAttribute("page", page);
    model.addAttribute("query", query);
    return "page/describe";
}

From source file:org.cloudfoundry.identity.uaa.config.YamlBindingTests.java

private BindingResult bind(Object target, String values) {
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(new ByteArrayResource[] { new ByteArrayResource(values.getBytes()) });
    Map<Object, Object> map = factory.getObject();
    DataBinder binder = new DataBinder(target) {

        @Override//from w w  w .j  a va 2 s . c  o  m
        protected void doBind(MutablePropertyValues mpvs) {
            modifyProperties(mpvs, getTarget());
            super.doBind(mpvs);
        }

        private void modifyProperties(MutablePropertyValues mpvs, Object target) {

            List<PropertyValue> list = mpvs.getPropertyValueList();
            BeanWrapperImpl bw = new BeanWrapperImpl(target);

            for (int i = 0; i < list.size(); i++) {
                PropertyValue pv = list.get(i);

                String name = pv.getName();
                StringBuilder builder = new StringBuilder();

                for (String key : StringUtils.delimitedListToStringArray(name, ".")) {
                    if (builder.length() != 0) {
                        builder.append(".");
                    }
                    builder.append(key);
                    String base = builder.toString();
                    Class<?> type = bw.getPropertyType(base);
                    if (type != null && Map.class.isAssignableFrom(type)) {
                        String suffix = name.substring(base.length());
                        Map<String, Object> nested = new LinkedHashMap<String, Object>();
                        if (bw.getPropertyValue(base) != null) {
                            @SuppressWarnings("unchecked")
                            Map<String, Object> existing = (Map<String, Object>) bw.getPropertyValue(base);
                            nested = existing;
                        } else {
                            bw.setPropertyValue(base, nested);
                        }
                        Map<String, Object> value = nested;
                        String[] tree = StringUtils.delimitedListToStringArray(suffix, ".");
                        for (int j = 1; j < tree.length - 1; j++) {
                            String subtree = tree[j];
                            value.put(subtree, nested);
                            value = nested;
                        }
                        String refName = base + suffix.replaceAll("\\.([a-zA-Z0-9]*)", "[$1]");
                        mpvs.setPropertyValueAt(new PropertyValue(refName, pv.getValue()), i);
                        break;
                    }
                }

            }

        }

    };
    binder.setIgnoreUnknownFields(false);
    LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
    validatorFactoryBean.afterPropertiesSet();
    binder.setValidator(validatorFactoryBean);
    binder.bind(new MutablePropertyValues(map));
    binder.validate();

    return binder.getBindingResult();
}

From source file:com.helpinput.spring.refresher.SessiontRefresher.java

@Override
public void refresh(ApplicationContext context, Map<Class<?>, ScanedType> scanedClasses) {

    boolean needRefreshSessionFactory = false;
    for (Entry<Class<?>, ScanedType> entry : scanedClasses.entrySet()) {
        if (entry.getValue().getValue() > ScanedType.SAME.getValue()
                && entry.getKey().getAnnotation(Entity.class) != null) {
            needRefreshSessionFactory = true;
            break;
        }/* w  w w  .  j  a  v a 2 s. c  om*/
    }
    if (needRefreshSessionFactory) {
        DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) ((AbstractApplicationContext) context)
                .getBeanFactory();

        //testUserManager(dlbf);

        final String sessionFactory = "sessionFactory";
        final String annotatedClasses = "annotatedClasses";
        final String setSessionFactory = "setSessionFactory";

        BeanDefinition oldSessionFactoryDef = dlbf.getBeanDefinition(sessionFactory);

        if (oldSessionFactoryDef != null) {
            dlbf.removeBeanDefinition(sessionFactory);
            MutablePropertyValues propertyValues = oldSessionFactoryDef.getPropertyValues();
            PropertyValue oldPropertyValue = propertyValues.getPropertyValue(annotatedClasses);

            propertyValues.removePropertyValue(annotatedClasses);

            BeanDefinition newSessionFactoryDef = BeanDefinitionBuilder
                    .rootBeanDefinition(oldSessionFactoryDef.getBeanClassName()).getBeanDefinition();

            List<PropertyValue> propertyValueList = newSessionFactoryDef.getPropertyValues()
                    .getPropertyValueList();

            propertyValueList.addAll(propertyValues.getPropertyValueList());
            propertyValueList.add(new PropertyValue(annotatedClasses, getManageList(dlbf, oldPropertyValue)));

            dlbf.registerBeanDefinition(sessionFactory, newSessionFactoryDef);

            SessionFactory sessionFactoryImpl = (SessionFactory) dlbf.getBean(sessionFactory);

            String[] beanNames = dlbf.getBeanDefinitionNames();
            for (String beanName : beanNames) {
                BeanDefinition beanDefinition = dlbf.getBeanDefinition(beanName);

                PropertyValues pValues = beanDefinition.getPropertyValues();
                if (pValues.getPropertyValue(sessionFactory) != null) {
                    Object theBean = dlbf.getBean(beanName);
                    Method method = Utils.findMethod(theBean, setSessionFactory, sessionFactoryImpl);
                    if (method != null)
                        Utils.InvokedMethod(theBean, method, sessionFactoryImpl);
                }
            }
        }
    }
}

From source file:com.jaspersoft.jasperserver.api.common.util.spring.BeanDefinitionOverrider.java

protected void overrideDefinition(ConfigurableListableBeanFactory beanFactory) {
    BeanDefinition originalBean = beanFactory.getBeanDefinition(originalBeanName);
    BeanDefinition overridingBean = beanFactory.getBeanDefinition(overridingBeanName);

    if (log.isDebugEnabled()) {
        log.debug("Overriding " + originalBeanName + " bean definition with " + overridingBeanName);
    }//from   www .  j  av a  2 s.  c o  m

    if (!originalBean.getBeanClassName().equals(overridingBean.getBeanClassName())) {
        if (log.isDebugEnabled()) {
            log.debug("Setting " + originalBeanName + " class name to " + " to "
                    + overridingBean.getBeanClassName());
        }

        originalBean.setBeanClassName(overridingBean.getBeanClassName());
    }

    if (log.isDebugEnabled()) {
        log.debug("Adding " + overridingBeanName + " properties to " + originalBeanName);
    }

    MutablePropertyValues originalProps = originalBean.getPropertyValues();
    MutablePropertyValues overridingProps = overridingBean.getPropertyValues();

    if (!mergeProperties) {
        originalProps.getPropertyValueList().clear();
    }

    originalProps.addPropertyValues(overridingProps);
}

From source file:org.alfresco.util.BeanExtender.java

/**
 * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
 *///from w ww  . j a  v a 2 s  . c  o m
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    ParameterCheck.mandatory("beanName", beanName);
    ParameterCheck.mandatory("extendingBeanName", extendingBeanName);

    // check for bean name
    if (!beanFactory.containsBean(beanName)) {
        throw new NoSuchBeanDefinitionException("Can't find bean '" + beanName + "' to be extended.");
    }

    // check for extending bean
    if (!beanFactory.containsBean(extendingBeanName)) {
        throw new NoSuchBeanDefinitionException("Can't find bean '" + extendingBeanName
                + "' that is going to extend original bean definition.");
    }

    // get the bean definitions
    BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
    BeanDefinition extendingBeanDefinition = beanFactory.getBeanDefinition(extendingBeanName);

    // update class
    if (StringUtils.isNotBlank(extendingBeanDefinition.getBeanClassName())
            && !beanDefinition.getBeanClassName().equals(extendingBeanDefinition.getBeanClassName())) {
        beanDefinition.setBeanClassName(extendingBeanDefinition.getBeanClassName());
    }

    // update properties
    MutablePropertyValues properties = beanDefinition.getPropertyValues();
    MutablePropertyValues extendingProperties = extendingBeanDefinition.getPropertyValues();
    for (PropertyValue propertyValue : extendingProperties.getPropertyValueList()) {
        properties.add(propertyValue.getName(), propertyValue.getValue());
    }
}

From source file:org.gridgain.grid.kernal.processors.spring.GridSpringProcessorImpl.java

/**
 * Creates Spring application context. Optionally excluded properties can be specified,
 * it means that if such a property is found in {@link GridConfiguration}
 * then it is removed before the bean is instantiated.
 * For example, {@code streamerConfiguration} can be excluded from the configs that Visor uses.
 *
 * @param cfgUrl Resource where config file is located.
 * @param excludedProps Properties to be excluded.
 * @return Spring application context./* w w w .  ja  va  2  s  .c om*/
 */
public static ApplicationContext applicationContext(URL cfgUrl, final String... excludedProps) {
    GenericApplicationContext springCtx = new GenericApplicationContext();

    BeanFactoryPostProcessor postProc = new BeanFactoryPostProcessor() {
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            for (String beanName : beanFactory.getBeanDefinitionNames()) {
                BeanDefinition def = beanFactory.getBeanDefinition(beanName);

                if (def.getBeanClassName() != null) {
                    try {
                        Class.forName(def.getBeanClassName());
                    } catch (ClassNotFoundException ignored) {
                        ((BeanDefinitionRegistry) beanFactory).removeBeanDefinition(beanName);

                        continue;
                    }
                }

                MutablePropertyValues vals = def.getPropertyValues();

                for (PropertyValue val : new ArrayList<>(vals.getPropertyValueList())) {
                    for (String excludedProp : excludedProps) {
                        if (val.getName().equals(excludedProp))
                            vals.removePropertyValue(val);
                    }
                }
            }
        }
    };

    springCtx.addBeanFactoryPostProcessor(postProc);

    new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(cfgUrl));

    springCtx.refresh();

    return springCtx;
}