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

net.java.dev.vcc.util.ServiceLoaderCache Maven / Gradle / Ivy

Go to download

The API for control of virtual computers running on a virtual computer host

The newest version!
package net.java.dev.vcc.util;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * Created by IntelliJ IDEA.
 * User: user
 * Date: 06-May-2009
 * Time: 18:13:01
 * To change this template use File | Settings | File Templates.
 */
public final class ServiceLoaderCache {

    private final Class serviceClass;

    /**
     * A weak cache of service loader proxies that should allow unloading without a permgen leak (I hope).
     * Guarded by itself.
     */
    private final Map>> serviceLoaderCache =
            new WeakHashMap>>();

    public ServiceLoaderCache(Class serviceClass) {
        this.serviceClass = serviceClass;
    }

    /**
     * Gets the {@link ServiceLoaderProxy} from the weak cache, or creates a new one if needed.
     *
     * @param classloader The classloader we are to get the {@link ServiceLoaderProxy} from.
     * @return The {@link ServiceLoaderProxy} for the specified classloader.
     */
    public final ServiceLoaderProxy get(ClassLoader classloader) {
        synchronized (serviceLoaderCache) {
            WeakReference> ref = serviceLoaderCache.get(classloader);
            if (ref != null) {
                final ServiceLoaderProxy result = ref.get();
                if (result != null) {
                    return result;
                }
            }
            final ServiceLoaderProxy result =
                    ServiceLoaderProxy.load(serviceClass, classloader);
            serviceLoaderCache.put(classloader, new WeakReference>(result));
            return result;
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy