use of org.apache.xbean.recipe.ObjectRecipe in project meecrowave by apache.
the class Cli method toValue.
private static Object toValue(final Meecrowave.Builder builder, final String name, final String[] optionValues, final Class<?> type) {
if (optionValues == null || optionValues.length == 0) {
return null;
}
// decode options while it is strings
IntStream.range(0, optionValues.length).forEach(i -> optionValues[i] = builder.getExtension(Meecrowave.ValueTransformers.class).apply(optionValues[i]));
if (String.class == type) {
return optionValues[0];
}
if (int.class == type) {
return Integer.parseInt(optionValues[0]);
}
if (File.class == type) {
return new File(optionValues[0]);
}
if (Properties.class == type) {
final Properties props = new Properties();
Stream.of(optionValues).map(v -> v.split("=")).forEach(v -> props.setProperty(v[0], v[1]));
return props;
}
if (Map.class == type) {
final Map<String, String> props = new HashMap<>();
Stream.of(optionValues).map(v -> v.split("=")).forEach(v -> props.put(v[0], v[1]));
return props;
}
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
switch(name) {
case // org.foo.Impl:attr1=val1;attr2=val2
"realm":
try {
int end = optionValues[0].indexOf(':');
if (end < 0) {
return loader.loadClass(optionValues[0]).newInstance();
}
final ObjectRecipe recipe = new ObjectRecipe(optionValues[0].substring(0, end));
Stream.of(optionValues[0].substring(end + 1, optionValues[0].length()).split(";")).map(v -> v.split("=")).forEach(v -> recipe.setProperty(v[0], v[1]));
return recipe.create(loader);
} catch (final Exception cnfe) {
throw new IllegalArgumentException(optionValues[0]);
}
case // attr1=val1;attr2=val2
"security-constraint":
return Stream.of(optionValues).map(item -> {
try {
final ObjectRecipe recipe = new ObjectRecipe(Meecrowave.SecurityConstaintBuilder.class);
Stream.of(item.split(";")).map(v -> v.split("=")).forEach(v -> recipe.setProperty(v[0], v[1]));
return recipe.create(loader);
} catch (final Exception cnfe) {
throw new IllegalArgumentException(optionValues[0]);
}
}).collect(toList());
case "login-config":
try {
final ObjectRecipe recipe = new ObjectRecipe(Meecrowave.LoginConfigBuilder.class);
Stream.of(optionValues[0].split(";")).map(v -> v.split("=")).forEach(v -> recipe.setProperty(v[0], v[1]));
return recipe.create(loader);
} catch (final Exception cnfe) {
throw new IllegalArgumentException(optionValues[0]);
}
case // org.foo.Impl:attr1=val1;attr2=val2
"connector":
return Stream.of(optionValues).map(v -> {
try {
int end = v.indexOf(':');
if (end < 0) {
return new Connector(v);
}
final Connector connector = new Connector(optionValues[0].substring(0, end));
Stream.of(v.substring(end + 1, v.length()).split(";")).map(i -> i.split("=")).forEach(i -> connector.setProperty(i[0], i[1]));
return connector;
} catch (final Exception cnfe) {
throw new IllegalArgumentException(optionValues[0]);
}
}).collect(toList());
default:
throw new IllegalArgumentException("Unsupported " + name);
}
}
Aggregations