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

📄 modelxmlconfig.java

📁 Java/J2EE框架Jdon-Framework系统的Sample
💻 JAVA
字号:
/**
 * Copyright 2005 Jdon.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.jdon.model.config;

import java.util.*;
import com.jdon.util.Debug;

import com.jdon.controller.model.Model;
import com.jdon.model.ModelHandler;
import com.jdon.model.handler.HandlerMetaDef;
import com.jdon.model.mapping.ModelMapping;
import com.jdon.model.handler.HandlerClassFactory;

/**
 * 根据modelmapping.xml生产相应的实例
 * 下面两个方法预先需要执行:
 * loadMapping(); //获取xml
 * createModelClass();//预先创建一些class
 *
 * ModelFactory是有状态的类。
 *
 * <p>Copyright: Jdon.com Copyright (c) 2003</p>
 * <p></p>
 * @author banq
 * @version 1.0
 */
public final class ModelXmlConfig implements org.picocontainer.Startable {
  public final static String module = ModelXmlConfig.class.getName();

  private final static int MODEL_INSTANCE_COUNT = 50;
  private final static int HANDLER_INSTANCE_COUNT = 20;

  private HandlerClassFactory handlerClassFactory;

  private Map configLoadedList = new HashMap();

  //formName 与ModelMapping对应关系
  private Map mps = new HashMap();

  //Model class 池
  private Map modelClasses = new HashMap();
  //Hanlder class池
  private Map handlerClasses = new HashMap();

  //Model 缓存池
  private Map modelPool = new HashMap();
  //空闲ModelHandler池
  private Map handlerFreePool = new HashMap();
  //在使用的ModelHandler池
  private Map handlerUsedPool = new HashMap();


  public ModelXmlConfig(Collection configList,
                        HandlerClassFactory handlerClassFactory) {
    this.handlerClassFactory = handlerClassFactory;

    Iterator iter = configList.iterator();
    while (iter.hasNext()) {
      String configFile = (String) iter.next();
      if (!configLoadedList.containsKey(configFile)) {
        ConfigureReader configureReader = new ConfigureReader(configFile);
        Debug.logVerbose("init configFile = " + configFile, module);
        configLoadedList.put(configFile, configureReader);
      }
    }
  }

  /**
   * 将所有的ConfigureLoader包含的内容合并在一起。
   * 本方法相当于start()
   * 参考 {@link #detroy()} method
   *
   */
 public void start() {
    try {
      Iterator iter = configLoadedList.keySet().iterator();
      while (iter.hasNext()) {
        String configFile = (String) iter.next();
        Debug.logVerbose(" start configFile = " + configFile, module);
        ConfigureReader configureLoader = (ConfigureReader)configLoadedList.get(configFile);
        Map xmlMaps = configureLoader.load();
        mps.putAll(xmlMaps);
        Iterator mpsIter = xmlMaps.keySet().iterator();
        while(mpsIter.hasNext()){
          String formName = (String)mpsIter.next();
          ModelMapping mp = (ModelMapping)xmlMaps.get(formName);
          modelClasses.put(formName, handlerClassFactory.createModel(mp));
          handlerClasses.put(formName, handlerClassFactory.createHandler(mp));
        }
      }
      configLoadedList.clear();
    } catch (Exception ex) {
      Debug.logError(" !!!!!!!framework started error: " + ex, module);
    }
  }

  /**
   * 清除内存
   */
  public void stop() {
    mps.clear();

    modelClasses.clear();
    handlerClasses.clear();
  }

  public ModelMapping getModelMapping(String formName) {
    return (ModelMapping) mps.get(formName);
  }



  /**
   * 获得一个空闲的ModelHandler
   * 如果空闲池没有,就重新生成指定个数的ModelHandler实例。
   * 获得成功,则记入在用池。
   * 当客户端调用完毕,调用returnHandlerObject返回该ModelHandler实例备重用。
   * @param formName
   * @return
   * @throws java.lang.Exception
   */
  public synchronized ModelHandler borrowtHandlerObject(String formName) throws
      Exception {

    ModelHandler modelHandler = null;
    String poolKey = (getModelMapping(formName)).getHandler();

    LinkedList listFree = (LinkedList) handlerFreePool.get(poolKey);
    if ( (listFree == null) || (listFree.isEmpty())) { //如果空了,生产
      listFree = makeHandlerObjects(formName);
      handlerFreePool.put(poolKey, listFree);
    }
    modelHandler = (ModelHandler) listFree.removeFirst();

    //加入已经用的池
    LinkedList listUsed = (LinkedList) handlerUsedPool.get(poolKey);
    if (listUsed == null) {
      listUsed = new LinkedList();
      handlerUsedPool.put(poolKey, listUsed);
    }
    listUsed.add(modelHandler);

    Debug.logVerbose("--> borrow Modelhandler instance " + poolKey +
                     listFree.size() + " for " + formName, module);

    return modelHandler;
  }

  /**
   * 生成指定个数的实例
   * @param formName
   * @return
   * @throws java.lang.Exception
   */
  private LinkedList makeHandlerObjects(String formName) throws Exception {
    Debug.logVerbose("--> create Modelhandler instance " +
                     HANDLER_INSTANCE_COUNT, module);
    int count = 0;
    ModelHandler modelHandler = null;
    LinkedList list = new LinkedList();
    while (count < HANDLER_INSTANCE_COUNT) {
      modelHandler = makeHandlerObject(formName);
      list.add(modelHandler);
      count++;
    }
    return list;
  }

  /**
   * 返还使用过的ModelHandler
   * 1.从使用池中删除该实例
   * 2.将该实例加入空闲池
   * @param modelHandler
   * @throws java.lang.Exception
   */
  public synchronized void returnHandlerObject(ModelHandler modelHandler) throws
      Exception {
    String poolKey = modelHandler.getClass().getName();
    LinkedList listUsed = (LinkedList) handlerUsedPool.get(poolKey);
    if (listUsed == null) {
      Debug.logError("ERROR:not find the used pool: class = " + poolKey, module);
      return;
    }
    listUsed.remove(modelHandler);
    LinkedList listFree = (LinkedList) handlerFreePool.get(poolKey);
    if (listFree == null) {
      Debug.logError("ERROR:not find the free pool: class = " + poolKey, module);
      return;
    }
    listFree.add(modelHandler);
    Debug.logVerbose("--> return Modelhandler instance successfullu" + poolKey +
                     listFree.size(), module);
  }

  public Model getModelObject(String formName) throws Exception {

    Model model = null;
    String poolKey = (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();
    return model;
  }

  private Model makeModelObject(String formName) throws Exception {
    Model object = null;
    Class modelClass = null;
    try {
      modelClass = (Class) modelClasses.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;
  }

  private ModelHandler makeHandlerObject(String formName) throws Exception {
    ModelHandler object = null;
    Class handlerClass = null;
    try {
      handlerClass = (Class) handlerClasses.get(formName);
      if (handlerClass == null) {
        throw new Exception(
            " not found the handler in config xml formName=" + formName);
      }
      object = (ModelHandler) handlerClass.newInstance();
    } catch (Exception e) {
      Debug.logError("--> call Handler: " + handlerClass + " error:" + e,
                     module);
      throw new Exception(e);
    }
    return object;
  }

}

⌨️ 快捷键说明

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