All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.kylin.common.KylinConfigExt Maven / Gradle / Ivy

There is a newer version: 5.0.0-beta
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.kylin.common;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import lombok.val;
import org.apache.commons.lang3.text.StrSubstitutor;

import com.google.common.collect.Maps;

/**
 * Extends a KylinConfig with additional overrides.
 */
@SuppressWarnings("serial")
public class KylinConfigExt extends KylinConfig {

    private final Map overrides;
    final KylinConfig base;

    public static KylinConfigExt createInstance(KylinConfig kylinConfig, Map overrides) {
        if (kylinConfig instanceof KylinConfigExt) {
            return new KylinConfigExt((KylinConfigExt) kylinConfig, overrides);
        } else {
            return new KylinConfigExt(kylinConfig, overrides);
        }
    }

    private KylinConfigExt(KylinConfig base, Map overrides) {
        super(base.getRawAllProperties(), true);
        if (base.getClass() != KylinConfig.class) {
            throw new IllegalArgumentException();
        }
        this.base = base;
        this.overrides = BCC.check(overrides);
    }

    private KylinConfigExt(KylinConfigExt ext, Map overrides) {
        super(ext.base.getRawAllProperties(), true);
        this.base = ext.base;
        this.overrides = BCC.check(overrides);
    }

    @Override
    public String getOptional(String prop, String dft) {
        String value = overrides.get(prop);
        if (value != null) {
            return getSubstitutor().replace(value);
        } else {
            return super.getOptional(prop, dft);
        }
    }

    @Override
    protected Properties getAllProperties() {
        Properties result = new Properties();
        result.putAll(super.getAllProperties());
        result.putAll(overrides);
        return result;
    }

    @Override
    public Map getReadonlyProperties() {
        val substitutor = getSubstitutor();
        HashMap config = Maps.newHashMap();
        for (Map.Entry entry : this.overrides.entrySet()) {
            config.put(entry.getKey(), substitutor.replace(entry.getValue()));
        }
        return config;
    }

    @Override
    protected StrSubstitutor getSubstitutor() {
        // overrides > env > properties
        final Map all = Maps.newHashMap();
        // all.putAll((Map) properties); // use putAll will call CompositeMapView.size()sss
        for(Map.Entry e: properties.entrySet()) {
            all.put(e.getKey().toString(), e.getValue());
        }
        all.putAll(STATIC_SYSTEM_ENV);
        all.putAll(overrides);
        return new StrSubstitutor(all);
    }

    public Map getExtendedOverrides() {
        return overrides;
    }

    @Override
    public KylinConfig base() {
        return this.base;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy