diff --git a/core/src/main/java/hudson/model/AbstractItem.java b/core/src/main/java/hudson/model/AbstractItem.java index c980d84d3d..28c93090f5 100644 --- a/core/src/main/java/hudson/model/AbstractItem.java +++ b/core/src/main/java/hudson/model/AbstractItem.java @@ -139,6 +139,10 @@ public abstract class AbstractItem extends Actionable implements Item { return getParent().getUrlChildPrefix()+'/'+getName()+'/'; } + public String getSearchUrl() { + return getShortUrl(); + } + @Exported(visibility=2,name="url") public final String getAbsoluteUrl() { StaplerRequest request = Stapler.getCurrentRequest(); diff --git a/core/src/main/java/hudson/model/AbstractModelObject.java b/core/src/main/java/hudson/model/AbstractModelObject.java index ec5b131bfb..a2ae9aab13 100644 --- a/core/src/main/java/hudson/model/AbstractModelObject.java +++ b/core/src/main/java/hudson/model/AbstractModelObject.java @@ -6,12 +6,15 @@ import org.kohsuke.stapler.StaplerResponse; import javax.servlet.ServletException; import java.io.IOException; +import hudson.search.SearchableModelObject; +import hudson.search.SearchIndex; + /** * {@link ModelObject} with some convenience methods. * * @author Kohsuke Kawaguchi */ -public abstract class AbstractModelObject implements ModelObject { +public abstract class AbstractModelObject implements SearchableModelObject { /** * Displays the error in a page. */ @@ -23,4 +26,18 @@ public abstract class AbstractModelObject implements ModelObject { req.setAttribute("message",message); rsp.forward(this,"error",req); } + + /** + * Default implementation that returns empty index. + */ + public SearchIndex getSearchIndex() { + return SearchIndex.EMPTY; + } + + /** + * Default implementation that returns the display name. + */ + public String getSearchName() { + return getDisplayName(); + } } diff --git a/core/src/main/java/hudson/model/Api.java b/core/src/main/java/hudson/model/Api.java index cceaf87253..5f5a8bf814 100644 --- a/core/src/main/java/hudson/model/Api.java +++ b/core/src/main/java/hudson/model/Api.java @@ -43,6 +43,10 @@ public class Api extends AbstractModelObject { return "API"; } + public String getSearchUrl() { + return "api"; + } + /** * Exposes the bean as XML. */ diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index de14bfb560..e6c5acdc19 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -15,7 +15,6 @@ import hudson.TcpSlaveAgentListener; import hudson.Util; import static hudson.Util.fixEmpty; import hudson.XmlFile; -import hudson.node_monitors.NodeMonitor; import hudson.model.Descriptor.FormException; import hudson.model.listeners.ItemListener; import hudson.model.listeners.JobListener; @@ -29,6 +28,8 @@ import hudson.scm.RepositoryBrowsers; import hudson.scm.SCM; import hudson.scm.SCMDescriptor; import hudson.scm.SCMS; +import hudson.search.SearchIndex; +import hudson.search.SearchIndexBuilder; import hudson.tasks.BuildStep; import hudson.tasks.BuildWrapper; import hudson.tasks.BuildWrappers; @@ -40,12 +41,12 @@ import hudson.tasks.Publisher; import hudson.triggers.Trigger; import hudson.triggers.TriggerDescriptor; import hudson.triggers.Triggers; +import hudson.util.ClockDifference; import hudson.util.CopyOnWriteList; import hudson.util.CopyOnWriteMap; import hudson.util.FormFieldValidator; import hudson.util.MultipartFormDataParser; import hudson.util.XStream2; -import hudson.util.ClockDifference; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; @@ -678,6 +679,15 @@ public final class Hudson extends View implements ItemGroup, Node return ""; } + @Override + public SearchIndex getSearchIndex() { + return new SearchIndexBuilder() + .add(super.getSearchIndex()) + .add("configure", "config","configure") + .add("log", "log") + .make(); + } + public String getUrlChildPrefix() { return "job"; } @@ -758,9 +768,7 @@ public final class Hudson extends View implements ItemGroup, Node return getItem(name); } - /** - * Gets the {@link TopLevelItem} of the given name. - */ + @Override public TopLevelItem getItem(String name) { return items.get(name); } diff --git a/core/src/main/java/hudson/model/Item.java b/core/src/main/java/hudson/model/Item.java index dda3cadf73..6e97d46b82 100644 --- a/core/src/main/java/hudson/model/Item.java +++ b/core/src/main/java/hudson/model/Item.java @@ -5,6 +5,8 @@ import org.kohsuke.stapler.StaplerRequest; import java.io.IOException; import java.util.Collection; +import hudson.search.SearchableModelObject; + /** * Basic configuration unit in Hudson. * @@ -33,7 +35,7 @@ import java.util.Collection; * @author Kohsuke Kawaguchi * @see Items */ -public interface Item extends PersistenceRoot, ModelObject { +public interface Item extends PersistenceRoot, SearchableModelObject { /** * Gets the parent that contains this item. */ diff --git a/core/src/main/java/hudson/model/ListView.java b/core/src/main/java/hudson/model/ListView.java index d0f0c00f79..4efb63de0a 100644 --- a/core/src/main/java/hudson/model/ListView.java +++ b/core/src/main/java/hudson/model/ListView.java @@ -56,10 +56,14 @@ public class ListView extends View { return Arrays.asList(items); } - public TopLevelItem getJob(String name) { + public TopLevelItem getItem(String name) { return owner.getItem(name); } + public TopLevelItem getJob(String name) { + return getItem(name); + } + public boolean contains(TopLevelItem item) { return jobNames.contains(item.getName()); } diff --git a/core/src/main/java/hudson/model/Run.java b/core/src/main/java/hudson/model/Run.java index c6bd5b44f3..d4bd36f2ca 100644 --- a/core/src/main/java/hudson/model/Run.java +++ b/core/src/main/java/hudson/model/Run.java @@ -395,6 +395,10 @@ public abstract class Run ,RunT extends Run getItems(); + /** + * Gets the {@link TopLevelItem} of the given name. + */ + public abstract TopLevelItem getItem(String name); + /** * Checks if the job is in this collection. */ @@ -58,6 +66,10 @@ public abstract class View extends AbstractModelObject { */ public abstract String getUrl(); + public String getSearchUrl() { + return getUrl(); + } + /** * Gets the absolute URL of this view. */ @@ -168,6 +180,16 @@ public abstract class View extends AbstractModelObject { return r; } + @Override + public SearchIndex getSearchIndex() { + return new SearchIndexBuilder() + .add(new CollectionSearchIndex() { + protected TopLevelItem get(String key) { return getItem(key); } + protected Collection all() { return getItems(); } + }) + .make(); + } + /** * Creates a new {@link Item} in this collection. * diff --git a/core/src/main/java/hudson/scm/AbstractScmTagAction.java b/core/src/main/java/hudson/scm/AbstractScmTagAction.java index 92e2c71e22..2b83178650 100644 --- a/core/src/main/java/hudson/scm/AbstractScmTagAction.java +++ b/core/src/main/java/hudson/scm/AbstractScmTagAction.java @@ -51,6 +51,10 @@ public abstract class AbstractScmTagAction extends AbstractModelObject implement return "tagBuild"; } + public String getSearchUrl() { + return getUrlName(); + } + public AbstractBuild getBuild() { return build; } diff --git a/core/src/main/java/hudson/search/CollectionSearchIndex.java b/core/src/main/java/hudson/search/CollectionSearchIndex.java new file mode 100644 index 0000000000..56d37859ff --- /dev/null +++ b/core/src/main/java/hudson/search/CollectionSearchIndex.java @@ -0,0 +1,28 @@ +package hudson.search; + +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * {@link SearchIndex} built on a {@link Map}. + * + * @author Kohsuke Kawaguchi + */ +public abstract class CollectionSearchIndex implements SearchIndex { + protected abstract SearchItem get(String key); + protected abstract Collection all(); + + public void find(String token, List result) { + SearchItem p = get(token); + if(p!=null) + result.add(p); + } + + public void suggest(String token, List result) { + for (SearchableModelObject o : all()) { + if(o.getDisplayName().contains(token)) + result.add(o); + } + } +} diff --git a/core/src/main/java/hudson/search/SearchItem.java b/core/src/main/java/hudson/search/SearchItem.java index bc554b3558..d9fed2a7bb 100644 --- a/core/src/main/java/hudson/search/SearchItem.java +++ b/core/src/main/java/hudson/search/SearchItem.java @@ -1,10 +1,39 @@ package hudson.search; +import hudson.model.Build; + /** + * Represents an item reachable from {@link SearchIndex}. + * + *

+ * The act of searching in this package is really a traversal of a directed graph. + * And in that notion, this interface represents an edge, not a node. + * So it's possible for single entity (let's say {@link Build}) to + * have multiple {@link SearchItem}s representing it (for example, + * a 'last successful build' search item and '#123' search item.) + * * @author Kohsuke Kawaguchi */ public interface SearchItem { + /** + * Name of this item. This is matched against the query. + */ String getSearchName(); - String getSearchUrlFragment(); + /** + * Returns the URL of this item relative to the parent {@link SearchItem}. + * + * @return + * URL like "foo" or "foo/bar". The path can end with '/'. + * The path that starts with '/' will be interpreted as the absolute path + * (within the context path of Hudson.) + */ + String getSearchUrl(); + + /** + * Returns the {@link SearchIndex} to further search into this item. + * + * @return + * {@link SearchIndex#EMPTY} if this is a leaf. + */ SearchIndex getSearchIndex(); } diff --git a/core/src/main/java/hudson/search/SearchItems.java b/core/src/main/java/hudson/search/SearchItems.java index 780397ab2c..8646536cfd 100644 --- a/core/src/main/java/hudson/search/SearchItems.java +++ b/core/src/main/java/hudson/search/SearchItems.java @@ -14,7 +14,7 @@ public class SearchItems { return searchName; } - public String getSearchUrlFragment() { + public String getSearchUrl() { return url; } @@ -30,7 +30,7 @@ public class SearchItems { return searchName; } - public String getSearchUrlFragment() { + public String getSearchUrl() { return url; } diff --git a/core/src/main/java/hudson/search/SearchableModelObject.java b/core/src/main/java/hudson/search/SearchableModelObject.java index d873595fee..434be0998d 100644 --- a/core/src/main/java/hudson/search/SearchableModelObject.java +++ b/core/src/main/java/hudson/search/SearchableModelObject.java @@ -4,9 +4,14 @@ import hudson.model.ModelObject; /** * {@link ModelObject} that has {@link SearchIndex}. - * + * + *

+ * This interface also extends {@link SearchItem} since + * often {@link ModelObject}s form a natural tree structure, + * and it's convenient for the model objects themselves to implement + * the {@link SearchItem} for the edges that form this tree. + * * @author Kohsuke Kawaguchi */ -public interface SearchableModelObject extends ModelObject { - SearchIndex getSearchIndex(); +public interface SearchableModelObject extends ModelObject, SearchItem { } diff --git a/core/src/main/java/hudson/search/SuggestedItem.java b/core/src/main/java/hudson/search/SuggestedItem.java index 632d19a25a..02a52c38a5 100644 --- a/core/src/main/java/hudson/search/SuggestedItem.java +++ b/core/src/main/java/hudson/search/SuggestedItem.java @@ -45,6 +45,14 @@ public class SuggestedItem { if(parent!=null) { parent.getUrl(buf); } - buf.append('/').append(item.getSearchUrlFragment()); + String f = item.getSearchUrl(); + if(f.startsWith("/")) { + buf.setLength(0); + buf.append(f); + } else { + if(buf.charAt(buf.length()-1)!='/') + buf.append('/'); + buf.append(f); + } } } diff --git a/core/src/main/java/hudson/tasks/JavadocArchiver.java b/core/src/main/java/hudson/tasks/JavadocArchiver.java index a736532d5c..1802a5008a 100644 --- a/core/src/main/java/hudson/tasks/JavadocArchiver.java +++ b/core/src/main/java/hudson/tasks/JavadocArchiver.java @@ -3,7 +3,7 @@ package hudson.tasks; import hudson.FilePath; import hudson.Launcher; import hudson.Util; -import hudson.util.FormFieldValidator; +import hudson.model.AbstractItem; import hudson.model.Action; import hudson.model.Build; import hudson.model.BuildListener; @@ -12,8 +12,7 @@ import hudson.model.DirectoryBrowserSupport; import hudson.model.Project; import hudson.model.ProminentProjectAction; import hudson.model.Result; -import hudson.model.Actionable; -import hudson.model.AbstractItem; +import hudson.util.FormFieldValidator; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerResponse; @@ -81,7 +80,7 @@ public class JavadocArchiver extends Publisher { public static final Descriptor DESCRIPTOR = new DescriptorImpl(); - public static final class JavadocAction extends Actionable implements ProminentProjectAction { + public static final class JavadocAction implements ProminentProjectAction { private final AbstractItem project; public JavadocAction(AbstractItem project) {