bsfmanager.java

来自「Bean Scripting Framework (BSF)为在java应用中使」· Java 代码 · 共 911 行 · 第 1/3 页

JAVA
911
字号
        final int lineNof = lineNo, columnNof = columnNo;        final Object exprf = expr;        final CodeBuffer cbf = cb;        try {            AccessController.doPrivileged(new PrivilegedExceptionAction() {                    public Object run() throws Exception {                        e.compileExpr(sourcef, lineNof, columnNof, exprf, cbf);                        return null;                    }                });        } catch (PrivilegedActionException prive) {        	logger.error("Exception :", prive);            throw (BSFException) prive.getException();        }    }    /**     * Compile the given script of the given language into the given     * <tt>CodeBuffer</tt>.     *     * @param lang     language identifier     * @param source   (context info) the source of this script     (e.g., filename)     * @param lineNo   (context info) the line number in source for script     * @param columnNo (context info) the column number in source for script     * @param script   the script to compile     * @param cb       code buffer to compile into     *     * @exception BSFException if any error while compiling the script     */    public void compileScript(String lang,                              String source,                              int lineNo,                              int columnNo,                              Object script,                              CodeBuffer cb)        throws BSFException {    	logger.debug("BSFManager:compileScript");        final BSFEngine e = loadScriptingEngine(lang);        final String sourcef = source;        final int lineNof = lineNo, columnNof = columnNo;        final Object scriptf = script;        final CodeBuffer cbf = cb;        try {            AccessController.doPrivileged(new PrivilegedExceptionAction() {                    public Object run() throws Exception {                        e.compileScript(sourcef, lineNof, columnNof,                                        scriptf, cbf);                        return null;                    }                });        } catch (PrivilegedActionException prive) {        	logger.error("Exception :", prive);            throw (BSFException) prive.getException();        }    }    /**     * Declare a bean. The difference between declaring and registering     * is that engines are spsed to make declared beans "pre-available"     * in the scripts as far as possible. That is, if a script author     * needs a registered bean, he needs to look it up in some way. However     * if he needs a declared bean, the language has the responsibility to     * make those beans avaialable "automatically."     * <p>     * When a bean is declared it is automatically registered as well     * so that any declared bean can be gotton to by looking it up as well.     * <p>     * If any of the languages that are already running in this manager     * says they don't like this (by throwing an exception) then this     * method will simply quit with that exception. That is, any engines     * that come after than in the engine enumeration will not even be     * told about this new bean.     * <p>     * So, in general its best to declare beans before the manager has     * been asked to load any engines because then the user can be informed     * when an engine rejects it. Also, its much more likely that an engine     * can declare a bean at start time than it can at any time.     *     * @param beanName name to declare bean as     * @param bean     the bean that's being declared     * @param type     the type to represent the bean as     *     * @exception BSFException if any of the languages that are already     *            running decides to throw an exception when asked to     *            declare this bean.     */    public void declareBean(String beanName, Object bean, Class type)        throws BSFException {    	logger.debug("BSFManager:declareBean");        registerBean(beanName, bean);        BSFDeclaredBean tempBean = new BSFDeclaredBean(beanName, bean, type);        declaredBeans.addElement(tempBean);        Enumeration enginesEnum = loadedEngines.elements();        BSFEngine engine;        while (enginesEnum.hasMoreElements()) {            engine = (BSFEngine) enginesEnum.nextElement();            engine.declareBean(tempBean);        }    }    /**     * Evaluate the given expression of the given language and return the     * resulting value.     *     * @param lang language identifier     * @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 expr the expression to evaluate     *     * @exception BSFException if anything goes wrong while running the script     */    public Object eval(String lang,                       String source,                       int lineNo,                       int columnNo,                       Object expr)        throws BSFException {    	logger.debug("BSFManager:eval");        final BSFEngine e = loadScriptingEngine(lang);        final String sourcef = source;        final int lineNof = lineNo, columnNof = columnNo;        final Object exprf = expr;        Object result = null;        try {            final Object resultf =                AccessController.doPrivileged(new PrivilegedExceptionAction() {                        public Object run() throws Exception {                            return e.eval(sourcef, lineNof, columnNof, exprf);                        }                    });            result = resultf;        } catch (PrivilegedActionException prive) {        	logger.error("Exception: ", prive);            throw (BSFException) prive.getException();        }        return result;    }    //////////////////////////////////////////////////////////////////////    //    // Convenience functions for exec'ing and eval'ing scripts directly    // without loading and dealing with engines etc..    //    //////////////////////////////////////////////////////////////////////    /**     * Execute the given script of the given language.     *     * @param lang     language identifier     * @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     *     * @exception BSFException if anything goes wrong while running the script     */    public void exec(String lang,                     String source,                     int lineNo,                     int columnNo,                     Object script)        throws BSFException {    	logger.debug("BSFManager:exec");        final BSFEngine e = loadScriptingEngine(lang);        final String sourcef = source;        final int lineNof = lineNo, columnNof = columnNo;        final Object scriptf = script;        try {            AccessController.doPrivileged(new PrivilegedExceptionAction() {                    public Object run() throws Exception {                        e.exec(sourcef, lineNof, columnNof, scriptf);                        return null;                    }                });        } catch (PrivilegedActionException prive) {        	logger.error("Exception :", prive);            throw (BSFException) prive.getException();        }    }    /**     * Execute the given script of the given language, attempting to     * emulate an interactive session w/ the language.     *     * @param lang     language identifier     * @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     *     * @exception BSFException if anything goes wrong while running the script     */    public void iexec(String lang,                     String source,                     int lineNo,                     int columnNo,                     Object script)        throws BSFException {    	logger.debug("BSFManager:iexec");        final BSFEngine e = loadScriptingEngine(lang);        final String sourcef = source;        final int lineNof = lineNo, columnNof = columnNo;        final Object scriptf = script;        try {            AccessController.doPrivileged(new PrivilegedExceptionAction() {                    public Object run() throws Exception {                        e.iexec(sourcef, lineNof, columnNof, scriptf);                        return null;                    }                });        } catch (PrivilegedActionException prive) {        	logger.error("Exception :", prive);            throw (BSFException) prive.getException();        }    }    /**     * Get classLoader     */    public ClassLoader getClassLoader() {    	logger.debug("BSFManager:getClassLoader");        return classLoader;    }    /**     * Get classPath     */    public String getClassPath() {    	logger.debug("BSFManager:getClassPath");        if (classPath == null) {            try {                classPath = System.getProperty("java.class.path");            } catch (Throwable t) {            	logger.debug("Exception :", t);                // prolly a security exception .. so no can do            }        }        return classPath;    }    /**     * Determine the language of a script file by looking at the file     * extension.     *     * @param fileName the name of the file     *     * @return the scripting language the file is in if the file extension     *         is known to me (must have been registered via     *         registerScriptingEngine).     *     * @exception BSFException if file's extension is unknown.     */    public static String getLangFromFilename(String fileName)        throws BSFException {        int dotIndex = fileName.lastIndexOf(".");        if (dotIndex != -1) {            String extn = fileName.substring(dotIndex + 1);            String langval = (String) extn2Lang.get(extn);            String lang = null;            int index, loops = 0;            if (langval != null) {                while ((index = langval.indexOf(":", 0)) != -1) {                    // Great. Multiple language engines registered                    // for this extension.                    // Try to find first one that is in our classpath.                    lang = langval.substring(0, index);                    langval = langval.substring(index + 1);                    loops++;                    // Test to see if in classpath                    try {                        String engineName =                            (String) registeredEngines.get(lang);                        Class.forName(engineName);                    } catch (ClassNotFoundException cnfe) {                        // Bummer.                        lang = langval;

⌨️ 快捷键说明

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