private static Object toValue()

in meecrowave-core/src/main/java/org/apache/meecrowave/runner/Cli.java [160:251]


    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 (long.class == type) {
            return Long.parseLong(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 "realm": // org.foo.Impl:attr1=val1;attr2=val2
                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 "security-constraint": // attr1=val1;attr2=val2
                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 "connector": // org.foo.Impl:attr1=val1;attr2=val2
                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);
        }
    }