📄 jsfbeandefinitionparserutils.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.aspectj;import java.lang.reflect.Method;import java.util.Collection;import java.util.List;import java.util.Map;import javax.faces.component.UIComponent;import net.sf.cglib.core.DefaultNamingPolicy;import net.sf.cglib.core.NamingPolicy;import net.sf.cglib.core.Predicate;import net.sf.cglib.proxy.Callback;import net.sf.cglib.proxy.CallbackFilter;import net.sf.cglib.proxy.Enhancer;import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.MethodProxy;import net.sf.cglib.proxy.NoOp;import org.springframework.beans.factory.support.AbstractBeanDefinition;import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;import org.springframework.beans.factory.xml.ParserContext;import org.springframework.util.ClassUtils;import org.w3c.dom.Element;/** * Miscellaneous parser utility methods for the '<code>jsh</code>' * namespace. Mainly for internal use within the framework. * * @author Andreas Kuhrwahl * */public final class JsfBeanDefinitionParserUtils { /** Name policy for cglib created classes. */ static final String JSF_SPRING_MAGIC = "ForJsfSpring"; /** * Special naming policy for classes generated by cglib for JSF-Spring. * * @author Andreas Kuhrwahl * */ private static class JsfSpringNamingPolicy extends DefaultNamingPolicy { /** The singleton instance. */ public static final NamingPolicy INSTANCE = new JsfSpringNamingPolicy(); /** * {@inheritDoc} */ public String getClassName(final String prefix, final String source, final Object key, final Predicate names) { return super.getClassName(prefix, source, key, names) + JSF_SPRING_MAGIC; } } /** * Interface that will be implemented by the UIComponent subclasses * generated by the SubclassCreator. Adds JavaBeans compatible accessors to * the UIComponent class. * * @author Andreas Kuhrwahl * */ public interface UIComponentExtension { /** * JavaBeans compatible accessor for setting attributes. * * @param attributes * attributes to set */ void setAttributes(Map attributes); /** * JavaBeans compatible accessor for setting a List of UIComponent * children. * * @param children * the children */ void setChildren(List children); /** * JavaBeans compatible accessor for setting a Map of UIComponents * interpreted as facets. * * @param facets * the facets */ void setFacets(Map facets); } /** * CGLIB MethodInterceptor to implement methods defined by the * UIComponentExtension interface. * * @author Andreas Kuhrwahl */ private static final class UIComponentInterceptor implements MethodInterceptor { /** The singleton instance. */ public static final UIComponentInterceptor INSTANCE = new UIComponentInterceptor(); /** * Default constructor. */ private UIComponentInterceptor() { super(); } /** * {@inheritDoc} */ public Object intercept(final Object obj, final Method method, final Object[] args, final MethodProxy mp) throws Throwable { if ("setAttributes".equals(method.getName())) { ((UIComponent) obj).getAttributes().putAll((Map) args[0]); } else if ("setChildren".equals(method.getName())) { ((UIComponent) obj).getChildren().addAll((Collection) args[0]); } else if ("setFacets".equals(method.getName())) { ((UIComponent) obj).getFacets().putAll((Map) args[0]); } else { throw new UnsupportedOperationException(method.toString()); } return null; } } /** * The '<code>id</code>' attribute of a bean definition. */ public static final String ID_ATTRIBUTE = BeanDefinitionParserDelegate.ID_ATTRIBUTE; /** * The '<code>for-class</code>' attribute of a * {@link javax.faces.convert.Converter} definition. */ public static final String FOR_CLASS_ATTRIBUTE = "for-class"; /** * The '<code>class</code>' attribute of a bean definition. */ public static final String CLASS_ATTRIBUTE = BeanDefinitionParserDelegate.CLASS_ATTRIBUTE; /** * The '<code>name</code>' attribute of a bean definition. */ public static final String NAME_ATTRIBUTE = BeanDefinitionParserDelegate.NAME_ATTRIBUTE; /** * The '<code>type</code>' attribute of a {@link UIComponent} * definition. */ public static final String TYPE_ATTRIBUTE = "type"; /** * The '<code>component-type</code>' attribute of a * {@link javax.faces.render.Renderer} definition. */ public static final String COMPONENT_TYPE_ATTRIBUTE = "component-type"; /** * Static class. */ private JsfBeanDefinitionParserUtils() { super(); } /** * Generates a subclass of the given ui component class name * <code>originalClassName</code> with a special naming policy via CGLib. * Valid component types are {@link javax.faces.validator.Validator}, * {@link UIComponent} and {@link javax.faces.convert.Converter}. * * @param originalClassName * the original class name of the component * @param targetSuperClass * UIComponent.class, Converter.class or Validator.class * @param parserContext * the {@link ParserContext} * @return the generated subclass with a special unique JSF-Spring name. */ public static Class createComponentClass(final String originalClassName, final Class targetSuperClass, final ParserContext parserContext) { Class componentClass = null; try { Enhancer enhancer = new Enhancer(); enhancer.setCallbackType(NoOp.class); enhancer.setCallbackFilter(new CallbackFilter() { public int accept(final Method method) { return 0; } }); enhancer.setSuperclass(ClassUtils.forName(originalClassName, parserContext.getReaderContext().getReader() .getBeanClassLoader())); enhancer.setNamingPolicy(JsfSpringNamingPolicy.INSTANCE); componentClass = enhancer.createClass(); } catch (Throwable ex) { parserContext.getReaderContext().error( "Could not generate component subclass of " + originalClassName, null); } if (!targetSuperClass.isAssignableFrom(componentClass)) { parserContext.getReaderContext().error( "Generated component class is not of type " + targetSuperClass.getName(), null); componentClass = null; } return componentClass; } /** * Generates a subclass of the given {@link UIComponent} class with name * <code>originalClassName</code> which implements the interface * {@link UIComponentExtension}. * * @param originalClassName * th class name of the component * @param parserContext * the {@link ParserContext} * @return the via CGLib generated extended class */ public static Class createExtendedUIComponentClass( final String originalClassName, final ParserContext parserContext) { Class componentClass = null; try { Enhancer enhancer = new Enhancer(); enhancer.setCallbackTypes(new Class[] { NoOp.class, UIComponentInterceptor.class }); enhancer.setCallbackFilter(new CallbackFilter() { public int accept(final Method method) { int returnCode; if (method.getDeclaringClass() == UIComponentExtension.class) { returnCode = 1; } else { returnCode = 0; } return returnCode; } }); enhancer.setSuperclass(ClassUtils.forName(originalClassName, parserContext.getReaderContext().getReader() .getBeanClassLoader())); enhancer.setInterfaces(new Class[] { UIComponentExtension.class }); enhancer.setNamingPolicy(JsfSpringNamingPolicy.INSTANCE); componentClass = enhancer.createClass(); Enhancer.registerStaticCallbacks(componentClass, new Callback[] { NoOp.INSTANCE, UIComponentInterceptor.INSTANCE }); } catch (Throwable ex) { parserContext.getReaderContext().error( "Could not generate component subclass of " + originalClassName, null); } if (!UIComponent.class.isAssignableFrom(componentClass)) { parserContext.getReaderContext().error( "Generated component class is not of type " + UIComponent.class.getName(), null); componentClass = null; } return componentClass; } /** * Determine the class of the wrapped bean of the given * {@link AbstractBeanDefinition} <code>bd</code>, if the bean definition * has a bean class. * * @param element * the {@link Element} representing the bean definition * @param bd * the bean definition * @param parserContext * the {@link ParserContext} * @return the resolved bean class or <code>null</code> if the bean * defintion has no bean class. */ public static Class resolveBeanClass(final Element element, final AbstractBeanDefinition bd, final ParserContext parserContext) { return de.mindmatters.faces.spring.factory.xml.JsfBeanDefinitionParserUtils .resolveBeanClass(element, bd, parserContext); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -