From a34090cf930dd36b68ef0380605db5550d517cce Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Fri, 6 Oct 2017 07:39:02 +0200 Subject: [PATCH] Reverting the schedule definition back to cron string Considered it too complex to convert the cron expressions into java interface that takes all features into account that are possible by quartz cron strings. --- .../repository/AbstractRepository.java | 48 +--- .../repository/EditableRepository.java | 35 +-- .../apache/archiva/repository/Repository.java | 8 +- .../repository/ScheduleDefinition.java | 267 ------------------ 4 files changed, 28 insertions(+), 330 deletions(-) delete mode 100644 archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/ScheduleDefinition.java diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRepository.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRepository.java index 1b9585e2b..0d67d5da3 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRepository.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRepository.java @@ -19,6 +19,10 @@ package org.apache.archiva.repository; * under the License. */ +import com.cronutils.model.CronType; +import com.cronutils.model.definition.CronDefinition; +import com.cronutils.model.definition.CronDefinitionBuilder; +import com.cronutils.parser.CronParser; import org.apache.archiva.repository.features.RepositoryFeature; import java.net.URI; @@ -49,13 +53,13 @@ public abstract class AbstractRepository implements EditableRepository private Set failoverLocations = new HashSet<>( ); private Set uFailoverLocations = Collections.unmodifiableSet( failoverLocations ); private boolean scanned = true; - private List schedulingTimes = new ArrayList<>( ); - private List uSchedulingTimes = Collections.unmodifiableList( schedulingTimes ); + String schedulingDefinition = "0 0 02 * *"; private boolean index; private URI indexPath; private String layout; private Set activeReleaseSchemes = new HashSet<>( ); private Set uActiveReleaseSchemes = Collections.unmodifiableSet( activeReleaseSchemes ); + public static final CronDefinition CRON_DEFINITION = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ); public AbstractRepository(RepositoryType type, String id, String name) { this.id = id; @@ -129,9 +133,9 @@ public abstract class AbstractRepository implements EditableRepository } @Override - public List getSchedulingTimes( ) + public String getSchedulingDefinition( ) { - return uSchedulingTimes; + return schedulingDefinition; } @Override @@ -221,35 +225,6 @@ public abstract class AbstractRepository implements EditableRepository this.scanned = scanned; } - @Override - public void addSchedulingTime( int index, ScheduleDefinition scheduleDefinition ) - { - this.schedulingTimes.add( index, scheduleDefinition ); - } - - @Override - public void addSchedulingTime( ScheduleDefinition scheduleDefinition ) - { - this.schedulingTimes.add(scheduleDefinition); - } - - @Override - public void removeSchedulingTime( ScheduleDefinition scheduleDefinition ) - { - this.schedulingTimes.remove(scheduleDefinition); - } - - @Override - public void removeSchedulingTime(int index) { - this.schedulingTimes.remove(index); - } - - @Override - public void clearSchedulingTimes( ) - { - this.schedulingTimes.clear(); - } - @Override public void setIndex( boolean hasIndex ) { @@ -286,5 +261,10 @@ public abstract class AbstractRepository implements EditableRepository this.activeReleaseSchemes.clear(); } - + @Override + public void setSchedulingDefinition(String cronExpression) { + CronParser parser = new CronParser(CRON_DEFINITION); + parser.parse(cronExpression).validate(); + this.schedulingDefinition = cronExpression; + } } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/EditableRepository.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/EditableRepository.java index 96f610d3e..43839197f 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/EditableRepository.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/EditableRepository.java @@ -95,35 +95,16 @@ public interface EditableRepository extends Repository void setScanned(boolean scanned); /** - * Adds a scheduling time at the given index. - * @param index the index where time should be set - * @param scheduleDefinition the scheduling definition to add - */ - void addSchedulingTime(int index, ScheduleDefinition scheduleDefinition); - - /** - * Adds a scheduling time to the end of the list. - * @param scheduleDefinition the scheduling definition to set - */ - void addSchedulingTime(ScheduleDefinition scheduleDefinition); - - /** - * Removes the scheduling time definition from the list. - * @param scheduleDefinition the scheduling definition to remove. - */ - void removeSchedulingTime(ScheduleDefinition scheduleDefinition); - - /** - * Removes the scheduling time definition add the given index. + * Sets the scheduling definition, that defines the times, when the regular repository + * jobs are started. The cronExpression must be a valid + * quartz cron definition. * - * @param index the index to remove - */ - void removeSchedulingTime(int index); - - /** - * Clears the list of scheduling definitions. + * @See http://www.quartz-scheduler.org/api/2.2.1/org/quartz/CronExpression.html + * + * @param cronExpression the cron expression. + * @throws IllegalArgumentException if the cron expression is not valid. */ - void clearSchedulingTimes(); + void setSchedulingDefinition(String cronExpression) throws IllegalArgumentException; /** * Set true, if the repository has indexes stored. diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/Repository.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/Repository.java index 8fef46656..3e508848d 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/Repository.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/Repository.java @@ -98,10 +98,14 @@ public interface Repository { boolean isScanned(); /** - * The definition when the scheduler should run to scan this repository. + * Returns the definition, when the repository jobs are executed. + * This must return a valid a cron string. + * + * @See http://www.quartz-scheduler.org/api/2.2.1/org/quartz/CronExpression.html + * * @return */ - List getSchedulingTimes(); + String getSchedulingDefinition(); /** * Returns true, if this repository has a index available diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/ScheduleDefinition.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/ScheduleDefinition.java deleted file mode 100644 index 3084bbc98..000000000 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/ScheduleDefinition.java +++ /dev/null @@ -1,267 +0,0 @@ -package org.apache.archiva.repository; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - - -import com.cronutils.model.Cron; -import com.cronutils.model.definition.CronDefinition; -import com.cronutils.model.definition.CronDefinitionBuilder; -import com.cronutils.model.field.CronFieldName; -import com.cronutils.model.field.expression.Always; -import com.cronutils.model.field.expression.And; -import com.cronutils.model.field.expression.Between; -import com.cronutils.model.field.expression.Every; -import com.cronutils.model.field.expression.FieldExpression; -import com.cronutils.model.field.expression.On; -import com.cronutils.model.field.expression.QuestionMark; -import com.cronutils.model.field.expression.visitor.FieldExpressionVisitor; -import com.cronutils.model.field.value.IntegerFieldValue; -import com.cronutils.parser.CronParser; -import org.apache.tools.ant.types.resources.Sort; - -import java.time.*; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - -import static com.cronutils.model.CronType.QUARTZ; - -/** - * A definition of schedule times. - */ -public class ScheduleDefinition { - - - final SortedSet daysOfWeek = new TreeSet<>(); - - final SortedSet daysOfMonth = new TreeSet<>(); - - final SortedSet months = new TreeSet<>( ); - - final SortedSet scheduleTimes = new TreeSet<>(); - - final LocalTime startTime; - - final Duration timeInterval; - - boolean fixedTimes = false; - - public ScheduleDefinition(Collection daysOfWeek, - Collection daysOfMonth, - Collection months, - Collection scheduleTimes, - LocalTime startTime, Duration timeInterval) { - if (daysOfWeek!=null) - this.daysOfWeek.addAll(daysOfWeek); - if (daysOfMonth!=null) - this.daysOfMonth.addAll(daysOfMonth); - if (months!=null) { - this.months.addAll(months); - } - if (scheduleTimes!=null && scheduleTimes.size()>0) - { - this.fixedTimes = true; - this.scheduleTimes.addAll( scheduleTimes ); - this.startTime=null; - this.timeInterval=null; - } else - { - this.fixedTimes = false; - this.startTime = startTime; - this.timeInterval = timeInterval; - } - } - - /** - * Returns the days of the week on which the action should be run. - * @return The set of week days. - */ - public SortedSet getDaysOfWeek() { - return daysOfWeek; - } - - /** - * Returns the days in the month on which the action should be run. - * @return The set of days. - */ - public SortedSet getDaysOfMonth() { - return daysOfMonth; - } - - /** - * Returns the months on which the action should be run. - * @return - */ - public SortedSet getMonths() { - return months; - } - - /** - * Returns the time on each day on which the action should be run. - * @return a set of times on which the action should be run. - */ - public SortedSet getScheduleTimes() { - return scheduleTimes; - } - - /** - * Returns the start time each day on which the action should be run. - * @return the start time. - */ - public LocalTime getStartTime() { - return startTime; - } - - /** - * The interval after that the next run should be scheduled. - * @return The interval between runs. - */ - public Duration getTimeInterval() { - return timeInterval; - } - - /** - * Returns true, if the task should be run on fixed times. Otherwise - * the tasks are scheduled repeatedly with the time interval. - * @return true, if the schedule times are fixed. - */ - public boolean isFixedTimes() { - return fixedTimes; - }; - - public static ScheduleDefinition fromCronExpression(String cron) { - CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor( QUARTZ ); - CronParser parser = new CronParser( cronDefinition ); - Cron pCron = parser.parse(cron); - if (pCron.validate()==null) { - throw new IllegalArgumentException( "Cron expression not valid "+cron ); - }; - CronVisitor secondsVisit = new CronVisitor( ); - pCron.retrieve( CronFieldName.SECOND ).getExpression().accept(secondsVisit); - CronVisitor minutesVisit = new CronVisitor( ); - pCron.retrieve( CronFieldName.MINUTE ).getExpression().accept(minutesVisit); - CronVisitor hoursVisit = new CronVisitor( 24 ); - pCron.retrieve( CronFieldName.HOUR ).getExpression().accept(hoursVisit); - SortedSet times = new TreeSet<>( ); - for (Integer hour : hoursVisit.getTimes()) { - for (Integer minute : minutesVisit.getTimes()) { - for (Integer second : secondsVisit.getTimes()) { - times.add(LocalTime.of( hour, minute, second)); - } - } - } - - return null; - } - - private static class CronVisitor implements FieldExpressionVisitor { - - private int range = 60; - private SortedSet times = new TreeSet<>( ); - - CronVisitor() { - - } - - CronVisitor(int range) { - this.range = range; - } - - private SortedSet getTimes() { - return times; - } - - @Override - public FieldExpression visit( FieldExpression expression ) - { - try { - Integer in = new Integer(expression.asString()); - times.add(in); - } catch (NumberFormatException ex) { - // - } - return expression; - } - - @Override - public FieldExpression visit( Always always ) - { - for (int i=0; i