⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eventhandlerutil.java

📁 velocity官方工具包 包括各种JAR包 示例 文档等
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.apache.velocity.app.event;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.    
 */

import java.util.Iterator;

import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.util.ExceptionUtils;
import org.apache.velocity.util.introspection.Info;


/**
 * Calls on request all registered event handlers for a particular event. Each
 * method accepts two event cartridges (typically one from the application and
 * one from the context). All appropriate event handlers are executed in order
 * until a stopping condition is met. See the docs for the individual methods to
 * see what the stopping condition is for that method.
 *
 * @author <a href="mailto:wglass@wglass@forio.com">Will Glass-Husain </a>
 * @version $Id: EventHandlerUtil.java 470256 2006-11-02 07:20:36Z wglass $
 */
public class EventHandlerUtil {
    
    
    /**
     * Called before a reference is inserted. All event handlers are called in
     * sequence. The default implementation inserts the reference as is.
     *
     * @param reference reference from template about to be inserted
     * @param value value about to be inserted (after toString() )
     * @param rsvc current instance of RuntimeServices
     * @param context The internal context adapter.
     * @return Object on which toString() should be called for output.
     */
    public static Object referenceInsert(RuntimeServices rsvc,
            InternalContextAdapter context, String reference, Object value)
    {
        // app level cartridges have already been initialized
        EventCartridge ev1 = rsvc.getApplicationEventCartridge();
        Iterator applicationEventHandlerIterator = 
            (ev1 == null) ? null: ev1.getReferenceInsertionEventHandlers();              
        
        EventCartridge ev2 = context.getEventCartridge();
        initializeEventCartridge(rsvc, ev2);
        Iterator contextEventHandlerIterator = 
            (ev2 == null) ? null: ev2.getReferenceInsertionEventHandlers();              
        
        try 
        {
            EventHandlerMethodExecutor methodExecutor = 
                new ReferenceInsertionEventHandler.referenceInsertExecutor(context, reference, value);

            callEventHandlers(
                    applicationEventHandlerIterator, 
                    contextEventHandlerIterator, methodExecutor);
            
            return methodExecutor.getReturnValue();   
        }
        catch (RuntimeException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw ExceptionUtils.createRuntimeException("Exception in event handler.",e);
        }
    }
    
    /**
     * Called when a null is evaluated during a #set. All event handlers are
     * called in sequence until a false is returned. The default implementation
     * always returns true.
     *
     * @param lhs Left hand side of the expression.
     * @param rhs Right hand side of the expression.
     * @param rsvc current instance of RuntimeServices
     * @param context The internal context adapter.
     * @return true if to be logged, false otherwise
     */
    public static boolean shouldLogOnNullSet(RuntimeServices rsvc,
            InternalContextAdapter context, String lhs, String rhs)
    {
        // app level cartridges have already been initialized
        EventCartridge ev1 = rsvc.getApplicationEventCartridge();
        Iterator applicationEventHandlerIterator = 
            (ev1 == null) ? null: ev1.getNullSetEventHandlers();              
        
        EventCartridge ev2 = context.getEventCartridge();
        initializeEventCartridge(rsvc, ev2);
        Iterator contextEventHandlerIterator = 
            (ev2 == null) ? null: ev2.getNullSetEventHandlers();              
                
        try 
        {
            EventHandlerMethodExecutor methodExecutor = 
                new NullSetEventHandler.ShouldLogOnNullSetExecutor(context, lhs, rhs);

            callEventHandlers(
                    applicationEventHandlerIterator, 
                    contextEventHandlerIterator, methodExecutor);
            
            return ((Boolean) methodExecutor.getReturnValue()).booleanValue();    
        }
        catch (RuntimeException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw ExceptionUtils.createRuntimeException("Exception in event handler.",e);
        }
    }
    
    /**
     * Called when a method exception is generated during Velocity merge. Only
     * the first valid event handler in the sequence is called. The default
     * implementation simply rethrows the exception.
     *
     * @param claz
     *            Class that is causing the exception
     * @param method
     *            method called that causes the exception
     * @param e
     *            Exception thrown by the method
     * @param rsvc current instance of RuntimeServices
     * @param context The internal context adapter.
     * @return Object to return as method result
     * @throws Exception
     *             to be wrapped and propogated to app
     */
    public static Object methodException(RuntimeServices rsvc,
            InternalContextAdapter context, Class claz, String method,
            Exception e) throws Exception 
    {
        // app level cartridges have already been initialized
        EventCartridge ev1 = rsvc.getApplicationEventCartridge();
        Iterator applicationEventHandlerIterator = 
            (ev1 == null) ? null: ev1.getMethodExceptionEventHandlers();              
        
        EventCartridge ev2 = context.getEventCartridge();
        initializeEventCartridge(rsvc, ev2);
        Iterator contextEventHandlerIterator = 
            (ev2 == null) ? null: ev2.getMethodExceptionEventHandlers();              
        
        EventHandlerMethodExecutor methodExecutor = 
            new MethodExceptionEventHandler.MethodExceptionExecutor(context, claz, method, e);
        
        if ( ((applicationEventHandlerIterator == null) || !applicationEventHandlerIterator.hasNext()) &&
                ((contextEventHandlerIterator == null) || !contextEventHandlerIterator.hasNext()) )
        {
            throw e;
        }
            
        callEventHandlers(
                applicationEventHandlerIterator, 
                contextEventHandlerIterator, methodExecutor);
        
        return methodExecutor.getReturnValue();
    }
    
    /**
     * Called when an include-type directive is encountered (#include or
     * #parse). All the registered event handlers are called unless null is
     * returned. The default implementation always processes the included
     * resource.
     *
     * @param includeResourcePath
     *            the path as given in the include directive.
     * @param currentResourcePath
     *            the path of the currently rendering template that includes the
     *            include directive.
     * @param directiveName
     *            name of the directive used to include the resource. (With the
     *            standard directives this is either "parse" or "include").
     * @param rsvc current instance of RuntimeServices
     * @param context The internal context adapter.
     *
     * @return a new resource path for the directive, or null to block the
     *         include from occurring.
     */
    public static String includeEvent(RuntimeServices rsvc,
            InternalContextAdapter context, String includeResourcePath,
            String currentResourcePath, String directiveName)
    {
        // app level cartridges have already been initialized
        EventCartridge ev1 = rsvc.getApplicationEventCartridge();
        Iterator applicationEventHandlerIterator = 
            (ev1 == null) ? null: ev1.getIncludeEventHandlers();              
        
        EventCartridge ev2 = context.getEventCartridge();
        initializeEventCartridge(rsvc, ev2);
        Iterator contextEventHandlerIterator = 
            (ev2 == null) ? null: ev2.getIncludeEventHandlers();              
        
        try 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -