All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.yawp.commons.utils.ServiceLookup Maven / Gradle / Ivy

There is a newer version: 2.08alpha
Show newest version
package io.yawp.commons.utils;

import java.util.List;

public class ServiceLookup {

    public static final String SERVICES_PATH = "META-INF/services/";

    public static  T lookup(Class clazz) {
        ResourceFinder finder = new ResourceFinder(SERVICES_PATH);
        return lookup(finder, clazz);
    }

    public static  T lookup(Class clazz, ClassLoader classLoader) {
        ResourceFinder finder = new ResourceFinder(SERVICES_PATH, classLoader);
        return lookup(finder, clazz);
    }

    private static  T lookup(ResourceFinder finder, Class clazz) {
        try {
            List clazzes = finder.findAllImplementations(clazz);

            if(clazzes.size() == 0) {
                throw new RuntimeException(String.format("No service implementation for %s.", clazz.getSimpleName()));
            }

            return (T) clazzes.get(0).newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy