Skip to content

Commit

Permalink
[FIXED JENKINS-8866] Look for Descriptor.getId()==null and warn the a…
Browse files Browse the repository at this point in the history
…dministrator
  • Loading branch information
kohsuke committed Apr 7, 2011
1 parent a39141b commit 6694994
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 2 deletions.
4 changes: 3 additions & 1 deletion changelog.html
Expand Up @@ -58,7 +58,9 @@
<!-- Record your changes in the trunk here. -->
<div id="trunk" style="display:none"><!--=TRUNK-BEGIN=-->
<ul class=image>
<li class=>
<li class=rfe>
Proactively watch out for incomplete extensions to avoid cryptic NPE.
(<a href="http://issues.jenkins-ci.org/browse/JENKINS-8866">issue 8866</a>)
</ul>
</div><!--=TRUNK-END=-->

Expand Down
14 changes: 14 additions & 0 deletions core/src/main/java/hudson/PluginManager.java
Expand Up @@ -455,6 +455,20 @@ public <T> Collection<Class<? extends T>> discover( Class<T> spi ) {
return result;
}

/**
* Return the {@link PluginWrapper} that loaded the given class 'c'.
*
* @since 1.402.
*/
public PluginWrapper whichPlugin(Class c) {
ClassLoader cl = c.getClassLoader();
for (PluginWrapper p : activePlugins) {
if (p.classLoader==cl)
return p;
}
return null;
}

/**
* Orderly terminates all the plugins.
*/
Expand Down
80 changes: 80 additions & 0 deletions core/src/main/java/hudson/diagnosis/NullIdDescriptorMonitor.java
@@ -0,0 +1,80 @@
/*
* The MIT License
*
* Copyright (c) 2011, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.diagnosis;

import hudson.Extension;
import hudson.PluginWrapper;
import hudson.init.Initializer;
import hudson.model.AdministrativeMonitor;
import hudson.model.Descriptor;
import hudson.model.Hudson;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

import static hudson.init.InitMilestone.EXTENSIONS_AUGMENTED;

/**
* Some old descriptors apparently has the getId() method that's used in different ways
* and that's causing errors like JENKINS-8866, so detect and report that.
*
* @author Kohsuke Kawaguchi
* @since 1.402
*/
@Extension
public class NullIdDescriptorMonitor extends AdministrativeMonitor {

private final List<Descriptor> problems = new ArrayList<Descriptor>();

@Override
public boolean isActivated() {
return !problems.isEmpty();
}

public List<Descriptor> getProblems() {
return Collections.unmodifiableList(problems);
}

private void verify() {
Hudson h = Hudson.getInstance();
for (Descriptor d : h.getExtensionList(Descriptor.class)) {
if (d.getId()==null) {
PluginWrapper p = h.getPluginManager().whichPlugin(d.getClass());
LOGGER.severe(MessageFormat.format("Descriptor {0} from plugin {1} with display name {2} has null ID",
d, p==null?"???":p.getLongName(), d.getDisplayName()));
problems.add(d);
}
}
}

@Initializer(after=EXTENSIONS_AUGMENTED)
public static void verifyId() {
AdministrativeMonitor.all().get(NullIdDescriptorMonitor.class).verify();
}

private static final Logger LOGGER = Logger.getLogger(NullIdDescriptorMonitor.class.getName());
}
4 changes: 4 additions & 0 deletions core/src/main/java/hudson/model/Descriptor.java
Expand Up @@ -30,6 +30,8 @@
import hudson.Util;
import static hudson.Functions.jsStringEscape;
import hudson.diagnosis.OldDataMonitor;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.model.listeners.SaveableListener;
import hudson.util.ReflectionUtils;
import hudson.util.ReflectionUtils.Parameter;
Expand All @@ -41,12 +43,14 @@
import org.jvnet.tiger_types.Types;
import org.apache.commons.io.IOUtils;

import static hudson.init.InitMilestone.EXTENSIONS_AUGMENTED;
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/Hudson.java
Expand Up @@ -1681,7 +1681,7 @@ public static final class DescriptorImpl extends NodeDescriptor {
public static final DescriptorImpl INSTANCE = new DescriptorImpl();

public String getDisplayName() {
throw new UnsupportedOperationException();
return "";
}

@Override
Expand Down
@@ -0,0 +1,36 @@
<!--
The MIT License
Copyright (c) 2011, CloudBees, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<div class="error">
${%blurb}
<ul>
<j:forEach var="d" items="${it.problems}">
<li>
${%problem(d, d.displayName, app.pluginManager.whichPlugin(d.getClass()))}
</li>
</j:forEach>
</ul>
</div>
</j:jelly>
@@ -0,0 +1,3 @@
blurb=The following extensions have no ID value and therefore likely cause a problem. Please upgrade these plugins if they are not the latest, \
and if they are the latest, please file a bug so that we can fix them.
problem=Descriptor {0} from plugin {2} with display name {1}

0 comments on commit 6694994

Please sign in to comment.