Skip to content
Permalink
Browse files
Merge pull request #2935 from oleg-nenashev/bug/JENKINS-34914-getRoot…
…Url()-NPE

[JENKINS-34914] - Prevent NPE in Jenkins#getRootURL() when the instance is not fully loaded
  • Loading branch information
oleg-nenashev committed Jul 18, 2017
2 parents 82d2e5c + c064d88 commit de10b2b690af8a392660f813b23683fa7145d666
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 de10b2b

Please sign in to comment.