
com.mtvnet.boxspring.scheduling.quartz.RegularAndPeriodicScheduleParser Maven / Gradle / Ivy
The newest version!
/**
* Copyright (c) 2009, MTV Networks. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.mtvnet.boxspring.scheduling.quartz;
import org.quartz.Trigger;
import org.springframework.beans.factory.InitializingBean;
import java.util.List;
import java.util.Arrays;
/**
* Creates a SimpleTriggerBean
based on the syntax specified in the ATG 2006.3
* Programming Guide
* (http://www.atg.com/repositories/ContentCatalogRepository_en/manuals/ATG2006.3/dynprog/index.html)
* Chapter 5, section Scheduler Service. Currently we support RelativeSchedule and
* PeriodicSchedule, but not CalendarSchedule, though the "with catch up" clause is ignored
* in PeriodicSchedule because there does not seem to be an analog to it in Quartz.
*
* @see org.quartz.Trigger
* @see atg.service.scheduler.RelativeSchedule
* @see atg.service.scheduler.PeriodicSchedule
*/
class RegularAndPeriodicScheduleParser extends PropertyValueParser {
/**
* Tokenized representation of the property value.
*/
private List tokens;
/**
* The simple trigger bean.
*/
private SimpleTriggerBean bean = new SimpleTriggerBean();
public RegularAndPeriodicScheduleParser(String text) {
tokens = createTokenList(text);
}
protected void parse() {
consumeEveryClause();
consumeInClause();
consumeCatchUpClause();
if (!tokens.isEmpty()) {
throw new IllegalArgumentException("There are extra tokens: " + tokens);
}
}
protected Trigger asTrigger() {
return bean;
}
protected InitializingBean asInitializingBean() {
return bean;
}
// consume it if its there, but ignore it
private void consumeCatchUpClause() {
if (tokens.size() >= 2 &&
(tokens.get(0).equals("without") &&
tokens.get(1).equals("catchup"))) {
tokens = tokens.subList(2, tokens.size());
}
if (tokens.size() >= 2 &&
(tokens.get(0).equals("with") &&
tokens.get(1).equals("catchup"))) {
tokens = tokens.subList(2, tokens.size());
}
if (tokens.size() >= 3 &&
(tokens.get(0).equals("with") &&
tokens.get(1).equals("catch") &&
tokens.get(2).equals("up"))) {
tokens = tokens.subList(3, tokens.size());
}
if (tokens.size() >= 3 &&
(tokens.get(0).equals("without") &&
tokens.get(1).equals("catch") &&
tokens.get(2).equals("up"))) {
tokens = tokens.subList(3, tokens.size());
}
}
private void consumeInClause() {
if (tokens.isEmpty() || !tokens.get(0).equals("in")) {
return;
}
bean.setStartDelay(TimeUnit.fromName(tokens.get(2)).toMilliseconds(Long.parseLong(tokens.get(1))));
tokens = tokens.subList(3, tokens.size());
}
private void consumeEveryClause() {
if (tokens.isEmpty() || !tokens.get(0).equals("every")) {
bean.setRepeatCount(-1);
return;
}
bean.setRepeatInterval(TimeUnit.fromName(tokens.get(2)).toMilliseconds(Long.parseLong(tokens.get(1))));
tokens = tokens.subList(3, tokens.size());
}
private enum TimeUnit {
MS(new String[]{"ms", "msec", "millisecond", "milliseconds"}) {
long toMilliseconds(long val) {
return val;
}
},
SEC(new String[]{"sec", "second", "seconds"}) {
long toMilliseconds(long val) {
return val * 1000;
}
},
MIN(new String[]{"min", "minute", "minutes"}) {
long toMilliseconds(long val) {
return SEC.toMilliseconds(val) * 60;
}
},
HOUR(new String[]{"hour", "hours"}) {
long toMilliseconds(long val) {
return MIN.toMilliseconds(val) * 60;
}
},
DAY(new String[]{"day", "days"}) {
long toMilliseconds(long val) {
return HOUR.toMilliseconds(val) * 24;
}
};
List names;
TimeUnit(String[] names) {
this.names = Arrays.asList(names);
}
static TimeUnit fromName(String name) {
for (TimeUnit cur : values()) {
if (cur.names.contains(name)) {
return cur;
}
}
throw new IllegalArgumentException("Could not find TimeUnit for " + name);
}
abstract long toMilliseconds(long val);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy