Added DescriptorByNameOwner to enable context-sensitive descriptor mapping in a generic way.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@16484 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2009-03-23 17:13:06 +00:00
1 parent 3237ee5fe7
commit 7138ab22b2
5 files changed
+57 -26

No files matched your search

@@ -55,7 +55,7 @@ import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
// Item doesn't necessarily have to be Actionable, but
// Java doesn't let multiple inheritance.
@ExportedBean
public abstract class AbstractItem extends Actionable implements Item, HttpDeletable, AccessControlled {
public abstract class AbstractItem extends Actionable implements Item, HttpDeletable, AccessControlled, DescriptorByNameOwner {
/**
* Project name.
*/
@@ -221,25 +221,8 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet
return Items.getConfigFile(this);
}
/**
* Exposes {@link Descriptor} by its name to URL.
*
* This allows descriptor to perform a job-specific check easily.
*
* @since 1.270.
*
* @param className
* Either fully qualified class name (recommended) or the short name.
*/
public Descriptor getDescriptorByName(String className) {
for( Descriptor d : Iterators.sequence(Hudson.getInstance().getExtensionList(Descriptor.class), DescriptorExtensionList.listLegacyInstances()) ) {
String name = d.clazz.getName();
if(name.equals(className))
return d;
if(name.substring(name.lastIndexOf('.')+1).equals(className))
return d;
}
return null;
return Hudson.getInstance().getDescriptorByName(className);
}
/**
@@ -32,7 +32,7 @@ import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.MetaClassLoader;
import org.kohsuke.stapler.Ancestor;
import org.springframework.util.StringUtils;
import org.jvnet.tiger_types.Types;
import org.apache.commons.io.IOUtils;
@@ -255,7 +255,10 @@ public abstract class Descriptor<T extends Describable<T>> implements Saveable {
if(method==NONE)
return null;
return singleQuote(Stapler.getCurrentRequest().getContextPath()+"/descriptor/"+clazz.getName()+"/check"+capitalizedFieldName+"?value=")+"+encode(this.value)";
StaplerRequest req = Stapler.getCurrentRequest();
Ancestor a = req.findAncestor(DescriptorByNameOwner.class);
// a is always non-null because we already have Hudson as the sentinel
return singleQuote(a.getUrl()+"/descriptorByName/"+clazz.getName()+"/check"+capitalizedFieldName+"?value=")+"+encode(this.value)";
}
/**
@@ -0,0 +1,27 @@
package hudson.model;
/**
* Adds {@link #getDescriptorByName(String)} to bind {@link Descriptor}s to URL.
* Binding them at some specific object (instead of {@link Hudson}), allows
* {@link Descriptor}s to perform context-specific form field validation.
*
* <p>
* {@link Descriptor#getCheckUrl(String)} finds an ancestor with this interface
* and generates URLs against it.
*
* @author Kohsuke Kawaguchi
* @since 1.294
* @see Descriptor#getCheckUrl(String)
*/
public interface DescriptorByNameOwner extends ModelObject {
/**
* Exposes all {@link Descriptor}s by its name to URL.
*
* <p>
* Implementation should always delegate to {@link Hudson#getDescriptorByName(String)}.
*
* @param className
* Either fully qualified class name (recommended) or the short name.
*/
Descriptor getDescriptorByName(String className);
}
+18 -4
View File
@@ -192,7 +192,7 @@ import groovy.lang.GroovyShell;
* @author Kohsuke Kawaguchi
*/
@ExportedBean
public final class Hudson extends Node implements ItemGroup<TopLevelItem>, StaplerProxy, StaplerFallback, ViewGroup, AccessControlled {
public final class Hudson extends Node implements ItemGroup<TopLevelItem>, StaplerProxy, StaplerFallback, ViewGroup, AccessControlled, DescriptorByNameOwner {
private transient final Queue queue;
/**
@@ -707,15 +707,29 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
*
* After doing all the {@code getXXX(shortClassName)} methods, I finally realized that
* this just doesn't scale.
*
* @param className
* Either fully qualified class name (recommended) or the short name.
*/
public Descriptor getDescriptor(String fullyQualifiedClassName) {
public Descriptor getDescriptor(String className) {
// legacy descriptors that are reigstered manually doesn't show up in getExtensionList, so check them explicitly.
for( Descriptor d : Iterators.sequence(getExtensionList(Descriptor.class),DescriptorExtensionList.listLegacyInstances()) )
if(d.clazz.getName().equals(fullyQualifiedClassName))
for( Descriptor d : Iterators.sequence(getExtensionList(Descriptor.class),DescriptorExtensionList.listLegacyInstances()) ) {
String name = d.clazz.getName();
if(name.equals(className))
return d;
if(name.substring(name.lastIndexOf('.')+1).equals(className))
return d;
}
return null;
}
/**
* Alias for {@link #getDescriptor(String)}.
*/
public Descriptor getDescriptorByName(String className) {
return getDescriptor(className);
}
/**
* Gets the {@link Descriptor} that corresponds to the given {@link Describable} type.
* <p>
+5 -1
View File
@@ -105,7 +105,7 @@ import com.thoughtworks.xstream.XStream;
*/
@ExportedBean
public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,RunT>>
extends Actionable implements ExtensionPoint, Comparable<RunT>, AccessControlled, PersistenceRoot {
extends Actionable implements ExtensionPoint, Comparable<RunT>, AccessControlled, PersistenceRoot, DescriptorByNameOwner {
protected transient final JobT project;
@@ -552,6 +552,10 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
return ID_FORMATTER.get().format(new Date(timestamp));
}
public Descriptor getDescriptorByName(String className) {
return Hudson.getInstance().getDescriptorByName(className);
}
/**
* Root directory of this {@link Run} on the master.
*