File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\scm\SCMDescriptor.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.Descriptor;
27 import hudson.model.AbstractProject;
28
29 import java.util.List;
30 import java.util.Collections;
31 import java.util.logging.Logger;
32 import static java.util.logging.Level.WARNING;
33 import java.lang.reflect.Field;
34
35 /**
36 * {@link Descriptor} for {@link SCM}.
37 *
38 * @param <T>
39 * The 'self' type that represents the type of {@link SCM} that
40 * this descriptor describes.
41 * @author Kohsuke Kawaguchi
42 */
43 public abstract class SCMDescriptor<T extends SCM> extends Descriptor<SCM> {
44 /**
45 * If this SCM has corresponding {@link RepositoryBrowser},
46 * that type. Otherwise this SCM will not have any repository browser.
47 */
48 public transient final Class<? extends RepositoryBrowser> repositoryBrowser;
49
50 /**
51 * Incremented every time a new {@link SCM} instance is created from this descriptor.
52 * This is used to invalidate cache. Due to the lack of synchronization and serialization,
53 * this field doesn't really count the # of instances created to date,
54 * but it's good enough for the cache invalidation.
55 */
56 public volatile int generation = 1;
57
58 protected SCMDescriptor(Class<T> clazz, Class<? extends RepositoryBrowser> repositoryBrowser) {
59 super(clazz);
60 this.repositoryBrowser = repositoryBrowser;
61 }
62
63 /**
64 * Infers the type of the corresponding {@link SCM} from the outer class.
65 * This version works when you follow the common convention, where a descriptor
66 * is written as the static nested class of the describable class.
67 *
68 * @since 1.278
69 */
70 protected SCMDescriptor(Class<? extends RepositoryBrowser> repositoryBrowser) {
71 this.repositoryBrowser = repositoryBrowser;
72 }
73
74 // work around HUDSON-4514. The repositoryBrowser field was marked as non-transient until 1.325,
75 // causing the field to be persisted and overwritten on the load method.
76 @SuppressWarnings({"ConstantConditions"})
77 @Override
78 public void load() {
79 Class<? extends RepositoryBrowser> rb = repositoryBrowser;
80 super.load();
81 if (repositoryBrowser!=rb) { // XStream may overwrite even the final field.
82 try {
83 Field f = SCMDescriptor.class.getDeclaredField("repositoryBrowser");
84 f.setAccessible(true);
85 f.set(this,rb);
86 } catch (NoSuchFieldException e) {
87 LOGGER.log(WARNING, "Failed to overwrite the repositoryBrowser field",e);
88 } catch (IllegalAccessException e) {
89 LOGGER.log(WARNING, "Failed to overwrite the repositoryBrowser field",e);
90 }
91 }
92 }
93
94 /**
95 * Optional method used by the automatic SCM browser inference.
96 *
97 * <p>
98 * Implementing this method allows Hudson to reuse {@link RepositoryBrowser}
99 * configured for one project to be used for other "compatible" projects.
100 *
101 * @return
102 * true if the two given SCM configurations are similar enough
103 * that they can reuse {@link RepositoryBrowser} between them.
104 */
105 public boolean isBrowserReusable(T x, T y) {
106 return false;
107 }
108
109 /**
110 * Allows {@link SCMDescriptor}s to choose which projects it wants to be configurable against.
111 *
112 * <p>
113 * When this method returns false, this {@link SCM} will not appear in the configuration screen
114 * for the given project. The default method always return true.
115 *
116 * @since 1.294
117 */
118 public boolean isApplicable(AbstractProject project) {
119 return true;
120 }
121
122 /**
123 * Returns the list of {@link RepositoryBrowser} {@link Descriptor}
124 * that can be used with this SCM.
125 *
126 * @return
127 * can be empty but never null.
128 */
129 public List<Descriptor<RepositoryBrowser<?>>> getBrowserDescriptors() {
130 if(repositoryBrowser==null) return Collections.emptyList();
131 return RepositoryBrowsers.filter(repositoryBrowser);
132 }
133
134 private static final Logger LOGGER = Logger.getLogger(SCMDescriptor.class.getName());
135 }
136