18
18
*/
19
19
package org .apache .brooklyn .policy .action ;
20
20
21
+ import java .text .DateFormat ;
22
+ import java .text .ParseException ;
23
+ import java .text .SimpleDateFormat ;
21
24
import java .time .LocalTime ;
22
25
import java .util .Calendar ;
23
26
import java .util .Date ;
@@ -56,6 +59,9 @@ public abstract class AbstractScheduledEffectorPolicy extends AbstractPolicy imp
56
59
57
60
private static final Logger LOG = LoggerFactory .getLogger (AbstractScheduledEffectorPolicy .class );
58
61
62
+ private static final String TIME_FORMAT = "HH:mm:ss" ;
63
+ private static final DateFormat FORMATTER = SimpleDateFormat .getTimeInstance ();
64
+
59
65
public static final ConfigKey <String > EFFECTOR = ConfigKeys .builder (String .class )
60
66
.name ("effector" )
61
67
.description ("The effector to be executed by this policy" )
@@ -69,9 +75,9 @@ public abstract class AbstractScheduledEffectorPolicy extends AbstractPolicy imp
69
75
.defaultValue (ImmutableMap .<String , Object >of ())
70
76
.build ();
71
77
72
- public static final ConfigKey <Date > TIME = ConfigKeys .builder (Date .class )
78
+ public static final ConfigKey <String > TIME = ConfigKeys .builder (String .class )
73
79
.name ("time" )
74
- .description ("An optional time when this policy should be first executed" )
80
+ .description ("An optional time when this policy should be first executed, formatted as HH:mm:ss " )
75
81
.build ();
76
82
77
83
public static final ConfigKey <Duration > WAIT = ConfigKeys .builder (Duration .class )
@@ -113,15 +119,22 @@ protected Effector<?> getEffector() {
113
119
return effector .get ();
114
120
}
115
121
116
- protected Duration getWaitUntil (Date time ) {
117
- Calendar now = Calendar .getInstance ();
118
- Calendar when = Calendar .getInstance ();
119
- when .setTime (time );
120
- when .set (now .get (Calendar .YEAR ), now .get (Calendar .MONTH ), now .get (Calendar .DATE ));
121
- if (when .before (now )) {
122
- when .add (Calendar .DATE , 1 );
122
+ protected Duration getWaitUntil (String time ) {
123
+ try {
124
+ Calendar now = Calendar .getInstance ();
125
+ Calendar when = Calendar .getInstance ();
126
+ boolean formatted = time .contains (":" ); // FIXME deprecated TimeDuration coercion
127
+ Date parsed = formatted ? FORMATTER .parse (time ) : new Date (Long .parseLong (time ) * 1000 );
128
+ when .setTime (parsed );
129
+ when .set (now .get (Calendar .YEAR ), now .get (Calendar .MONTH ), now .get (Calendar .DATE ));
130
+ if (when .before (now )) {
131
+ when .add (Calendar .DATE , 1 );
132
+ }
133
+ return Duration .millis (Math .max (0 , when .getTimeInMillis () - now .getTimeInMillis ()));
134
+ } catch (ParseException | NumberFormatException e ) {
135
+ LOG .warn ("{}: Time should be formatted as {}: {}" , new Object [] { this , TIME_FORMAT , e .getMessage () });
136
+ throw Exceptions .propagate (e );
123
137
}
124
- return Duration .millis (Math .max (0 , when .getTimeInMillis () - now .getTimeInMillis ()));
125
138
}
126
139
127
140
@ Override
0 commit comments