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

📄 modelmanagerimp.java

📁 用jbuilder写的源程序
💻 JAVA
字号:
/**
 * Copyright 2003-2006 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 java.util.concurrent.ConcurrentHashMap;

import com.jdon.controller.model.ModelIF;
import com.jdon.model.cache.ModelCacheManager;
import com.jdon.model.config.PoolParameter;
import com.jdon.model.factory.ModelHandlerClassBuilder;
import com.jdon.model.handler.HandlerObjectFactory;
import com.jdon.util.Debug;



/**
 * ModelManager implemention
 * @author banq
 */
public class ModelManagerImp implements ModelManager{

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

  private ModelHandlerClassBuilder modelFactory;
  private ModelCacheManager modelCacheManager;
  private HandlerObjectFactory handlerObjectFactory;
  
  private Map modelPool = new ConcurrentHashMap();;
  private int poolSize;
  private final static int MODEL_INSTANCE_COUNT = 200;
  
  public ModelManagerImp(ModelHandlerClassBuilder modelXmlLoader, 
                        ModelCacheManager modelCacheManager,
                        HandlerObjectFactory handlerObjectFactory,
                        PoolParameter poolParameter) {
    this.modelFactory = modelXmlLoader;
    this.modelCacheManager = modelCacheManager;
    this.handlerObjectFactory = handlerObjectFactory;
    this.poolSize = poolParameter.getModelPoolSize();
  }

  /**
   * borrow a Handler instance from Modelhandler pool
   */
  public ModelHandler borrowtHandlerObject(String formName) {
    ModelHandler modelHandler = null;
    try {
      modelHandler = handlerObjectFactory.borrowHandlerObject(formName);
      modelHandler.setModelMapping(modelFactory.getModelMapping(formName));
    } catch (Exception ex) {
      Debug.logError("[JdonFramework]can't get the modelHandler for the formName " + formName,
                     module);
      returnHandlerObject(modelHandler);
    }
    return modelHandler;
  }

  /**
   * return the Handler instance.
   * 
   */
  public void returnHandlerObject(ModelHandler modelHandler) {
    if (modelHandler == null) return;
    try {
        handlerObjectFactory.returnHandlerObject(modelHandler);
    } catch (Exception ex) {
      Debug.logError("[JdonFramework] return modelHandler error" + ex, module);
    }

  }

  /**
   * get a Model instance from the model pool
   * the model poo size is decided by the PoolParameter configured in container.xml
   *  
   */
  public ModelIF getModelObject(String formName) {
      ModelIF model = null;
      try {
          String poolKey = (modelFactory.getModelMapping(formName)).getClassName();
          Debug.logVerbose("[JdonFramework]--> get Model object " + poolKey, module);

          LinkedList list = (LinkedList) modelPool.get(poolKey);
          if ( (list == null) || (list.isEmpty())) { 
            Debug.logVerbose("[JdonFramework]--> create Model object " + poolSize,
                             module);
            int count = 0;
            list = new LinkedList();
            while (count < poolSize) {
              model = makeModelObject(formName);
              list.add(model);
              count++;
            }
            modelPool.put(poolKey, list);
          }
          model = (ModelIF) list.removeFirst();
    } catch (Exception ex) {
        Debug.logError("[JdonFramework]getModelObject error: " + ex, module);
    }      
    return model;
  }

 
  /**
   * create model instance from the model class that read from the xml configure.
   * @param formName
   * @return
   * @throws Exception
   */
  private ModelIF makeModelObject(String formName) {
      ModelIF object = null;
      Class modelClass = null;
      try {
        modelClass = (Class) modelFactory.getModelClasses(formName);
        if (modelClass == null) {
          throw new Exception(
              " not found the model in config xml, formName=" + formName);
        }
        object = (ModelIF) modelClass.newInstance();
      } catch (Exception e) {
        Debug.logError("[JdonFramework]--> call Model: " + modelClass + " error:" + e, module);
      }
      return object;
  }  
  
  /**
   * add the model to the cache
   * @param modelKey
   * @param model
   */
  public void addCache(ModelKey modelKey, ModelIF 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);

  }

  /**
   * add the model to the cache
   */
  public void addCache(Object key, String className, ModelIF model) {

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

  }

  /**
   * get the model instance from the cache
   */

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

  }

  /**
   * get the model instance from the cache
   */

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

  }

  /**
   * remove the model instance from the cache
   */

  public void removeCache(Object dataKey) throws Exception {
    modelCacheManager.removeCache(dataKey);
  }
  
  /**
   * clear all models in the cache.
   */
  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 + -