PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/init/InitStrategy.java

http://github.com/hudson/hudson
Java | 115 lines | 58 code | 16 blank | 41 comment | 8 complexity | e26e66c8802ca52bdffa2bd6aa561c86 MD5 | raw file
  1package hudson.init;
  2
  3import org.kohsuke.MetaInfServices;
  4import org.jvnet.hudson.reactor.Task;
  5
  6import java.io.File;
  7import java.io.FilenameFilter;
  8import java.io.IOException;
  9import java.util.List;
 10import java.util.ArrayList;
 11import java.util.Arrays;
 12import java.util.logging.Logger;
 13
 14import hudson.PluginManager;
 15import hudson.util.Service;
 16
 17/**
 18 * Strategy pattern of the various key decision making during the Hudson initialization.
 19 *
 20 * <p>
 21 * Because the act of initializing plugins is a part of the Hudson initialization,
 22 * this extension point cannot be implemented in a plugin. You need to place your jar
 23 * inside {@code WEB-INF/lib} instead.
 24 *
 25 * <p>
 26 * To register, put {@link MetaInfServices} on your implementation.
 27 * 
 28 * @author Kohsuke Kawaguchi
 29 */
 30public class InitStrategy {
 31    /**
 32     * Returns the list of *.hpi and *.hpl to expand and load.
 33     *
 34     * <p>
 35     * Normally we look at {@code $HUDSON_HOME/plugins/*.hpi} and *.hpl.
 36     *
 37     * @return
 38     *      never null but can be empty. The list can contain different versions of the same plugin,
 39     *      and when that happens, Hudson will ignore all but the first one in the list.
 40     */
 41    public List<File> listPluginArchives(PluginManager pm) throws IOException {
 42        File[] hpi = pm.rootDir.listFiles(new FilterByExtension(".hpi")); // plugin jar file
 43        File[] hpl = pm.rootDir.listFiles(new FilterByExtension(".hpl")); // linked plugin. for debugging.
 44        if (hpi==null || hpl==null)
 45            throw new IOException("Hudson is unable to create " + pm.rootDir + "\nPerhaps its security privilege is insufficient");
 46
 47        List<File> r = new ArrayList<File>();
 48
 49        // the ordering makes sure that during the debugging we get proper precedence among duplicates.
 50        // for example, while doing "mvn hpi:run" on a plugin that's bundled with Hudson, we want to the
 51        // *.hpl file to override the bundled hpi file.
 52        getBundledPluginsFromProperty(r);
 53        r.addAll(Arrays.asList(hpl));
 54        r.addAll(Arrays.asList(hpi));
 55
 56        return r;
 57    }
 58
 59    /**
 60     * Lists up additional bundled plugins from the system property.
 61     *
 62     * For use in the "mvn hudson-dev:run".
 63     * TODO: maven-hpi-plugin should inject its own InitStrategy instead of having this in the core.
 64     */
 65    protected void getBundledPluginsFromProperty(List<File> r) {
 66        String hplProperty = System.getProperty("hudson.bundled.plugins");
 67        if (hplProperty != null) {
 68            for (String hplLocation : hplProperty.split(",")) {
 69                File hpl = new File(hplLocation.trim());
 70                if (hpl.exists())
 71                    r.add(hpl);
 72                else
 73                    LOGGER.warning("bundled plugin " + hplLocation + " does not exist");
 74            }
 75        }
 76    }
 77
 78    /**
 79     * Selectively skip some of the initialization tasks.
 80     * 
 81     * @return
 82     *      true to skip the execution.
 83     */
 84    public boolean skipInitTask(Task task) {
 85        return false;
 86    }
 87
 88
 89    /**
 90     * Obtains the instance to be used.
 91     */
 92    public static InitStrategy get(ClassLoader cl) throws IOException {
 93        List<InitStrategy> r = Service.loadInstances(cl, InitStrategy.class);
 94        if (r.isEmpty())    return new InitStrategy();      // default
 95
 96        InitStrategy s = r.get(0);
 97        LOGGER.fine("Using "+s+" as InitStrategy");
 98        return s;
 99    }
100
101    private static final Logger LOGGER = Logger.getLogger(InitStrategy.class.getName());
102
103    private static class FilterByExtension implements FilenameFilter {
104        private final String extension;
105
106        public FilterByExtension(String extension) {
107            this.extension = extension;
108        }
109
110        public boolean accept(File dir, String name) {
111            return name.endsWith(extension)        // plugin jar file
112                || name.endsWith(".hpl");       // linked plugin. for debugging.
113        }
114    }
115}