Skip to content

Commit 63a5803

Browse files
Merge pull request #3362 from jglick/SystemProperties-JENKINS-46386
[JENKINS-46386] Make SystemProperties safe to call from agent JVMs
2 parents 9e667a2 + 7948315 commit 63a5803

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

core/src/main/java/jenkins/util/SystemProperties.java

+36-28
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package jenkins.util;
2525

2626
import edu.umd.cs.findbugs.annotations.CheckForNull;
27-
import edu.umd.cs.findbugs.annotations.Nullable;
2827
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2928
import hudson.EnvVars;
3029
import java.util.logging.Level;
@@ -67,34 +66,40 @@
6766
*/
6867
//TODO: Define a correct design of this engine later. Should be accessible in libs (remoting, stapler) and Jenkins modules too
6968
@Restricted(NoExternalUse.class)
70-
public class SystemProperties implements ServletContextListener, OnMaster {
71-
// this class implements ServletContextListener and is declared in WEB-INF/web.xml
69+
public class SystemProperties {
7270

73-
/**
74-
* The ServletContext to get the "init" parameters from.
75-
*/
76-
@CheckForNull
77-
private static ServletContext theContext;
71+
// declared in WEB-INF/web.xml
72+
public static final class Listener implements ServletContextListener, OnMaster {
73+
74+
/**
75+
* The ServletContext to get the "init" parameters from.
76+
*/
77+
@CheckForNull
78+
private static ServletContext theContext;
79+
80+
/**
81+
* Called by the servlet container to initialize the {@link ServletContext}.
82+
*/
83+
@Override
84+
@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD",
85+
justification = "Currently Jenkins instance may have one ond only one context")
86+
public void contextInitialized(ServletContextEvent event) {
87+
theContext = event.getServletContext();
88+
}
89+
90+
@Override
91+
public void contextDestroyed(ServletContextEvent event) {
92+
theContext = null;
93+
}
94+
95+
}
7896

7997
/**
8098
* Logger.
8199
*/
82100
private static final Logger LOGGER = Logger.getLogger(SystemProperties.class.getName());
83101

84-
/**
85-
* Public for the servlet container.
86-
*/
87-
public SystemProperties() {}
88-
89-
/**
90-
* Called by the servlet container to initialize the {@link ServletContext}.
91-
*/
92-
@Override
93-
@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD",
94-
justification = "Currently Jenkins instance may have one ond only one context")
95-
public void contextInitialized(ServletContextEvent event) {
96-
theContext = event.getServletContext();
97-
}
102+
private SystemProperties() {}
98103

99104
/**
100105
* Gets the system property indicated by the specified key.
@@ -373,9 +378,16 @@ public static Long getLong(String name, Long def, Level logLevel) {
373378

374379
@CheckForNull
375380
private static String tryGetValueFromContext(String key) {
376-
if (StringUtils.isNotBlank(key) && theContext != null) {
381+
if (!JenkinsJVM.isJenkinsJVM()) {
382+
return null;
383+
}
384+
return doTryGetValueFromContext(key);
385+
}
386+
387+
private static String doTryGetValueFromContext(String key) {
388+
if (StringUtils.isNotBlank(key) && Listener.theContext != null) {
377389
try {
378-
String value = theContext.getInitParameter(key);
390+
String value = Listener.theContext.getInitParameter(key);
379391
if (value != null) {
380392
return value;
381393
}
@@ -387,8 +399,4 @@ private static String tryGetValueFromContext(String key) {
387399
return null;
388400
}
389401

390-
@Override
391-
public void contextDestroyed(ServletContextEvent event) {
392-
// nothing to do
393-
}
394402
}

test/src/test/java/jenkins/util/SystemPropertiesTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class SystemPropertiesTest {
4646

4747
@Before
4848
public void setUp() {
49-
new SystemProperties().contextInitialized(new ServletContextEvent(j.jenkins.servletContext));
49+
new SystemProperties.Listener().contextInitialized(new ServletContextEvent(j.jenkins.servletContext));
5050
}
5151

5252
@Test

war/src/main/webapp/WEB-INF/web.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ THE SOFTWARE.
153153

154154
<listener>
155155
<!-- Must be before WebAppMain in order to initialize the context before the first use of this class. -->
156-
<listener-class>jenkins.util.SystemProperties</listener-class>
156+
<listener-class>jenkins.util.SystemProperties$Listener</listener-class>
157157
</listener>
158158
<listener>
159159
<listener-class>hudson.WebAppMain</listener-class>

0 commit comments

Comments
 (0)