/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

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