ofbiz實體引擎(三) GenericDelegator實例化的具體過程

/**
     * @author 鄭小康
     * 1.設置delegatorFullName 基本delegatorName+"#"+tenantId 如果tenantId爲空 則就是默認的delegatorName
     *
     * 2.獲取EntityConfig實例,並獲取基本delegatorBaseName的delegator標籤,並解析爲對應的DelegatorElement實例
     * <delegator name="default"  entity-model-reader="main" entity-group-reader="main"></delegator>
     *
     * 3.判斷delegatorTenantId是否爲空,這是租戶id
     *   第一種情況租戶id不爲空:獲取默認的Delegator,用delegator查詢Tenant表中當前tenantId的對應GenericValue
     *                      :獲取對應租戶的kekText FIXME:暫時未應用 網上搜索說對數據庫連接密碼進行解密的操作
     *   第二種情況租戶id爲空 :獲取delegator標籤實例的key-encrypting-key
     *
     * 4.獲取ModelReader 檢查了實體緩存之類的操作,獲取所有ModelEntity
     *
     * 5.獲取所有ModelGroupReader
     *   該類的主要操作是構造對應groupCache緩存,將entity-name爲k,groupName爲v這樣存放,並提供一些獲取方法,如獲取所有組名,根據實體名獲取組名
     *
     * 6.緩存當前delegatorFullName
     *
     * 7.對實體進行檢查 有檢查組裏面是否有對應實體 實體名是否是保留字 建立視圖一個字段是否被引用多次
     *
     * 8.獲取組名集合
     *
     * 9.遍歷delegaot組,通過ThreadPoolExecutor線程池提交Future中任務,對每個組的實體創建到其組對應數據源的數據庫
     *   調用Future的原因是,是因爲建表很耗時間,所以採用異步執行
     *
     * */
    protected GenericDelegator(String delegatorFullName) throws GenericEntityException {

        this.setDelegatorNames(delegatorFullName);
        //獲取基本的delegator中的信息
        this.delegatorInfo = EntityConfig.getInstance().getDelegator(delegatorBaseName);

        String kekText;
        // before continuing, if there is a tenantId use the base delegator to see if it is valid
        if (UtilValidate.isNotEmpty(this.delegatorTenantId)) {
            Delegator baseDelegator = DelegatorFactory.getDelegator(this.delegatorBaseName);
            GenericValue tenant = EntityQuery.use(baseDelegator).from("Tenant").where("tenantId", this.delegatorTenantId).cache(true).queryOne();
            if (tenant == null) {
                throw new GenericEntityException("No Tenant record found for delegator [" + this.delegatorFullName + "] with tenantId [" + this.delegatorTenantId + "]");
            } else if ("Y".equals(tenant.getString("disabled"))) {
                throw new GenericEntityException("No Tenant record found for delegator [" + this.delegatorFullName + "] with tenantId [" + this.delegatorTenantId + "]");
            }
            GenericValue kekValue = EntityQuery.use(baseDelegator).from("TenantKeyEncryptingKey").where("tenantId", getDelegatorTenantId()).cache(true).queryOne();
            if (kekValue != null) {
                kekText = kekValue.getString("kekText");
            } else {
                kekText = this.delegatorInfo.getKeyEncryptingKey();
            }
        } else {
            kekText = this.delegatorInfo.getKeyEncryptingKey();
        }

        this.modelReader = ModelReader.getModelReader(delegatorBaseName);
        this.modelGroupReader = ModelGroupReader.getModelGroupReader(delegatorBaseName);

        cache = new Cache(delegatorFullName);

        //對實體進行檢查 有檢查組裏面是否有對應實體 實體名是否是保留字 建立視圖一個字段是否被引用多次
        List<String> warningList = new LinkedList<String>();
        Debug.logInfo("Doing entity definition check...", module);
        ModelEntityChecker.checkEntities(this, warningList);
        if (warningList.size() > 0) {
            Debug.logWarning("=-=-=-=-= Found " + warningList.size() + " warnings when checking the entity definitions:", module);
            for (String warning: warningList) {
                Debug.logWarning(warning, module);
            }
        }

        //獲取當前delegator中的groupNames集合,遍歷創建對應的GenericHelper,同時在數據庫中創建未創建的表和字段
        Set<String> groupNames = getModelGroupReader().getGroupNames(delegatorBaseName);
        List<Future<Void>> futures = new LinkedList<Future<Void>>();
        for (String groupName: groupNames) {
            futures.add(ExecutionPool.GLOBAL_BATCH.submit(createHelperCallable(groupName)));
        }
        ExecutionPool.getAllFutures(futures);

        // NOTE: doing some things before the ECAs and such to make sure it is in place just in case it is used in a service engine startup thing or something

        // setup the crypto class; this also after the delegator is in the cache otherwise we get infinite recursion
        this.crypto = new EntityCrypto(this, kekText);
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章