File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\init\InitStrategy.java.html

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