File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\Items.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.model;
25
26 import com.thoughtworks.xstream.XStream;
27 import hudson.DescriptorExtensionList;
28 import hudson.Extension;
29 import hudson.matrix.MatrixProject;
30 import hudson.matrix.MatrixConfiguration;
31 import hudson.XmlFile;
32 import hudson.matrix.Axis;
33 import hudson.triggers.Trigger;
34 import hudson.util.DescriptorList;
35 import hudson.util.XStream2;
36 import jenkins.model.Jenkins;
37 import org.apache.commons.lang.StringUtils;
38
39 import java.io.File;
40 import java.io.IOException;
41 import java.util.*;
42
43 /**
44 * Convenience methods related to {@link Item}.
45 *
46 * @author Kohsuke Kawaguchi
47 */
48 public class Items {
49 /**
50 * List of all installed {@link TopLevelItem} types.
51 *
52 * @deprecated as of 1.286
53 * Use {@link #all()} for read access and {@link Extension} for registration.
54 */
55 public static final List<TopLevelItemDescriptor> LIST = (List)new DescriptorList<TopLevelItem>(TopLevelItem.class);
56
57 /**
58 * Used to behave differently when loading posted configuration as opposed to persisted configuration.
59 * @see Trigger#start
60 * @since 1.482
61 */
62 static final ThreadLocal<Boolean> updatingByXml = new ThreadLocal<Boolean>() {
63 @Override protected Boolean initialValue() {
64 return false;
65 }
66 };
67
68 /**
69 * Returns all the registered {@link TopLevelItemDescriptor}s.
70 */
71 public static DescriptorExtensionList<TopLevelItem,TopLevelItemDescriptor> all() {
72 return Jenkins.getInstance().<TopLevelItem,TopLevelItemDescriptor>getDescriptorList(TopLevelItem.class);
73 }
74
75 public static TopLevelItemDescriptor getDescriptor(String fqcn) {
76 return Descriptor.find(all(), fqcn);
77 }
78
79 /**
80 * Converts a list of items into a comma-separated list of full names.
81 */
82 public static String toNameList(Collection<? extends Item> items) {
83 StringBuilder buf = new StringBuilder();
84 for (Item item : items) {
85 if(buf.length()>0)
86 buf.append(", ");
87 buf.append(item.getFullName());
88 }
89 return buf.toString();
90 }
91
92 /**
93 * @deprecated as of 1.406
94 * Use {@link #fromNameList(ItemGroup, String, Class)}
95 */
96 public static <T extends Item> List<T> fromNameList(String list, Class<T> type) {
97 return fromNameList(null,list,type);
98 }
99
100 /**
101 * Does the opposite of {@link #toNameList(Collection)}.
102 */
103 public static <T extends Item> List<T> fromNameList(ItemGroup context, String list, Class<T> type) {
104 Jenkins hudson = Jenkins.getInstance();
105
106 List<T> r = new ArrayList<T>();
107 StringTokenizer tokens = new StringTokenizer(list,",");
108 while(tokens.hasMoreTokens()) {
109 String fullName = tokens.nextToken().trim();
110 T item = hudson.getItem(fullName, context, type);
111 if(item!=null)
112 r.add(item);
113 }
114 return r;
115 }
116
117 /**
118 * Computes the canonical full name of a relative path in an {@link ItemGroup} context, handling relative
119 * positions ".." and "." as absolute path starting with "/". The resulting name is the item fullName from Jenkins
120 * root.
121 */
122 public static String getCanonicalName(ItemGroup context, String path) {
123 String[] c = context.getFullName().split("/");
124 String[] p = path.split("/");
125
126 Stack name = new Stack();
127 for (int i=0; i<c.length;i++) {
128 if (i==0 && c[i].equals("")) continue;
129 name.push(c[i]);
130 }
131 for (int i=0; i<p.length;i++) {
132 if (i==0 && p[i].equals("")) {
133 // Absolute path starting with a "/"
134 name.clear();
135 continue;
136 }
137 if (p[i].equals("..")) {
138 name.pop();
139 continue;
140 }
141 if (p[i].equals(".")) {
142 continue;
143 }
144 name.push(p[i]);
145 }
146 return StringUtils.join(name, '/');
147 }
148
149 /**
150 * Compute the relative name of list of items after a rename occurred. Used to manage job references as names in
151 * plugins to support {@link hudson.model.listeners.ItemListener#onRenamed(hudson.model.Item, String, String)}.
152 * <p>
153 * In a hierarchical context, when a plugin has a reference to a job as <code>../foo/bar</code> this method will
154 * handle the relative path as "foo" is renamed to "zot" to compute <code>../zot/bar</code>
155 *
156 * @param oldFullName the old full name of the item
157 * @param newFullName the new full name of the item
158 * @param relativeNames coma separated list of Item relative names
159 * @param context the {link ItemGroup} relative names refer to
160 * @return relative name for the renamed item, based on the same ItemGroup context
161 */
162 public static String computeRelativeNamesAfterRenaming(String oldFullName, String newFullName, String relativeNames, ItemGroup context) {
163
164 StringTokenizer tokens = new StringTokenizer(relativeNames,",");
165 List<String> newValue = new ArrayList<String>();
166 while(tokens.hasMoreTokens()) {
167 String relativeName = tokens.nextToken().trim();
168 String canonicalName = getCanonicalName(context, relativeName);
169 if (canonicalName.startsWith(oldFullName)) {
170 String newCanonicalName = newFullName + canonicalName.substring(oldFullName.length());
171 // relative name points to the renamed item, let's compute the new relative name
172 newValue.add( computeRelativeNameAfterRenaming(canonicalName, newCanonicalName, relativeName) );
173 } else {
174 newValue.add(relativeName);
175 }
176 }
177 return StringUtils.join(newValue, ",");
178 }
179
180 /**
181 * Compute the relative name of an Item after renaming
182 */
183 private static String computeRelativeNameAfterRenaming(String oldFullName, String newFullName, String relativeName) {
184
185 String[] a = oldFullName.split("/");
186 String[] n = newFullName.split("/");
187 assert a.length == n.length;
188 String[] r = relativeName.split("/");
189
190 int j = a.length-1;
191 for(int i=r.length-1;i>=0;i--) {
192 String part = r[i];
193 if (part.equals("") && i==0) {
194 continue;
195 }
196 if (part.equals(".")) {
197 continue;
198 }
199 if (part.equals("..")) {
200 j--;
201 continue;
202 }
203 if (part.equals(a[j])) {
204 r[i] = n[j];
205 j--;
206 continue;
207 }
208 }
209 return StringUtils.join(r, '/');
210 }
211
212
213 /**
214 * Loads a {@link Item} from a config file.
215 *
216 * @param dir
217 * The directory that contains the config file, not the config file itself.
218 */
219 public static Item load(ItemGroup parent, File dir) throws IOException {
220 Item item = (Item)getConfigFile(dir).read();
221 item.onLoad(parent,dir.getName());
222 return item;
223 }
224
225 /**
226 * The file we save our configuration.
227 */
228 public static XmlFile getConfigFile(File dir) {
229 return new XmlFile(XSTREAM,new File(dir,"config.xml"));
230 }
231
232 /**
233 * The file we save our configuration.
234 */
235 public static XmlFile getConfigFile(Item item) {
236 return getConfigFile(item.getRootDir());
237 }
238
239 /**
240 * Used to load/save job configuration.
241 *
242 * When you extend {@link Job} in a plugin, try to put the alias so
243 * that it produces a reasonable XML.
244 */
245 public static final XStream XSTREAM = new XStream2();
246
247 /**
248 * Alias to {@link #XSTREAM} so that one can access additional methods on {@link XStream2} more easily.
249 */
250 public static final XStream2 XSTREAM2 = (XStream2)XSTREAM;
251
252 static {
253 XSTREAM.alias("project",FreeStyleProject.class);
254 XSTREAM.alias("matrix-project",MatrixProject.class);
255 XSTREAM.alias("axis", Axis.class);
256 XSTREAM.alias("matrix-config",MatrixConfiguration.class);
257 }
258 }
259