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

📄 modelmanagerimp.java

📁 一个非常好的FRAMWRK!是一个外国组织做的!不!
💻 JAVA
字号:
/**
 * Copyright 2003-2005 the original author or authors.
 * 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.jdon.model;

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

import com.jdon.controller.model.Model;
import com.jdon.model.cache.ModelCacheManager;
import com.jdon.model.config.ModelXmlConfig;
import com.jdon.model.handler.HandlerObjectFactory;
import com.jdon.util.Debug;



/**
 * 本类是产生框架使用的各种Model,面向客户端调用
 * 如果需要调用Model相关,调用本ModelManager
 *
 * 为提高性能,可在此嵌入对象池。
 *
 * @author banq
 */
public class ModelManagerImp implements ModelManager{

  public final static String module = ModelManagerImp.class.getName();

  private ModelXmlConfig modelXmlLoader;
  private ModelCacheManager modelCacheManager;
  private HandlerObjectFactory handlerObjectFactory;
  
  //Model 缓存池
  private Map modelPool = new HashMap();
  private final static int MODEL_INSTANCE_COUNT = 50;
  

  public ModelManagerImp(ModelXmlConfig modelXmlLoader, 
                        ModelCacheManager modelCacheManager,
                        HandlerObjectFactory handlerObjectFactory) {
    this.modelXmlLoader = modelXmlLoader;
    this.modelCacheManager = modelCacheManager;
    this.handlerObjectFactory = handlerObjectFactory;
  }




  /**
   * 从Modelhandler池中借用一个Handler实例
   * 并且初始化
   *
   * 借用后必须归还
   */
  public ModelHandler borrowtHandlerObject(String formName) {
    ModelHandler modelHandler = null;
    try {
      modelHandler = handlerObjectFactory.borrowHandlerObject(formName);
      modelHandler.setModelMapping(modelXmlLoader.getModelMapping(formName));
    } catch (Exception ex) {
      Debug.logError("can't get the modelHandler for the formName " + formName,
                     module);
    }
    return modelHandler;
  }

  /**
   * 归还从Modelhandler池中借用一个实例
   */
  public void returnHandlerObject(ModelHandler modelHandler) {
    if (modelHandler == null) return;
    try {
        handlerObjectFactory.returnHandlerObject(modelHandler);
    } catch (Exception ex) {
      Debug.logError(" return modelHandler error" + ex, module);
    }

  }

  /**
   * 获得一个新的Model实例
   */ 
  public Model getModelObject(String formName) {
      Model model = null;
      try {
          String poolKey = (modelXmlLoader.getModelMapping(formName)).getClassName();
          Debug.logVerbose("--> get Model object " + poolKey, module);

          LinkedList list = (LinkedList) modelPool.get(poolKey);
          if ( (list == null) || (list.isEmpty())) { //如果空了,生产
            Debug.logVerbose("--> create Model object " + MODEL_INSTANCE_COUNT,
                             module);
            int count = 0;
            list = new LinkedList();
            while (count < MODEL_INSTANCE_COUNT) {
              model = makeModelObject(formName);
              list.add(model);
              count++;
            }
            modelPool.put(poolKey, list);
          }
          model = (Model) list.removeFirst();
    } catch (Exception ex) {
        Debug.logError("getModelObject error: " + ex, module);
    }      
    return model;
  }

  private Model makeModelObject(String formName) throws Exception {
      Model object = null;
      Class modelClass = null;
      try {
        modelClass = (Class) modelXmlLoader.getModelClasses().get(formName);
        if (modelClass == null) {
          throw new Exception(
              " not found the model in config xml, formName=" + formName);
        }
        object = (Model) modelClass.newInstance();
      } catch (Exception e) {
        Debug.logError("--> call Model: " + modelClass + " error:" + e, module);
        throw new Exception(e);
      }
      return object;
  }  
  
  /**
   * 将Model实例加入缓存
   * @param modelKey
   * @param model
   */
  public void addCache(ModelKey modelKey, Model model) {
    if ((modelKey == null) || (modelKey.getDataKey() == null))
      return;

    if (modelKey.getModelClassName() != null)
      modelCacheManager.setCache2(modelKey.getDataKey(),
                          modelKey.getModelClassName().getName(), model);
    else
      modelCacheManager.setCache(modelKey.getDataKey(),
                         modelKey.getFormName(), model);

  }

  /**
   * 将Model实例加入缓存
   */
  public void addCache(Object key, String className, Model model) {

    if (key == null)
      return;
    modelCacheManager.setCache2(key, className, model);

  }

  /**
   * 获取加入缓存的Model实例
   */

  public Model getCache(ModelKey modelKey) {
    if (modelKey.getModelClassName() != null)
      return modelCacheManager.getCache2(modelKey.getDataKey(),
                                 modelKey.getModelClassName().getName());
    else
      return modelCacheManager.getCache(modelKey.getDataKey(),
                                modelKey.getFormName());

  }

  /**
   * 获取加入缓存的Model实例
   */

  public Model getCache(Object key, String className) {
    return modelCacheManager.getCache2(key, className);

  }

  /**
   * 取出缓存中的Model实例
   */

  public void removeCache(Object dataKey) throws Exception {
    modelCacheManager.removeCache(dataKey);
  }
  
  public void clearCache(){
    modelCacheManager.clearCache();
  }

  public boolean isNull(String s) {
    boolean isNull = false;
    if (s == null)
      isNull = true;
    else if (s.equals(""))
      isNull = true;
    else if (s.equals("null"))
      isNull = true;
    return isNull;
  }



}

⌨️ 快捷键说明

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