/** * 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; }
private static Set<String> getInititalEntityClasses(String[] domainPackages) { if (ArrayUtils.isEmpty(domainPackages)) { return null; } ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider(false); componentProvider.addIncludeFilter(new AnnotationTypeFilter(Document.class)); componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class)); Set<String> classes = new ManagedSet<>(); for (String domainPackage : domainPackages) { if (StringUtils.isBlank(domainPackage)) { continue; } for (BeanDefinition candidate : componentProvider.findCandidateComponents(domainPackage)) { classes.add(candidate.getBeanClassName()); } } return classes; }
/** * Parses a list of elements into a set of beans/standard content. * * @param grandChildren - The set of beans/content in a bean property * @param child - The property tag for the parent. * @param parent - The parent bean that the tag is nested in. * @param parserContext - Provided information and functionality regarding current bean set. * @return A managedSet of the nested content. */ protected ManagedSet parseSet(ArrayList<Element> grandChildren, Element child, BeanDefinitionBuilder parent, ParserContext parserContext) { ManagedSet setItems = new ManagedSet(); for (int i = 0; i < grandChildren.size(); i++) { Element grandChild = grandChildren.get(i); if (child.getTagName().compareTo("value") == 0) { setItems.add(grandChild.getTextContent()); } else { setItems.add(parseBean(grandChild, parent, parserContext)); } } String merge = child.getAttribute("merge"); if (merge != null) { setItems.setMergeEnabled(Boolean.valueOf(merge)); } return setItems; }
/** * Iterates through the set values and calls helpers to process the value * * @param setVal the set to process * @param propertyName name of the property which has the set value * @param nestedBeanStack stack of bean containers which contains the set property */ protected void visitSet(Set setVal, String propertyName, Stack<BeanDefinitionHolder> nestedBeanStack) { boolean isMergeEnabled = false; if (setVal instanceof ManagedSet) { isMergeEnabled = ((ManagedSet) setVal).isMergeEnabled(); } ManagedSet newSet = new ManagedSet(); newSet.setMergeEnabled(isMergeEnabled); for (Object elem : setVal) { if (isStringValue(elem)) { elem = processSetStringPropertyValue(propertyName, setVal, getString(elem), nestedBeanStack, beanProcessors); } else { elem = visitPropertyValue(propertyName, elem, nestedBeanStack); } newSet.add(elem); } setVal.clear(); setVal.addAll(newSet); }
private void parseLogsElement(Element logsElement, BeanDefinitionBuilder factory, ParserContext ctx) { if (logsElement == null) { return; } List<Element> childElements = DomUtils.getChildElementsByTagName(logsElement, "log"); ManagedSet<BeanDefinition> logs = new ManagedSet<BeanDefinition>(childElements.size()); for (Element element : childElements) { BeanDefinition bd = ctx.getRegistry().getBeanDefinition(element.getAttribute("ref")); if (bd != null) { logs.add(bd); } } if (logs.size() != 0) { factory.addPropertyValue("eventLogs", logs); } }
@Override public BeanDefinition parseFilterManagerElement(Element filterElement) { BeanDefinitionBuilder filterManagerFactory = BeanDefinitionBuilder.rootBeanDefinition(SimpleFilterManager.class); filterManagerFactory.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); filterManagerFactory.setLazyInit(false); filterManagerFactory.addPropertyValue("type", getComponentName()); List<Element> itemElements = DomUtils.getChildElementsByTagName(filterElement, "item"); ManagedSet<BeanDefinition> items = new ManagedSet<BeanDefinition>(itemElements.size()); for (Element element : itemElements) { BeanDefinition bd = parseItemElement(element); if (bd != null) { items.add(bd); } } filterManagerFactory.addPropertyValue("filters", items); return filterManagerFactory.getBeanDefinition(); }
private XStream createStream() { XStream stream = new XStream(); stream.registerConverter(new BeanDefinitionConverter(stream.getMapper())); stream.registerConverter(new BeanDefinitionHolderConverter(stream.getMapper())); stream.registerConverter(new TypedStringValueConverter()); stream.registerConverter(new ManagedCollectionConverter(stream.getMapper())); stream.registerConverter(new ManagedMapConverter(stream.getMapper())); stream.registerConverter(new RuntimeBeanReferenceConverter()); stream.alias("map", ManagedMap.class); stream.alias("list", ManagedList.class); stream.alias("set", ManagedSet.class); stream.alias("array", ManagedArray.class); stream.aliasType("bean", BeanDefinition.class); stream.alias("bean", BeanDefinitionHolder.class); stream.alias("ref", RuntimeBeanReference.class); return stream; }
/** * Parses a list of elements into a set of beans/standard content. * * @param grandChildren - The set of beans/content in a bean property * @param child - The property tag for the parent. * @param parent - The parent bean that the tag is nested in. * @param parserContext - Provided information and functionality regarding current bean set. * @return A managedSet of the nested content. */ private ManagedSet parseSet(ArrayList<Element> grandChildren, Element child, BeanDefinitionBuilder parent, ParserContext parserContext) { ManagedSet setItems = new ManagedSet(); for (int i = 0; i < grandChildren.size(); i++) { Element grandChild = grandChildren.get(i); if (child.getTagName().compareTo("spring:value") == 0) { setItems.add(grandChild.getTextContent()); } else { setItems.add(parseBean(grandChild, parent, parserContext)); } } String merge = child.getAttribute("merge"); if (merge != null) { setItems.setMergeEnabled(Boolean.valueOf(merge)); } return setItems; }
private void parseNodeRoles(BeanDefinitionBuilder def, Element el) { ManagedSet<String> roles = new ManagedSet<>(); roles.addAll(subElements(el, "roles", "role").stream() .map(roleEl -> getTextValue(roleEl).trim()) .filter(role -> !role.isEmpty()) .collect(toSet()) ); def.addPropertyValue("roles", roles); }
/** * Parse a set element. */ public Set<?> parseSetElement(Element collectionEle, BeanDefinition bd) { String defaultElementType = collectionEle.getAttribute(BeanDefinitionParserDelegate.VALUE_TYPE_ATTRIBUTE); NodeList nl = collectionEle.getChildNodes(); ManagedSet<Object> target = new ManagedSet<Object>(nl.getLength()); target.setSource(extractSource(collectionEle)); target.setElementTypeName(defaultElementType); target.setMergeEnabled(parseMergeAttribute(collectionEle)); parseCollectionElements(nl, target, bd, defaultElementType); return target; }
/** * Parse a set element. */ public Set<Object> parseSetElement(Element collectionEle, BeanDefinition bd) { String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE); NodeList nl = collectionEle.getChildNodes(); ManagedSet<Object> target = new ManagedSet<Object>(nl.getLength()); target.setSource(extractSource(collectionEle)); target.setElementTypeName(defaultElementType); target.setMergeEnabled(parseMergeAttribute(collectionEle)); parseCollectionElements(nl, target, bd, defaultElementType); return target; }
private String parseFilterChain(Element element, ParserContext parserContext) { Element filterChain = DomUtils.getChildElementByTagName(element, AttributeNames.FILTER_CHAIN); List<Element> filters = DomUtils.getChildElementsByTagName(filterChain, AttributeNames.FILTER); BeanDefinitionBuilder filterChainBuilder = BeanDefinitionBuilder.genericBeanDefinition(FilterChain.class); ManagedSet<BeanDefinition> filterBeanDefinitions = new ManagedSet<BeanDefinition>(); for (Element filter : filters) { String ref = filter.getAttribute(AttributeNames.REF); BeanDefinition filterDefinition = parserContext.getRegistry().getBeanDefinition(ref); filterBeanDefinitions.add(filterDefinition); } filterChainBuilder.addConstructorArgValue(filterBeanDefinitions); String filterChainString = StringUtils.uncapitalize(FilterChain.class.getSimpleName()); parserContext.getRegistry().registerBeanDefinition(filterChainString, filterChainBuilder.getBeanDefinition()); return filterChainString; }
/** * 更新手术安排 * * @param id 手术id * @param doctor_ids 医生数组 * @param nurse_ids 护士数组 * @param room 房间号 */ @Override public void updateArrange(int id, int[] doctor_ids, int[] nurse_ids, int room) { Surgery surgery = surgeryDao.findById(id); //设置从申请状态到安排状态 surgery.setState(1); //Set<Doctor> assists = surgery.getAssists(); // Doctor doctor = new Doctor(); // doctor.setId(1); // doctor.setName("一拳"); // assists.add(doctor); // for (Doctor doctor1 : assists) { // doctor1.setName("只有我不在的"); // } //重新建立数组,设置医生 Set<Doctor> assists = new ManagedSet<Doctor>(); for (int doctor_id : doctor_ids) { Doctor doctor = new Doctor(); doctor.setId(doctor_id); assists.add(doctor); } surgery.setAssists(assists); //设置护士 Set<Nurse> nurses = new ManagedSet<Nurse>(); for (int nurse_id : nurse_ids) { Nurse nurse = new Nurse(); nurse.setNurseId(nurse_id); nurses.add(nurse); } surgery.setNurses(nurses); //设置手术室 Operationroom operationroom = new Operationroom(); operationroom.setNum(room); surgery.setRoom(operationroom); }
/** * 取消手术安排 * * @param id 手术id */ public void cancelArrange(int id) { Surgery surgery = surgeryDao.findById(id); //设置从安排状态到申请状态 surgery.setState(0); //重新建立数组,设置医生 Set<Doctor> assists = new ManagedSet<Doctor>(); //设置护士 Set<Nurse> nurses = new ManagedSet<Nurse>(); surgery.setAssists(assists); surgery.setNurses(nurses); surgery.setRoom(null); }
protected void shallowCloneManagedCollections(final AbstractBeanDefinition cloneBeanDefinition) { // clone is not a deep clone - managed lists / maps are by-reference which is problematic for placeholder resolution final MutablePropertyValues propertyValues = cloneBeanDefinition.getPropertyValues(); for (final PropertyValue pv : propertyValues.getPropertyValues()) { final Object value = pv.getValue(); if (value instanceof ManagedList<?>) { final ManagedList<Object> newList = new ManagedList<>(); newList.setSource(((ManagedList<?>) value).getSource()); newList.setElementTypeName(((ManagedList<?>) value).getElementTypeName()); newList.addAll((ManagedList<?>) value); propertyValues.add(pv.getName(), newList); } else if (value instanceof ManagedSet<?>) { final ManagedSet<Object> newSet = new ManagedSet<>(); newSet.setSource(((ManagedSet<?>) value).getSource()); newSet.setElementTypeName(((ManagedSet<?>) value).getElementTypeName()); newSet.addAll((ManagedSet<?>) value); propertyValues.add(pv.getName(), newSet); } else if (value instanceof ManagedMap<?, ?>) { final ManagedMap<Object, Object> newMap = new ManagedMap<>(); newMap.setSource(((ManagedMap<?, ?>) value).getSource()); newMap.setKeyTypeName(((ManagedMap<?, ?>) value).getKeyTypeName()); newMap.setValueTypeName(((ManagedMap<?, ?>) value).getValueTypeName()); newMap.putAll((ManagedMap<?, ?>) value); propertyValues.add(pv.getName(), newMap); } } }
private void parseShardingDataSource(Element element, ParserContext parserContext){ BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(ShardingDataSourceRepositorySupport.class); Element partitionsElement= DomUtils.getChildElementByTagName(element, DATA_SOURCE_PARTITIONS); List<Element> partitions= DomUtils.getChildElementsByTagName(partitionsElement, DATA_SOURCE_PARTITION); ManagedSet<BeanDefinition> partitionDataSources = new ManagedSet<BeanDefinition>(); for(Element each: partitions){ partitionDataSources.add(parseDataSourcePartition(each,parserContext)); } factory.addPropertyValue("partitionDataSources", partitionDataSources); factory.addPropertyValue("databaseType", DatabaseType.valueOf(element.getAttribute(DATA_BASE_TYPE))); shardingDataSourceRepositoryDefinition= factory.getBeanDefinition(); }
/** * Parse a set element. */ public Set parseSetElement(Element collectionEle, BeanDefinition bd) { String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE); NodeList nl = collectionEle.getChildNodes(); ManagedSet<Object> target = new ManagedSet<Object>(nl.getLength()); target.setSource(extractSource(collectionEle)); target.setElementTypeName(defaultElementType); target.setMergeEnabled(parseMergeAttribute(collectionEle)); parseCollectionElements(nl, target, bd, defaultElementType); return target; }
protected Object handleSetValues(final PropertyValue configuredValue) { final Object value; LOGGER.debug("[{}] Set of values / bean reference names has been configured - treating property {} of {} as <set>", this.beanName, this.propertyName, this.targetBeanName); final ManagedSet<Object> set = new ManagedSet<>(); if (this.merge && configuredValue != null) { final Object configuredValueDefinition = configuredValue.getValue(); if (configuredValueDefinition instanceof ManagedSet<?>) { final ManagedSet<?> oldSet = (ManagedSet<?>) configuredValueDefinition; set.setElementTypeName(oldSet.getElementTypeName()); set.setMergeEnabled(oldSet.isMergeEnabled()); set.setSource(oldSet.getSource()); set.addAll(oldSet); LOGGER.debug("[{}] Merged existing value set values: {}", this.beanName, oldSet); } } Set<Object> valuesToAdd; if (this.valueSet != null) { LOGGER.debug("[{}] Set of configured values for {} of {}: ", this.beanName, this.propertyName, this.targetBeanName, this.valueSet); valuesToAdd = this.valueSet; } else { LOGGER.debug("[{}] Set of configured bean reference names for {} of {}: ", this.beanName, this.propertyName, this.targetBeanName, this.beanReferenceNameSet); valuesToAdd = new HashSet<>(); for (final String beanReferenceName : this.beanReferenceNameSet) { valuesToAdd.add(new RuntimeBeanReference(beanReferenceName)); } } LOGGER.debug("[{}] Adding new entries to set for {} of {}", this.beanName, this.propertyName, this.targetBeanName); set.addAll(valuesToAdd); if (!set.isMergeEnabled() && this.mergeParent) { LOGGER.debug("[{}] Enabling \"merge\" for <set> on {} of {}", this.beanName, this.propertyName, this.targetBeanName); } set.setMergeEnabled(set.isMergeEnabled() || this.mergeParent); value = set; return value; }
@SuppressWarnings("unchecked") protected void visitSet(String propertyName, Map<String, String> parentPropertyExpressions, Map<String, String> propertyExpressions, Set setVal, ConfigurableListableBeanFactory beanFactory, Set<String> processedBeanNames) { boolean isMergeEnabled = false; if (setVal instanceof ManagedSet) { isMergeEnabled = ((ManagedSet) setVal).isMergeEnabled(); } ManagedSet newSet = new ManagedSet(); newSet.setMergeEnabled(isMergeEnabled); for (Object elem : setVal) { if (hasExpression(elem)) { String strValue = getStringValue(elem); propertyExpressions.put(propertyName + ExpressionEvaluator.EMBEDDED_PROPERTY_NAME_ADD_INDICATOR, strValue); } else { // process set value bean definition as a top level bean if ((elem instanceof BeanDefinition) || (elem instanceof BeanDefinitionHolder)) { String beanName = null; BeanDefinition beanDefinition; if (elem instanceof BeanDefinition) { beanDefinition = (BeanDefinition) elem; } else { beanDefinition = ((BeanDefinitionHolder) elem).getBeanDefinition(); beanName = ((BeanDefinitionHolder) elem).getBeanName(); } processBeanDefinition(beanName, beanDefinition, beanFactory, processedBeanNames); } newSet.add(elem); } } // determine if we need to clear any parent expressions for this list if (!isMergeEnabled) { // clear any expressions that match the property name minus index Map<String, String> adjustedParentExpressions = new HashMap<String, String>(); for (Map.Entry<String, String> parentExpression : parentPropertyExpressions.entrySet()) { if (!parentExpression.getKey().startsWith( propertyName + ExpressionEvaluator.EMBEDDED_PROPERTY_NAME_ADD_INDICATOR)) { adjustedParentExpressions.put(parentExpression.getKey(), parentExpression.getValue()); } } parentPropertyExpressions.clear(); parentPropertyExpressions.putAll(adjustedParentExpressions); } setVal.clear(); setVal.addAll(newSet); }
private void parseLifecycleElement(Element lifecycleElement, BeanDefinitionBuilder factory) { if (lifecycleElement == null) { return; } List<Element> childElements = DomUtils.getChildElementsByTagName(lifecycleElement, "item"); ManagedSet<BeanDefinition> items = new ManagedSet<BeanDefinition>(childElements.size()); for (Element element : childElements) { BeanDefinition bd = parseItemElement(element); if (bd != null) { items.add(bd); } } factory.addPropertyValue("lifecycleListeners", items); }
@Test @Deprecated public void testServletContextPropertyPlaceholderConfigurerWithAttributes() { MockServletContext sc = new MockServletContext(); sc.addInitParameter("key4", "mykey4"); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", "${age}"); pvs.add("name", "name${var}${var}${"); pvs.add("spouse", new RuntimeBeanReference("${ref}")); wac.registerSingleton("tb1", TestBean.class, pvs); ConstructorArgumentValues cas = new ConstructorArgumentValues(); cas.addIndexedArgumentValue(1, "${age}"); cas.addGenericArgumentValue("${var}name${age}"); pvs = new MutablePropertyValues(); List<Object> friends = new ManagedList<Object>(); friends.add("na${age}me"); friends.add(new RuntimeBeanReference("${ref}")); pvs.add("friends", friends); Set<Object> someSet = new ManagedSet<Object>(); someSet.add("na${age}me"); someSet.add(new RuntimeBeanReference("${ref}")); pvs.add("someSet", someSet); Map<String, Object> someMap = new ManagedMap<String, Object>(); someMap.put("key1", new RuntimeBeanReference("${ref}")); someMap.put("key2", "${age}name"); MutablePropertyValues innerPvs = new MutablePropertyValues(); innerPvs.add("touchy", "${os.name}"); RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class); innerBd.setPropertyValues(innerPvs); someMap.put("key3", innerBd); MutablePropertyValues innerPvs2 = new MutablePropertyValues(innerPvs); someMap.put("${key4}", new BeanDefinitionHolder(new ChildBeanDefinition("tb1", innerPvs2), "child")); pvs.add("someMap", someMap); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, cas, pvs); wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd); pvs = new MutablePropertyValues(); pvs.add("properties", "var=${m}var\nref=tb2\nm=my"); pvs.add("searchContextAttributes", Boolean.TRUE); wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs); sc.setAttribute("age", new Integer(98)); wac.refresh(); TestBean tb1 = (TestBean) wac.getBean("tb1"); TestBean tb2 = (TestBean) wac.getBean("tb2"); assertEquals(98, tb1.getAge()); assertEquals(98, tb2.getAge()); assertEquals("namemyvarmyvar${", tb1.getName()); assertEquals("myvarname98", tb2.getName()); assertEquals(tb2, tb1.getSpouse()); assertEquals(2, tb2.getFriends().size()); assertEquals("na98me", tb2.getFriends().iterator().next()); assertEquals(tb2, tb2.getFriends().toArray()[1]); assertEquals(2, tb2.getSomeSet().size()); assertTrue(tb2.getSomeSet().contains("na98me")); assertTrue(tb2.getSomeSet().contains(tb2)); assertEquals(4, tb2.getSomeMap().size()); assertEquals(tb2, tb2.getSomeMap().get("key1")); assertEquals("98name", tb2.getSomeMap().get("key2")); TestBean inner1 = (TestBean) tb2.getSomeMap().get("key3"); TestBean inner2 = (TestBean) tb2.getSomeMap().get("mykey4"); assertEquals(0, inner1.getAge()); assertEquals(null, inner1.getName()); assertEquals(System.getProperty("os.name"), inner1.getTouchy()); assertEquals(98, inner2.getAge()); assertEquals("namemyvarmyvar${", inner2.getName()); assertEquals(System.getProperty("os.name"), inner2.getTouchy()); }
public boolean canConvert(Class type) { return type.equals(ManagedList.class) || type.equals(ManagedArray.class) || type.equals(ManagedSet.class); }