protected ValueDefinition readSet(BeanContainer container, XmlReader reader, LoaderContext context) { boolean merge = boolAttribute(reader, MERGE_ATTRIBUTE, false); Class<?> valueType = javaTypeAttribute(reader, VALUE_TYPE_ATTRIBUTE); Set<ValueDefinition> values = new LinkedHashSet<>(); final QName elementName = reader.getElementName(); while (reader.nextWhileNotEnd(elementName)) { if (reader.isStartElement()) { if (reader.isStartElement(DESCRIPTION_ELEMENT)) { continue; } ValueDefinition vd = readValueElement(container, reader, context); if (null != vd) { values.add(vd); } } } return new ValueDefinition(values, merge, null, null, valueType); }
public <T> BeanDefinitionBase create(Class<? super T> typeClass, boolean primary, boolean lazyInit, Class<?> beanClass, Object... constructorArguments) { return create(typeClass, primary, null, lazyInit, beanClass, constructorArguments); }
/** * Loads all the beans definitions from the given resources. * * <p/> * * throws {@link IllegalStateException} if this container already finish loading. */ public BeanContainer loadFromResources(AppResource[] resources) throws IllegalStateException { log.debug("Load beans from {} resources", resources.length); ensureContainerNotInited(); this.xmlBeanDefinitionLoader.load(resources); return this; }
protected void readBeans(BeanContainer container, Resource resource, XmlReader reader, LoaderContext context) { try { if (!testIfAttributes(container, reader)) { reader.nextToEndElement(); return; readDefinitions(container, importResource, override); } else { readDefinitions(container, importResource, context.defaultOverride); readInit(container, reader, context); return; container.addAliasDefinition(readAlias(container, reader, context)); return; removeBean(container, reader, context); return; BeanDefinitionBase bd = readBean(container, reader, context); if (null != bd) { container.addBeanDefinition(bd); BeanDefinitionBase bd = readBean(container, reader, context, true); if (null != bd) { container.addBeanDefinition(bd, true); processBeanList(container, reader, context); return;
protected ValueDefinition readValueElement(BeanContainer container, XmlReader reader, LoaderContext context) { if (reader.isStartElement(VALUE_ELEMENT)) { return resolvedString(javaTypeAttribute(reader, TYPE_ATTRIBUTE), reader.getElementTextAndEnd()); Class<?> c = forName(className); return resolvedValue(c); } catch (ObjectNotFoundException e) { throw new BeanDefinitionException("Invallid class name '" + className + "', source : " + reader.getSource(), e); return readRefElement(context, reader); return resolvedValue(null); return readArray(container, reader, context); return readList(container, reader, context); return readSet(container, reader, context); return readMap(container, reader, context); return readProps(container, reader, context); BeanDefinition bd = readBean(container, reader, context); if (null != bd) { return new ValueDefinition(bd);
protected BeanDefinitionBase readBean(BeanContainer container, XmlReader reader, LoaderContext context, boolean proxy) { if(!testIfAttributes(container, reader)) { reader.nextToEndElement(); return null; AppClassLoader.addInstrumentClass(beanClassName); log.trace("Resolving bean class '{}'...", beanClassName); bean.setBeanClass(forName(beanClassName)); } catch (NestedClassNotFoundException e) { throw new BeanDefinitionException("Error resolving bean class '" + beanClassName + "' , source : " + reader.getSource(), e); Class<?> type = tryForName(typeClassName); bean.setLazyInit(boolAttribute(reader, LAZY_INIT_ATTRIBUTE, context.defaultLazyInit)); bean.setPrimary(boolAttribute(reader, proxy ? TARGET_PRIMARY_ATTRIBUTE : PRIMARY_ATTRIBUTE, false)); bean.setOverride(override); bean.setOverrideAnnotation(overrideAnnotation); readBeanTypeDef(container, reader, context, bean); continue; readBeanFactoryDef(container, reader, context, bean); continue; readFactoryDefinition(container, reader, context, bean); continue; readConstructorArgument(container, reader, context, bean); continue;
@SuppressWarnings({"rawtypes", "unchecked"}) protected ValueDefinition readMap(BeanContainer container, XmlReader reader, LoaderContext context) { boolean merge = boolAttribute(reader, MERGE_ATTRIBUTE, false); Class<?> keyType = javaTypeAttribute(reader, KEY_TYPE_ATTRIBUTE); Class<?> valueType = javaTypeAttribute(reader, VALUE_TYPE_ATTRIBUTE); Map map = new LinkedHashMap(); final QName elementName = reader.getElementName(); while (reader.nextWhileNotEnd(elementName)) { if (reader.isStartElement()) { if (reader.isStartElement(DESCRIPTION_ELEMENT)) { continue; } if (reader.isStartElement(ENTRY_ELEMENT)) { ValueDefinition key = readEntryKey(container, reader, context); ValueDefinition val = readEntryValue(container, reader, context); map.put(key, val); continue; } throw new BeanDefinitionException("unsupported child element '" + reader.getElementLocalName() + "' in element 'map', source : " + reader.getSource()); } } //Skip END event reader.next(); return new ValueDefinition(map, merge, null, keyType, valueType); }
protected ValueDefinition readEntryValue(BeanContainer container, XmlReader reader, LoaderContext context) { Class<?> valueType = javaTypeAttribute(reader, VALUE_TYPE_ATTRIBUTE); String value = reader.getAttribute(VALUE_ATTRIBUTE); if (!Strings.isEmpty(value)) { return resolvedString(valueType, value); } String valueRef = reader.getAttribute(VALUE_REF_ATTRIBUTE); if (!Strings.isEmpty(valueRef)) { return beanReference(context, valueRef); } while (reader.nextWhileNotEnd(ENTRY_ELEMENT)) { if (reader.isStartElement()) { if (reader.isStartElement(DESCRIPTION_ELEMENT)) { continue; } if (reader.isStartElement()) { return readValueElement(container, reader, context); } throw new BeanDefinitionException("unsupported child element '" + reader.getElementLocalName() + "' in element 'entry', source : " + reader.getSource()); } } return null; }
protected BeanDefinitionBase readMapBean(BeanContainer container, XmlReader reader, LoaderContext context) { BeanDefinitionBase bean = new BeanDefinitionBase(reader.getSource()); readValuedBeanAttributes(container, reader, context, bean); bean.setBeanClass(classAttribute(reader, MAP_CLASS_ATTRIBUTE, false)); bean.setValueDefinition(readMap(container, reader, context)); bean.getValueDefinition().setDefinedType(bean.getBeanClass()); if (null == bean.getBeanClass()) { bean.setBeanClass(LinkedHashMap.class); } return bean; }
protected BeanDefinitionBase readListBean(BeanContainer container, XmlReader reader, LoaderContext context) { BeanDefinitionBase bean = new BeanDefinitionBase(reader.getSource()); readValuedBeanAttributes(container, reader, context, bean); bean.setBeanClass(classAttribute(reader, LIST_CLASS_ATTRIBUTE, false)); bean.setValueDefinition(readList(container, reader, context)); bean.getValueDefinition().setDefinedType(bean.getBeanClass()); if (null == bean.getBeanClass()) { bean.setBeanClass(ArrayList.class); } return bean; }
protected BeanDefinitionBase readSetBean(BeanContainer container, XmlReader reader, LoaderContext context) { BeanDefinitionBase bean = new BeanDefinitionBase(reader.getSource()); readValuedBeanAttributes(container, reader, context, bean); bean.setBeanClass(classAttribute(reader, SET_CLASS_ATTRIBUTE, false)); bean.setValueDefinition(readSet(container, reader, context)); bean.getValueDefinition().setDefinedType(bean.getBeanClass()); if (null == bean.getBeanClass()) { bean.setBeanClass(LinkedHashSet.class); } return bean; }
protected void processBeanList(BeanContainer container, XmlReader reader, LoaderContext context) { Object source = reader.getSource(); Class<?> beanType = classAttribute(reader, TYPE_ATTRIBUTE, true); String qualifier = reader.getAttribute(QUALIFIER_ATTRIBUTE); boolean override = reader.getBooleanAttribute(OVERRIDE_ATTRIBUTE, context.defaultOverride); List<ValueDefinition> values = new ArrayList<ValueDefinition>(); final QName elementName = reader.getElementName(); while (reader.nextWhileNotEnd(elementName)) { if (reader.isStartElement()) { if (reader.isStartElement(DESCRIPTION_ELEMENT)) { continue; } if (reader.isStartElement(REF_ELEMENT)) { values.add(readRefElement(context, reader)); continue; } if (reader.isStartElement(BEAN_ELEMENT)) { BeanDefinition bd = readBean(container, reader, context); if (null != bd) { values.add(new ValueDefinition(bd)); } continue; } throw new BeanDefinitionException("The value element '" + reader.getElementLocalName() + "' not supported in 'bean-list' element, source : " + reader.getSource()); } } container.addBeanList(new BeanListDefinition(source, beanType, qualifier, override, values)); }
protected ValueDefinition tryCreateBeanReference(LoaderContext context, XmlReader reader, String refId, String refType, String refName) { if (!Strings.isEmpty(refId)) { return beanReference(context, refId); } if (!Strings.isAllEmpty(refType, refName)) { if (Strings.isEmpty(refType)) { throw new BeanDefinitionException("'The referenced bean type must not be empty, check :" + reader.getCurrentLocation()); } try { Class<?> c = forName(refType); return beanReference(context, c, refName); } catch (NestedClassNotFoundException e) { throw new BeanDefinitionException("Invalid class name '" + refType + "', source : " + reader.getSource(), e); } } return null; }
protected ValueDefinition readEntryKey(BeanContainer container, XmlReader reader, LoaderContext context) { String key = reader.getAttribute(KEY_ATTRIBUTE); String keyRef = reader.getAttribute(KEY_REF_ATTRIBUTE); if (Strings.isAllEmpty(key, keyRef)) { throw new BeanDefinitionException("'key' or 'key-ref' must be defined in element '" + reader.getElementLocalName() + "', source : " + reader.getSource()); } if (!Strings.isEmpty(key)) { return resolvedString(key); } else { return beanReference(context, keyRef); } }
IfAttr profile = readIfAttr(e, IF_PROFILE); if(null != profile && !profile.test(Strings.equalsIgnoreCase(config.getProfile(), profile.value))) { return false; IfAttr property = readIfAttr(e, IF_PROPERTY); if(null != property && !property.test(Converts.toBoolean(config.getProperty(property.value)))) { return false; IfAttr className = readIfAttr(e, IF_CLASS_PRESENT); if(null != className && !className.test(Classes.isPresent(className.value))) { return false; IfAttr exprText = readIfAttr(e, IF_EXPR); if(null == exprText) { exprText = readIfAttr(e, IF); if(!exprText.test(EL.test(expr.getValue(newExprVars(config)), true))) { return false; IfAttr isServletEnvironment = readIfAttr(e, IF_SERVLET_ENVIRONMENT); if(null != isServletEnvironment) { if(!isServletEnvironment.test(container.getAppContext().isServletEnvironment())) {
protected void removeBean(BeanContainer container, XmlReader reader, LoaderContext context) { if(!testIfAttributes(container, reader)) { reader.nextToEndElement(); return; bds.remove(forName(typeName), beanName); return; }else if(!Strings.isEmpty(className)) { bds.remove(forName(typeName), forName(className)); return; }else if(null != primary && primary) {
public BeanContainer(AppConfig config){ this.config = config; this.beanConfigurator = new BeanConfigurator(config); this.placeholderResolver = config.getPlaceholderResolver(); this.annotationBeanDefinitionLoader = new AnnotationBeanDefinitionLoader(); this.xmlBeanDefinitionLoader = new XmlBeanDefinitionLoader(this); }
protected ValueDefinition readProps(BeanContainer container, XmlReader reader, LoaderContext context) { boolean merge = boolAttribute(reader, MERGE_ATTRIBUTE, false); Properties props = new Properties(); while (reader.nextWhileNotEnd(PROPS_ELEMENT)) { if (reader.isStartElement()) { if (reader.isStartElement(PROP_ELEMENT)) { props.put(reader.getRequiredAttribute(KEY_ATTRIBUTE), reader.getElementTextAndEnd()); } else { throw new BeanDefinitionException("unsupported child element '" + reader.getElementLocalName() + "' in 'props' element, source : " + reader.getSource()); } } } return new ValueDefinition(props, merge); }
protected AliasDefinition readAlias(BeanContainer container, XmlReader reader, LoaderContext context) { String alias = reader.getRequiredAttribute(ALIAS_ATTRIBUTE); String id = reader.getAttribute(ID_ATTRIBUTE); if (!Strings.isEmpty(id)) { return new AliasDefinition(reader.getSource(), alias, id); } return new AliasDefinition(reader.getSource(), alias, forName(reader.getRequiredAttribute(TYPE_ATTRIBUTE)), reader.getRequiredAttribute(NAME_ATTRIBUTE) ); }
Expression expr = SPEL.createExpression(parseContext, exprText); if(not == EL.test(expr.getValue(newExprVars(config)), true)) { return not;