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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.scm;
25
26 import hudson.model.AbstractProject;
27 import jenkins.model.Jenkins;
28
29 /**
30 * Maintains the automatically inferred {@link RepositoryBrowser} instance.
31 *
32 * <p>
33 * To reduce the user's work, Hudson tries to infer applicable {@link RepositoryBrowser}
34 * from configurations of other jobs. But this needs caution &mdash; for example,
35 * such inferred {@link RepositoryBrowser} must be recalculated whenever
36 * a job configuration changes somewhere.
37 *
38 * <p>
39 * This class makes such tracking easy by hiding this logic.
40 */
41 final class AutoBrowserHolder {
42 private int cacheGeneration;
43 private RepositoryBrowser cache;
44 private SCM owner;
45
46 public AutoBrowserHolder(SCM owner) {
47 this.owner = owner;
48 }
49
50 public RepositoryBrowser get() {
51 int g = owner.getDescriptor().generation;
52 if(g!=cacheGeneration) {
53 cacheGeneration = g;
54 cache = infer();
55 }
56 return cache;
57 }
58
59 /**
60 * Picks up a {@link RepositoryBrowser} that matches the
61 * given {@link SCM} from existing other jobs.
62 *
63 * @return
64 * null if no applicable configuration was found.
65 */
66 private RepositoryBrowser infer() {
67 for( AbstractProject p : Jenkins.getInstance().getAllItems(AbstractProject.class) ) {
68 SCM scm = p.getScm();
69 if (scm!=null && scm.getClass()==owner.getClass() && scm.getBrowser()!=null &&
70 ((SCMDescriptor)scm.getDescriptor()).isBrowserReusable(scm,owner)) {
71 return scm.getBrowser();
72 }
73 }
74 return null;
75 }
76 }
77