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

panda.dao.sql.expert.SqlExpertConfig Maven / Gradle / Ivy

Go to download

Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.

There is a newer version: 1.8.0
Show newest version
package panda.dao.sql.expert;

import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import panda.bind.json.JsonObject;
import panda.lang.Charsets;
import panda.lang.ClassLoaders;
import panda.lang.Classes;
import panda.log.Log;
import panda.log.Logs;

public class SqlExpertConfig {
	private static Log log = Logs.getLog(SqlExpertConfig.class);
	
	private Map> experts;

	private Map options;

	public SqlExpertConfig() {
		InputStream is = ClassLoaders.getResourceAsStream("sql-experts.json");
		if (is == null) {
			is = ClassLoaders.getResourceAsStream(
				SqlExpertConfig.class.getPackage().getName().replace('.', '/') + "/sql-experts.json");
		}
		if (is == null) {
			throw new RuntimeException("Failed to find sql-experts.json");
		}
		
		JsonObject jo = JsonObject.fromJson(is, Charsets.UTF_8);
		
		setExperts(jo.getJsonObject("experts"));
		setOptions(jo.optJsonObject("options"));
	}

	public Map getOptions() {
		return options;
	}

	public void setOptions(Map options) {
		this.options = options;
	}

	public SqlExpert matchExpert(String name) {
		for (Entry> entry : experts.entrySet()) {
			if (entry.getKey().matcher(name).find()) {
				try {
					return entry.getValue().newInstance();
				}
				catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
		}
		return null;
	}

	public Map> getExperts() {
		return experts;
	}

	@SuppressWarnings("unchecked")
	public void setExperts(Map exps) {
		experts = new LinkedHashMap>();
		for (Entry entry : exps.entrySet()) {
			Class clazz;
			try {
				clazz = (Class)Classes.getClass(entry.getValue().toString());
			}
			catch (ClassNotFoundException e) {
				log.warn("Failed to get class " + entry.getValue() + " for " + entry.getKey());
				continue;
			}

			// case insensitive
			Pattern pattern = Pattern.compile(entry.getKey(), Pattern.DOTALL & Pattern.CASE_INSENSITIVE);
			experts.put(pattern, clazz);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy