bsfmanager.java

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

JAVA
911
字号
                        continue;                    }                    // Got past that? Good.                    break;                }                if (loops == 0) { lang = langval; }            }            if (lang != null && lang != "") {                return lang;            }        }        throw new BSFException(BSFException.REASON_OTHER_ERROR,                               "file extension missing or unknown: "                               + "unable to determine language for '"                               + fileName                               + "'");    }    /**     * Return the current object registry of the manager.     *     * @return the current registry.     */    public ObjectRegistry getObjectRegistry() {        return objectRegistry;    }    /**     * Get tempDir     */    public String getTempDir() {        return tempDir;    }    /**     * Determine whether a language is registered.     *     * @param lang string identifying a language     *     * @return true iff it is     */    public static boolean isLanguageRegistered(String lang) {        return (registeredEngines.get(lang) != null);    }    //////////////////////////////////////////////////////////////////////    //    // Bean scripting framework services    //    //////////////////////////////////////////////////////////////////////    /**     * Load a scripting engine based on the lang string identifying it.     *     * @param lang string identifying language     * @exception BSFException if the language is unknown (i.e., if it     *            has not been registered) with a reason of     *            REASON_UNKNOWN_LANGUAGE. If the language is known but     *            if the interface can't be created for some reason, then     *            the reason is set to REASON_OTHER_ERROR and the actual     *            exception is passed on as well.     */    public BSFEngine loadScriptingEngine(String lang) throws BSFException {    	logger.debug("BSFManager:loadScriptingEngine");        // if its already loaded return that        BSFEngine eng = (BSFEngine) loadedEngines.get(lang);        if (eng != null) {            return eng;        }        // is it a registered language?        String engineClassName = (String) registeredEngines.get(lang);        if (engineClassName == null) {        	logger.error("unsupported language: " + lang);            throw new BSFException(BSFException.REASON_UNKNOWN_LANGUAGE,                                   "unsupported language: " + lang);        }        // create the engine and initialize it. if anything goes wrong        // except.        try {            Class engineClass =                (classLoader == null)                ? Class.forName(engineClassName)                : classLoader.loadClass(engineClassName);            final BSFEngine engf = (BSFEngine) engineClass.newInstance();            final BSFManager thisf = this;            final String langf = lang;            final Vector dbf = declaredBeans;            AccessController.doPrivileged(new PrivilegedExceptionAction() {                    public Object run() throws Exception {                        engf.initialize(thisf, langf, dbf);                        return null;                    }                });            eng = engf;            loadedEngines.put(lang, eng);            pcs.addPropertyChangeListener(eng);            return eng;        } catch (PrivilegedActionException prive) {        	    logger.error("Exception :", prive);                throw (BSFException) prive.getException();        } catch (Throwable t) {        	logger.error("Exception :", t);            throw new BSFException(BSFException.REASON_OTHER_ERROR,                                   "unable to load language: " + lang,                                   t);        }    }    /**     * return a handle to a bean registered in the bean registry by the     * application or a scripting engine. Returns null if bean is not found.     *     * @param beanName name of bean to look up     *     * @return the bean if its found or null     */    public Object lookupBean(String beanName) {    	logger.debug("BSFManager:lookupBean");        try {            return ((BSFDeclaredBean)objectRegistry.lookup(beanName)).bean;        } catch (IllegalArgumentException e) {        	logger.debug("Exception :", e);            return null;        }    }    /**     * Registering a bean allows a scripting engine or the application to     * access that bean by name and to manipulate it.     *     * @param beanName name to register under     * @param bean     the bean to register     */    public void registerBean(String beanName, Object bean) {    	logger.debug("BSFManager:registerBean");        BSFDeclaredBean tempBean;        if(bean == null) {            tempBean = new BSFDeclaredBean(beanName, null, null);        } else {            tempBean = new BSFDeclaredBean(beanName, bean, bean.getClass());        }        objectRegistry.register(beanName, tempBean);    }    /**     * Register a scripting engine in the static registry of the     * BSFManager.     *     * @param lang string identifying language     * @param engineClassName fully qualified name of the class interfacing     *        the language to BSF.     * @param extensions array of file extensions that should be mapped to     *        this language type. may be null.     */    public static void registerScriptingEngine(String lang,                                               String engineClassName,                                               String[] extensions) {        registeredEngines.put(lang, engineClassName);        if (extensions != null) {            for (int i = 0; i < extensions.length; i++) {                String langstr = (String) extn2Lang.get(extensions[i]);                langstr = (langstr == null) ? lang : lang + ":" + langstr;                extn2Lang.put(extensions[i], langstr);            }        }    }    /**     * Set the class loader for those that need to use it. Default is he     * who loaded me or null (i.e., its Class.forName).     *     * @param classLoader the class loader to use.     */    public void setClassLoader(ClassLoader classLoader) {    	logger.debug("BSFManager:setClassLoader");        pcs.firePropertyChange("classLoader", this.classLoader, classLoader);        this.classLoader = classLoader;    }    /**     * Set the classpath for those that need to use it. Default is the value     * of the java.class.path property.     *     * @param classPath the classpath to use     */    public void setClassPath(String classPath) {    	logger.debug("BSFManager:setClassPath");        pcs.firePropertyChange("classPath", this.classPath, classPath);        this.classPath = classPath;    }    /**     * Set the object registry used by this manager. By default a new     * one is created when the manager is new'ed and this overwrites     * that one.     *     * @param objectRegistry the registry to use     */    public void setObjectRegistry(ObjectRegistry objectRegistry) {    	logger.debug("BSFManager:setObjectRegistry");        this.objectRegistry = objectRegistry;    }    /**     * Temporary directory to put stuff into (for those who need to). Note     * that unless this directory is in the classpath or unless the     * classloader knows to look in here, any classes here will not     * be found! BSFManager provides a service method to load a class     * which uses either the classLoader provided by the class loader     * property or, if that fails, a class loader which knows to load from     * the tempdir to try to load the class. Default value of tempDir     * is "." (current working dir).     *     * @param tempDir the temporary directory     */    public void setTempDir(String tempDir) {    	logger.debug("BSFManager:setTempDir");        pcs.firePropertyChange("tempDir", this.tempDir, tempDir);        this.tempDir = tempDir;    }    /**     * Gracefully terminate all engines     */    public void terminate() {    	logger.debug("BSFManager:terminate");        Enumeration enginesEnum = loadedEngines.elements();        BSFEngine engine;        while (enginesEnum.hasMoreElements()) {            engine = (BSFEngine) enginesEnum.nextElement();            engine.terminate();        }        loadedEngines = new Hashtable();    }    /**     * Undeclare a previously declared bean. This removes the bean from     * the list of declared beans in the manager as well as asks every     * running engine to undeclared the bean. As with above, if any     * of the engines except when asked to undeclare, this method does     * not catch that exception. Quietly returns if the bean is unknown.     *     * @param beanName name of bean to undeclare     *     * @exception BSFException if any of the languages that are already     *            running decides to throw an exception when asked to     *            undeclare this bean.     */    public void undeclareBean(String beanName) throws BSFException {    	logger.debug("BSFManager:undeclareBean");        unregisterBean(beanName);        BSFDeclaredBean tempBean = null;        boolean found = false;        for (Iterator i = declaredBeans.iterator(); i.hasNext();) {            tempBean = (BSFDeclaredBean) i.next();            if (tempBean.name.equals(beanName)) {            	found = true;                break;            }        }        if (found) {            declaredBeans.removeElement(tempBean);            Enumeration enginesEnum = loadedEngines.elements();            while (enginesEnum.hasMoreElements()) {                BSFEngine engine = (BSFEngine) enginesEnum.nextElement();                engine.undeclareBean(tempBean);            }        }    }    /**     * Unregister a previously registered bean. Silent if name is not found.     *     * @param beanName name of bean to unregister     */    public void unregisterBean(String beanName) {    	logger.debug("BSFManager:unregisterBean");        objectRegistry.unregister(beanName);    }}

⌨️ 快捷键说明

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