<commit_before>/**
 * Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
 * All rights reserved. Use is subject to license terms. See LICENSE.TXT
 */
package org.diirt.util.config;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * A wrapper for ServiceLoader that works in OSGi as well.
 * <p>
 * OSGi is known to break the ServiceLoader mechanism. Unfortunately, we have
 * not been able to make the standard based solution based on Ares SPI-Fly work.
 * Therefore we have created this wrapper that detects whether the class has
 * been loaded with OSGi. If not, uses the standard ServiceLoader. If so, uses
 * introspection to access the OSGi framework and replicate the functionality.
 * <p>
 * Note that the implementation on OSGi will not give all the nice debugging
 * information that the standard ServiceLoader implementation does. It also is
 * not optimized: makes heavy use of introspection and does not use lazy
 * initialization. It's essentially a hack to make things to work.
 *
 * @author carcassi
 */
public class ServiceLoaderOSGiWrapper {

    public static <T> Iterable<T> load(Class<T> serviceClazz) {
        if (isOSGi(serviceClazz)) {
            return loadOSGi(serviceClazz);
        } else {
            return ServiceLoader.load(serviceClazz);
        }
    }

    private static boolean isOSGi(Class<?> clazz) {
        return osgi.frameworkUtilClass != null;
    }

    private static <T> List<T> loadOSGi(Class<T> serviceClazz) {
        // TODO: improve logging
        Object bundle = osgi.getBundle(serviceClazz);<commit_msg>util: automating logging for ServiceLoader<commit_after>/**
 * Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
 * All rights reserved. Use is subject to license terms. See LICENSE.TXT
 */
package org.diirt.util.config;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * A wrapper for ServiceLoader that works in OSGi as well.
 * <p>
 * OSGi is known to break the ServiceLoader mechanism. Unfortunately, we have
 * not been able to make the standard based solution based on Ares SPI-Fly work.
 * Therefore we have created this wrapper that detects whether the class has
 * been loaded with OSGi. If not, uses the standard ServiceLoader. If so, uses
 * introspection to access the OSGi framework and replicate the functionality.
 * <p>
 * Note that the implementation on OSGi will not give all the nice debugging
 * information that the standard ServiceLoader implementation does. It also is
 * not optimized: makes heavy use of introspection and does not use lazy
 * initialization. It's essentially a hack to make things to work.
 *
 * @author carcassi
 */
public class ServiceLoaderOSGiWrapper {
    
    public static <T> void load(Class<T> serviceClazz, Logger log, Consumer<T> consumer) {
        log.log(Level.CONFIG, "Fetching {0}s", serviceClazz.getSimpleName());
        int count = 0;
        for (T service : ServiceLoaderOSGiWrapper.load(serviceClazz)) {
            log.log(Level.CONFIG, "Found {0} ({1})", new Object[] {serviceClazz.getSimpleName(), service.getClass().getSimpleName()});
            consumer.accept(service);
            count++;
        }
        log.log(Level.CONFIG, "Found {0} {1}s", new Object[] {count, serviceClazz.getSimpleName()});
    }

    public static <T> Iterable<T> load(Class<T> serviceClazz) {
        if (isOSGi(serviceClazz)) {
            return loadOSGi(serviceClazz);
        } else {
            return ServiceLoader.load(serviceClazz);
        }
    }

    private static boolean isOSGi(Class<?> clazz) {
        return osgi.frameworkUtilClass != null;
    }

    private static <T> List<T> loadOSGi(Class<T> serviceClazz) {
        // TODO: improve logging
        Object bundle = osgi.getBundle(serviceClazz);
{'fraction_non_alphanumeric': 0.09230769230769231, 'fraction_numerical': 0.004013377926421404, 'mean_word_length': 3.629514963880289, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4741762', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1243, 'n_words': 448}