1 package hudson.util; 2 3 import com.thoughtworks.xstream.converters.Converter; 4 import com.thoughtworks.xstream.converters.MarshallingContext; 5 import com.thoughtworks.xstream.converters.UnmarshallingContext; 6 import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter; 7 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 8 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 9 import com.thoughtworks.xstream.mapper.Mapper; 10 import hudson.model.Describable; 11 import hudson.model.Descriptor; 12 import hudson.model.Descriptor.FormException; 13 import hudson.maven.MavenReporterDescriptor; 14 import org.kohsuke.stapler.StaplerRequest; 15 16 import java.io.IOException ; 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Collection ; 22 23 29 public class DescribableList<T extends Describable<T>, D extends Descriptor<T>> implements Iterable <T> { 30 private final CopyOnWriteList<T> data = new CopyOnWriteList<T>(); 31 private Owner owner; 32 33 private DescribableList() { 34 } 35 36 public DescribableList(Owner owner) { 37 setOwner(owner); 38 } 39 40 public void setOwner(Owner owner) { 41 this.owner = owner; 42 } 43 44 public void add(T item) throws IOException { 45 data.add(item); 46 owner.save(); 47 } 48 49 public T get(D descriptor) { 50 for (T t : data) 51 if(t.getDescriptor()==descriptor) 52 return t; 53 return null; 54 } 55 56 public boolean contains(D d) { 57 return get(d)!=null; 58 } 59 60 public void remove(D descriptor) throws IOException { 61 for (T t : data) { 62 if(t.getDescriptor()==descriptor) { 63 data.remove(t); 64 owner.save(); 65 return; 66 } 67 } 68 } 69 70 public Iterator <T> iterator() { 71 return data.iterator(); 72 } 73 74 @SuppressWarnings ("unchecked") 75 public Map <D,T> toMap() { 76 return (Map )Descriptor.toMap(data); 77 } 78 79 82 public T[] toArray(T[] array) { 83 return data.toArray(array); 84 } 85 86 public void addAllTo(Collection <? super T> dst) { 87 data.addAllTo(dst); 88 } 89 90 97 public void rebuild(StaplerRequest req, List <? extends Descriptor<T>> descriptors, String prefix) throws FormException { 98 List <T> newList = new ArrayList <T>(); 99 100 for( int i=0; i< descriptors.size(); i++ ) { 101 if(req.getParameter(prefix +i)!=null) { 102 T instance = descriptors.get(i).newInstance(req); 103 newList.add(instance); 104 } 105 } 106 107 data.replaceBy(newList); 108 } 109 110 public interface Owner { 111 114 void save() throws IOException ; 115 } 116 117 120 public static final class ConverterImpl extends AbstractCollectionConverter { 121 CopyOnWriteList.ConverterImpl copyOnWriteListConverter; 122 123 public ConverterImpl(Mapper mapper) { 124 super(mapper); 125 copyOnWriteListConverter = new CopyOnWriteList.ConverterImpl(mapper()); 126 } 127 128 public boolean canConvert(Class type) { 129 return type==DescribableList.class; 130 } 131 132 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { 133 for (Object o : (DescribableList) source) 134 writeItem(o, context, writer); 135 } 136 137 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 138 CopyOnWriteList core = copyOnWriteListConverter.unmarshal(reader, context); 139 140 DescribableList r = new DescribableList(); 141 r.data.replaceBy(core); 142 return r; 143 } 144 } 145 } 146 | Popular Tags |