diff --git a/core/src/main/java/lucee/runtime/schedule/SchedulerImpl.java b/core/src/main/java/lucee/runtime/schedule/SchedulerImpl.java index 26c4a0e28a..8a5e865542 100644 --- a/core/src/main/java/lucee/runtime/schedule/SchedulerImpl.java +++ b/core/src/main/java/lucee/runtime/schedule/SchedulerImpl.java @@ -21,6 +21,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; import org.xml.sax.SAXException; @@ -46,7 +48,7 @@ */ public final class SchedulerImpl implements Scheduler { - private ScheduleTaskImpl[] tasks; + private Queue tasks; private Resource schedulerFile; private StorageUtil su = new StorageUtil(); private String charset; @@ -86,7 +88,7 @@ public SchedulerImpl(CFMLEngine engine, Config config, Array tasks) throws PageE public SchedulerImpl(CFMLEngine engine, String xml, Config config) { this.engine = (CFMLEngineImpl) engine; this.config = config; - tasks = new ScheduleTaskImpl[0]; + tasks = new ConcurrentLinkedQueue<>(); init(); } @@ -94,24 +96,24 @@ public SchedulerImpl(CFMLEngine engine, String xml, Config config) { * initialize all tasks */ private void init() { - for (int i = 0; i < tasks.length; i++) { - init(tasks[i]); + for (TaskRef ref: tasks) { + init(ref.task); } } public void startIfNecessary() { - for (int i = 0; i < tasks.length; i++) { - init(tasks[i]); + for (TaskRef ref: tasks) { + init(ref.task); } } - private void init(ScheduleTask task) { - ((ScheduleTaskImpl) task).startIfNecessary(engine); + private void init(ScheduleTaskImpl task) { + task.startIfNecessary(engine); } public void stop() { - for (int i = 0; i < tasks.length; i++) { - tasks[i].stop(); + for (TaskRef ref: tasks) { + ref.task.stop(); } } @@ -123,13 +125,13 @@ public void stop() { * @return all schedule tasks * @throws PageException */ - private ScheduleTaskImpl[] readInAllTasks(Array tasks) throws PageException { - ArrayList list = new ArrayList(); + private Queue readInAllTasks(Array tasks) throws PageException { + Queue queue = new ConcurrentLinkedQueue<>(); Iterator it = tasks.getIterator(); while (it.hasNext()) { - list.add(readInTask((Struct) it.next())); + queue.add(new TaskRef(readInTask((Struct) it.next()))); } - return list.toArray(new ScheduleTaskImpl[list.size()]); + return queue; } /** @@ -159,38 +161,33 @@ private ScheduleTaskImpl readInTask(Struct el) throws PageException { } private void addTask(ScheduleTaskImpl task) { - for (int i = 0; i < tasks.length; i++) { - if (!tasks[i].getTask().equals(task.getTask())) continue; - if (!tasks[i].md5().equals(task.md5())) { - tasks[i].log(Log.LEVEL_INFO, "invalidate task because the task is replaced with a new one"); - tasks[i].setValid(false); - tasks[i] = task; + for (TaskRef ref: tasks) { + if (!ref.task.getTask().equals(task.getTask())) continue; + if (!ref.task.md5().equals(task.md5())) { + ref.task.log(Log.LEVEL_INFO, "invalidate task because the task is replaced with a new one"); + ref.task.setValid(false); + ref.task = task; init(task); } return; } - ScheduleTaskImpl[] tmp = new ScheduleTaskImpl[tasks.length + 1]; - for (int i = 0; i < tasks.length; i++) { - tmp[i] = tasks[i]; - } - tmp[tasks.length] = task; - tasks = tmp; + tasks.add(new TaskRef(task)); init(task); } @Override public ScheduleTask getScheduleTask(String name) throws ScheduleException { - for (int i = 0; i < tasks.length; i++) { - if (tasks[i].getTask().equalsIgnoreCase(name)) return tasks[i]; + for (TaskRef ref: tasks) { + if (ref.task.getTask().equalsIgnoreCase(name)) return ref.task; } throw new ScheduleException("schedule task with name " + name + " doesn't exist"); } @Override public ScheduleTask getScheduleTask(String name, ScheduleTask defaultValue) { - for (int i = 0; i < tasks.length; i++) { - if (tasks[i] != null && tasks[i].getTask().equalsIgnoreCase(name)) return tasks[i]; + for (TaskRef ref: tasks) { + if (ref.task.getTask().equalsIgnoreCase(name)) return ref.task; } return defaultValue; } @@ -198,8 +195,8 @@ public ScheduleTask getScheduleTask(String name, ScheduleTask defaultValue) { @Override public ScheduleTask[] getAllScheduleTasks() { ArrayList list = new ArrayList(); - for (int i = 0; i < tasks.length; i++) { - if (!tasks[i].isHidden()) list.add(tasks[i]); + for (TaskRef ref: tasks) { + if (!ref.task.isHidden()) list.add(ref.task); } return list.toArray(new ScheduleTask[list.size()]); } @@ -224,41 +221,24 @@ public void pauseScheduleTask(String name, boolean pause, boolean throwWhenNotEx throw ExceptionUtil.toIOException(e); } - for (int i = 0; i < tasks.length; i++) { - if (tasks[i].getTask().equalsIgnoreCase(name)) { - tasks[i].setPaused(pause); + for (TaskRef ref: tasks) { + if (ref.task.getTask().equalsIgnoreCase(name)) { + ref.task.setPaused(pause); } } } @Override public void removeScheduleTask(String name, boolean throwWhenNotExist) throws IOException, ScheduleException { - synchronized (sync) { - int pos = -1; - for (int i = 0; i < tasks.length; i++) { - if (tasks[i].getTask().equalsIgnoreCase(name)) { - tasks[i].log(Log.LEVEL_INFO, "task gets removed"); - tasks[i].setValid(false); - pos = i; - } - } - if (pos != -1) { - ScheduleTaskImpl[] newTasks = new ScheduleTaskImpl[tasks.length - 1]; - int count = 0; - for (int i = 0; i < tasks.length; i++) { - if (i != pos) newTasks[count++] = tasks[i]; - - } - tasks = newTasks; - } - try { - ConfigAdmin.removeScheduledTask((ConfigPro) config, name, true); - } - catch (Exception e) { - throw ExceptionUtil.toIOException(e); - } + tasks.removeIf(ref -> ref.task.getTask().equalsIgnoreCase(name)); + try { + ConfigAdmin.removeScheduledTask((ConfigPro) config, name, true); } + catch (Exception e) { + throw ExceptionUtil.toIOException(e); + } + } public void removeIfNoLonerValid(ScheduleTask task) throws IOException { @@ -300,4 +280,12 @@ public String getCharset() { public boolean active() { return engine == null || engine.active(); } + + private static class TaskRef { + private ScheduleTaskImpl task; + + public TaskRef(ScheduleTaskImpl task) { + this.task = task; + } + } } \ No newline at end of file diff --git a/loader/build.xml b/loader/build.xml index d610e47b15..3d3e239f3a 100644 --- a/loader/build.xml +++ b/loader/build.xml @@ -2,7 +2,7 @@ - + diff --git a/loader/pom.xml b/loader/pom.xml index 40c2ec8dd6..59b450b4aa 100644 --- a/loader/pom.xml +++ b/loader/pom.xml @@ -3,7 +3,7 @@ org.lucee lucee - 6.0.1.65-SNAPSHOT + 6.0.1.66-SNAPSHOT jar Lucee Loader Build