Skip to content

Commit

Permalink
允许子类扩展YamlConfigurationLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Feb 20, 2017
1 parent 08ed9da commit 9193f48
Showing 1 changed file with 19 additions and 12 deletions.
Expand Up @@ -39,6 +39,7 @@
import org.yaml.snakeyaml.introspector.PropertyUtils;

public class YamlConfigurationLoader implements ConfigurationLoader {

private static final Logger logger = LoggerFactory.getLogger(YamlConfigurationLoader.class);

private final static String DEFAULT_CONFIGURATION = "lealone.yaml";
Expand All @@ -65,12 +66,11 @@ private URL getStorageConfigURL() throws ConfigurationException {
"Expecting URI in variable: [lealone.config]. Please prefix the file with " + required
+ File.separator + " for local files or " + required + "<server>" + File.separator
+ " for remote files. Aborting.");
throw new ConfigurationException("Cannot locate " + configUrl
+ ". If this is a local file, please confirm you've provided " + required + File.separator
+ " as a URI prefix.");
throw new ConfigurationException(
"Cannot locate " + configUrl + ". If this is a local file, please confirm you've provided "
+ required + File.separator + " as a URI prefix.");
}
}

return url;
}

Expand All @@ -90,23 +90,30 @@ public Config loadConfig(URL url) throws ConfigurationException {
throw new AssertionError(e);
}

Constructor configConstructor = new Constructor(Config.class);

TypeDescription engineDesc = new TypeDescription(PluggableEngineDef.class);
engineDesc.putMapPropertyType("parameters", String.class, String.class);
configConstructor.addTypeDescription(engineDesc);
Constructor configConstructor = new Constructor(getConfigClass());
addTypeDescription(configConstructor);

MissingPropertiesChecker propertiesChecker = new MissingPropertiesChecker();
configConstructor.setPropertyUtils(propertiesChecker);
Yaml yaml = new Yaml(configConstructor);
Config result = yaml.loadAs(new ByteArrayInputStream(configBytes), Config.class);
Config result = (Config) yaml.loadAs(new ByteArrayInputStream(configBytes), getConfigClass());
propertiesChecker.check();
return result;
} catch (YAMLException e) {
throw new ConfigurationException("Invalid yaml", e);
}
}

protected Class<?> getConfigClass() {
return Config.class;
}

protected void addTypeDescription(Constructor configConstructor) {
TypeDescription engineDesc = new TypeDescription(PluggableEngineDef.class);
engineDesc.putMapPropertyType("parameters", String.class, String.class);
configConstructor.addTypeDescription(engineDesc);
}

private static class MissingPropertiesChecker extends PropertyUtils {
private final Set<String> missingProperties = new HashSet<>();

Expand All @@ -125,8 +132,8 @@ public Property getProperty(Class<? extends Object> type, String name) throws In

public void check() throws ConfigurationException {
if (!missingProperties.isEmpty()) {
throw new ConfigurationException("Invalid yaml. Please remove properties " + missingProperties
+ " from your lealone.yaml");
throw new ConfigurationException(
"Invalid yaml. Please remove properties " + missingProperties + " from your lealone.yaml");
}
}
}
Expand Down

0 comments on commit 9193f48

Please sign in to comment.