📄 abstractapplicationpluginbeandefinitionparser.java
字号:
/* * Copyright 2002-2004 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package de.mindmatters.faces.spring.factory.xml;import org.springframework.beans.factory.config.BeanDefinition;import org.springframework.beans.factory.config.BeanDefinitionHolder;import org.springframework.beans.factory.parsing.BeanComponentDefinition;import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;import org.springframework.beans.factory.support.RootBeanDefinition;import org.springframework.beans.factory.xml.BeanDefinitionParser;import org.springframework.beans.factory.xml.ParserContext;import org.springframework.beans.factory.xml.XmlReaderContext;import org.springframework.core.Ordered;import org.w3c.dom.Element;import de.mindmatters.faces.spring.factory.OrderedRootBeanDefinition;/** * {@link org.springframework.beans.factory.xml.BeanDefinitionParser} * implementation for parsing JSF application plugin tags and creating * {@link #pluginClass()} definitions. * * @author Andreas Kuhrwahl * */abstract class AbstractApplicationPluginBeanDefinitionParser implements BeanDefinitionParser { /** * {@inheritDoc} */ public final BeanDefinition parse(final Element element, final ParserContext parserContext) { XmlReaderContext readerContext = parserContext.getReaderContext(); Class pluginClass = pluginClass(); RootBeanDefinition bd = (RootBeanDefinition) parserContext .getDelegate().parseBeanDefinitionElement(element, null, null); Class beanClass = JsfBeanDefinitionParserUtils.resolveBeanClass( element, bd, parserContext); if (pluginClass.isAssignableFrom(beanClass)) { bd.setRole(BeanDefinition.ROLE_SUPPORT); if (bd.getConstructorArgumentValues().isEmpty() && hasPluginConstructor(beanClass, pluginClass)) { Object arg = createConstructorArgumentValue(bd, parserContext); if (bd != null) { bd.getConstructorArgumentValues().addIndexedArgumentValue( 0, arg, pluginClass.getName()); } } int order = Ordered.LOWEST_PRECEDENCE; if (element .hasAttribute(JsfBeanDefinitionParserUtils.ORDER_ATTRIBUTE)) { order = Integer .parseInt(element .getAttribute(JsfBeanDefinitionParserUtils.ORDER_ATTRIBUTE)); } bd = new OrderedRootBeanDefinition(bd, order); BeanDefinitionHolder holder = new BeanDefinitionHolder(bd, BeanDefinitionReaderUtils.generateBeanName(bd, parserContext.getRegistry(), parserContext .isNested())); parserContext.getDelegate().decorateBeanDefinitionIfRequired( element, holder); BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry()); parserContext.getReaderContext().fireComponentRegistered( new BeanComponentDefinition(holder)); } else { readerContext.error("Application-plugin '" + bd.getBeanClassName() + "' is not of type '" + pluginClass.getName() + "'", readerContext.extractSource(element)); } return null; } /** * Checks whether the <code>beanClass</code> has an one-argument * constructor with parameter-type <code>pluginClass</code> or not. * * @param beanClass * the class to check * @param pluginClass * the parameter-type of the argument of the constructor * @return <code>true</code> if <code>beanClass</code> has an * one-argument constructor with parameter-type * <code>pluginClass</code> <code>false</code> otherwise */ private boolean hasPluginConstructor(final Class beanClass, final Class pluginClass) { try { beanClass.getConstructor(new Class[] { pluginClass }); } catch (NoSuchMethodException ex) { return false; } return true; } /** * Creates the argument of the one-argument constructor of the bean defined * by {@link BeanDefinition} <code>bd</code>. * * @param bd * the {@link BeanDefinition} * @param parserContext * the {@link ParserContext} * @return the created argument */ protected abstract Object createConstructorArgumentValue( RootBeanDefinition bd, ParserContext parserContext); /** * Returns the type of the class defined by the {@link BeanDefinition} this * parser parses. * * @return the type of the plugin */ protected abstract Class pluginClass(); /** * {@inheritDoc} */ protected final boolean shouldGenerateId() { return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -