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

📄 formactionmanager.java

📁 欢迎使用 FastJsp 开发框架! 编译说明: * 若要生成Api Javadoc文档
💻 JAVA
字号:
// Copyright 2005-2007 onetsoft.com
//
// 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 com.onetsoft.fastjsp;

import com.onetsoft.fastjsp.util.ResourceResolver;
import com.onetsoft.fastjsp.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 表单action管理
 * 注:"Default"是一个特殊的action,在没有form action时作为缺省action来执行
 *
 * @author <a href="mailto:hgw@onetsoft.com">hgw</a>
 */
class FormActionManager implements ActionManager {

    private final Map pool = new HashMap(2);        //key:form   value:FormActions
    private Class componentClass = null;

    public FormActionManager(AbstractComponent component) {
        componentClass = component.getClass();
    }

    public AbstractForm getForm(String form) {
        try {
            return (AbstractForm) getFormActions(form).formClass.newInstance();
        } catch (InstantiationException e) {
            throw new ApplicationRuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new ApplicationRuntimeException(e);
        }
    }

    private FormActions getFormActions(String form) {
        FormActions o = (FormActions) pool.get(form);
        if (o == null) {
            synchronized (form.intern()) {
                o = (FormActions) pool.get(form);
                if (o == null) {
                    o = loadFormActions(form);
                    synchronized (pool) {
                        pool.put(form, o);
                    }
                }
            }
        }
        return o;
    }

    private FormActions loadFormActions(String form) {
        try {
            Class cl = ResourceResolver.forName(new StringBuffer().append(componentClass.getPackage().getName()).append(".form.")
                    .append(form).append('.').append(StringUtils.firstCharUpper(form)).toString());
            FormActions o = new FormActions();
            o.formClass = cl;
            o.init();
            return o;
        } catch (ClassNotFoundException e) {
            throw new ApplicationRuntimeException("Fail to load form \"" + form + "\",forget to implement form class \"" +
                    new StringBuffer().append(componentClass.getPackage().getName()).append(".form.")
                            .append(form).append('.').append(StringUtils.firstCharUpper(form)).toString() + "\" in component \"" +
                    componentClass.getClass().getName() + "\"?", e);
        }
    }

    /**
     * 执行action
     * Action寻找规则:若找不到指定action,将执行缺省的action "Default"
     * 注:不存在的 action  将会被忽略
     *
     * @param cycle
     */
    public void executeAction(AbstractPageCycle cycle) {
        try {
            Class cl = getFormActions(cycle.service.data.getString(StringUtils.FORM, StringUtils.EMPTY).trim())
                    .getAction(cycle.service.data.getString(cycle.service.servicer.module.actionName));
            if (cl != null) {
                AbstractAction a = (AbstractAction) cl.newInstance();
                a.setCycle(cycle);
                a.execute();
            }
        } catch (InstantiationException e) {
            throw new ApplicationRuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new ApplicationRuntimeException(e);
        }
    }

    /**
     * 表单 actions
     * 注:没有缺省 Url action
     */
    private class FormActions {
        private Class formClass = null;
        private Map  pool =null;
        private Class defaultActionClass = null; //缺省action ,直接存取,不通过 pool
        private boolean defaultActionPrefered = true;  //本表单缺省action

        public FormActions() {
        }

        /*初始化表单缺省action*/
        private void init() {
            try {
                AbstractForm form = ((AbstractForm) formClass.newInstance());
                defaultActionPrefered = form.isDefaultActionPreferred();
                String defaultAction = StringUtils.trimToEmpty(form.getDefaultAction());
                if (defaultAction.length() != 0)
                    defaultActionClass = loadAction(defaultAction);
            } catch (Exception e) {
                /* ignored */
            }
        }
        private Map newHashMap(int capacity){
            return new HashMap(capacity);
        }

        private Class getAction(String action) {
            if (defaultActionPrefered && defaultActionClass != null)
                return defaultActionClass;
            Class cl = null;
            if (action.length() != 0) {
                if(pool==null){
                    synchronized(this){
                        if(pool==null){
                            pool= newHashMap(2);
                        }
                    }
                }
                cl = (Class) pool.get(action);
                if (cl == null) {
                    synchronized (action.intern()) {
                        cl = (Class) pool.get(action);
                        if (cl == null) {
                            try {
                                cl = loadAction(action);
                            } catch (Exception e) {
                                /* ignored */
                            }
                            if (cl != null) {
                                synchronized (pool) {
                                    pool.put(action, cl);
                                }
                            }
                        }
                    }
                }
            }
            return cl != null ? cl : defaultActionClass;
        }

        public Class getFormClass() {
            return formClass;
        }


        private Class loadAction(String action) {
            String className = new StringBuffer(48).append(formClass.getPackage().getName()).append(".action.")
                    .append(StringUtils.firstCharUpper(action)).toString();
            try {
                return ResourceResolver.forName(className);
            } catch (ClassNotFoundException e) {
                throw new ApplicationRuntimeException(e);
            }
        }
    }
}

⌨️ 快捷键说明

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