Skip to content

Commit

Permalink
[JENKINS-34914] - Prevent NPE in Jenkins#getRootURL() when the nstanc…
Browse files Browse the repository at this point in the history
…e is not fully loaded
  • Loading branch information
oleg-nenashev committed Jul 9, 2017
1 parent 00956ff commit c064d88
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 12 additions & 3 deletions core/src/main/java/jenkins/model/Jenkins.java
Expand Up @@ -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,"/");
}
Expand Down
Expand Up @@ -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);
}
Expand Down

0 comments on commit c064d88

Please sign in to comment.