protected Class<?> resolveBeanType(Class<?> cls, Bean a) { if(!a.type().equals(void.class)){ if(a.type().isAssignableFrom(cls)){ throw new BeanDefinitionException(a.type() + " is not assignable from " + cls.getName() + ", source:"+cls.getName()); } return a.type(); }else if(cls.getInterfaces().length == 1){ return cls.getInterfaces()[0]; } return cls; }
protected void readBeanFactoryDef(BeanContainer container, XmlReader reader, LoaderContext context, BeanDefinitionBase bean) { if (!(FactoryBean.class.isAssignableFrom(bean.getBeanClass()))) { throw new BeanDefinitionException("Bean [" + bean + "] must be implements FactoryBean"); } String targetTypeName = reader.getRequiredAttribute(TARGET_TYPE_ATTRIBUTE); if (Strings.isEmpty(targetTypeName)) { throw new BeanDefinitionException("Attribute '" + TARGET_TYPE_ATTRIBUTE + "' of element '" + REGISTER_BEAN_FACTORY_ELEMENT + "' must not be empty, source : " + reader.getSource()); } Class<?> targetType = tryForName(targetTypeName); if (null == targetType) { throw new BeanDefinitionException("Target type '" + targetTypeName + "' not found, source : " + reader.getSource()); } bean.addFactoryBeanDef(new FactoryBeanDefinitionBase(targetType)); }
protected void addAliasDefinition(AliasDefinition ad){ if(!Strings.isEmpty(ad.getId())){ AliasDefinition existsAliasDefinition = bds.aliasDefinitions.get(ad.getAlias()); if(null != existsAliasDefinition){ throw new BeanDefinitionException("Found duplicated bean alias '" + ad.getAlias() + "' in resource : " + ad.getSource() + ", " + ad.getSource()); } bds.aliasDefinitions.put(ad.getAlias(),ad); }else{ String key = ad.getType().getName() + "$" + ad.getAlias(); AliasDefinition existsAliasDefinition = bds.aliasDefinitions.get(key); if(null != existsAliasDefinition){ throw new BeanDefinitionException("Found duplicated bean alias '" + key + "' in resource : " + ad.getSource() + ", " + ad.getSource()); } bds.aliasDefinitions.put(key,ad); } }
protected Object doGetBeanReferenceInstance(BeanReference br){ BeanDefinitionBase referenced = findBeanDefinition(br); if(referenced == null){ throw new BeanDefinitionException("The referenced bean not exists : " + br); } br.setTargetBeanDefinition(referenced); Object bean = doGetBean(referenced); if(null == bean) { throw new BeanCreationException("The referenced bean '" + referenced + "' not exists!"); } return bean; }
protected Class<?> javaTypeAttribute(XmlReader reader, String name) { String className = reader.getAttribute(name); if (Strings.isEmpty(className)) { return null; } try { return Classes.forName(className); } catch (ObjectNotFoundException e) { throw new BeanDefinitionException("invalid java type '" + className + "', must be a fully qualified class name, source : " + reader.getSource()); } }
@Override public <T> void addBean(String id, boolean lazyInit, Class<? extends T> beanClass, Object... constructorArgs) throws BeanException { Args.notNull(beanClass); Args.notEmpty(id); if(tryGetBean(id) != null){ throw new BeanDefinitionException("the to be added bean '" + id + "' aleady exists"); } this.beanContainer.addBean(id, lazyInit, beanClass, constructorArgs); }
protected void addBeanList(BeanListDefinition bld){ String key = bld.getType().getName(); if(!Strings.isEmpty(bld.getQualifier())){ key = key + "$" + bld.getQualifier(); } if(!bld.isOverride()){ BeanListDefinition exists = beanListDefinitions.get(key); if(null != exists){ throw new BeanDefinitionException("Found duplicated bean list of type '" + bld.getType().getName() + "', qualifier '" + bld.getQualifier() + "' in " + exists.getSource() + ", " + bld.getSource()); } } beanListDefinitions.put(key, bld); }
@Override public <T> void addBean(Class<? super T> beanType, boolean primary, String name, boolean lazyInit, Class<T> beanClass, Object... constructorArgs) throws BeanException { Args.notNull(beanClass); Args.notNull(beanType); Args.notEmpty(name); /* if(primary && tryGetBean(beanType) != null){ throw new BeanDefinitionException("primary bean for type '" + beanType.getName() + "' aleady exists"); } */ if(tryGetBean(beanType, name) != null){ throw new BeanDefinitionException("the bean '" + name + "' for type '" + beanType.getName() + "' aleady exists"); } this.beanContainer.addBean(beanType, primary, name, lazyInit, beanClass, constructorArgs); }
protected ValueDefinition readRefElement(LoaderContext context, XmlReader reader) { String refId = reader.resolveAttribute(ID_ATTRIBUTE); String refType = reader.resolveAttribute(TYPE_ATTRIBUTE); String refName = reader.resolveAttribute(NAME_ATTRIBUTE); if (Strings.isAllEmpty(refId, refType, refName)) { throw new BeanDefinitionException("Invalid bean reference in '" + reader.getCurrentLocation() + "'"); } if (Strings.isNotEmpty(refName) && Strings.isEmpty(refType)) { throw new BeanDefinitionException("The referenced type must be exists in '" + reader.getCurrentLocation() + "'"); } return tryCreateBeanReference(context, reader, refId, refType, refName); }
protected void parseAdditionalTypeDef(BeanDefinitionBase bd,Class<?> cls,Bean a){ if(a.additionalTypeDef() != null && a.additionalTypeDef().length > 0){ typeEach: for(Class<?> additional : a.additionalTypeDef()){ for(TypeDefinition def : bd.getAdditionalTypeDefs()){ if(def.getType() == additional){ continue typeEach; } } if(!additional.isAssignableFrom(cls)){ throw new BeanDefinitionException(additional.getName() + " is not assignable from " + cls.getName()); } TypeDefinitionBase def = new TypeDefinitionBase(); def.setType(additional); bd.addAdditionalTypeDef(def); } } }
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 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 void readFactoryDefinition(BeanContainer container, XmlReader reader, LoaderContext context, BeanDefinitionBase bd) { FactoryDefinition fd = new FactoryDefinition(); String className = reader.getRequiredAttribute(CLASS_ATTRIBUTE); if(!Strings.isEmpty(className)) { Class<?> cls = tryForName(className); if(null == cls) { throw new BeanDefinitionException("Invalid factory class name '" + className + "' in " + bd); } readMethodDefinition(reader, context, bd, cls, fd, false); } bd.setFactoryDefinition(fd); }
protected Class<?> classAttribute(XmlReader reader, String name, boolean required) { String value = required ? reader.getRequiredAttribute(name) : reader.getAttribute(name); if (Strings.isEmpty(value)) { return null; } Class<?> clazz = tryForName(value); if (null == clazz) { throw new BeanDefinitionException("invalid class name '" + value + "' in source : " + reader.getSource() + ", line number : " + reader.getLineNumber()); } return clazz; }
protected ValueDefinition readArray(BeanContainer container, XmlReader reader, LoaderContext context) { boolean merge = boolAttribute(reader, MERGE_ATTRIBUTE, false); Class<?> valueType = javaTypeAttribute(reader, VALUE_TYPE_ATTRIBUTE); List<ValueDefinition> values = new ArrayList<>(); while (reader.nextWhileNotEnd(ARRAY_ELEMENT)) { if (reader.isStartElement()) { if (reader.isStartElement(DESCRIPTION_ELEMENT)) { continue; } if (reader.isStartElement()) { ValueDefinition vd = readValueElement(container, reader, context); if (null != vd) { values.add(vd); } continue; } throw new BeanDefinitionException("unsupported child element '" + reader.getElementLocalName() + "' in element 'array', source : " + reader.getSource()); } } return new ValueDefinition(values.toArray(), merge, null, null, valueType); }
protected void readBeanTypeDef(BeanContainer container, XmlReader reader, LoaderContext context, BeanDefinitionBase bean) { TypeDefinitionBase def = new TypeDefinitionBase(); String typeClassName = reader.getRequiredAttribute(TYPE_ATTRIBUTE); Class<?> typeClass = tryForName(typeClassName); if (null == typeClass) { throw new BeanDefinitionException("Class '" + typeClassName + "' not found, check source : " + bean.getSource()); } if (!typeClass.isAssignableFrom(bean.getBeanClass())) { throw new BeanDefinitionException("bean's type '" + typeClassName + "' must be assignable from bean's class '" + bean.getBeanClass().getName() + "', source : " + reader.getSource()); } def.setType(typeClass); def.setName(reader.getAttribute(NAME_ATTRIBUTE)); def.setPrimary(reader.getBooleanAttribute(PRIMARY_ATTRIBUTE, false)); def.setOverride(reader.getBooleanAttribute(OVERRIDE_ATTRIBUTE, context.defaultOverride)); bean.addAdditionalTypeDef(def); }
protected ValueDefinition readValue(BeanContainer container, XmlReader reader, LoaderContext context, String elementName) { String value = reader.getAttributeOrNull(VALUE_ATTRIBUTE); if (null != value) { return resolvedString(value); } ValueDefinition br = tryReadRefAttributes(context, reader); if (null != br) { return br; } while (reader.nextWhileNotEnd(elementName)) { 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 '" + elementName + "', source : " + reader.getSource()); } } throw new BeanDefinitionException("No value definition at line " + reader.getLineNumber() + ", element '" + elementName + "', in xml '" + reader.getSource() + "'"); }
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); } }
protected void readProperty(BeanContainer container, XmlReader reader, LoaderContext context, BeanDefinitionBase bean) { if(!testIfAttributes(container, reader)) { reader.nextToEndElement(); return; } PropertyDefinition prop = new PropertyDefinition(); prop.setName(reader.getRequiredAttribute(NAME_ATTRIBUTE)); prop.setDefaultValue(reader.getAttribute(DEFAULT_VALUE_ATTRIBUTE)); prop.setValueDefinition(readValue(container, reader, context, PROPERTY_ELEMENT)); prop.setProperty(bean.getBeanClassType().getProperty(prop.getName())); if (null == prop.getProperty()) { throw new BeanDefinitionException("property '" + prop.getName() + "' not found in bean class '" + bean.getBeanClass().getName() + ", source : " + reader.getSource()); } if (null == prop.getValueDefinition()) { throw new BeanDefinitionException("property '" + prop.getName() + "' must define a value, check source : " + bean.getSource()); } bean.addProperty(prop); }
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; }