Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate a javax.ejb.ScheduleExpression

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?

like image 303
Matteo Avatar asked Jan 17 '13 17:01

Matteo


1 Answers

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.

like image 156
BigAl Avatar answered Sep 24 '22 15:09

BigAl