List of usage examples for org.springframework.beans MutablePropertyValues removePropertyValue
public void removePropertyValue(String propertyName)
From source file:com.thinker.arch.common.spring.SpeedUpSpringProcessor.java
private boolean needRemove(String beanName, BeanDefinition beanDefinition) { if (ArrayUtils.isNotEmpty(removedBeanNames)) { for (String removedBeanName : removedBeanNames) { if (beanName.equals(removedBeanName)) { return true; }//from ww w . j a v a 2 s. co m if (beanDefinition.getBeanClassName().equals(removedBeanName)) { return true; } } } if (this.removedBeanProperties != null) { Set<String[]> propertiesSet = removedBeanProperties.get(beanName); if (propertiesSet != null) { Iterator<String[]> iter = propertiesSet.iterator(); MutablePropertyValues propertyValues = beanDefinition.getPropertyValues(); while (iter.hasNext()) { String[] properties = iter.next(); if (properties.length == 1) { // propertyValues.removePropertyValue(properties[0]); //????? if (this.replaceBeanProperties != null) { String key = beanName + "@" + properties[0]; if (this.replaceBeanProperties.containsKey(key)) { propertyValues.add(properties[0], this.replaceBeanProperties.get(key)); } } } else { PropertyValue propertyValue = propertyValues.getPropertyValue(properties[0]); if (propertyValue != null) { Object nextValue = propertyValue.getValue(); //??? + Map if (nextValue instanceof ManagedMap) { TypedStringValue typedStringValue = new TypedStringValue(properties[1]); ((ManagedMap) nextValue).remove(typedStringValue); //????? if (this.replaceBeanProperties != null) { String key = beanName + "@" + properties[0] + "@" + properties[1]; if (this.replaceBeanProperties.containsKey(key)) { ((ManagedMap) nextValue).put(properties[1], this.replaceBeanProperties.get(key)); } } } } } } } } String className = beanDefinition.getBeanClassName(); //spring data jpa if (className.equals("com.thinker.arch.common.repository.support.SimpleBaseRepositoryFactoryBean") || className.equals("org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean")) { PropertyValue repositoryInterfaceValue = beanDefinition.getPropertyValues() .getPropertyValue("repositoryInterface"); if (repositoryInterfaceValue != null) { className = repositoryInterfaceValue.getValue().toString(); } } if (ArrayUtils.isEmpty(this.removedClassPatterns)) { return false; } if (ArrayUtils.isNotEmpty(this.includeClassPatterns)) { for (String includeClassPattern : includeClassPatterns) { if (className.matches(includeClassPattern)) { return false; } } } for (String removedClassPattern : removedClassPatterns) { if (className.matches(removedClassPattern)) { return true; } } return false; }
From source file:org.alfresco.repo.management.subsystems.LegacyConfigPostProcessor.java
/** * Given a bean name (assumed to implement {@link org.springframework.core.io.support.PropertiesLoaderSupport}) * checks whether it already references the <code>global-properties</code> bean. If not, 'upgrades' the bean by * appending all additional resources it mentions in its <code>locations</code> property to * <code>globalPropertyLocations</code>, except for those resources mentioned in <code>newLocations</code>. A * reference to <code>global-properties</code> will then be added and the resource list in * <code>newLocations<code> will then become the new <code>locations</code> list for the bean. * /*from ww w.j av a2 s.c o m*/ * @param beanFactory * the bean factory * @param globalPropertyLocations * the list of global property locations to be appended to * @param beanName * the bean name * @param newLocations * the new locations to be set on the bean * @return the mutable property values */ @SuppressWarnings("unchecked") private MutablePropertyValues processLocations(ConfigurableListableBeanFactory beanFactory, Collection<Object> globalPropertyLocations, String beanName, String[] newLocations) { // Get the bean an check its existing properties value MutablePropertyValues beanProperties = beanFactory.getBeanDefinition(beanName).getPropertyValues(); PropertyValue pv = beanProperties.getPropertyValue(LegacyConfigPostProcessor.PROPERTY_PROPERTIES); Object value; // If the properties value already references the global-properties bean, we have nothing else to do. Otherwise, // we have to 'upgrade' the bean definition. if (pv == null || (value = pv.getValue()) == null || !(value instanceof BeanReference) || ((BeanReference) value).getBeanName() .equals(LegacyConfigPostProcessor.BEAN_NAME_GLOBAL_PROPERTIES)) { // Convert the array of new locations to a managed list of type string values, so that it is // compatible with a bean definition Collection<Object> newLocationList = new ManagedList(newLocations.length); if (newLocations != null && newLocations.length > 0) { for (String preserveLocation : newLocations) { newLocationList.add(new TypedStringValue(preserveLocation)); } } // If there is currently a locations list, process it pv = beanProperties.getPropertyValue(LegacyConfigPostProcessor.PROPERTY_LOCATIONS); if (pv != null && (value = pv.getValue()) != null && value instanceof Collection) { Collection<Object> locations = (Collection<Object>) value; // Compute the set of locations that need to be added to globalPropertyLocations (preserving order) and // warn about each Set<Object> addedLocations = new LinkedHashSet<Object>(locations); addedLocations.removeAll(globalPropertyLocations); addedLocations.removeAll(newLocationList); for (Object location : addedLocations) { LegacyConfigPostProcessor.logger.warn("Legacy configuration detected: adding " + (location instanceof TypedStringValue ? ((TypedStringValue) location).getValue() : location.toString()) + " to global-properties definition"); globalPropertyLocations.add(location); } } // Ensure the bean now references global-properties beanProperties.addPropertyValue(LegacyConfigPostProcessor.PROPERTY_PROPERTIES, new RuntimeBeanReference(LegacyConfigPostProcessor.BEAN_NAME_GLOBAL_PROPERTIES)); // Ensure the new location list is now set on the bean if (newLocationList.size() > 0) { beanProperties.addPropertyValue(LegacyConfigPostProcessor.PROPERTY_LOCATIONS, newLocationList); } else { beanProperties.removePropertyValue(LegacyConfigPostProcessor.PROPERTY_LOCATIONS); } } return beanProperties; }
From source file:org.broadleafcommerce.openadmin.server.service.artifact.upload.UploadAddOrUpdateController.java
@Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { Map<String, String> model = new HashMap<String, String>(); String callbackName = null;/* w ww .j ava 2 s. c o m*/ try { MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request); if (request instanceof MultipartRequest) { MultipartRequest multipartRequest = (MultipartRequest) request; bindMultipart(multipartRequest.getMultiFileMap(), mpvs); } //check for XSRF String csrfToken = (String) mpvs.getPropertyValue("csrfToken").getValue(); exploitProtectionService.compareToken(csrfToken); PersistencePackage persistencePackage = new PersistencePackage(); persistencePackage.setPersistencePerspective(new PersistencePerspective()); persistencePackage.setCsrfToken(csrfToken); String ceilingEntity = (String) mpvs.getPropertyValue("ceilingEntityFullyQualifiedClassname") .getValue(); callbackName = (String) mpvs.getPropertyValue("callbackName").getValue(); String operation = (String) mpvs.getPropertyValue("operation").getValue(); String customCriteria = (String) mpvs.getPropertyValue("customCriteria").getValue(); mpvs.removePropertyValue("ceilingEntityFullyQualifiedClassname"); mpvs.removePropertyValue("sandbox"); mpvs.removePropertyValue("callbackName"); mpvs.removePropertyValue("operation"); mpvs.removePropertyValue("customCriteria"); persistencePackage.setCeilingEntityFullyQualifiedClassname(ceilingEntity); persistencePackage.setCustomCriteria(new String[] { customCriteria }); Entity entity = new Entity(); persistencePackage.setEntity(entity); entity.setType(new String[] { ceilingEntity }); List<Property> propertyList = new ArrayList<Property>(); for (PropertyValue propertyValue : mpvs.getPropertyValues()) { if (propertyValue.getValue() instanceof MultipartFile) { MultipartFile file = (MultipartFile) propertyValue.getValue(); if (file.getSize() > maximumFileSizeInBytes) { throw new MaxUploadSizeExceededException(maximumFileSizeInBytes); } if (file.getOriginalFilename() == null || file.getOriginalFilename().indexOf(".") < 0) { throw new FileExtensionUnavailableException( "Unable to determine file extension for uploaded file. The filename for the uploaded file is: " + file.getOriginalFilename()); } Map<String, MultipartFile> fileMap = UploadedFile.getUpload(); fileMap.put(propertyValue.getName(), (MultipartFile) propertyValue.getValue()); UploadedFile.setUpload(fileMap); entity.setMultiPartAvailableOnThread(true); } else { Property property = new Property(); property.setName(propertyValue.getName()); property.setValue((String) propertyValue.getValue()); propertyList.add(property); } } entity.setProperties(propertyList.toArray(new Property[] {})); Entity result = null; if (operation.equals("add")) { result = dynamicEntityRemoteService.add(persistencePackage); } else if (operation.equals("update")) { result = dynamicEntityRemoteService.update(persistencePackage); } model.put("callbackName", callbackName); model.put("result", buildJSON(result)); return new ModelAndView("blUploadCompletedView", model); } catch (MaxUploadSizeExceededException e) { if (callbackName != null) { model.put("callbackName", callbackName); model.put("error", buildErrorJSON(e.getMessage())); } return new ModelAndView("blUploadCompletedView", model); } catch (FileExtensionUnavailableException e) { if (callbackName != null) { model.put("callbackName", callbackName); model.put("error", buildErrorJSON(e.getMessage())); } return new ModelAndView("blUploadCompletedView", model); } catch (Exception e) { e.printStackTrace(); if (callbackName != null) { model.put("callbackName", callbackName); model.put("error", buildErrorJSON(e.getMessage())); } return new ModelAndView("blUploadCompletedView", model); } finally { UploadedFile.remove(); } }
From source file:org.codehaus.groovy.grails.web.binding.GrailsDataBinder.java
private void filterNestedParameterMaps(MutablePropertyValues mpvs) { for (PropertyValue pv : mpvs.getPropertyValues()) { final Object value = pv.getValue(); if (JSONObject.NULL.getClass().isInstance(value)) { mpvs.removePropertyValue(pv); }/*from w w w . j a v a2 s . c o m*/ if (!isCandidateForBinding(pv)) { mpvs.removePropertyValue(pv); } } }
From source file:org.codehaus.groovy.grails.web.binding.GrailsDataBinder.java
/** * Interrogates the specified properties looking for properites that represent associations to other * classes (e.g., 'author.id'). If such a property is found, this method attempts to load the specified * instance of the association (by ID) and set it on the target object. * * @param mpvs the <code>MutablePropertyValues</code> object holding the parameters from the request *///from www .ja va 2s . c o m protected void bindAssociations(MutablePropertyValues mpvs) { for (PropertyValue pv : mpvs.getPropertyValues()) { String propertyName = pv.getName(); String propertyNameToCheck = propertyName; final int i = propertyName.indexOf('.'); if (i > -1) { propertyNameToCheck = propertyName.substring(0, i); } if (!isAllowed(propertyNameToCheck)) continue; if (propertyName.endsWith(IDENTIFIER_SUFFIX)) { propertyName = propertyName.substring(0, propertyName.length() - 3); if (!isAllowed(propertyName)) continue; if (isReadableAndPersistent(propertyName) && bean.isWritableProperty(propertyName)) { if (NULL_ASSOCIATION.equals(pv.getValue())) { bean.setPropertyValue(propertyName, null); mpvs.removePropertyValue(pv); } else { Class<?> type = getPropertyTypeForPath(propertyName); final Object persisted = getPersistentInstance(type, pv.getValue()); if (persisted != null) { bean.setPropertyValue(propertyName, persisted); if (domainClass != null) { GrailsDomainClassProperty property = domainClass .getPersistentProperty(propertyName); if (property != null) { final GrailsDomainClassProperty otherSide = property.getOtherSide(); if (otherSide != null && List.class.isAssignableFrom(otherSide.getType()) && !property.isOptional()) { DeferredBindingActions.addBindingAction(new Runnable() { public void run() { if (otherSide.isOneToMany()) { Collection collection = GrailsMetaClassUtils .getPropertyIfExists(persisted, otherSide.getName(), Collection.class); if (collection != null && !collection.contains(getTarget())) { String methodName = "addTo" + GrailsNameUtils.getClassName(otherSide.getName()); GrailsMetaClassUtils.invokeMethodIfExists(persisted, methodName, new Object[] { getTarget() }); } } } }); } } } } } } } else { if (isReadableAndPersistent(propertyName)) { Class<?> type = getPropertyTypeForPath(propertyName); if (type != null) { if (Collection.class.isAssignableFrom(type)) { bindCollectionAssociation(mpvs, pv); } } } } } }
From source file:org.codehaus.groovy.grails.web.binding.GrailsDataBinder.java
@SuppressWarnings("unchecked") private void bindCollectionAssociation(MutablePropertyValues mpvs, PropertyValue pv) { Object v = pv.getValue();/*from ww w .j a v a2 s .co m*/ final boolean isArray = v != null && v.getClass().isArray(); if (!isArray && !(v instanceof String)) return; Collection collection = (Collection) bean.getPropertyValue(pv.getName()); collection.clear(); final Class associatedType = getReferencedTypeForCollection(pv.getName(), getTarget()); final PropertyEditor propertyEditor = findCustomEditor(collection.getClass(), pv.getName()); if (propertyEditor == null) { if (isDomainAssociation(associatedType)) { if (isArray) { Object[] identifiers = (Object[]) v; for (Object id : identifiers) { if (id != null) { associateObjectForId(pv, id, associatedType); } } mpvs.removePropertyValue(pv); } else if (v instanceof String) { associateObjectForId(pv, v, associatedType); mpvs.removePropertyValue(pv); } } else if (GrailsDomainConfigurationUtil.isBasicType(associatedType)) { Object[] values = null; if (isArray) { values = (Object[]) v; } else if (v instanceof String) { values = new String[] { (String) v }; } if (values != null) { List list = collection instanceof List ? (List) collection : null; for (int i = 0; i < values.length; i++) { Object value = values[i]; try { Object newValue = getTypeConverter().convertIfNecessary(value, associatedType); if (list != null) { if (i > list.size() - 1) { list.add(i, newValue); } else { list.set(i, newValue); } } else { collection.add(newValue); } } catch (TypeMismatchException e) { // ignore } } mpvs.removePropertyValue(pv); } } } }
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./*from w w w. j av a2s . c o m*/ */ 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; }
From source file:org.kuali.rice.krad.datadictionary.DictionaryBeanFactoryPostProcessor.java
/** * Iterates through the properties defined for the bean definition and invokes helper methods to process * the property value/*from w w w . j a va2s . c om*/ * * @param beanDefinition bean definition whose properties will be processed * @param nestedBeanStack stack of beans which contain the given bean */ protected void processBeanProperties(BeanDefinition beanDefinition, Stack<BeanDefinitionHolder> nestedBeanStack) { // iterate through properties and check for any configured message keys within the value MutablePropertyValues pvs = beanDefinition.getPropertyValues(); PropertyValue[] pvArray = pvs.getPropertyValues(); for (PropertyValue pv : pvArray) { Object newPropertyValue = null; if (isStringValue(pv.getValue())) { newPropertyValue = processStringPropertyValue(pv.getName(), getString(pv.getValue()), nestedBeanStack); } else { newPropertyValue = visitPropertyValue(pv.getName(), pv.getValue(), nestedBeanStack); } pvs.removePropertyValue(pv.getName()); pvs.addPropertyValue(pv.getName(), newPropertyValue); } }
From source file:org.kuali.rice.krad.datadictionary.uif.UifBeanFactoryPostProcessor.java
/** * If the bean class is type UifDictionaryBean, iterate through configured property values * and check for expressions./*from w ww .jav a 2s .c o m*/ * * @param beanName name of the bean in the factory (only set for top level beans, not nested) * @param beanDefinition bean definition to process for expressions * @param nestedPropertyName * @param expressionGraph * @param beanFactory bean factory being processed * @param processedBeanNames */ protected void processNestedBeanDefinition(String beanName, BeanDefinition beanDefinition, String nestedPropertyName, Map<String, String> expressionGraph, ConfigurableListableBeanFactory beanFactory, Set<String> processedBeanNames) { Class<?> beanClass = getBeanClass(beanDefinition, beanFactory); if ((beanClass == null) || !UifDictionaryBean.class.isAssignableFrom(beanClass) || processedBeanNames.contains(beanName)) { return; } LOG.debug("Processing bean name '" + beanName + "'"); Map<String, String> parentExpressionGraph = getExpressionGraphFromParent(beanDefinition.getParentName(), beanFactory, processedBeanNames); // process expressions on property values MutablePropertyValues pvs = beanDefinition.getPropertyValues(); PropertyValue[] pvArray = pvs.getPropertyValues(); for (PropertyValue pv : pvArray) { if (pv.getName().equals(UifPropertyPaths.EXPRESSION_GRAPH)) { continue; } String propertyPath = pv.getName(); if (StringUtils.isNotBlank(nestedPropertyName)) { propertyPath = nestedPropertyName + "." + propertyPath; } // for reloading, need to remove the property from the previously loaded bean definition if (expressionGraph.containsKey(propertyPath)) { expressionGraph.remove(propertyPath); } if (hasExpression(pv.getValue())) { // process expression String strValue = getStringValue(pv.getValue()); expressionGraph.put(propertyPath, strValue); // remove property value so expression will not cause binding exception pvs.removePropertyValue(pv.getName()); } else { // process nested objects Object newValue = processPropertyValue(propertyPath, pv.getName(), pv.getValue(), beanDefinition, parentExpressionGraph, expressionGraph, beanFactory, processedBeanNames); pvs.removePropertyValue(pv.getName()); pvs.addPropertyValue(pv.getName(), newValue); } // removed expression (if exists) from parent map since the property was set on child if (parentExpressionGraph.containsKey(pv.getName())) { parentExpressionGraph.remove(pv.getName()); } } // if nested bean set expression graph to null so it is not inherited from parent definition if (StringUtils.isNotBlank(nestedPropertyName)) { pvs.addPropertyValue(UifPropertyPaths.EXPRESSION_GRAPH, null); } // add remaining expressions from parent to expression graph for (Map.Entry<String, String> parentExpression : parentExpressionGraph.entrySet()) { String expressionPath = parentExpression.getKey(); if (StringUtils.isNotBlank(nestedPropertyName)) { expressionPath = nestedPropertyName + "." + expressionPath; } if (!expressionGraph.containsKey(expressionPath)) { expressionGraph.put(expressionPath, parentExpression.getValue()); } } if (StringUtils.isNotBlank(beanName)) { processedBeanNames.add(beanName); } }
From source file:org.kuali.rice.krad.uif.util.UifBeanFactoryPostProcessor.java
/** * If the bean class is type UifDictionaryBean, iterate through configured property values * and check for expressions/*from w w w . ja v a 2s. c om*/ * * @param beanName name of the bean in the factory (only set for top level beans, not nested) * @param beanDefinition bean definition to process for expressions * @param nestedPropertyName * @param expressionGraph * @param beanFactory bean factory being processed * @param processedBeanNames */ protected void processNestedBeanDefinition(String beanName, BeanDefinition beanDefinition, String nestedPropertyName, Map<String, String> expressionGraph, ConfigurableListableBeanFactory beanFactory, Set<String> processedBeanNames) { Class<?> beanClass = getBeanClass(beanDefinition, beanFactory); if ((beanClass == null) || !UifDictionaryBean.class.isAssignableFrom(beanClass) || processedBeanNames.contains(beanName)) { return; } LOG.debug("Processing bean name '" + beanName + "'"); Map<String, String> parentExpressionGraph = getExpressionGraphFromParent(beanDefinition.getParentName(), beanFactory, processedBeanNames); // process expressions on property values MutablePropertyValues pvs = beanDefinition.getPropertyValues(); PropertyValue[] pvArray = pvs.getPropertyValues(); for (PropertyValue pv : pvArray) { if (pv.getName().equals(UifPropertyPaths.EXPRESSION_GRAPH)) { continue; } String propertyPath = pv.getName(); if (StringUtils.isNotBlank(nestedPropertyName)) { propertyPath = nestedPropertyName + "." + propertyPath; } // for reloading, need to remove the property from the previously loaded bean definition if (expressionGraph.containsKey(propertyPath)) { expressionGraph.remove(propertyPath); } if (hasExpression(pv.getValue())) { // process expression String strValue = getStringValue(pv.getValue()); expressionGraph.put(propertyPath, strValue); // remove property value so expression will not cause binding exception pvs.removePropertyValue(pv.getName()); } else { // process nested objects Object newValue = processPropertyValue(propertyPath, pv.getName(), pv.getValue(), beanDefinition, parentExpressionGraph, expressionGraph, beanFactory, processedBeanNames); pvs.removePropertyValue(pv.getName()); pvs.addPropertyValue(pv.getName(), newValue); } // removed expression (if exists) from parent map since the property was set on child if (parentExpressionGraph.containsKey(pv.getName())) { parentExpressionGraph.remove(pv.getName()); } } // if nested bean set expression graph to null so it is not inherited from parent definition if (StringUtils.isNotBlank(nestedPropertyName)) { pvs.addPropertyValue(UifPropertyPaths.EXPRESSION_GRAPH, null); } // add remaining expressions from parent to expression graph for (Map.Entry<String, String> parentExpression : parentExpressionGraph.entrySet()) { String expressionPath = parentExpression.getKey(); if (StringUtils.isNotBlank(nestedPropertyName)) { expressionPath = nestedPropertyName + "." + expressionPath; } if (!expressionGraph.containsKey(expressionPath)) { expressionGraph.put(expressionPath, parentExpression.getValue()); } } // if bean name is given and factory does not have it registered we need to add it (inner beans that // were given an id) if (StringUtils.isNotBlank(beanName) && !StringUtils.contains(beanName, "$") && !StringUtils.contains(beanName, "#") && !beanFactory.containsBean(beanName)) { ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(beanName, beanDefinition); } if (StringUtils.isNotBlank(beanName)) { processedBeanNames.add(beanName); } }