ofbiz3-ComponentContainer load 流程

类别:Java 点击:0 评论:0 推荐:

ComponentContainer load 流程
/*
    1 : 前言
    ContainerLoader.java文件首先把ofbiz -
    container.xml定义的container读入到了.ContainerConfig
    2 : 加载
 */
    private Container loadContainer(String classname, String configFileLocation) throws
    StartupException {
  // load the component container class
  ClassLoader loader = Thread.currentThread().getContextClassLoader();
  if (loader == null) {
    Debug.logWarning("Unable to get context classloader; using system", module);
    loader = ClassLoader.getSystemClassLoader();
  }
  Class componentClass = null;
  try {
    componentClass = loader.loadClass(classname);
  }
  catch (ClassNotFoundException e) {
    throw new StartupException("Cannot locate container class", e);
  }
  if (componentClass == null) {
    throw new StartupException("Component container class not loaded");
  }

  Container componentObj = null;
  try {
    componentObj = (Container) componentClass.newInstance();
////通过classname生成实例
  }
  catch (InstantiationException e) {
    throw new StartupException(e);
  }
  catch (IllegalAccessException e) {
    throw new StartupException(e);
  }
  catch (ClassCastException e) {
    throw new StartupException(e);
  }

  if (componentObj == null) {
    throw new StartupException(
        "Unable to create instance of component container");
  }

  try {
    componentObj.start(configFileLocation); //容器初始化
  }
  catch (ContainerException e) {
    throw new StartupException(e);
  }

  return componentObj;
}
3 : ComponentContainer加载
    ComponentContainer.java类
    /*
       loadConfig参数是Components配置文件名,如果没有,将自动取系统自定义的
       Component-Load.xml,如果需要定义,可以
       <container
     name="component-container" class="org.ofbiz.base.container.ComponentContainer">
          <propterty name="load-config"  value="xxx.xml"/>
       </container>
       updateClasspath:加载完后是否重新设置classpath

     */
    public synchronized void loadComponents(String loaderConfig,
                                            boolean updateClasspath) throws
    AlreadyLoadedException, ComponentException {
  // 设置load list,如果失败,抛出Already Loaded Exception
  if (loadedComponents == null) {
    loadedComponents = new LinkedList(); //用Linklist保存将要加载的Components
  }
  else {
    throw new AlreadyLoadedException("Components already loaded, cannot start");
  }

  /*
     ComponentLoader将读取Component-Load.xml, 存储所有的组件,
             components存储的实例名是ComponentDef,它对应于:
      <load-component component-location="${ofbiz.home}/components/minerva"/>
   */
  List components = ComponentLoaderConfig.getComponentsToLoad(loaderConfig);

  /*
     加载每一个Components
   */
  if (components != null) {
    Iterator ci = components.iterator();
    while (ci.hasNext()) {
      ComponentLoaderConfig.ComponentDef def = (ComponentLoaderConfig.
                                                ComponentDef) ci.next();
      if (def.type == ComponentLoaderConfig.SINGLE_COMPONENT) {
        ComponentConfig config = null;
        try {
          /*在这里将要读取每个组件下的 ofbiz-component.xml文件
                比如:
                /components/minerva/ofbiz-component.xml
            /components/entity/ofbiz-component.xml
            ComponentConfig用一个静态的HashMap存储系统中所在的Component.
                每个ComponentConfig对应于一个ofbiz-component.xml文件.
           */
          config = ComponentConfig.getComponentConfig(def.name, def.location);
          if (UtilValidate.isEmpty(def.name)) {
            def.name = config.getGlobalName();
          }
        }
        catch (ComponentException e) {
          Debug.logError("Cannot load component : " + def.name + " @ " +
                         def.location + " : " + e.getMessage(), module);
        }
        if (config == null) {
          Debug.logError("Cannot load component : " + def.name + " @ " +
                         def.location, module);
        }
        else {

          /*
                   主要作要就是把当前Components用到的jar放入classpath中
           */
          loadComponent(config);
        }
      }
      else if (def.type == ComponentLoaderConfig.COMPONENT_DIRECTORY) {

        loadComponentDirectory(def.location);
      }
    }
  }

  // set the new classloader/classpath on the current thread
  if (updateClasspath) {
    System.setProperty("java.class.path", classPath.toString());
    ClassLoader cl = classPath.getClassLoader();
    Thread.currentThread().setContextClassLoader(cl);
  }

  Debug.logInfo("All components loaded", module);
}


mail: [email protected]

本文地址:http://com.8s8s.com/it/it11265.htm