KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > Items


1 package hudson.model;
2
3 import hudson.XmlFile;
4 import hudson.maven.MavenModuleSet;
5 import hudson.maven.MavenModule;
6 import hudson.util.XStream2;
7
8 import java.util.Collection JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 import com.thoughtworks.xstream.XStream;
16
17 /**
18  * Convenience methods related to {@link Item}.
19  *
20  * @author Kohsuke Kawaguchi
21  */

22 public class Items {
23     /**
24      * List of all installed {@link TopLevelItem} types.
25      */

26     public static final List JavaDoc<TopLevelItemDescriptor> LIST = Descriptor.toList(
27         Project.DESCRIPTOR,
28         MavenModuleSet.DESCRIPTOR,
29         ExternalJob.DESCRIPTOR
30     );
31
32     public static TopLevelItemDescriptor getDescriptor(String JavaDoc displayName) {
33         for (TopLevelItemDescriptor job : LIST) {
34             if(job.getDisplayName().equals(displayName))
35                 return job;
36         }
37         return null;
38     }
39
40     /**
41      * Converts a list of items into a camma-separated full names.
42      */

43     public static String JavaDoc toNameList(Collection JavaDoc<? extends Item> items) {
44         StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
45         for (Item item : items) {
46             if(buf.length()>0)
47                 buf.append(", ");
48             buf.append(item.getFullName());
49         }
50         return buf.toString();
51     }
52
53     /**
54      * Does the opposite of {@link #toNameList(Collection)}.
55      */

56     public static <T extends Item> List JavaDoc<T> fromNameList(String JavaDoc list,Class JavaDoc<T> type) {
57         Hudson hudson = Hudson.getInstance();
58
59         List JavaDoc<T> r = new ArrayList JavaDoc<T>();
60         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(list,",");
61         while(tokens.hasMoreTokens()) {
62             String JavaDoc fullName = tokens.nextToken().trim();
63             T item = hudson.getItemByFullName(fullName,type);
64             if(item!=null)
65                 r.add(item);
66         }
67         return r;
68     }
69
70     /**
71      * Loads a {@link Item} from a config file.
72      *
73      * @param dir
74      * The directory that contains the config file, not the config file itself.
75      */

76     public static Item load(ItemGroup parent, File dir) throws IOException JavaDoc {
77         Item item = (Item)getConfigFile(dir).read();
78         item.onLoad(parent,dir.getName());
79         return item;
80     }
81
82     /**
83      * The file we save our configuration.
84      */

85     static XmlFile getConfigFile(File dir) {
86         return new XmlFile(XSTREAM,new File(dir,"config.xml"));
87     }
88
89     /**
90      * The file we save our configuration.
91      */

92     public static XmlFile getConfigFile(Item item) {
93         return getConfigFile(item.getRootDir());
94     }
95
96     /**
97      * Used to load/save job configuration.
98      *
99      * When you extend {@link Job} in a plugin, try to put the alias so
100      * that it produces a reasonable XML.
101      */

102     public static final XStream XSTREAM = new XStream2();
103
104     static {
105         XSTREAM.alias("project",Project.class);
106         XSTREAM.alias("maven2", MavenModule.class);
107         XSTREAM.alias("maven2-module-set", MavenModule.class);
108     }
109 }
110
Popular Tags