Browse Source

Defensively detect non-empty String fields in @Scheduled

Issue: SPR-11223
pull/106/merge
Juergen Hoeller 12 years ago
parent
commit
e2f85fc1d0
  1. 9
      spring-context/src/main/java/org/springframework/scheduling/annotation/Scheduled.java
  2. 14
      spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java

9
spring-context/src/main/java/org/springframework/scheduling/annotation/Scheduled.java

@ -61,12 +61,13 @@ public @interface Scheduled {
String cron() default ""; String cron() default "";
/** /**
* A time zone for which the cron expression will be resolved. * A time zone for which the cron expression will be resolved. By default, this
* By default, the server's local time zone will be used. * attribute is the empty String (i.e. the server's local time zone will be used).
* @return a zone id accepted by {@link java.util.TimeZone#getTimeZone(String)} * @return a zone id accepted by {@link java.util.TimeZone#getTimeZone(String)},
* or an empty String to indicate the server's default time zone
* @since 4.0
* @see org.springframework.scheduling.support.CronTrigger#CronTrigger(String, java.util.TimeZone) * @see org.springframework.scheduling.support.CronTrigger#CronTrigger(String, java.util.TimeZone)
* @see java.util.TimeZone * @see java.util.TimeZone
* @since 4.0
*/ */
String zone() default ""; String zone() default "";

14
spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java

@ -134,8 +134,8 @@ public class ScheduledAnnotationBeanPostProcessor
if (AopUtils.isJdkDynamicProxy(bean)) { if (AopUtils.isJdkDynamicProxy(bean)) {
try { try {
// found a @Scheduled method on the target class for this JDK proxy -> is it // Found a @Scheduled method on the target class for this JDK proxy ->
// also present on the proxy itself? // is it also present on the proxy itself?
method = bean.getClass().getMethod(method.getName(), method.getParameterTypes()); method = bean.getClass().getMethod(method.getName(), method.getParameterTypes());
} }
catch (SecurityException ex) { catch (SecurityException ex) {
@ -158,7 +158,7 @@ public class ScheduledAnnotationBeanPostProcessor
// Determine initial delay // Determine initial delay
long initialDelay = scheduled.initialDelay(); long initialDelay = scheduled.initialDelay();
String initialDelayString = scheduled.initialDelayString(); String initialDelayString = scheduled.initialDelayString();
if (!"".equals(initialDelayString)) { if (StringUtils.hasText(initialDelayString)) {
Assert.isTrue(initialDelay < 0, "Specify 'initialDelay' or 'initialDelayString', not both"); Assert.isTrue(initialDelay < 0, "Specify 'initialDelay' or 'initialDelayString', not both");
if (this.embeddedValueResolver != null) { if (this.embeddedValueResolver != null) {
initialDelayString = this.embeddedValueResolver.resolveStringValue(initialDelayString); initialDelayString = this.embeddedValueResolver.resolveStringValue(initialDelayString);
@ -174,7 +174,7 @@ public class ScheduledAnnotationBeanPostProcessor
// Check cron expression // Check cron expression
String cron = scheduled.cron(); String cron = scheduled.cron();
if (!"".equals(cron)) { if (StringUtils.hasText(cron)) {
Assert.isTrue(initialDelay == -1, "'initialDelay' not supported for cron triggers"); Assert.isTrue(initialDelay == -1, "'initialDelay' not supported for cron triggers");
processedSchedule = true; processedSchedule = true;
String zone = scheduled.zone(); String zone = scheduled.zone();
@ -183,7 +183,7 @@ public class ScheduledAnnotationBeanPostProcessor
zone = this.embeddedValueResolver.resolveStringValue(zone); zone = this.embeddedValueResolver.resolveStringValue(zone);
} }
TimeZone timeZone; TimeZone timeZone;
if (!"".equals(zone)) { if (StringUtils.hasText(zone)) {
timeZone = StringUtils.parseTimeZoneString(zone); timeZone = StringUtils.parseTimeZoneString(zone);
} }
else { else {
@ -205,7 +205,7 @@ public class ScheduledAnnotationBeanPostProcessor
this.registrar.addFixedDelayTask(new IntervalTask(runnable, fixedDelay, initialDelay)); this.registrar.addFixedDelayTask(new IntervalTask(runnable, fixedDelay, initialDelay));
} }
String fixedDelayString = scheduled.fixedDelayString(); String fixedDelayString = scheduled.fixedDelayString();
if (!"".equals(fixedDelayString)) { if (StringUtils.hasText(fixedDelayString)) {
Assert.isTrue(!processedSchedule, errorMessage); Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true; processedSchedule = true;
if (this.embeddedValueResolver != null) { if (this.embeddedValueResolver != null) {
@ -229,7 +229,7 @@ public class ScheduledAnnotationBeanPostProcessor
this.registrar.addFixedRateTask(new IntervalTask(runnable, fixedRate, initialDelay)); this.registrar.addFixedRateTask(new IntervalTask(runnable, fixedRate, initialDelay));
} }
String fixedRateString = scheduled.fixedRateString(); String fixedRateString = scheduled.fixedRateString();
if (!"".equals(fixedRateString)) { if (StringUtils.hasText(fixedRateString)) {
Assert.isTrue(!processedSchedule, errorMessage); Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true; processedSchedule = true;
if (this.embeddedValueResolver != null) { if (this.embeddedValueResolver != null) {

Loading…
Cancel
Save