6

I have a text field where users can enter a cron expression (e.g., 0 */5 * * * *). I then split it and build a javax.ejb.ScheduleExpression.

Now javax.ejb.ScheduleExpression accepts any String for the different elements without validation. I can for example

scheduleExpression.minute("randomText");

and is accepted. If then try to use the ScheduleExpression I obviously get errors (e.g., when I try to create a timer with it).

I was beginning to write code to validate the input but the rules are not so short and trivial: http://docs.oracle.com/javaee/6/api/javax/ejb/ScheduleExpression.html

Is there a simple way (in Java EE) or a library that does already does the job?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Matteo
  • 14,696
  • 9
  • 68
  • 106

1 Answers1

4

Since I ran into the same problem I have builded a bean validator annotation for EJB timers.

Feel free to use it. For all those who do not want to or can use bean validation, have a look at the regex in file CronField.java to validate your strings manually.

The regular expressions are designed for "ScheduleExpression" not for "CronExpression" so my chosen name might seem a bit confusing.

If you have improvements or optimization, please send me a pull request or a message.

The source is available at this repository.

Usage: public class Month {

    @CheckCronField(CronField.MONTH)
    public String expression;
    ...
}

Some more examples are available in test folder in the same repository.

BigAl
  • 313
  • 3
  • 10
  • Hi, seems interesting. Could you please add licensing information? Is your code open source? Which license? – Matteo Jul 08 '13 at 13:02
  • Hey BigAl, I see that you are allowing "/" for the year field, but this results in an invalid ScheduleExpression. So the regex (^((((\\d{4})|\\*)(-(\\d{4}))?,)*((\\d{4})|\\*)(-(\\d{4}))?)$) would be the one to use here. – user1983983 Jul 18 '13 at 10:34
  • Thanks for the hint. I have changed this regex in the repo. Sorry for the late reply, but I was on vacation. Additionally I searching for a license. But I dont see which one of this huge amount of licenses is better to use?! – BigAl Jul 22 '13 at 19:52