提交 c7cb521a 编写于 作者: O Oleg Nenashev 提交者: Oliver Gondža

[JENKINS-34914] - Prevent NPE in Jenkins#getRootURL() when the nstance is not fully loaded

(cherry picked from commit c064d88a)
上级 2eea8902
......@@ -2326,12 +2326,21 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve
* It is done in this order so that it can work correctly even in the face
* of a reverse proxy.
*
* @return null if this parameter is not configured by the user and the calling thread is not in an HTTP request; otherwise the returned URL will always have the trailing {@code /}
* @return {@code null} if this parameter is not configured by the user and the calling thread is not in an HTTP request;
* otherwise the returned URL will always have the trailing {@code /}
* @throws IllegalStateException {@link JenkinsLocationConfiguration} cannot be retrieved.
* Jenkins instance may be not ready, or there is an extension loading glitch.
* @since 1.66
* @see <a href="https://wiki.jenkins-ci.org/display/JENKINS/Hyperlinks+in+HTML">Hyperlinks in HTML</a>
*/
public @Nullable String getRootUrl() {
String url = JenkinsLocationConfiguration.get().getUrl();
public @Nullable String getRootUrl() throws IllegalStateException {
final JenkinsLocationConfiguration config = JenkinsLocationConfiguration.get();
if (config == null) {
// Try to get standard message if possible
final Jenkins j = Jenkins.getInstance();
throw new IllegalStateException("Jenkins instance " + j + " has been successfully initialized, but JenkinsLocationConfiguration is undefined.");
}
String url = config.getUrl();
if(url!=null) {
return Util.ensureEndsWith(url,"/");
}
......
......@@ -41,6 +41,12 @@ public class JenkinsLocationConfiguration extends GlobalConfiguration {
// just to suppress warnings
private transient String charset,useSsl;
/**
* Gets local configuration.
*
* @return {@code null} if the {@link GlobalConfiguration#all()} list does not contain this extension.
* Most likely it means that the Jenkins instance has not been fully loaded yet.
*/
public static @CheckForNull JenkinsLocationConfiguration get() {
return GlobalConfiguration.all().get(JenkinsLocationConfiguration.class);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册