📄 managedbeandefinitiondocumentreader.java
字号:
private XmlReaderContext readerContext; /** * The {@link ParseState} used within this * {@link BeanDefinitionDocumentReader}. */ private ParseState parseState = new ParseState(); /** * Extract the text value from the given DOM element, ignoring XML comments. * * @param element * the given DOM element * @return the text value of the given DOM element */ protected static String getTextValue(final Element element) { return DomUtils.getTextValue(element).trim(); } /** * Report an error with the given message for the given source element. * * @param message * The message to report * @param source * The Element source of the error to report */ protected final void error(final String message, final Element source) { getReaderContext().error(message, source, this.parseState.snapshot()); } /** * Report an error with the given message for the given source element. * * @param message * The message to report * @param source * The Element source of the error to report * @param cause * The cause of the error if any */ protected final void error(final String message, final Element source, final Throwable cause) { getReaderContext().error(message, source, this.parseState.snapshot(), cause); } /** * {@inheritDoc} */ public final void registerBeanDefinitions(final Document doc, final XmlReaderContext context) { this.readerContext = context; Element root = doc.getDocumentElement(); if (root.getTagName().equals(ROOT_ELEMENT)) { registerManagedBeanDefinitions(doc); } else { createDefaultBeanDefinitionDocumentReader() .registerBeanDefinitions(doc, readerContext); } } /** * Creates a {@link BeanDefinitionDocumentReader} that parses bean * definitions other than managed beans according to the "faces-config" DTD. * * <p> * Default implementation returns a * {@link DefaultBeanDefinitionDocumentReader}. May be overriden in * subclasses. * </p> * * @return the {@link BeanDefinitionDocumentReader} */ protected BeanDefinitionDocumentReader createDefaultBeanDefinitionDocumentReader() { return new DefaultBeanDefinitionDocumentReader(); } /** * Parses bean definitions from the given DOM document, and register them * with the help of the given {@link XmlReaderContext}. * * @param doc * the DOM document */ protected void registerManagedBeanDefinitions(final Document doc) { if (logger.isDebugEnabled()) { logger.debug("Loading bean definitions"); } Element root = doc.getDocumentElement(); NodeList managedBeanElements = root.getElementsByTagName(BEAN_ELEMENT); for (int i = 0; i < managedBeanElements.getLength(); i++) { registerManagedBeanDefinition((Element) managedBeanElements.item(i)); } NodeList messageBeanElements = root .getElementsByTagName(MESSAGE_BUNDLE_ELEMENT); if (messageBeanElements.getLength() != 0) { registerMessageBeanDefinitions(messageBeanElements, root .getElementsByTagName(LOCALE_CONFIG_ELEMENT)); } } /** * Parses an element definition: We know this is a MANAGED-BEAN element. * Bean elements specify their canonical name as managed-bean-name tag and * their aliases as an id attribute (if specified). * * @param element * the DOM element representing a MANAGED-BEAN */ protected void registerManagedBeanDefinition(final Element element) { String alias = element.getAttribute(ID_ATTRIBUTE); String beanName = getTextValue((Element) element.getElementsByTagName( NAME_ELEMENT).item(0)); if (logger.isDebugEnabled()) { logger.debug("Registering bean definition with name '" + beanName + "'"); } BeanDefinitionHolder bd = new BeanDefinitionHolder( parseManagedBeanDefinition(element, beanName), beanName); BeanDefinitionReaderUtils.registerBeanDefinition(bd, getReaderContext() .getRegistry()); getReaderContext().fireComponentRegistered( new BeanComponentDefinition(bd)); if (StringUtils.hasText(alias)) { getReaderContext().getReader().getBeanFactory().registerAlias( beanName, alias); getReaderContext().fireAliasRegistered(beanName, alias, getReaderContext().extractSource(element)); } } /** * Parses a standard faces managed bean definition. * * @param element * the DOM element representing a MANAGED-BEAN * @param beanName * the name of the bean represented by the delivered * BeanDefinition * @return the created BeanDefinition * @see de.mindmatters.faces.spring.factory.ManagedBeanDefinition */ protected BeanDefinition parseManagedBeanDefinition(final Element element, final String beanName) { String beanClassName = getTextValue((Element) element .getElementsByTagName(CLASS_ELEMENT).item(0)); RootBeanDefinition bd = null; try { this.parseState.push(new BeanEntry(beanName)); bd = ManagedBeanFactory.createManagedBeanDefinition(beanClassName, getReaderContext().getReader().getBeanClassLoader()); bd.setResourceDescription(getReaderContext().getResource() .getDescription()); bd.setScope(createScope(element)); bd.setPropertyValues(parseManagedBeanPropertyDefinitions(element .getElementsByTagName(PROPERTY_ELEMENT), beanName)); ConstructorArgumentValues constructorArgs = parseManagedBeanListEntries( element, beanName); if (constructorArgs == null) { constructorArgs = parseManagedBeanMapEntries(element, beanName); } if (constructorArgs != null) { bd.setConstructorArgumentValues(constructorArgs); } } catch (ClassNotFoundException ex) { error("Bean class [" + beanClassName + "] not found", element, ex); } catch (NoClassDefFoundError err) { error("Class that bean class [" + beanClassName + "] depends on not found", element, err); } catch (Throwable ex) { error("Unexpected failure during bean definition parsing", element, ex); } finally { this.parseState.pop(); } return bd; } /** * Evaluates the scope of the given managed bean definition. * * @param element * the managed bean definition * @return the scope of the bean defined by the given managed bean * definition */ private String createScope(final Element element) { String scope = getTextValue((Element) element.getElementsByTagName( SCOPE_ELEMENT).item(0)); if (FacesWebApplicationContext.SCOPE_APPLICATION.equals(scope)) { scope = SCOPE_SINGLETON; } else if (FacesWebApplicationContext.SCOPE_NONE.equals(scope)) { scope = SCOPE_PROTOTYPE; } return scope; } /** * Parses list-entries subelements of the given MANAGED-BEAN element. * * @param element * the DOM element representing a MANAGED-BEAN element * @param beanName * the name of the bean represented by the delivered * BeanDefinition * @return a PropertyValue representing the defined list * @see PropertyValue */ protected ConstructorArgumentValues parseManagedBeanListEntries( final Element element, final String beanName) { Element listEntry = null; ConstructorArgumentValues constructorArgs = null; final NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { final Node node = nodeList.item(i); if (node instanceof Element) { final Element child = (Element) node; if (child.getTagName().equals(LIST_ENTRIES_ELEMENT)) { listEntry = child; break; } } } if (listEntry != null) { constructorArgs = new ConstructorArgumentValues(); constructorArgs.addGenericArgumentValue(createPropertyListEntries( listEntry, beanName)); } return constructorArgs; } /** * Parses map-entries subelements of the given MANAGED-BEAN element. * * @param element * the DOM element representing a MANAGED-BEAN element * @param beanName * the name of the bean represented by the delivered * BeanDefinition * @return a PropertyValue representing the defined map * @see PropertyValue */ protected ConstructorArgumentValues parseManagedBeanMapEntries( final Element element, final String beanName) { Element mapEntry = null; ConstructorArgumentValues constructorArgs = null; final NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { final Node node = nodeList.item(i); if (node instanceof Element) { final Element child = (Element) node; if (child.getTagName().equals(MAP_ENTRIES_ELEMENT)) { mapEntry = child; break; } } } if (mapEntry != null) { constructorArgs = new ConstructorArgumentValues(); constructorArgs.addGenericArgumentValue(createPropertyMapEntries( mapEntry, beanName)); } return constructorArgs; } /** * Parses managed-property subelements of the given bean element. * * @param propertyElements * the DOM node list representing a the property definitions of a * managed bean definition * @param beanName * the name of the bean represented by the delivered * BeanDefinition * @return MutablePropertyValues representing all properties defined for * this managed bean definition * @see MutablePropertyValues
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -