24
24
package jenkins .util ;
25
25
26
26
import edu .umd .cs .findbugs .annotations .CheckForNull ;
27
- import edu .umd .cs .findbugs .annotations .Nullable ;
28
27
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
29
28
import hudson .EnvVars ;
30
29
import java .util .logging .Level ;
67
66
*/
68
67
//TODO: Define a correct design of this engine later. Should be accessible in libs (remoting, stapler) and Jenkins modules too
69
68
@ 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 {
72
70
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
+ }
78
96
79
97
/**
80
98
* Logger.
81
99
*/
82
100
private static final Logger LOGGER = Logger .getLogger (SystemProperties .class .getName ());
83
101
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 () {}
98
103
99
104
/**
100
105
* Gets the system property indicated by the specified key.
@@ -373,9 +378,16 @@ public static Long getLong(String name, Long def, Level logLevel) {
373
378
374
379
@ CheckForNull
375
380
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 ) {
377
389
try {
378
- String value = theContext .getInitParameter (key );
390
+ String value = Listener . theContext .getInitParameter (key );
379
391
if (value != null ) {
380
392
return value ;
381
393
}
@@ -387,8 +399,4 @@ private static String tryGetValueFromContext(String key) {
387
399
return null ;
388
400
}
389
401
390
- @ Override
391
- public void contextDestroyed (ServletContextEvent event ) {
392
- // nothing to do
393
- }
394
402
}
0 commit comments