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

net.java.trueupdate.util.Services Maven / Gradle / Ivy

There is a newer version: 0.8.1
Show newest version
/*
 * Copyright (C) 2013 Schlichtherle IT Services & Stimulus Software.
 * All rights reserved. Use is subject to license terms.
 */
package net.java.trueupdate.util;

import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import javax.annotation.concurrent.Immutable;

/**
 * Loads a service implementation using the current thread's context class
 * loader.
 *
 * @author Christian Schlichtherle
 */
@Immutable
public final class Services {

    /**
     * Returns an instance of the given service interface or class.
     * The service implementation is loaded using the current thread's context
     * class loader.
     *
     * @throws ServiceConfigurationError if no implementation of the given
     *         service interface or class can be found on the class path.
     * @see    ServiceLoader#load(Class)
     */
    public static  T load(final Class service) {
        try {
            return ServiceLoader.load(service).iterator().next();
        } catch (NoSuchElementException ex) {
            throw new ServiceConfigurationError(String.format(Locale.ENGLISH,
                    "Could not find an implementation of the service %s on the class path.",
                    service), ex);
        }
    }

    private Services() { }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy