actionservlet.java

来自「这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用s」· Java 代码 · 共 1,671 行 · 第 1/5 页

JAVA
1,671
字号
    }


    /**
     * Log the specified message if the current debugging detail level for
     * this servlet has been set to an equal or higher value.  Otherwise,
     * ignore this message.
     *
     * @param message Message to be logged
     * @param level Debugging detail level of this message
     * @deprecated Use commons-logging instead.
     */
    public void log(String message, int level) {

        if (debug >= level) {
            log(message);
        }

    }


    // ------------------------------------------------------ Protected Methods


    /**
     * Gracefully terminate use of any modules associated with this
     * application (if any).
     * @since Struts 1.1
     * @deprecated replaced by destroyModules()
     */
    protected void destroyApplications() {
        destroyModules();
    }

    /**
     * Gracefully terminate use of any modules associated with this
     * application (if any).
     * @since Struts 1.1
     */
    protected void destroyModules() {

        ArrayList values = new ArrayList();
        Enumeration names = getServletContext().getAttributeNames();
        while (names.hasMoreElements()) {
            values.add(names.nextElement());
        }

        Iterator keys = values.iterator();
        while (keys.hasNext()) {
            String name = (String) keys.next();
            Object value = getServletContext().getAttribute(name);
            if (value instanceof ModuleConfig) {
                ModuleConfig config = (ModuleConfig) value;
                    try {
                        getRequestProcessor(config).destroy();
                    } catch (ServletException e) {
                        log.error(e);
                    }
                    
                getServletContext().removeAttribute(name);

                PlugIn plugIns[] =
                    (PlugIn[]) getServletContext().getAttribute(
                        Globals.PLUG_INS_KEY + config.getPrefix());
                if (plugIns != null) {
                    for (int i = 0; i < plugIns.length; i++) {
                        int j = plugIns.length - (i + 1);
                        plugIns[j].destroy();
                    }
                    getServletContext().removeAttribute
                        (Globals.PLUG_INS_KEY + config.getPrefix());
                }
            }
        }

    }


    /**
     * Gracefully release any configDigester instance that we have created.
     * @since Struts 1.1
     */
    protected void destroyConfigDigester() {

        configDigester = null;

    }


    /**
     * Gracefully terminate use of the data source associated with this
     * application (if any).
     *
     * @deprecated Will no longer be required with module support
     */
    protected void destroyDataSources() {

        synchronized (dataSources) {
            Iterator keys = dataSources.keySet().iterator();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                getServletContext().removeAttribute(key);
                DataSource dataSource = findDataSource(key);
                if (dataSource instanceof GenericDataSource) {
                    if (log.isDebugEnabled()) {
                        log.debug(internal.getMessage("dataSource.destroy", key));
                    }
                    try {
                        ((GenericDataSource) dataSource).close();
                    } catch (SQLException e) {
                        log.error(internal.getMessage("destroyDataSource", key), e);
                    }
                }
            }
            dataSources.clear();
        }

    }


    /**
     * Gracefully terminate use of the internal MessageResources.
     */
    protected void destroyInternal() {

        internal = null;

    }


    /**
     * Return the module configuration object for the currently selected
     * module.
     *
     * @param request The servlet request we are processing
     * @since Struts 1.1
     * @deprecated use {@link #getModuleConfig(HttpServletRequest)}
     */
    protected ApplicationConfig getApplicationConfig
        (HttpServletRequest request) {
        /* FIXME for Struts 1.2
           Since Struts 1.1 only has one implementation for
           ModuleConfig casting is safe here. Used only for
           transition purposes !
        */
        return new ApplicationConfig((ModuleConfigImpl)getModuleConfig(request));
    }

    /**
     * Return the module configuration object for the currently selected
     * module.
     *
     * @param request The servlet request we are processing
     * @since Struts 1.1
     */
    protected ModuleConfig getModuleConfig
        (HttpServletRequest request) {

        ModuleConfig config = (ModuleConfig)
            request.getAttribute(Globals.MODULE_KEY);
        if (config == null) {
            config = (ModuleConfig)
                getServletContext().getAttribute(Globals.MODULE_KEY);
        }
        return (config);

    }


    /**
     * Look up and return the {@link RequestProcessor} responsible for the
     * specified module, creating a new one if necessary.
     *
     * @param config The module configuration for which to
     *  acquire and return a RequestProcessor.
     *
     * @exception ServletException if we cannot instantiate a RequestProcessor
     *  instance
     * @since Struts 1.1
     */
    protected synchronized RequestProcessor getRequestProcessor(ModuleConfig config)
        throws ServletException {    
            
        String key = Globals.REQUEST_PROCESSOR_KEY + config.getPrefix();
        RequestProcessor processor = (RequestProcessor)
            getServletContext().getAttribute(key);
            
        if (processor == null) {
            try {
                processor =
                    (RequestProcessor) RequestUtils.applicationInstance(
                        config.getControllerConfig().getProcessorClass());
                        
            } catch (Exception e) {
                throw new UnavailableException(
                    "Cannot initialize RequestProcessor of class "
                        + config.getControllerConfig().getProcessorClass()
                        + ": "
                        + e);
            }

            processor.init(this, config);
            getServletContext().setAttribute(key, processor);

        }
        return (processor);

    }
    
    /**
     * <p>Initialize the application configuration information for the
     * specified module.</p>
     *
     * @param prefix Module prefix for this module
     * @param path Context-relative resource path for this modules's
     *  configuration resource
     *
     * @exception ServletException if initialization cannot be performed
     * @deprecated use {@link #initModuleConfig(String,String)}
     * @since Struts 1.1
     */
    protected ApplicationConfig initApplicationConfig
        (String prefix, String path) throws ServletException {
        /* FIXME for Struts 1.2
           Since Struts 1.1 only has one implementation for
           ModuleConfig casting is safe here. Used only for
           transition purposes !
        */
        return new ApplicationConfig((ModuleConfigImpl)initModuleConfig(prefix,path));
    }
    /**
     * <p>Initialize the application configuration information for the
     * specified module.</p>
     *
     * @param prefix Module prefix for this module
     * @param paths Comma-separated list of context-relative resource path(s)
     *  for this modules's configuration resource(s)
     *
     * @exception ServletException if initialization cannot be performed
     * @since Struts 1.1
     */
    protected ModuleConfig initModuleConfig(String prefix, String paths)
        throws ServletException {

        if (log.isDebugEnabled()) {
            log.debug(
                "Initializing module path '"
                    + prefix
                    + "' configuration from '"
                    + paths
                    + "'");
        }

        // Parse the configuration for this module
        //@todo & FIXME replace with a FactoryMethod
        ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
        ModuleConfig config = factoryObject.createModuleConfig(prefix);

        // Support for module-wide ActionMapping type override
        String mapping = getServletConfig().getInitParameter("mapping");
        if (mapping != null) {
            config.setActionMappingClass(mapping);
        }

        // Configure the Digester instance we will use
        Digester digester = initConfigDigester();

        // Process each specified resource path
        while (paths.length() > 0) {
            digester.push(config);
            String path = null;
            int comma = paths.indexOf(',');
            if (comma >= 0) {
                path = paths.substring(0, comma).trim();
                paths = paths.substring(comma + 1);
            } else {
                path = paths.trim();
                paths = "";
            }

            if (path.length() < 1) {
                break;
            }
            
            this.parseModuleConfigFile(prefix, paths, config, digester, path);
        }

        // Force creation and registration of DynaActionFormClass instances
        // for all dynamic form beans we wil be using
        FormBeanConfig fbs[] = config.findFormBeanConfigs();
        for (int i = 0; i < fbs.length; i++) {
            if (fbs[i].getDynamic()) {
                DynaActionFormClass.createDynaActionFormClass(fbs[i]);
            }
        }

        // Special handling for the default module (for
        // backwards compatibility only, will be removed later)
        if (prefix.length() < 1) {
            defaultControllerConfig(config);
            defaultMessageResourcesConfig(config);
            defaultFormBeansConfig(config);
            defaultForwardsConfig(config);
            defaultMappingsConfig(config);
        }

        // Return the completed configuration object
        //config.freeze();  // Now done after plugins init
        return (config);

    }

    /**
     * Parses one module config file.
     * @param prefix
     * @param paths
     * @param config
     * @param digester Digester instance that does the parsing
     * @param path The path to the config file to parse.
     * @throws UnavailableException
     */
    private void parseModuleConfigFile(
        String prefix,
        String paths,
        ModuleConfig config,
        Digester digester,
        String path)
        throws UnavailableException {

        InputStream input = null;
        try {
            URL url = getServletContext().getResource(path);
            InputSource is = new InputSource(url.toExternalForm());
            input = getServletContext().getResourceAsStream(path);
            is.setByteStream(input);

⌨️ 快捷键说明

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