private String[] getInterfaces(PropertyValue pv) { if (pv == null) return new String[0]; Object value = pv.getValue(); if (value instanceof Collection) { Collection collection = (Collection) value; String[] strs = new String[collection.size()]; int index = 0; for (Object obj : collection) { if (value instanceof TypedStringValue) { strs[index] = ((TypedStringValue) value).getValue(); } else { strs[index] = value.toString(); } index++; } return strs; } else { return new String[] { value.toString() }; } }
/** * Utility method used for maintaining backwards compatibility by converting Class objects to String (using their * class names). Used by importer and exporter parsing to set the 'interfaces' property. * * @param parsedClasses collection of parsed classes * @return a collection of converted (if necessary) metadata */ public static Set<?> convertClassesToStrings(Set<?> parsedClasses) { Set<Object> interfaces = new ManagedSet<Object>(parsedClasses.size()); for (Object clazz : parsedClasses) { if (clazz instanceof TypedStringValue || clazz instanceof String) { interfaces.add(clazz); } else { // add adapter definition for bean references (which can be classes) interfaces.add(BeanDefinitionBuilder.genericBeanDefinition(ToStringClassAdapter.class) .addConstructorArgValue(clazz).getBeanDefinition()); } } return interfaces; }
/** * Return a typed String value Object for the given value element. * * @param ele element * @param defaultTypeName type class name * @return typed String value Object */ private Object parseValueElement(Element ele, String defaultTypeName) { // It's a literal value. String value = DomUtils.getTextValue(ele); String specifiedTypeName = ele.getAttribute(BeanDefinitionParserDelegate.TYPE_ATTRIBUTE); String typeName = specifiedTypeName; if (!StringUtils.hasText(typeName)) { typeName = defaultTypeName; } try { TypedStringValue typedValue = buildTypedStringValue(value, typeName); typedValue.setSource(extractSource(ele)); typedValue.setSpecifiedTypeName(specifiedTypeName); return typedValue; } catch (ClassNotFoundException ex) { error("Type class [" + typeName + "] not found for <value> element", ele, ex); return value; } }
/** * Parse a props element. */ public Properties parsePropsElement(Element propsEle) { ManagedProperties props = new OrderedManagedProperties(); props.setSource(extractSource(propsEle)); props.setMergeEnabled(parseMergeAttribute(propsEle)); List propEles = DomUtils.getChildElementsByTagName(propsEle, BeanDefinitionParserDelegate.PROP_ELEMENT); for (Iterator it = propEles.iterator(); it.hasNext();) { Element propEle = (Element) it.next(); String key = propEle.getAttribute(BeanDefinitionParserDelegate.KEY_ATTRIBUTE); // Trim the text value to avoid unwanted whitespace // caused by typical XML formatting. String value = DomUtils.getTextValue(propEle).trim(); TypedStringValue keyHolder = new TypedStringValue(key); keyHolder.setSource(extractSource(propEle)); TypedStringValue valueHolder = new TypedStringValue(value); valueHolder.setSource(extractSource(propEle)); props.put(keyHolder, valueHolder); } return props; }
/** * Return a typed String value Object for the given value element. */ public Object parseValueElement(Element ele, String defaultTypeName) { // It's a literal value. String value = DomUtils.getTextValue(ele); String specifiedTypeName = ele.getAttribute(TYPE_ATTRIBUTE); String typeName = specifiedTypeName; if (!StringUtils.hasText(typeName)) { typeName = defaultTypeName; } try { TypedStringValue typedValue = buildTypedStringValue(value, typeName); typedValue.setSource(extractSource(ele)); typedValue.setSpecifiedTypeName(specifiedTypeName); return typedValue; } catch (ClassNotFoundException ex) { error("Type class [" + typeName + "] not found for <value> element", ele, ex); return value; } }
/** * Parse a props element. */ public Properties parsePropsElement(Element propsEle) { ManagedProperties props = new ManagedProperties(); props.setSource(extractSource(propsEle)); props.setMergeEnabled(parseMergeAttribute(propsEle)); List<Element> propEles = DomUtils.getChildElementsByTagName(propsEle, PROP_ELEMENT); for (Element propEle : propEles) { String key = propEle.getAttribute(KEY_ATTRIBUTE); // Trim the text value to avoid unwanted whitespace // caused by typical XML formatting. String value = DomUtils.getTextValue(propEle).trim(); TypedStringValue keyHolder = new TypedStringValue(key); keyHolder.setSource(extractSource(propEle)); TypedStringValue valueHolder = new TypedStringValue(value); valueHolder.setSource(extractSource(propEle)); props.put(keyHolder, valueHolder); } return props; }
private void addIncludePatterns(Element element, ParserContext parserContext, BeanDefinition beanDef) { ManagedList<TypedStringValue> includePatterns = new ManagedList<TypedStringValue>(); NodeList childNodes = element.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node instanceof Element) { Element includeElement = (Element) node; TypedStringValue valueHolder = new TypedStringValue(includeElement.getAttribute("name")); valueHolder.setSource(parserContext.extractSource(includeElement)); includePatterns.add(valueHolder); } } if (!includePatterns.isEmpty()) { includePatterns.setSource(parserContext.extractSource(element)); beanDef.getPropertyValues().add("includePatterns", includePatterns); } }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void postProcessBeanFactory( ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { String[] igniteConfigNames = configurableListableBeanFactory.getBeanNamesForType(IgniteConfiguration.class); if (igniteConfigNames.length != 1) { throw new IllegalArgumentException("Spring config must contain exactly one ignite configuration!"); } String[] activeStoreConfigNames = configurableListableBeanFactory.getBeanNamesForType(BaseActiveStoreConfiguration.class); if (activeStoreConfigNames.length != 1) { throw new IllegalArgumentException("Spring config must contain exactly one active store configuration!"); } BeanDefinition igniteConfigDef = configurableListableBeanFactory.getBeanDefinition(igniteConfigNames[0]); MutablePropertyValues propertyValues = igniteConfigDef.getPropertyValues(); if (!propertyValues.contains(USER_ATTRS_PROP_NAME)) { propertyValues.add(USER_ATTRS_PROP_NAME, new ManagedMap()); } PropertyValue propertyValue = propertyValues.getPropertyValue(USER_ATTRS_PROP_NAME); Map userAttrs = (Map)propertyValue.getValue(); TypedStringValue key = new TypedStringValue(CONFIG_USER_ATTR); RuntimeBeanReference value = new RuntimeBeanReference(beanName); userAttrs.put(key, value); }
/** * Returns the class which is configured in the given {@link PropertyValue}. In case it is not a * {@link TypedStringValue} or the value contained cannot be interpreted as {@link Class} it will return null. * * @param propertyValue * @param beanName * @return */ private Class<?> getClassForPropertyValue(PropertyValue propertyValue, String beanName) { Object value = propertyValue.getValue(); String className = null; if (value instanceof TypedStringValue) { className = ((TypedStringValue) value).getValue(); } else if (value instanceof String) { className = (String) value; } else if (value instanceof Class<?>) { return (Class<?>) value; } else { return Void.class; } try { return ClassUtils.resolveClassName(className, context.getBeanClassLoader()); } catch (IllegalArgumentException ex) { LOGGER.warn(String.format("Couldn't load class %s referenced as repository interface in bean %s!", className, beanName)); return Void.class; } }
private String updatePropertyValue(String propertyName, PropertyValues values) { PropertyValue property = values.getPropertyValue(propertyName); if (property == null) { return null; } Object value = property.getValue(); if (value == null) { return null; } else if (value instanceof String) { return value.toString(); } else if (value instanceof TypedStringValue) { return ((TypedStringValue) value).getValue(); } else { return null; } }
/** * Helper method for getting the string value of a property from a {@link PropertyValues} * * @param propertyValues property values instance to pull from * @param propertyName name of property whose value should be retrieved * @return String value for property or null if property was not found */ public static String getStringValFromPVs(PropertyValues propertyValues, String propertyName) { String propertyValue = null; if ((propertyValues != null) && propertyValues.contains(propertyName)) { Object pvValue = propertyValues.getPropertyValue(propertyName).getValue(); if (pvValue instanceof TypedStringValue) { TypedStringValue typedStringValue = (TypedStringValue) pvValue; propertyValue = typedStringValue.getValue(); } else if (pvValue instanceof String) { propertyValue = (String) pvValue; } } return propertyValue; }
/** * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) */ @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { for (String bean : getSecurityBeanNames(beanFactory)) { if (beanFactory.containsBeanDefinition(bean)) { if (logger.isDebugEnabled()) { logger.debug("Adding RM method security definitions for " + bean); } BeanDefinition beanDef = beanFactory.getBeanDefinition(bean); PropertyValue beanValue = beanDef.getPropertyValues().getPropertyValue(PROP_OBJECT_DEFINITION_SOURCE); if (beanValue != null) { String beanStringValue = (String)((TypedStringValue)beanValue.getValue()).getValue(); String mergedStringValue = merge(beanStringValue); beanDef.getPropertyValues().addPropertyValue(PROP_OBJECT_DEFINITION_SOURCE, new TypedStringValue(mergedStringValue)); } } } }
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { Map<String, ResourceBundleNameProvider> beans = beanFactory.getBeansOfType(ResourceBundleNameProvider.class); if (beans != null) { BeanDefinition messageSourceDef = beanFactory.getBeanDefinition("messageSource"); ManagedList<TypedStringValue> bundleNames = new ManagedList<>(); bundleNames.setMergeEnabled(true); Collection<ResourceBundleNameProvider> nameProviders = beans.values(); for (ResourceBundleNameProvider resourceBundleNameProvider : nameProviders) { for (String bundleName : resourceBundleNameProvider.getBundleNames()) { bundleNames.add(new TypedStringValue(bundleName)); } } messageSourceDef.getPropertyValues().add("basenames", bundleNames); } }
@Test public void testParsePropertyEnum() throws Exception { TypedStringValue enumPropertyValue = mockTypedStringValue("my enum type", "my enum type value"); ElementConfiguration parsedEnumFieldConfiguration = beanPropertyParser.parseProperty(enumPropertyValue); assertEquals(ElementConfigurationType.Object, parsedEnumFieldConfiguration.getFieldConfigurationType()); assertTrue(parsedEnumFieldConfiguration instanceof ObjectConfiguration); ObjectConfiguration enumObjectConfiguration = (ObjectConfiguration) parsedEnumFieldConfiguration; assertEquals(enumPropertyValue.getTargetTypeName(), enumObjectConfiguration.getClassName()); ElementConfiguration actual = enumObjectConfiguration.getConstructorArguments().get(0); assertEquals(ElementConfigurationType.Primitive, actual.getFieldConfigurationType()); PrimitiveConfiguration primitiveConfiguration = (PrimitiveConfiguration) actual; assertEquals(enumPropertyValue.getValue(), primitiveConfiguration.getValue()); }
@Test public void testParsePropertyClassicList() { ManagedList<TypedStringValue> list = new ManagedList<TypedStringValue>(); mock(ManagedList.class); list.add(mockTypedStringValue(null, "my first value")); list.add(mockTypedStringValue(null, "my second value")); ElementConfiguration parsedFieldConfiguration = beanPropertyParser.parseProperty(list); assertEquals(ElementConfigurationType.List, parsedFieldConfiguration.getFieldConfigurationType()); ListConfiguration listConfiguration = (ListConfiguration) parsedFieldConfiguration; assertEquals(2, listConfiguration.getListConfiguration().size()); assertEquals(ElementConfigurationType.Primitive, listConfiguration.getListConfiguration().get(0) .getFieldConfigurationType()); assertEquals(ElementConfigurationType.Primitive, listConfiguration.getListConfiguration().get(1) .getFieldConfigurationType()); assertEquals(list.get(0).getValue(), ((PrimitiveConfiguration) listConfiguration.getListConfiguration().get(0)).getValue()); assertEquals(list.get(1).getValue(), ((PrimitiveConfiguration) listConfiguration.getListConfiguration().get(1)).getValue()); }
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { ManagedMap<?, ?> map = (ManagedMap<?, ?>) source; for (Map.Entry<?, ?> entry : map.entrySet()) { writer.startNode("entry"); writer.startNode("key"); if (entry.getKey().getClass().equals(TypedStringValue.class)) { writer.startNode("value"); writer.setValue(((TypedStringValue) entry.getKey()).getValue()); writer.endNode(); } else { writeItem(entry.getKey(), context, writer); } writer.endNode(); if (entry.getValue().getClass().equals(TypedStringValue.class)) { writer.startNode("value"); writer.setValue(((TypedStringValue) entry.getValue()).getValue()); writer.endNode(); } else { writeItem(entry.getValue(), context, writer); } writer.endNode(); } }
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { BeanDefinitionHolder holder = (BeanDefinitionHolder) source; BeanDefinition definition = holder.getBeanDefinition(); writer.addAttribute("class", definition.getBeanClassName()); writer.addAttribute("name", holder.getBeanName()); for (PropertyValue property : definition.getPropertyValues().getPropertyValueList()) { writer.startNode("property"); writer.addAttribute("name", property.getName()); if (property.getValue().getClass().equals(TypedStringValue.class)) { context.convertAnother(property.getValue()); } else { writeItem(property.getValue(), context, writer); } writer.endNode(); } }
/** * @param node * @param v */ private void addFieldToNode(Graph.Node node, final PropertyValue v) { if (v.getValue() instanceof TypedStringValue) { if (!v.getName().toUpperCase().contains("PASSWORD")) { node.addField(v.getName() + "=" + ((TypedStringValue) v.getValue()).getValue()); } else { node.addField(v.getName() + "=********"); } } else if (v.getValue() instanceof String | v.getValue() instanceof Boolean | v.getValue() instanceof Integer) { node.addField(v.getName() + "=" + v.getValue()); } else if (v.getValue() instanceof BeanDefinitionHolder) { node.addField(v.getName() + "=" + ((BeanDefinitionHolder) v.getValue()).getBeanDefinition().getBeanClassName()); } else if (!(v.getValue() instanceof RuntimeBeanReference)) { node.addField(v.getName() + "=(" + v.getValue().getClass().getSimpleName() + ")"); } else { //This is a RuntimeBeanReference which is handled as an edge } }
/** * Parses a list of regular expressions stored inside list bean, defined by the standard <util:list>. */ private List<String> parseListOfRegexs(BeanDefinition listBean) { Object value = listBean.getPropertyValues().getPropertyValue("sourceList").getValue(); List<String> regexs = new ArrayList<String>(); if (value instanceof ManagedList) { @SuppressWarnings("rawtypes") ManagedList regexList = (ManagedList) value; for (int i = 0; i < regexList.size(); i++) { Object obj = regexList.get(i); if (obj instanceof TypedStringValue) { regexs.add(((TypedStringValue) obj).getValue()); } else { throw new RuntimeException("Can't parse the list of regular expressions from bean " + listBean); } } } else { throw new RuntimeException("Can't parse the list of regular expressions from bean " + listBean); } return regexs; }
private String getString(PropertyValue pv) { if (pv == null) return ""; Object value = pv.getValue(); if (value == null) { return ""; } if (value instanceof TypedStringValue) { return ((TypedStringValue) value).getValue(); } return value.toString(); }