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

📄 engineutils.java

📁 Bean Scripting Framework (BSF)为在java应用中使用脚本语言
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2004,2004 The Apache Software Foundation. *  * 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 org.apache.bsf.util;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import org.apache.bsf.BSFEngine;import org.apache.bsf.BSFException;import org.apache.bsf.BSFManager;/** * This class contains utilities that language integrators can use * when implementing the BSFEngine interface. * * @author   Sanjiva Weerawarana * @author   Sam Ruby * @author   Rony G. Flatscher (added addEventListenerReturningEventInfos) */public class EngineUtils {    // the BSF class loader that knows how to load from the a specific    // temp directory    static BSFClassLoader bsfCL;    // ---rgf, 2003-02-13, determine whether changing accessibility of Methods is possible    static boolean bMethodHasSetAccessible=false;    static {        Class mc=Method.class;            // get the "Method" class object        Class arg[]={boolean.class};      // define an array with the primitive "boolean" pseudo class object        try {            mc.getMethod("setAccessible", arg ); // is this method available?            bMethodHasSetAccessible=true; // no exception, hence method exists        }        catch (Exception e)        {            bMethodHasSetAccessible=false;// exception occurred, hence method does not exist        }    }    /**     * Add a script as a listener to some event coming out of an object. The     * first two args identify the src of the event and the event set     * and the rest identify the script which should be run when the event     * fires.     *     * @param bean         event source     * @param eventSetName name of event set from event src to bind to     * @param filter       filter for events     * @param engine       BSFEngine which can run this script     * @param manager      BSFManager of the above engine     * @param source       (context info) the source of this expression     *                                    (e.g., filename)     * @param lineNo       (context info) the line number in source for expr     * @param columnNo     (context info) the column number in source for expr     * @param script       the script to execute when the event occurs     *     * @exception BSFException if anything goes wrong while running the script     */    public static void addEventListener (Object bean, String eventSetName,                                         String filter, BSFEngine engine,                                         BSFManager manager, String source,                                         int lineNo, int columnNo,                                         Object script) throws BSFException {        BSFEventProcessor ep = new BSFEventProcessor (engine, manager, filter,                                                      source, lineNo, columnNo,                                                      script);        try {            ReflectionUtils.addEventListener (bean, eventSetName, ep);        } catch (Exception e) {            e.printStackTrace ();            throw new BSFException (BSFException.REASON_OTHER_ERROR,                                    "ouch while adding event listener: "                                    + e, e);        }    }    /**     * Add a script as a listener to some event coming out of an object. The     * first two args identify the src of the event and the event set     * and the rest identify the script which should be run when the event     * fires. The processing will use the engine's apply() method.     *     * @param bean         event source     * @param eventSetName name of event set from event src to bind to     * @param filter       filter for events     * @param engine       BSFEngine which can run this script     * @param manager      BSFManager of the above engine     * @param source       (context info) the source of this expression (e.g., filename)     * @param lineNo       (context info) the line number in source for expr     * @param columnNo     (context info) the column number in source for expr     * @param script       the script to execute when the event occurs     * @param dataFromScriptingEngine     *                     this contains any object supplied by the scripting engine and gets sent     *                     back with the supplied script, if the event occurs.     *                     This could be used e.g. for indicating to the scripting engine which     *                     scripting engine object/routine/function/procedure     *                     should be ultimately informed of the event occurrence.     *     * @exception BSFException if anything goes wrong while running the script     */    public static void addEventListenerReturningEventInfos ( Object bean,                               String eventSetName,                               String filter,                               BSFEngine engine,                               BSFManager manager,                               String source,                               int lineNo,                               int columnNo,                               Object script,                               Object dataFromScriptingEngine                               ) throws BSFException    {        BSFEventProcessorReturningEventInfos ep =        new BSFEventProcessorReturningEventInfos (engine,                                                  manager,                                                  filter,                                                  source,                                                  lineNo,                                                  columnNo,                                                  script,                                                  dataFromScriptingEngine                                                  );        try {            ReflectionUtils.addEventListener (bean, eventSetName, ep);        } catch (Exception e) {            e.printStackTrace ();            throw new BSFException (BSFException.REASON_OTHER_ERROR,                                    "ouch while adding event listener: "                                    + e, e);        }    }    /**     * Finds and invokes a method with the given signature on the given     * bean. The signature of the method that's invoked is first taken     * as the types of the args, but if that fails, this tries to convert     * any primitive wrapper type args to their primitive counterparts     * to see whether a method exists that way. If it does, done.     *     * @param bean       the object on which to invoke the method     * @param methodName name of the method     * @param args       arguments to be given to the method     *     * @return the result of invoking the method, if any     *     * @exception BSFException if something goes wrong     */    public static Object callBeanMethod (Object bean, String methodName,                                         Object[] args) throws BSFException {        Class[] argTypes = null;        // determine arg types. note that a null argtype        // matches any object type        if (args != null) {            argTypes = new Class[args.length];            for (int i = 0; i < args.length; i++) {                argTypes[i] = (args[i] == null) ? null : args[i].getClass ();            }        }        // we want to allow a static call to occur on an object, similar        // to what Java allows. So isStaticOnly is set to false.        boolean isStaticOnly = false;        Class beanClass = (bean instanceof Class) ? (Class)bean :                                                    bean.getClass ();        // now try to call method with the right signature  	try {  	  Method m;  	  try {  	m = MethodUtils.getMethod (beanClass, methodName, argTypes,

⌨️ 快捷键说明

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