/**
 * Copyright (c) 2015-2017, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * 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.
 */
package io.jboot.schedule;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import io.jboot.Jboot;
import io.jboot.schedule.annotation.Cron4jTask;
import io.jboot.utils.ArrayUtils;
import io.jboot.utils.ClassNewer;
import io.jboot.utils.ClassScanner;

import java.util.List;


public class JbootTaskManager {

    private static JbootTaskManager manager;
    private Cron4jPlugin cron4jPlugin;

    private JbootTaskManager() {

        if (!isCron4jEnable()) {
            return;
        }

        initCron4jPlugin();
    }


    public static final JbootTaskManager me() {
        if (manager == null) {
            manager = ClassNewer.singleton(JbootTaskManager.class);
        }
        return manager;
    }

    private void initCron4jPlugin() {

        Prop prop = null;
        try {
            prop = PropKit.use(Jboot.me().getJbootConfig().getCron4jFile());
        } catch (Throwable ex) {
        }

        cron4jPlugin = prop == null ? new Cron4jPlugin() : new Cron4jPlugin(prop);

        List<Class<Runnable>> list = ClassScanner.scanSubClass(Runnable.class);
        if (ArrayUtils.isNullOrEmpty(list)) {
            return;
        }

        for (Class<Runnable> clazz : list) {
            Cron4jTask cron4jTask = clazz.getAnnotation(Cron4jTask.class);
            if (cron4jTask == null) continue;
            cron4jPlugin.addTask(cron4jTask.cron(), ClassNewer.newInstance(clazz), cron4jTask.daemon());
        }
    }

    public Cron4jPlugin getCron4jPlugin() {
        return cron4jPlugin;
    }

    public boolean isCron4jEnable() {
        return Jboot.me().getJbootConfig().isCron4jEnable();
    }


}

@@ -814,16 +814,58 @@
 .Jboot;%0A
+import io.jboot.exception.JbootException;%0A
 import i
@@ -1007,16 +1007,99 @@
 Scanner;
+%0Aimport it.sauronsoftware.cron4j.ProcessTask;%0Aimport it.sauronsoftware.cron4j.Task;
 %0A%0Aimport
@@ -1240,22 +1240,21 @@
 ;%0A%0A    p
-rivate
+ublic
  JbootTa
@@ -1263,25 +1263,24 @@
 Manager() %7B%0A
-%0A
         if (
@@ -1857,26 +1857,16 @@
 st%3CClass
-%3CRunnable%3E
 %3E list =
@@ -1887,31 +1887,48 @@
 scan
-Sub
 Class
-(Runnable.class
+ByAnnotation(Cron4jTask.class, true
 );%0A 
@@ -2025,18 +2025,8 @@
 lass
-%3CRunnable%3E
  cla
@@ -2073,16 +2073,29 @@
 4jTask =
+ (Cron4jTask)
  clazz.g
@@ -2147,37 +2147,380 @@
 f (c
-ron4jTask == null)
+lazz == Runnable.class) %7B%0A                cron4jPlugin.addTask(cron4jTask.cron(), (Runnable) ClassNewer.newInstance(clazz), cron4jTask.daemon());%0A            %7D else if (clazz == ProcessTask.class) %7B%0A                cron4jPlugin.addTask(cron4jTask.cron(), (ProcessTask) ClassNewer.newInstance(clazz),
  c
+r
 on
-tinue;%0A
+4jTask.daemon());%0A            %7D else if (clazz == Task.class) %7B%0A    
     
@@ -2566,16 +2566,23 @@
 .cron(),
+ (Task)
  ClassNe
@@ -2619,32 +2619,167 @@
 Task.daemon());%0A
+            %7D else %7B%0A                throw new JbootException(%22annotation Cron4jTask can not use for class : %22 + clazz);%0A            %7D%0A
         %7D%0A    %7D%0A

add : @Cron4jTask annotation support for ProcessTask.class and Task.class