Search in sources :

Example 6 with InitStep

use of com.google.gerrit.pgm.init.api.InitStep in project gerrit by GerritCodeReview.

the class InitPluginStepsLoader method loadInitStep.

private InitStep loadInitStep(Path jar) {
    try {
        URLClassLoader pluginLoader = URLClassLoader.newInstance(new URL[] { jar.toUri().toURL() }, InitPluginStepsLoader.class.getClassLoader());
        try (JarFile jarFile = new JarFile(jar.toFile())) {
            Attributes jarFileAttributes = jarFile.getManifest().getMainAttributes();
            String initClassName = jarFileAttributes.getValue("Gerrit-InitStep");
            if (initClassName == null) {
                return null;
            }
            @SuppressWarnings("unchecked") Class<? extends InitStep> initStepClass = (Class<? extends InitStep>) pluginLoader.loadClass(initClassName);
            return getPluginInjector(jar).getInstance(initStepClass);
        } catch (ClassCastException e) {
            ui.message("WARN: InitStep from plugin %s does not implement %s (Exception: %s)\n", jar.getFileName(), InitStep.class.getName(), e.getMessage());
            return null;
        } catch (NoClassDefFoundError e) {
            ui.message("WARN: Failed to run InitStep from plugin %s (Missing class: %s)\n", jar.getFileName(), e.getMessage());
            return null;
        }
    } catch (Exception e) {
        ui.message("WARN: Cannot load and get plugin init step for %s (Exception: %s)\n", jar, e.getMessage());
        return null;
    }
}
Also used : InitStep(com.google.gerrit.pgm.init.api.InitStep) URLClassLoader(java.net.URLClassLoader) Attributes(java.util.jar.Attributes) JarFile(java.util.jar.JarFile) IOException(java.io.IOException)

Aggregations

InitStep (com.google.gerrit.pgm.init.api.InitStep)6 IOException (java.io.IOException)1 URLClassLoader (java.net.URLClassLoader)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Attributes (java.util.jar.Attributes)1 JarFile (java.util.jar.JarFile)1