Skip to content
Snippets Groups Projects
environment.go 3.09 KiB
Newer Older
vito.he's avatar
vito.he committed
/*
 * 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.
 */

vito.he's avatar
vito.he committed
package config

vito.he's avatar
vito.he committed
import (
	"container/list"
vito.he's avatar
vito.he committed
	"sync"
)
vito.he's avatar
vito.he committed

vito.he's avatar
vito.he committed
// There is dubbo.properties file and application level config center configuration which higner than normal config center in java. So in java the
// configuration sequence will be config center > application level config center > dubbo.properties > spring bean configuration.
// But in go, neither the dubbo.properties file or application level config center configuration will not support for the time being.
// We just have config center configuration which can override configuration in consumer.yaml & provider.yaml.
// But for add these features in future ,I finish the environment struct following Environment class in java.
vito.he's avatar
vito.he committed
type Environment struct {
vito.he's avatar
vito.he committed
	configCenterFirst bool
	externalConfigs   sync.Map
	externalConfigMap sync.Map
vito.he's avatar
vito.he committed
}

var (
	instance *Environment
	once     sync.Once
)
vito.he's avatar
vito.he committed

func GetEnvInstance() *Environment {
	once.Do(func() {
vito.he's avatar
vito.he committed
		instance = &Environment{configCenterFirst: true}
vito.he's avatar
vito.he committed
	})
	return instance
}
vito.he's avatar
vito.he committed

//func (env *Environment) SetConfigCenterFirst() {
//	env.configCenterFirst = true
//}

//func (env *Environment) ConfigCenterFirst() bool {
//	return env.configCenterFirst
//}

func (env *Environment) UpdateExternalConfigMap(externalMap map[string]string) {
	for k, v := range externalMap {
		env.externalConfigMap.Store(k, v)
	}
}

func (env *Environment) Configuration() *list.List {
vito.he's avatar
vito.he committed
	list := list.New()
vito.he's avatar
vito.he committed
	memConf.setProperties(env.externalConfigMap)
	list.PushBack(memConf)
	return list
}

type InmemoryConfiguration struct {
func newInmemoryConfiguration() *InmemoryConfiguration {
	return &InmemoryConfiguration{}
vito.he's avatar
vito.he committed
}
func (conf *InmemoryConfiguration) setProperties(p sync.Map) {
	conf.store = p
}

func (conf *InmemoryConfiguration) GetProperty(key string) (bool, string) {
	v, ok := conf.store.Load(key)
vito.he's avatar
vito.he committed
	if ok {
vito.he's avatar
vito.he committed
	}
	return false, ""

vito.he's avatar
vito.he committed
}

func (conf *InmemoryConfiguration) GetSubProperty(subKey string) map[string]struct{} {
	properties := make(map[string]struct{})
	conf.store.Range(func(key, value interface{}) bool {
		if idx := strings.Index(key.(string), subKey); idx >= 0 {
			after := key.(string)[idx+len(subKey):]
			if i := strings.Index(after, "."); i >= 0 {
				properties[after[0:strings.Index(after, ".")]] = struct{}{}
			}

		}
		return true
	})
	return properties
}