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 ; 9 import java.util.List ; 10 import java.util.ArrayList ; 11 import java.util.StringTokenizer ; 12 import java.io.File ; 13 import java.io.IOException ; 14 15 import com.thoughtworks.xstream.XStream; 16 17 22 public class Items { 23 26 public static final List <TopLevelItemDescriptor> LIST = Descriptor.toList( 27 Project.DESCRIPTOR, 28 MavenModuleSet.DESCRIPTOR, 29 ExternalJob.DESCRIPTOR 30 ); 31 32 public static TopLevelItemDescriptor getDescriptor(String displayName) { 33 for (TopLevelItemDescriptor job : LIST) { 34 if(job.getDisplayName().equals(displayName)) 35 return job; 36 } 37 return null; 38 } 39 40 43 public static String toNameList(Collection <? extends Item> items) { 44 StringBuilder buf = new StringBuilder (); 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 56 public static <T extends Item> List <T> fromNameList(String list,Class <T> type) { 57 Hudson hudson = Hudson.getInstance(); 58 59 List <T> r = new ArrayList <T>(); 60 StringTokenizer tokens = new StringTokenizer (list,","); 61 while(tokens.hasMoreTokens()) { 62 String 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 76 public static Item load(ItemGroup parent, File dir) throws IOException { 77 Item item = (Item)getConfigFile(dir).read(); 78 item.onLoad(parent,dir.getName()); 79 return item; 80 } 81 82 85 static XmlFile getConfigFile(File dir) { 86 return new XmlFile(XSTREAM,new File(dir,"config.xml")); 87 } 88 89 92 public static XmlFile getConfigFile(Item item) { 93 return getConfigFile(item.getRootDir()); 94 } 95 96 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 |