1
1
package org .jenkinsci .plugins .gwt .jobfinder ;
2
2
3
+ import hudson .Extension ;
4
+ import hudson .model .Item ;
5
+ import hudson .model .listeners .ItemListener ;
3
6
import hudson .security .ACL ;
7
+ import java .util .ArrayList ;
4
8
import java .util .List ;
9
+ import java .util .Map ;
10
+ import java .util .TreeSet ;
11
+ import java .util .concurrent .ConcurrentHashMap ;
12
+ import java .util .logging .Level ;
5
13
import java .util .logging .Logger ;
6
14
import jenkins .model .Jenkins ;
7
15
import jenkins .model .ParameterizedJobMixIn .ParameterizedJob ;
8
16
import org .acegisecurity .context .SecurityContext ;
9
17
import org .acegisecurity .context .SecurityContextHolder ;
10
18
11
- public class JobFinderImpersonater {
19
+ @ Extension
20
+ public class JobFinderImpersonater extends ItemListener {
12
21
private static Logger LOGGER = Logger .getLogger (JobFinderImpersonater .class .getName ());
13
-
14
- public JobFinderImpersonater () {}
22
+ private static final Map <String , ParameterizedJob > JOBS_WITH_GWT = new ConcurrentHashMap <>();
15
23
16
24
public List <ParameterizedJob > getAllParameterizedJobs (final boolean impersonate ) {
25
+ if (LOGGER .isLoggable (Level .FINEST )) {
26
+ LOGGER .finest ("There are " + this .JOBS_WITH_GWT .size () + " jobs in cache:" );
27
+ final TreeSet <String > sortedSet = new TreeSet <>(this .JOBS_WITH_GWT .keySet ());
28
+ for (final String job : sortedSet ) {
29
+ LOGGER .finest (" " + job );
30
+ }
31
+ }
32
+ if (impersonate ) {
33
+ LOGGER .log (Level .FINE , "Using the cache" );
34
+ return new ArrayList <>(this .JOBS_WITH_GWT .values ());
35
+ }
36
+ LOGGER .log (
37
+ Level .FINE ,
38
+ "Not using the cache because jobs are not retreieved with impersonation SYSTEM. "
39
+ + "SYSTEM is only impersonated when using a token."
40
+ + " If SYSTEM is not impersonated, only jobs available for the currently authenticated user is found." );
41
+ return doGetAllParameterizedJobs (impersonate );
42
+ }
43
+
44
+ private static List <ParameterizedJob > doGetAllParameterizedJobs (final boolean impersonate ) {
17
45
SecurityContext orig = null ;
18
46
try {
19
47
if (impersonate ) {
@@ -26,4 +54,54 @@ public List<ParameterizedJob> getAllParameterizedJobs(final boolean impersonate)
26
54
}
27
55
}
28
56
}
57
+
58
+ @ Override
59
+ public void onLoaded () {
60
+ for (final ParameterizedJob job : doGetAllParameterizedJobs (true )) {
61
+ this .putJob (job );
62
+ }
63
+ LOGGER .info ("Loaded " + this .JOBS_WITH_GWT .size () + " jobs in cache" );
64
+ }
65
+
66
+ @ Override
67
+ public void onUpdated (final Item job ) {
68
+ this .putJob (job );
69
+ }
70
+
71
+ @ Override
72
+ public void onCreated (final Item job ) {
73
+ this .putJob (job );
74
+ }
75
+
76
+ @ Override
77
+ public void onDeleted (final Item job ) {
78
+ this .deleteJob (job );
79
+ }
80
+
81
+ @ Override
82
+ public void onCopied (final Item src , final Item job ) {
83
+ this .putJob (job );
84
+ }
85
+
86
+ @ Override
87
+ public void onLocationChanged (
88
+ final Item item , final String oldFullName , final String newFullName ) {
89
+ this .JOBS_WITH_GWT .remove (oldFullName );
90
+ this .putJob (item );
91
+ }
92
+
93
+ private void putJob (final Item job ) {
94
+ if (job instanceof ParameterizedJob ) {
95
+ final ParameterizedJob parameterizedJob = (ParameterizedJob ) job ;
96
+ final boolean hasGenericTrigger =
97
+ GenericTriggerFinder .findGenericTrigger (parameterizedJob .getTriggers ()) != null ;
98
+ if (hasGenericTrigger ) {
99
+ this .JOBS_WITH_GWT .put (parameterizedJob .getFullName (), parameterizedJob );
100
+ }
101
+ }
102
+ }
103
+
104
+ private void deleteJob (final Item job ) {
105
+ this .JOBS_WITH_GWT .remove (job .getFullName ());
106
+ }
29
107
}
0 commit comments