Java Class Loader load(Class serviceClass, ClassLoader loader)

Here you can find the source of load(Class serviceClass, ClassLoader loader)

Description

load

License

Open Source License

Declaration

private static <T> Set<Class<? extends T>> load(Class<T> serviceClass,
            ClassLoader loader) 

Method Source Code

//package com.java2s;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Set;

public class Main {
    private static final String SERVICES = "META-INF/services";

    private static <T> Set<Class<? extends T>> load(Class<T> serviceClass,
            ClassLoader loader) {
        String serviceFile = SERVICES + "/" + serviceClass.getName();

        LinkedHashSet<Class<? extends T>> providers = new LinkedHashSet<Class<? extends T>>();
        LinkedHashSet<Class<? extends T>> vetoedProviders = new LinkedHashSet<Class<? extends T>>();

        try {/*from  ww  w .j a  v  a 2 s  .c  o  m*/
            Enumeration<URL> enumeration = loader.getResources(serviceFile);
            while (enumeration.hasMoreElements()) {
                final URL url = enumeration.nextElement();
                final InputStream is = url.openStream();
                BufferedReader reader = null;

                try {
                    reader = new BufferedReader(new InputStreamReader(is,
                            "UTF-8"));
                    String line = reader.readLine();
                    while (null != line) {
                        line = skipCommentAndTrim(line);

                        if (line.length() > 0) {
                            try {
                                boolean mustBeVetoed = line.startsWith("!");
                                if (mustBeVetoed) {
                                    line = line.substring(1);
                                }

                                Class<? extends T> provider = loader
                                        .loadClass(line).asSubclass(
                                                serviceClass);

                                if (mustBeVetoed) {
                                    vetoedProviders.add(provider);
                                }

                                if (vetoedProviders.contains(provider)) {
                                    providers.remove(provider);
                                } else {
                                    providers.add(provider);
                                }
                            } catch (ClassCastException e) {
                                throw new IllegalStateException(
                                        "Service "
                                                + line
                                                + " does not implement expected type "
                                                + serviceClass.getName());
                            }
                        }
                        line = reader.readLine();
                    }
                } finally {
                    if (reader != null) {
                        reader.close();
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Could not load services for "
                    + serviceClass.getName(), e);
        }
        return providers;
    }

    private static String skipCommentAndTrim(String line) {
        final int comment = line.indexOf('#');
        if (comment > -1) {
            line = line.substring(0, comment);
        }

        line = line.trim();
        return line;
    }
}

Related

  1. getProjectClassLoader(IJavaProject javaProject)
  2. getProjectClassLoader(IJavaProject javaProject, ClassLoader parentClassLoader)
  3. getStreamForPath(ClassLoader loader, String path)
  4. getToolsClassLoader()
  5. isBizServicesLocatorXmlLoaded(File f, ClassLoader classLoader)
  6. load(String className)
  7. load(String path, String className)
  8. loadAllAutoConfigFiles(ClassLoader classloader)
  9. loadArrayClass(ClassLoader cl, String aName)