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

org.yx.db.conn.CommonConfigFactory Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
/**
 * Copyright (C) 2016 - 2030 youtongluan.
 *
 * 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 org.yx.db.conn;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.yx.annotation.Bean;
import org.yx.conf.AppInfo;
import org.yx.log.Logs;
import org.yx.util.CollectionUtil;

@Bean
public class CommonConfigFactory implements DBConfigFactory {

	protected Map> parseFromAppInfo(String dbName) {
		Map map = AppInfo.subMap("s.db." + dbName + ".");
		if (map.isEmpty()) {
			return null;
		}
		Map configMap = CollectionUtil.flatMapToTree(map);
		return pureMap(configMap);
	}

	@Override
	public List create(String name) throws Exception {
		Map> maps = parseFromAppInfo(name);
		List list = new ArrayList<>();
		if (maps == null || maps.isEmpty()) {
			return list;
		}
		for (Map map : maps.values()) {
			list.add(DBConfig.create(map));
		}
		return list;
	}

	public static Map> pureMap(Map objectMap) {
		if (objectMap == null || objectMap.isEmpty()) {
			return Collections.emptyMap();
		}
		final Map> ret = new HashMap>();

		for (Map.Entry entry : objectMap.entrySet()) {
			String catagory = entry.getKey();
			Object value = entry.getValue();
			if (!(value instanceof Map)) {
				Logs.db().info("{} is not valid config, value : {}", catagory, value);
				continue;
			}
			@SuppressWarnings("unchecked")
			Map config = (Map) value;
			Map real = new HashMap<>();
			for (Map.Entry e2 : config.entrySet()) {
				String propertyName = e2.getKey();
				Object v = e2.getValue();
				if (!(v instanceof String)) {
					Logs.db().info("{}.{} is not valid config,value:{}", catagory, propertyName, v);
					continue;
				}
				real.put(propertyName, (String) v);
			}
			if (real.size() > 0) {
				ret.put(catagory, real);
			}
		}
		return ret;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy