diff --git a/core/src/main/java/hudson/Util.java b/core/src/main/java/hudson/Util.java index 6c2ffe87d717754f0509d1f836c63c9d15a40d55..428e89092fe0024283b42b24dca72d2bc392ae31 100644 --- a/core/src/main/java/hudson/Util.java +++ b/core/src/main/java/hudson/Util.java @@ -1,6 +1,8 @@ package hudson; import hudson.model.TaskListener; +import hudson.model.Project; +import hudson.model.TopLevelItem; import hudson.util.IOException2; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.taskdefs.Chmod; @@ -24,6 +26,9 @@ import java.util.Map; import java.util.ResourceBundle; import java.util.SimpleTimeZone; import java.util.StringTokenizer; +import java.util.Collection; +import java.util.List; +import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; @@ -299,6 +304,18 @@ public class Util { return s; } + /** + * Create a sub-list by only picking up instances of the specified type. + */ + public static List createSubList( Collection source, Class type ) { + List r = new ArrayList(); + for (Object item : source) { + if(type.isInstance(item)) + r.add(type.cast(item)); + } + return r; + } + /** * Escapes non-ASCII characters. */ diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index b8c7cd931ae85f6616ea1109ffeaacc447a1234c..ddaf946cba2ad8ce7e9c76d410a64494245a8d60 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -104,15 +104,6 @@ public final class Hudson extends View implements ItemGroup, Node */ public transient final File root; - /** - * All {@link Job}s keyed by their names. - * Subset of {@link #items}. - * - * @deprecated - * TODO: get rid of this in favor of {@link #items}. - */ - /*package*/ transient final Map jobs = new TreeMap(); - /** * All {@link Item}s keyed by their {@link Item#getName() name}s. */ @@ -372,7 +363,7 @@ public final class Hudson extends View implements ItemGroup, Node * Gets the snapshot of all the jobs. */ public synchronized List getJobs() { - return new ArrayList(jobs.values()); + return Util.createSubList(items.values(),Job.class); } public synchronized List getItems() { @@ -383,12 +374,7 @@ public final class Hudson extends View implements ItemGroup, Node * Gets the snapshot of all the projects. */ public synchronized List getProjects() { - List r = new ArrayList(); - for (Job job : jobs.values()) { - if(job instanceof Project) - r.add((Project)job); - } - return r; + return Util.createSubList(items.values(),Project.class); } /** @@ -396,8 +382,9 @@ public final class Hudson extends View implements ItemGroup, Node */ public synchronized Collection getJobNames() { List names = new ArrayList(); - for (Job j : jobs.values()) { - names.add(j.getName()); + for (Item j : items.values()) { + if (j instanceof Job) + names.add(j.getName()); } return names; } @@ -601,7 +588,11 @@ public final class Hudson extends View implements ItemGroup, Node * if such a project doesn't exist. */ public synchronized Job getJob(String name) { - return jobs.get(name); + TopLevelItem item = items.get(name); + if(item instanceof Job) + return (Job)item; + else + return null; } /** @@ -628,7 +619,7 @@ public final class Hudson extends View implements ItemGroup, Node * if the project of the given name already exists. */ public synchronized TopLevelItem createProject( TopLevelItemDescriptor type, String name ) throws IOException { - if(jobs.containsKey(name)) + if(items.containsKey(name)) throw new IllegalArgumentException(); TopLevelItem job; @@ -650,7 +641,7 @@ public final class Hudson extends View implements ItemGroup, Node for (ItemListener l : viewItemListeners) l.onDeleted(job); - jobs.remove(job.getName()); + items.remove(job.getName()); if(views!=null) { for (ListView v : views) { synchronized(v) { @@ -665,9 +656,9 @@ public final class Hudson extends View implements ItemGroup, Node * Called by {@link Job#renameTo(String)} to update relevant data structure. * assumed to be synchronized on Hudson by the caller. */ - /*package*/ void onRenamed(Job job, String oldName, String newName) throws IOException { - jobs.remove(oldName); - jobs.put(newName,job); + /*package*/ void onRenamed(TopLevelItem job, String oldName, String newName) throws IOException { + items.remove(oldName); + items.put(newName,job); if(views!=null) { for (ListView v : views) { diff --git a/core/src/main/java/hudson/model/Job.java b/core/src/main/java/hudson/model/Job.java index 63428adf854e790d7dd990a960ed2316fcf47c75..7f0b295a1b54d17b69be6655d7c161df296b1258 100644 --- a/core/src/main/java/hudson/model/Job.java +++ b/core/src/main/java/hudson/model/Job.java @@ -223,10 +223,15 @@ public abstract class Job, RunT extends Run + * This method is defined on {@link Job} but really only applicable + * for {@link Job}s that are top-level items. */ public void renameTo(String newName) throws IOException { // always synchronize from bigger objects first final Hudson parent = Hudson.getInstance(); + assert this instanceof TopLevelItem; synchronized(parent) { synchronized(this) { // sanity check @@ -295,7 +300,7 @@ public abstract class Job, RunT extends Run