Skip to content

Commit a95b6ff

Browse files
author
Richard Valdivieso
committed
Spring Security provides a DelegatingSecurityContextRunnable
abstraction for Runnable that can be used for async and scheduled tasks. The primary contract for task scheduling is TaskScheduler and there's no such wrapper available at the moment. The new DelegatingSecurityContextTaskScheduler class implements TaskScheduler interface. Fixes gh-6043
1 parent 3c35f4c commit a95b6ff

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.scheduling;
17+
18+
import org.springframework.scheduling.TaskScheduler;
19+
import org.springframework.scheduling.Trigger;
20+
import org.springframework.util.Assert;
21+
22+
import java.util.Date;
23+
import java.util.concurrent.ScheduledFuture;
24+
25+
/**
26+
* An implementation of {@link TaskScheduler} invoking it whenever the trigger
27+
* indicates a next execution time.
28+
*
29+
* @author Richard Valdivieso
30+
* @since 5.1
31+
*/
32+
public class DelegatingSecurityContextTaskScheduler implements TaskScheduler {
33+
34+
private final TaskScheduler taskScheduler;
35+
36+
/**
37+
* Creates a new {@link DelegatingSecurityContextTaskScheduler}
38+
*
39+
* @param taskScheduler the {@link TaskScheduler}
40+
*/
41+
public DelegatingSecurityContextTaskScheduler(TaskScheduler taskScheduler) {
42+
Assert.notNull(taskScheduler, "Task scheduler must not be null");
43+
this.taskScheduler = taskScheduler;
44+
}
45+
46+
@Override
47+
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
48+
return taskScheduler.schedule(task, trigger);
49+
}
50+
51+
@Override
52+
public ScheduledFuture<?> schedule(Runnable task, Date startTime) {
53+
return taskScheduler.schedule(task, startTime);
54+
}
55+
56+
@Override
57+
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period) {
58+
return taskScheduler.scheduleAtFixedRate(task, startTime, period);
59+
}
60+
61+
@Override
62+
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) {
63+
return taskScheduler.scheduleAtFixedRate(task, period);
64+
}
65+
66+
@Override
67+
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
68+
return taskScheduler.scheduleWithFixedDelay(task, startTime, delay);
69+
}
70+
71+
@Override
72+
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay) {
73+
return taskScheduler.scheduleWithFixedDelay(task, delay);
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.scheduling;
17+
18+
import org.junit.After;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.mockito.Mock;
22+
import org.mockito.MockitoAnnotations;
23+
import org.springframework.scheduling.TaskScheduler;
24+
import org.springframework.scheduling.Trigger;
25+
26+
import java.time.Duration;
27+
import java.time.Instant;
28+
import java.util.Date;
29+
30+
import static org.mockito.Mockito.*;
31+
32+
/**
33+
* Test An implementation of {@link TaskScheduler} invoking it whenever the trigger
34+
* indicates a next execution time.
35+
*
36+
* @author Richard Valdivieso
37+
* @since 5.1
38+
*/
39+
public class DelegatingSecurityContextTaskSchedulerTests {
40+
41+
@Mock
42+
private TaskScheduler scheduler;
43+
@Mock
44+
private Runnable runnable;
45+
@Mock
46+
private Trigger trigger;
47+
48+
private DelegatingSecurityContextTaskScheduler delegatingSecurityContextTaskScheduler;
49+
50+
@Before
51+
public void setup() {
52+
MockitoAnnotations.initMocks(this);
53+
delegatingSecurityContextTaskScheduler = new DelegatingSecurityContextTaskScheduler(scheduler);
54+
}
55+
56+
@After
57+
public void cleanup() {
58+
delegatingSecurityContextTaskScheduler = null;
59+
}
60+
61+
@Test(expected = IllegalArgumentException.class)
62+
public void testSchedulerIsNotNull() {
63+
delegatingSecurityContextTaskScheduler = new DelegatingSecurityContextTaskScheduler(null);
64+
}
65+
66+
@Test
67+
public void testSchedulerWithRunnableAndTrigger() {
68+
delegatingSecurityContextTaskScheduler.schedule(runnable, trigger);
69+
verify(scheduler).schedule(any(Runnable.class), any(Trigger.class));
70+
}
71+
72+
@Test
73+
public void testSchedulerWithRunnableAndInstant() {
74+
Instant date = Instant.now();
75+
delegatingSecurityContextTaskScheduler.schedule(runnable, date);
76+
verify(scheduler).schedule(any(Runnable.class), any(Date.class));
77+
}
78+
79+
@Test
80+
public void testScheduleAtFixedRateWithRunnableAndDate() {
81+
Date date = new Date(1544751374L);
82+
Duration duration = Duration.ofSeconds(4L);
83+
delegatingSecurityContextTaskScheduler.scheduleAtFixedRate(runnable, date, 1000L);
84+
verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), isA(Date.class), eq(1000L));
85+
}
86+
87+
@Test
88+
public void testScheduleAtFixedRateWithRunnableAndLong() {
89+
delegatingSecurityContextTaskScheduler.scheduleAtFixedRate(runnable, 1000L);
90+
verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(1000L));
91+
}
92+
}

docs/manual/src/docs/asciidoc/_includes/servlet/additional-topics/concurrency.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,4 @@ They are quite self-explanatory once you understand the previous code.
165165
* DelegatingSecurityContextSchedulingTaskExecutor
166166
* DelegatingSecurityContextAsyncTaskExecutor
167167
* DelegatingSecurityContextTaskExecutor
168+
* DelegatingSecurityContextTaskScheduler

0 commit comments

Comments
 (0)