下面列出了怎么用com.typesafe.config.ConfigOrigin的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Object checkValue(String propName, Object value, ConfigOrigin loc) {
if (value instanceof String) {
try {
return Integer.parseInt((String) value);
}
catch (NumberFormatException e) {
throw cannotConvert(loc, propName, value, "integer");
}
}
else if (value instanceof Integer) {
return value;
}
else {
throw cannotConvert(loc, propName, value, "integer");
}
}
@Override
public Object checkValue(String propName, Object value, ConfigOrigin loc) {
if ("off".equals(value)) {
return Integer.MAX_VALUE;
}
else if (value instanceof String) {
try {
return Integer.parseInt((String) value);
}
catch (NumberFormatException e) {
throw cannotConvert(loc, propName, value, "integer");
}
}
else if (value instanceof Integer) {
Integer i = (Integer) value;
if (i > 0) {
return i;
}
}
throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" +
propName + "'. Must be an " + toString());
}
@Override
public Object checkValue(String propName, Object value, ConfigOrigin loc) {
if (value instanceof String) {
try {
return Double.parseDouble((String) value);
}
catch (NumberFormatException e) {
throw cannotConvert(loc, propName, value, "number");
}
}
else if (value instanceof Double) {
return value;
}
else {
throw cannotConvert(loc, propName, value, "number");
}
}
@Override
public Object checkValue(String propName, Object value, ConfigOrigin loc) {
if (value instanceof List) {
List<?> l = (List<?>) value;
boolean allStrings = true;
for (Object o : l) {
if (!(o instanceof String)) {
allStrings = false;
}
}
if (allStrings) {
return value;
}
}
else if (value instanceof String) {
// also allow comma separated strings in a string
return Arrays.asList(((String) value).split(",\\s*"));
}
throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" +
propName + "'. Must be a " + toString());
}
@Override
public Object checkValue(String propName, String value, ConfigOrigin loc) {
if (!choices.contains(value)) {
throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" +
propName + "'. Valid values are: " + pp(choices));
}
return value;
}
@Override
public Object checkValue(String propName, String value, ConfigOrigin loc) {
try {
URI u = new URI(value);
return value;
}
catch (Exception e) {
throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" +
propName + "'");
}
}
@Override
public Object checkValue(String propName, Object value, ConfigOrigin loc) {
if (value instanceof String) {
return Boolean.valueOf((String) value);
}
else if (value instanceof Boolean) {
return value;
}
else {
throw cannotConvert(loc, propName, value, "boolean");
}
}
@Override
public Object checkValue(String propName, Object value, ConfigOrigin loc) {
Integer ivalue = (Integer) super.checkValue(propName, value, loc);
if (ivalue <= 0) {
throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" +
propName + "'. Must be a " + toString());
}
return ivalue;
}
@Override
public Object checkValue(String propName, Object value, ConfigOrigin loc) {
Integer ivalue = (Integer) super.checkValue(propName, value, loc);
if (ivalue < 0) {
throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" +
propName + "'. Must be a " + toString());
}
return ivalue;
}
@Override
public Object checkValue(String propName, Object value, ConfigOrigin loc) {
Double dvalue = (Double) super.checkValue(propName, value, loc);
if (dvalue < 0) {
throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" +
propName + "'. Must be a " + toString());
}
return dvalue;
}
@Override
public Object checkValue(String propName, Object value, ConfigOrigin loc) {
Double dvalue = (Double) super.checkValue(propName, value, loc);
if (dvalue < l || dvalue > h) {
throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" +
propName + "'. Must be a " + toString());
}
return dvalue;
}
@Override
public Object checkValue(String propName, String value, ConfigOrigin loc) {
try {
WallTime.timeToSeconds(value);
}
catch (IllegalArgumentException e) {
throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid time value '" + value + "' for property '" +
propName + "'. Mist be a " + toString());
}
return value;
}
@Override
public Object checkValue(String propName, String value, ConfigOrigin loc) {
String[] els = value.split(",\\s*");
if (els.length == 2) {
try {
Integer.parseInt(els[0]);
Integer.parseInt(els[1]);
return value;
}
catch (NumberFormatException e) {
}
}
throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" +
propName + "'. Must be a " + toString());
}
@Override
public Object checkValue(String propName, String value, ConfigOrigin loc) {
File f = new File(value);
if (!f.exists()) {
throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" +
propName + "'. File does not exist.");
}
return value;
}
@Override
public ConfigOrigin origin() {
return c.origin();
}
@Override
public ConfigOrigin origin() {
return serviceScopedConfig.origin();
}
@Override
public ConfigOrigin origin() {
return config.origin();
}
@Override
public ConfigOrigin origin() {
return baseConfig.origin();
}
@Override
public ConfigOrigin origin() {
return config.origin();
}
public ValueLocationPair(Object value, ConfigOrigin loc) {
this.value = value;
this.loc = loc;
}
public static String location(ConfigOrigin loc) {
return loc.filename() + ":" + loc.lineNumber();
}
@SuppressWarnings("unchecked")
public Object check(String propName, Object value, ConfigOrigin loc) {
return checkValue(propName, (T) value, loc);
}
protected RuntimeException cannotConvert(ConfigOrigin loc, String propName, Object value, String toWhat) {
return new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tCannot convert value '" + value + "' for property '" +
propName + "' to " + toWhat);
}
@Override
public Object checkValue(String propName, String value, ConfigOrigin loc) {
// all values accepted
return value;
}
@Override
public Object checkValue(String propName, Object value, ConfigOrigin loc) {
return value;
}
public SwiftConfigSchema() {
validNames = new HashSet<String>();
schema = ConfigFactory.parseResources("swift.conf.schema");
schema = schema.resolve();
if (schema.isEmpty()) {
throw new RuntimeException("Could not find swift.conf.schema");
}
info = new ConfigTree<Info>();
for (Map.Entry<String, ConfigValue> e : schema.entrySet()) {
String k = e.getKey();
String nk = k.replace("\"*\"", "*");
String type = null;
Object defaultValue = null;
String doc = null;
ConfigOrigin loc = null;
if (k.endsWith(".\"_type\"")) {
type = schema.getString(k);
nk = nk.substring(0, nk.lastIndexOf('.'));
loc = e.getValue().origin();
}
else if (k.indexOf(".\"_") == -1){
type = schema.getString(k);
loc = e.getValue().origin();
}
else if (k.endsWith(".\"_default\"")){
defaultValue = e.getValue().unwrapped();
nk = nk.substring(0, nk.lastIndexOf('.'));
}
else if (k.endsWith(".\"_doc\"")){
doc = stripDoc((String) e.getValue().unwrapped());
nk = nk.substring(0, nk.lastIndexOf('.'));
}
else if (k.indexOf(".\"_") != -1) {
continue;
}
Info i = info.get(nk);
if (i == null) {
i = new Info();
info.put(nk, i);
setValid(nk);
}
if (type != null) {
if (type.startsWith("?")) {
i.optional = true;
type = type.substring(1);
}
i.type = getTypeInstance(type, e.getValue());
i.typeSpec = type;
}
if (defaultValue != null) {
i.value = defaultValue;
}
if (doc != null) {
i.doc = doc;
}
if (loc != null) {
i.loc = loc;
}
}
}
private String loc(ConfigOrigin o) {
return o.filename() + ":" + o.lineNumber();
}
public SwiftConfigException(ConfigOrigin loc, String message) {
super((loc.filename() == null ? loc.description() : loc.filename() + ":" + loc.lineNumber()) + " " + message);
}
protected static String location(Config config, String key) {
ConfigOrigin origin = config.getValue(key).origin();
return origin.description() + ":" + origin.lineNumber();
}
public HoconJsonLocation(final ConfigOrigin origin) {
super(origin.description(), -1L, origin.lineNumber(), -1);
this.origin = origin;
}