1818 */
1919package org .apache .brooklyn .policy .action ;
2020
21+ import java .text .DateFormat ;
22+ import java .text .ParseException ;
23+ import java .text .SimpleDateFormat ;
2124import java .time .LocalTime ;
2225import java .util .Calendar ;
2326import java .util .Date ;
@@ -56,6 +59,9 @@ public abstract class AbstractScheduledEffectorPolicy extends AbstractPolicy imp
5659
5760 private static final Logger LOG = LoggerFactory .getLogger (AbstractScheduledEffectorPolicy .class );
5861
62+ private static final String TIME_FORMAT = "HH:mm:ss" ;
63+ private static final DateFormat FORMATTER = SimpleDateFormat .getTimeInstance ();
64+
5965 public static final ConfigKey <String > EFFECTOR = ConfigKeys .builder (String .class )
6066 .name ("effector" )
6167 .description ("The effector to be executed by this policy" )
@@ -69,9 +75,9 @@ public abstract class AbstractScheduledEffectorPolicy extends AbstractPolicy imp
6975 .defaultValue (ImmutableMap .<String , Object >of ())
7076 .build ();
7177
72- public static final ConfigKey <Date > TIME = ConfigKeys .builder (Date .class )
78+ public static final ConfigKey <String > TIME = ConfigKeys .builder (String .class )
7379 .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 " )
7581 .build ();
7682
7783 public static final ConfigKey <Duration > WAIT = ConfigKeys .builder (Duration .class )
@@ -113,15 +119,22 @@ protected Effector<?> getEffector() {
113119 return effector .get ();
114120 }
115121
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 pe ) {
135+ LOG .warn ("{}: Time should be formatted as {}: {}" , new Object [] { this , TIME_FORMAT , pe .getMessage () });
136+ throw Exceptions .propagate (pe );
123137 }
124- return Duration .millis (Math .max (0 , when .getTimeInMillis () - now .getTimeInMillis ()));
125138 }
126139
127140 @ Override
0 commit comments