📄 phaselistenerhelper.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.context.servlet.lifecycle;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;import javax.faces.FacesException;import javax.faces.context.FacesContext;import org.springframework.util.Assert;/** * Utility class for invoking notification methods before and after processing * the standard JSF lifecycle phases via reflection on the current Spring MVC * handler. * * <p> * Suitable for: * <ul> * <li>{@link RestoreViewPhaseListener}</li> * <li>{@link ApplyRequestValuesPhaseListener}</li> * <li>{@link ProcessValidationsPhaseListener}</li> * <li>{@link UpdateModelValuesPhaseListener}</li> * <li>{@link InvokeApplicationPhaseListener}</li> * <li>{@link RenderResponsePhaseListener}</li> * </ul> * </p> * * @author Andreas Kuhrwahl * */public final class PhaseListenerHelper { /** * Type descriptor of a particular phase listener. * * @author Andreas Kuhrwahl * */ private class PhaseListenerTypeDescriptor { /** Cached methods of the given phase listener. */ private final Method beforePhaseMethod, afterPhaseMethod; /** * Creates a descriptor via the given phase listener type. * * @param phaseListenerType * the type of a particular phase listener */ public PhaseListenerTypeDescriptor(final Class phaseListenerType) { Method[] methods = phaseListenerType.getMethods(); Assert.isTrue(methods.length == 2); if (methods[0].getName().startsWith("before")) { this.beforePhaseMethod = methods[0]; this.afterPhaseMethod = methods[1]; } else { this.beforePhaseMethod = methods[1]; this.afterPhaseMethod = methods[0]; } } /** * @return the method to invoke after processing a particular phase */ public Method getAfterPhaseMethod() { return afterPhaseMethod; } /** * @return the method to invoke before processing a particular phase */ public Method getBeforePhaseMethod() { return beforePhaseMethod; } } /** The singleton instance. */ private static final PhaseListenerHelper INSTANCE = new PhaseListenerHelper(); /** Cache for descriptions of listeners. */ private final Map phaseListenerTypeDescs = new HashMap(6); /** * Creates the Helper and initializes the cache. */ private PhaseListenerHelper() { this.phaseListenerTypeDescs .put(RestoreViewPhaseListener.class, new PhaseListenerTypeDescriptor( RestoreViewPhaseListener.class)); this.phaseListenerTypeDescs.put(ApplyRequestValuesPhaseListener.class, new PhaseListenerTypeDescriptor( ApplyRequestValuesPhaseListener.class)); this.phaseListenerTypeDescs.put(ProcessValidationsPhaseListener.class, new PhaseListenerTypeDescriptor( ProcessValidationsPhaseListener.class)); this.phaseListenerTypeDescs.put(UpdateModelValuesPhaseListener.class, new PhaseListenerTypeDescriptor( UpdateModelValuesPhaseListener.class)); this.phaseListenerTypeDescs.put(InvokeApplicationPhaseListener.class, new PhaseListenerTypeDescriptor( InvokeApplicationPhaseListener.class)); this.phaseListenerTypeDescs.put(RenderResponsePhaseListener.class, new PhaseListenerTypeDescriptor( RenderResponsePhaseListener.class)); } /** * @return The singleton */ public static PhaseListenerHelper getInstance() { return INSTANCE; } /** * Invokes the appropriate template method of the executing phase. * * @param context * {@link FacesContext} for the current request * @param handler * the current handler * @param phaseListenerType * the type of the phase listener to invoke * @param phaseMethod * the method of the phase listener to invoke */ private void invokeTemplateMethod(final FacesContext context, final Object handler, final Class phaseListenerType, final Method phaseMethod) { Assert.notNull(phaseMethod); try { if (phaseListenerType.isAssignableFrom(handler.getClass())) { phaseMethod.invoke(handler, new Object[] { context }); } else { Method method = handler.getClass().getMethod( phaseMethod.getName(), new Class[] { FacesContext.class }); method.invoke(handler, new Object[] { context }); } } catch (NoSuchMethodException ex) { return; } catch (Exception ex) { throw new FacesException(ex); } } /** * Handle a notification implementation on the current handler (if * necessary) that the processing for a particular phase of the request * processing lifecycle is about to begin. * * @param context * {@link FacesContext} for the current request * @param handler * the current handler * @param phaseListenerType * the type of the phase listener to invoke */ public void beforePhase(final FacesContext context, final Object handler, final Class phaseListenerType) { Assert.notNull(context); Assert.notNull(phaseListenerType); if (handler != null) { PhaseListenerTypeDescriptor desc = (PhaseListenerTypeDescriptor) this.phaseListenerTypeDescs .get(phaseListenerType); if (desc != null) { invokeTemplateMethod(context, handler, phaseListenerType, desc .getBeforePhaseMethod()); } } } /** * Handle a notification implementation on the current handler (if * necessary) that the processing for a particular phase of the request * processing lifecycle is about to end. * * @param context * {@link FacesContext} for the current request * @param handler * the current handler * @param phaseListenerType * the type of the phase listener to invoke */ public void afterPhase(final FacesContext context, final Object handler, final Class phaseListenerType) { Assert.notNull(context); Assert.notNull(phaseListenerType); if (handler != null) { PhaseListenerTypeDescriptor desc = (PhaseListenerTypeDescriptor) this.phaseListenerTypeDescs .get(phaseListenerType); if (desc != null) { invokeTemplateMethod(context, handler, phaseListenerType, desc .getAfterPhaseMethod()); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -