Skip to content
Permalink
Browse files
[JENKINS-34914] - Prevent NPE in Jenkins#getRootURL() when the nstanc…
…e is not fully loaded
  • Loading branch information
oleg-nenashev committed Jul 9, 2017
1 parent 00956ff commit c064d88a3b418e4cc6e3a75b4faa7db4c69880f2
Showing with 18 additions and 3 deletions.
  1. +12 −3 core/src/main/java/jenkins/model/Jenkins.java
  2. +6 −0 core/src/main/java/jenkins/model/JenkinsLocationConfiguration.java
@@ -2326,12 +2326,21 @@ public String getUrlChildPrefix() {
* 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 @@
// 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);
}

0 comments on commit c064d88

Please sign in to comment.