Skip to content
Permalink
Browse files

Merge pull request #3362 from jglick/SystemProperties-JENKINS-46386

[JENKINS-46386] Make SystemProperties safe to call from agent JVMs
  • Loading branch information
oleg-nenashev committed Mar 23, 2018
2 parents 9e667a2 + 7948315 commit 63a58031624f196b18d93aef00c76a10543ec95f
@@ -24,7 +24,6 @@
package jenkins.util;

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

/**
* The ServletContext to get the "init" parameters from.
*/
@CheckForNull
private static ServletContext theContext;
// declared in WEB-INF/web.xml
public static final class Listener implements ServletContextListener, OnMaster {

/**
* The ServletContext to get the "init" parameters from.
*/
@CheckForNull
private static ServletContext theContext;

/**
* Called by the servlet container to initialize the {@link ServletContext}.
*/
@Override
@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD",
justification = "Currently Jenkins instance may have one ond only one context")
public void contextInitialized(ServletContextEvent event) {
theContext = event.getServletContext();
}

@Override
public void contextDestroyed(ServletContextEvent event) {
theContext = null;
}

}

/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(SystemProperties.class.getName());

/**
* Public for the servlet container.
*/
public SystemProperties() {}

/**
* Called by the servlet container to initialize the {@link ServletContext}.
*/
@Override
@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD",
justification = "Currently Jenkins instance may have one ond only one context")
public void contextInitialized(ServletContextEvent event) {
theContext = event.getServletContext();
}
private SystemProperties() {}

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

@CheckForNull
private static String tryGetValueFromContext(String key) {
if (StringUtils.isNotBlank(key) && theContext != null) {
if (!JenkinsJVM.isJenkinsJVM()) {
return null;
}
return doTryGetValueFromContext(key);
}

private static String doTryGetValueFromContext(String key) {
if (StringUtils.isNotBlank(key) && Listener.theContext != null) {
try {
String value = theContext.getInitParameter(key);
String value = Listener.theContext.getInitParameter(key);
if (value != null) {
return value;
}
@@ -387,8 +399,4 @@ private static String tryGetValueFromContext(String key) {
return null;
}

@Override
public void contextDestroyed(ServletContextEvent event) {
// nothing to do
}
}
@@ -46,7 +46,7 @@

@Before
public void setUp() {
new SystemProperties().contextInitialized(new ServletContextEvent(j.jenkins.servletContext));
new SystemProperties.Listener().contextInitialized(new ServletContextEvent(j.jenkins.servletContext));
}

@Test
@@ -153,7 +153,7 @@ THE SOFTWARE.

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

0 comments on commit 63a5803

Please sign in to comment.
You can’t perform that action at this time.