1 package hudson.model; 2 3 import hudson.XmlFile; 4 import hudson.scm.CVSSCM; 5 import org.kohsuke.stapler.StaplerRequest; 6 7 import javax.servlet.http.HttpServletRequest ; 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.util.ArrayList ; 11 import java.util.LinkedHashMap ; 12 import java.util.List ; 13 import java.util.Map ; 14 import java.util.logging.Level ; 15 import java.util.logging.Logger ; 16 17 59 public abstract class Descriptor<T extends Describable<T>> { 60 67 @Deprecated 68 private transient Map <String ,Object > properties; 69 70 73 public transient final Class <? extends T> clazz; 74 75 protected Descriptor(Class <? extends T> clazz) { 76 this.clazz = clazz; 77 } 81 82 85 public abstract String getDisplayName(); 86 87 100 public abstract T newInstance(StaplerRequest req) throws FormException; 101 102 105 public String getHelpFile() { 106 return ""; 107 } 108 109 112 public final boolean isInstance( T instance ) { 113 return clazz.isInstance(instance); 114 } 115 116 120 @Deprecated 121 public boolean configure( HttpServletRequest req ) throws FormException { 122 return true; 123 } 124 125 133 public boolean configure( StaplerRequest req ) throws FormException { 134 return configure( (HttpServletRequest ) req ); 136 } 137 138 public String getConfigPage() { 139 return getViewPage(clazz, "config.jelly"); 140 } 141 142 public String getGlobalConfigPage() { 143 return getViewPage(clazz, "global.jelly"); 144 } 145 146 protected final String getViewPage(Class <?> clazz, String pageName) { 147 return '/'+ clazz.getName().replace('.','/').replace('$','/')+"/"+ pageName; 148 } 149 150 151 154 protected synchronized void save() { 155 try { 156 getConfigFile().write(this); 157 } catch (IOException e) { 158 LOGGER.log(Level.WARNING, "Failed to save "+getConfigFile(),e); 159 } 160 } 161 162 170 protected synchronized void load() { 171 XmlFile file = getConfigFile(); 172 if(!file.exists()) 173 return; 174 175 try { 176 Object o = file.unmarshal(this); 177 if(o instanceof Map ) { 178 @SuppressWarnings ("unchecked") 180 Map <String ,Object > _o = (Map ) o; 181 convert(_o); 182 save(); } 184 } catch (IOException e) { 185 LOGGER.log(Level.WARNING, "Failed to load "+file, e); 186 } 187 } 188 189 194 protected void convert(Map <String , Object > oldPropertyBag) { 195 } 196 197 private XmlFile getConfigFile() { 198 return new XmlFile(new File(Hudson.getInstance().getRootDir(),clazz.getName()+".xml")); 199 } 200 201 public static <T> T[] toArray( T... values ) { 203 return values; 204 } 205 206 public static <T> List <T> toList( T... values ) { 207 final ArrayList <T> r = new ArrayList <T>(); 208 for (T v : values) 209 r.add(v); 210 return r; 211 } 212 213 public static <T extends Describable<T>> 214 Map <Descriptor<T>,T> toMap(Iterable <T> describables) { 215 Map <Descriptor<T>,T> m = new LinkedHashMap <Descriptor<T>,T>(); 216 for (T d : describables) { 217 m.put(d.getDescriptor(),d); 218 } 219 return m; 220 } 221 222 public static final class FormException extends Exception { 223 private final String formField; 224 225 public FormException(String message, String formField) { 226 super(message); 227 this.formField = formField; 228 } 229 230 public FormException(String message, Throwable cause, String formField) { 231 super(message, cause); 232 this.formField = formField; 233 } 234 235 public FormException(Throwable cause, String formField) { 236 super(cause); 237 this.formField = formField; 238 } 239 240 243 public String getFormField() { 244 return formField; 245 } 246 } 247 248 private static final Logger LOGGER = Logger.getLogger(Descriptor.class.getName()); 249 } 250 | Popular Tags |