Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/JENKINS-26781' into JENKINS-26781
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Apr 11, 2015
2 parents 3304190 + 901194a commit 724df64
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
53 changes: 44 additions & 9 deletions core/src/main/java/hudson/model/Descriptor.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -909,38 +909,73 @@ List<T> newInstancesFromHeteroList(StaplerRequest req, Object formData,
if (formData!=null) { if (formData!=null) {
for (Object o : JSONArray.fromObject(formData)) { for (Object o : JSONArray.fromObject(formData)) {
JSONObject jo = (JSONObject)o; JSONObject jo = (JSONObject)o;
String kind = jo.optString("$class", null); Descriptor<T> d = null;
if (kind == null) { String kind = jo.optString("kind", null);
// Legacy: Remove once plugins have been staged onto $class if (kind != null) {
kind = jo.getString("kind"); d = findById(descriptors, kind);
}
if (d == null) {
kind = jo.getString("$class");
d = findByDescribableClassName(descriptors, kind);
if (d == null) d = findByClassName(descriptors, kind);
} }
Descriptor<T> d = find(descriptors, kind);
if (d != null) { if (d != null) {
items.add(d.newInstance(req, jo)); items.add(d.newInstance(req, jo));
} else {
LOGGER.warning("Received unexpected formData for descriptor " + kind);
} }
} }
} }


return items; return items;
} }


/**
* Finds a descriptor from a collection by its id.
*/
public static @CheckForNull <T extends Descriptor> T findById(Collection<? extends T> list, String id) {
for (T d : list) {
if(d.getId().equals(id))
return d;
}
return null;
}

/** /**
* Finds a descriptor from a collection by its class name. * Finds a descriptor from a collection by its class name.
* @deprecated Since we introduced {@link Descriptor#getId()}, it is a preferred method of identifying descriptor by a string.
*/ */
public static @CheckForNull <T extends Descriptor> T find(Collection<? extends T> list, String className) { public static @CheckForNull <T extends Descriptor> T findByClassName(Collection<? extends T> list, String className) {
for (T d : list) { for (T d : list) {
if(d.getClass().getName().equals(className)) if(d.getClass().getName().equals(className))
return d; return d;
} }
// Since we introduced Descriptor.getId(), it is a preferred method of identifying descriptor by a string. return null;
// To make that migration easier without breaking compatibility, let's also match up with the id. }

/**
* Finds a descriptor from a collection by the class name of the Describable it describes.
*/
public static @CheckForNull <T extends Descriptor> T findByDescribableClassName(Collection<? extends T> list, String className) {
for (T d : list) { for (T d : list) {
if(d.getId().equals(className)) if(d.clazz.getName().equals(className))
return d; return d;
} }
return null; return null;
} }


/**
* Finds a descriptor from a collection by its class name or ID.
* @deprecated choose between {@link #findById(java.util.Collection, String)} or {@link #findByClassName(java.util.Collection, String)}
*/
public static @CheckForNull <T extends Descriptor> T find(Collection<? extends T> list, String string) {
T d = findByClassName(list, string);
if (d != null) {
return d;
}
return findById(list, string);
}

public static @CheckForNull Descriptor find(String className) { public static @CheckForNull Descriptor find(String className) {
return find(ExtensionList.lookup(Descriptor.class),className); return find(ExtensionList.lookup(Descriptor.class),className);
} }
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/hudson/tools/DownloadFromUrlInstaller.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ protected DescriptorImpl() {
} }


protected Downloadable createDownloadable() { protected Downloadable createDownloadable() {
return new Downloadable(getId()); return new Downloadable(getDownloadableId());
} }


/** /**
* This ID needs to be unique, and needs to match the ID token in the JSON update file. * This ID needs to be unique, and needs to match the ID token in the JSON update file.
* <p> * <p>
* By default we use the fully-qualified class name of the {@link DownloadFromUrlInstaller} subtype. * By default we use the fully-qualified class name of the {@link DownloadFromUrlInstaller} subtype.
*/ */
public String getId() { public String getDownloadableId() {
return clazz.getName().replace('$','.'); return clazz.getName().replace('$','.');
} }


Expand All @@ -144,7 +144,7 @@ public String getId() {
* @return never null. * @return never null.
*/ */
public List<? extends Installable> getInstallables() throws IOException { public List<? extends Installable> getInstallables() throws IOException {
JSONObject d = Downloadable.get(getId()).getData(); JSONObject d = Downloadable.get(getDownloadableId()).getData();
if(d==null) return Collections.emptyList(); if(d==null) return Collections.emptyList();
return Arrays.asList(((InstallableList)JSONObject.toBean(d,InstallableList.class)).list); return Arrays.asList(((InstallableList)JSONObject.toBean(d,InstallableList.class)).list);
} }
Expand Down
19 changes: 11 additions & 8 deletions core/src/main/resources/lib/form/class-entry.jelly
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ THE SOFTWARE.
</st:documentation> </st:documentation>
<j:set var="clazz" value="${attrs.clazz ?: attrs.descriptor.clazz.name}" /> <j:set var="clazz" value="${attrs.clazz ?: attrs.descriptor.clazz.name}" />
<f:invisibleEntry> <f:invisibleEntry>
<!-- Legacy: Remove once plugins have been staged onto $class --> <j:choose>
<input type="hidden" name="stapler-class" value="${clazz}" /> <j:when test="${descriptor != null and descriptor.id != clazz}">
<!-- Legacy: Remove once plugins have been staged onto $class --> <input type="hidden" name="kind" value="${attrs.descriptor.id}" />
<j:if test="${attrs.descriptor != null}"> </j:when>
<input type="hidden" name="kind" value="${attrs.descriptor.id}" /> <j:otherwise>
</j:if> <!-- Legacy: Remove once plugins have been staged onto $class -->
<input type="hidden" name="$class" value="${clazz}" /> <input type="hidden" name="stapler-class" value="${clazz}" />
<input type="hidden" name="$class" value="${clazz}" />
</j:otherwise>
</j:choose>
</f:invisibleEntry> </f:invisibleEntry>
</j:jelly> </j:jelly>

0 comments on commit 724df64

Please sign in to comment.