Configuration toMeecrowaveConfiguration()

in meecrowave-arquillian/src/main/java/org/apache/meecrowave/arquillian/MeecrowaveConfiguration.java [123:237]


    Configuration toMeecrowaveConfiguration() {
        final Meecrowave.Builder builder = new Meecrowave.Builder();
        for (final Field field : MeecrowaveConfiguration.class.getDeclaredFields()) {
            final String name = field.getName();
            if ("users".equals(name) || "roles".equals(name) || "cxfServletParams".equals(name)
                    || "loginConfig".equals(name) || "securityConstraints".equals(name)
                    || "realm".equals(name)) {
                continue; // specific syntax
            }

            try {
                final Field configField = Configuration.class.getDeclaredField(field.getName());
                if (!configField.getType().equals(field.getType())) {
                    continue;
                }
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }

                final Object value = field.get(this);
                if (value != null) {
                    if (!configField.isAccessible()) {
                        configField.setAccessible(true);
                    }
                    configField.set(builder, value);
                }
            } catch (final NoSuchFieldException nsfe) {
                // ignored
            } catch (final Exception e) {
                throw new IllegalStateException(e);
            }
        }

        if (httpPort < 0) {
            builder.randomHttpPort();
        }

        // for Map use properties
        if (users != null) {
            final Properties properties = new Properties() {{
                try {
                    load(new ByteArrayInputStream(users.getBytes(StandardCharsets.UTF_8)));
                } catch (final IOException e) {
                    throw new IllegalStateException(e);
                }
            }};
            builder.setUsers(properties.stringPropertyNames().stream().collect(toMap(identity(), properties::getProperty)));
        }
        if (roles != null) {
            final Properties properties = new Properties() {{
                try {
                    load(new ByteArrayInputStream(roles.getBytes(StandardCharsets.UTF_8)));
                } catch (final IOException e) {
                    throw new IllegalStateException(e);
                }
            }};
            builder.setRoles(properties.stringPropertyNames().stream().collect(toMap(identity(), properties::getProperty)));
        }
        if (cxfServletParams != null) {
            final Properties properties = new Properties() {{
                try {
                    load(new ByteArrayInputStream(cxfServletParams.getBytes(StandardCharsets.UTF_8)));
                } catch (final IOException e) {
                    throw new IllegalStateException(e);
                }
            }};
            builder.setCxfServletParams(properties.stringPropertyNames().stream().collect(toMap(identity(), properties::getProperty)));
        }

        // for other not simple type use the Cli syntax
        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (realm != null) {
            try {
                int end = realm.indexOf(':');
                if (end < 0) {
                    builder.setRealm(Realm.class.cast(loader.loadClass(realm).newInstance()));
                } else {
                    final ObjectRecipe recipe = new ObjectRecipe(realm.substring(0, end));
                    Stream.of(realm.substring(end + 1, realm.length()).split(";"))
                            .map(v -> v.split("="))
                            .forEach(v -> recipe.setProperty(v[0], v[1]));
                    builder.setRealm(Realm.class.cast(recipe.create(loader)));
                }
            } catch (final Exception cnfe) {
                throw new IllegalArgumentException(realm);
            }
        }
        if (securityConstraints != null) {
            builder.setSecurityConstraints(Stream.of(securityConstraints.split("|"))
                    .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 Meecrowave.SecurityConstaintBuilder.class.cast(recipe.create(loader));
                        } catch (final Exception cnfe) {
                            throw new IllegalArgumentException(securityConstraints);
                        }
                    }).collect(toList()));
        }
        if (loginConfig != null) {
            try {
                final ObjectRecipe recipe = new ObjectRecipe(Meecrowave.LoginConfigBuilder.class);
                Stream.of(loginConfig.split(";"))
                        .map(v -> v.split("="))
                        .forEach(v -> recipe.setProperty(v[0], v[1]));
                builder.setLoginConfig(Meecrowave.LoginConfigBuilder.class.cast(recipe.create(loader)));
            } catch (final Exception cnfe) {
                throw new IllegalArgumentException(loginConfig);
            }
        }

        return builder;
    }