<commit_before>/*******************************************************************************
 * Copyright (c) 2011, 2016 Eurotech and/or its affiliates
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Eurotech - initial API and implementation
 *******************************************************************************/
package org.eclipse.kapua.locator.guice;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.eclipse.kapua.locator.KapuaLocatorErrorCodes;
import org.eclipse.kapua.locator.KapuaLocatorException;

public class LocatorConfig {

    private static final String SERVICE_RESOURCE_INTERFACES = "provided.api";
    private static final String SERVICE_RESOURCE_PACKAGES = "packages.package";

    private final URL url;
    private final List<String> packageNames;
    private final List<String> providedInterfaceNames;

    private LocatorConfig(URL url) {
        this.url = url;
        packageNames = new ArrayList<>();
        providedInterfaceNames = new ArrayList<>();
    }

    public static LocatorConfig fromURL(URL url) throws KapuaLocatorException {

        if (url == null) {
            throw new IllegalArgumentException();
        }

        LocatorConfig config = new LocatorConfig(url);

        XMLConfiguration xmlConfig;
        try {
            xmlConfig = new XMLConfiguration(url);
        } catch (ConfigurationException e) {
            throw new KapuaLocatorException(KapuaLocatorErrorCodes.INVALID_CONFIGURATION, e);
        }

        Object props = xmlConfig.getProperty(SERVICE_RESOURCE_PACKAGES);
        if (props instanceof Collection) {
            config.packageNames.addAll((Collection<String>) props);
        }
        if (props instanceof String) {
            config.packageNames.add((String) props);
        }

        props = xmlConfig.getProperty(SERVICE_RESOURCE_INTERFACES);
        if (props instanceof Collection) {
            config.providedInterfaceNames.addAll((Collection<String>) props);
        }
        if (props instanceof String) {
            config.providedInterfaceNames.add((String) props);
        }

        return config;
    }

    public URL getURL() {
        return url;
    }

    public Collection<String> getPackageNames() {
        return Collections.unmodifiableCollection(packageNames);
    }

    public Collection<String> getProvidedInterfaceNames() {
        return Collections.unmodifiableCollection(providedInterfaceNames);
    }
}<commit_msg>Cleanup initialization of LocatorConfig<commit_after>/*******************************************************************************
 * Copyright (c) 2011, 2017 Eurotech and/or its affiliates
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Eurotech - initial API and implementation
 *     Red Hat Inc
 *******************************************************************************/
package org.eclipse.kapua.locator.guice;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.eclipse.kapua.locator.KapuaLocatorErrorCodes;
import org.eclipse.kapua.locator.KapuaLocatorException;

public class LocatorConfig {

    private static final String SERVICE_RESOURCE_INTERFACES = "provided.api";
    private static final String SERVICE_RESOURCE_PACKAGES = "packages.package";

    private final URL url;
    private final List<String> packageNames;
    private final List<String> providedInterfaceNames;

    private LocatorConfig(final URL url, final List<String> packageNames, final List<String> providedInterfaceNames) {
        this.url = url;
        this.packageNames = packageNames;
        this.providedInterfaceNames = providedInterfaceNames;
    }

    public static LocatorConfig fromURL(final URL url) throws KapuaLocatorException {

        if (url == null) {
            throw new IllegalArgumentException("'url' must not be null");
        }

        final List<String> packageNames = new ArrayList<>();
        final List<String> providedInterfaceNames = new ArrayList<>();

        final XMLConfiguration xmlConfig;
        try {
            xmlConfig = new XMLConfiguration(url);
        } catch (ConfigurationException e) {
            throw new KapuaLocatorException(KapuaLocatorErrorCodes.INVALID_CONFIGURATION, e);
        }

        Object props = xmlConfig.getProperty(SERVICE_RESOURCE_PACKAGES);
        if (props instanceof Collection<?>) {
            addAllStrings(packageNames, (Collection<?>) props);
        }
        if (props instanceof String) {
            packageNames.add((String) props);
        }

        props = xmlConfig.getProperty(SERVICE_RESOURCE_INTERFACES);
        if (props instanceof Collection<?>) {
            addAllStrings(providedInterfaceNames, (Collection<?>) props);
        }
        if (props instanceof String) {
            providedInterfaceNames.add((String) props);
        }

        return new LocatorConfig(url, Collections.unmodifiableList(packageNames), Collections.unmodifiableList(providedInterfaceNames));
    }

    private static void addAllStrings(final List<String> list, Collection<?> other) {
        for (Object entry : other) {
            if (entry instanceof String) {
                list.add((String) entry);
            }
        }
    }

    public URL getURL() {
        return url;
    }

    public Collection<String> getPackageNames() {
        return packageNames;
    }

    public Collection<String> getProvidedInterfaceNames() {
        return providedInterfaceNames;
    }
}
{'fraction_non_alphanumeric': 0.13034493721189, 'fraction_numerical': 0.003814973772055317, 'mean_word_length': 3.390788555478018, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5530529', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1525, 'n_words': 404}