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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2012, Dominik Bartholdi, Seiji Sogabe
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 jenkins.model;
25
26 import hudson.DescriptorExtensionList;
27 import hudson.Extension;
28 import hudson.ExtensionPoint;
29 import hudson.Util;
30 import hudson.model.Describable;
31 import hudson.model.Descriptor;
32 import hudson.model.Failure;
33 import hudson.util.FormValidation;
34 import java.io.IOException;
35
36 import java.io.Serializable;
37 import java.util.regex.Pattern;
38 import java.util.regex.PatternSyntaxException;
39 import javax.servlet.ServletException;
40
41 import org.apache.commons.lang.StringUtils;
42 import org.kohsuke.stapler.DataBoundConstructor;
43 import org.kohsuke.stapler.QueryParameter;
44
45 /**
46 * This ExtensionPoint allows to enforce the name of projects/jobs.
47 *
48 * @author Dominik Bartholdi (imod)
49 */
50 public abstract class ProjectNamingStrategy implements Describable<ProjectNamingStrategy>, ExtensionPoint {
51
52 public ProjectNamingStrategyDescriptor getDescriptor() {
53 return (ProjectNamingStrategyDescriptor) Jenkins.getInstance().getDescriptor(getClass());
54 }
55
56 public static DescriptorExtensionList<ProjectNamingStrategy, ProjectNamingStrategyDescriptor> all() {
57 return Jenkins.getInstance().<ProjectNamingStrategy, ProjectNamingStrategyDescriptor> getDescriptorList(ProjectNamingStrategy.class);
58 }
59
60 /**
61 * Called when creating a new job.
62 *
63 * @param name
64 * the name given from the UI
65 * @throws Failure
66 * if the user has to be informed about an illegal name, forces the user to change the name before submitting. The message of the failure will be presented to the user.
67 */
68 public void checkName(String name) throws Failure {
69 // no op
70 }
71
72 /**
73 * This flag can be used to force existing jobs to be migrated to a new naming strategy - if this method returns true, the naming will be enforced at every config change. If <code>false</code> is
74 * returned, only new jobs have to follow the strategy.
75 *
76 * @return <code>true</code> if existing jobs should be enforced to confirm to the naming standard.
77 */
78 public boolean isForceExistingJobs() {
79 return false;
80 }
81
82 /**
83 * The default naming strategy which does not restrict the name of a job.
84 */
85 public static final ProjectNamingStrategy DEFAULT_NAMING_STRATEGY = new DefaultProjectNamingStrategy();
86
87 /**
88 * Default implementation which does not restrict the name to any form.
89 */
90 public static final class DefaultProjectNamingStrategy extends ProjectNamingStrategy implements Serializable {
91
92 private static final long serialVersionUID = 1L;
93
94 @DataBoundConstructor
95 public DefaultProjectNamingStrategy() {
96 }
97
98 @Override
99 public void checkName(String origName) throws Failure {
100 // default - should just do nothing (this is how Jenkins worked before introducing this ExtensionPoint)
101 }
102
103 /**
104 * DefaultProjectNamingStrategy is stateless, therefore save to keep the same instance
105 */
106 private Object readResolve() {
107 return DEFAULT_NAMING_STRATEGY;
108 }
109
110 @Extension
111 public static final class DescriptorImpl extends ProjectNamingStrategyDescriptor {
112 @Override
113 public String getDisplayName() {
114 return Messages.DefaultProjectNamingStrategy_DisplayName();
115 }
116
117 @Override
118 public String getHelpFile() {
119 return "/help/system-config/defaultJobNamingStrategy.html";
120 }
121 }
122 }
123
124 /**
125 * Naming strategy which allows the admin to define a pattern a job's name has to follow.
126 */
127 public static final class PatternProjectNamingStrategy extends ProjectNamingStrategy implements Serializable {
128 private static final long serialVersionUID = 1L;
129
130 /**
131 * regex pattern a job's name has to follow
132 */
133 private final String namePattern;
134
135 private boolean forceExistingJobs;
136
137 @DataBoundConstructor
138 public PatternProjectNamingStrategy(String namePattern, boolean forceExistingJobs) {
139 this.namePattern = namePattern;
140 this.forceExistingJobs = forceExistingJobs;
141 }
142
143 @Override
144 public void checkName(String name) throws Failure {
145 if (StringUtils.isNotBlank(namePattern) && StringUtils.isNotBlank(name)) {
146 if (!Pattern.matches(namePattern, name)) {
147 throw new Failure(jenkins.model.Messages._Hudson_JobNameConventionNotApplyed(name, namePattern).toString());
148 }
149 }
150 }
151
152 public String getNamePattern() {
153 return namePattern;
154 }
155
156 public boolean isForceExistingJobs() {
157 return forceExistingJobs;
158 }
159
160 @Extension
161 public static final class DescriptorImpl extends ProjectNamingStrategyDescriptor {
162
163 public static final String DEFAULT_PATTERN = ".*";
164
165 @Override
166 public String getDisplayName() {
167 return Messages.PatternProjectNamingStrategy_DisplayName();
168 }
169
170 @Override
171 public String getHelpFile() {
172 return "/help/system-config/patternJobNamingStrategy.html";
173 }
174
175 public FormValidation doCheckNamePattern(@QueryParameter String value)
176 throws IOException, ServletException {
177 String pattern = Util.fixEmptyAndTrim(value);
178 if (pattern == null) {
179 return FormValidation.error(Messages.PatternProjectNamingStrategy_NamePatternRequired());
180 }
181 try {
182 Pattern.compile(pattern);
183 } catch (PatternSyntaxException e) {
184 return FormValidation.error(Messages.PatternProjectNamingStrategy_NamePatternInvalidSyntax());
185 }
186 return FormValidation.ok();
187 }
188 }
189 }
190
191 public static abstract class ProjectNamingStrategyDescriptor extends Descriptor<ProjectNamingStrategy> {
192 }
193
194 }
195