Changeset 323 for kraken-cron/src

Show
Ignore:
Timestamp:
08/11/09 14:50:09 (13 months ago)
Author:
periphery
Message:

minor modification made for cron service.
(code refactoring, bug fix, comments, logging, tests, etc)

Location:
kraken-cron/src/main/java/org/krakenapps/cron
Files:
4 added
11 modified

Legend:

Unmodified
Added
Removed
  • kraken-cron/src/main/java/org/krakenapps/cron/CronScript.java

    r322 r323  
    44 
    55import org.krakenapps.api.Script; 
     6import org.krakenapps.api.ScriptArgument; 
    67import org.krakenapps.api.ScriptContext; 
     8import org.krakenapps.api.ScriptUsage; 
    79import org.krakenapps.cron.impl.CronField; 
    8 import org.krakenapps.cron.impl.CronServiceImpl; 
    910import org.osgi.framework.BundleContext; 
    1011import org.osgi.framework.InvalidSyntaxException; 
    1112import org.osgi.framework.ServiceReference; 
    1213 
     14/** 
     15 * Provides cron management command scripts. 
     16 *  
     17 * @author periphery 
     18 * @since 1.0.0 
     19 */ 
    1320public class CronScript implements Script { 
    1421 
     
    2734        } 
    2835 
     36        @ScriptUsage(description = "list all cron schedules") 
    2937        public void list(String[] args) { 
    3038                context.println("=================="); 
     
    3745        } 
    3846         
     47        @ScriptUsage(description = "view current state of cron scheduling queue") 
    3948        public void queue(String[] args) { 
    4049                context.println("=================="); 
     
    4756        } 
    4857 
     58        @ScriptUsage(description = "register new cron schedule", arguments = { 
     59                        @ScriptArgument(name = "min", type = "string", description = "(0 - 59)"), 
     60                        @ScriptArgument(name = "hour", type = "string", description = "(0 - 23)"), 
     61                        @ScriptArgument(name = "day_of_month", type = "string", description = "(1 - 31)"), 
     62                        @ScriptArgument(name = "month", type = "string", description = "(1 - 12)"), 
     63                        @ScriptArgument(name = "day_of_week", type = "string", description = "(0 - 6) (Sunday=0)"), 
     64                        @ScriptArgument(name = "task", type = "string", description = "Runnable alias")}) 
    4965        public void register(String[] args) { 
    50                 if (args.length != 6) { 
    51                         printUsage(); 
    52                         return; 
    53                 } 
    5466                try { 
    5567                        Schedule.Builder builder = new Schedule.Builder(args[5]); 
     
    6981        } 
    7082         
     83        @ScriptUsage(description = "unregister cron schedule", arguments = { 
     84                        @ScriptArgument(name = "id", type = "string", description = "cron schedule id")}) 
    7185        public void unregister(String[] args){ 
    72                 if(args.length < 1) { 
    73                         context.println("USAGE : SCHEDULE_ID"); 
    74                         return; 
    75                 } 
    7686                try { 
    7787                        manager.unregisterSchedule(Integer.parseInt(args[0])); 
     
    8696        } 
    8797 
    88         private void printUsage() { 
    89                 context.println("USAGE :"); 
     98        @ScriptUsage(description = "print cron schedule syntax") 
     99        public void usage(String[] args) { 
     100                context.println("SYNTAX :"); 
    90101                context.println("* * * * * RUNNABLE_ALIAS\n\r" + "- - - - -\n\r" + "| | | | |\n\r" 
    91102                                + "| | | | +----- day of week (0 - 6) (Sunday=0)\n\r" 
     
    94105                                + "| +----------- hour (0 - 23)\n\r" 
    95106                                + "+------------- min (0 - 59)\n\r"); 
     107                context.println("EXAMPLES : \n\r" 
     108                                + "Run once a year                      |   0 0 1 1 * \n\r" 
     109                                + "Run once a week                      |   0 0 * * 0 \n\r" 
     110                                + "Run every five minute                | */5 * * * * \n\r" 
     111                                + "Run once a day from monday to friday |   0 0 * * 1-5 \n\r" 
     112                                + "Run once a day on saturday and sunday|   0 0 * * 0,6 \n\r"); 
    96113 
    97114        } 
     
    99116 
    100117 
    101  
     118        @ScriptUsage(description = "list all active Runnables") 
    102119        public void test(String[] args) throws InvalidSyntaxException { 
    103                 if (args.length == 0) { 
    104                         context.println("input runnable alias"); 
    105                         return; 
    106                 } 
    107  
    108                  
    109                 context.println("cron test started"); 
     120                context.println("===================="); 
     121                context.println("Active Runnable List"); 
     122                context.println("===================="); 
    110123                ServiceReference[] refs; 
    111124                try { 
     
    113126                         
    114127                        refs = bundleContext.getServiceReferences(Runnable.class.getName(), 
    115                                         "(alias="+args[0]+")"); 
     128                                        null); 
    116129                        if (refs == null || refs.length == 0) { 
    117                                 context.println("can't get " + args[0]); 
     130                                context.println("empty list"); 
    118131                                return; 
    119132                        } 
     
    123136                        } 
    124137                        Runnable task = ((Runnable) bundleContext.getService(refs[0])); 
    125                          
    126                         context.println("test success"); 
    127138 
    128139 
    129140                } catch (InvalidSyntaxException e) { 
    130                         // TODO Auto-generated catch block 
    131141                        e.printStackTrace(); 
    132142                } 
    133 //              manager.getRef(args[0]); 
    134  
    135143        } 
    136144 
  • kraken-cron/src/main/java/org/krakenapps/cron/CronService.java

    r322 r323  
    33import java.util.List; 
    44 
    5 import org.osgi.framework.BundleContext; 
    6 import org.osgi.framework.InvalidSyntaxException; 
    7  
     5/** 
     6 * cronService interface 
     7 *  
     8 * @author periphery 
     9 * @since 1.0.0 
     10 */ 
    811public interface CronService { 
    912        void registerSchedule(Schedule schedule) throws Exception; 
  • kraken-cron/src/main/java/org/krakenapps/cron/Schedule.java

    r322 r323  
    77import java.util.Map; 
    88 
    9 import javassist.expr.Instanceof; 
    10  
    119import org.krakenapps.cron.impl.CronField; 
    12 import org.krakenapps.cron.impl.CronServiceImpl; 
    13 import org.krakenapps.cron.test.DummyBundleContext; 
    14 import org.osgi.framework.BundleContext; 
     10import org.krakenapps.cron.impl.NextOccurence; 
     11import org.krakenapps.cron.impl.CronField.Type; 
    1512import org.osgi.framework.InvalidSyntaxException; 
    1613 
     14/** 
     15 * Schedule used for registering cron jobs. 
     16 *  
     17 * @author periphery 
     18 * @since 1.0.0 
     19 */ 
    1720public final class Schedule { 
     21        private static final int MONTH_TYPE = 1; 
     22        private static final int DAY_TYPE = 2; 
     23        private static final int HOUR_TYPE = 3; 
     24        private static final int MINUTE_TYPE = 4; 
     25        private static final int None = -1; 
    1826        private final Map<String, CronField> map; 
    1927        private final String taskAlias; 
     
    3846                this.taskAlias = builder.taskAlias; 
    3947        } 
     48 
     49        private void setAllFirstValue(Calendar base, NextOccurence next, int type) 
     50                        throws Exception { 
     51                for (int i = type; i < 5; i++) { 
     52                        next.set(i, getFirstValue(i, base)); 
     53                } 
     54        } 
     55 
     56        /* 
     57         * returns the first date corresponding to the schedule, given current time 
     58         */ 
     59        public Date getNextOccurence(Date current) { 
     60                try { 
     61                        Calendar base = Calendar.getInstance(); 
     62                        NextOccurence next = new NextOccurence(); 
     63 
     64                        base.setTime(current); 
     65                        base.set(Calendar.SECOND, 0); 
     66                         
     67                        for (int type = MINUTE_TYPE; type > 0; type--) 
     68                                adjustNextOccurence(base, next, type); 
     69 
     70                        next.set(base.get(Calendar.YEAR), None, None, None, None); 
     71 
     72                        return newDate(next); 
     73 
     74                } catch (Exception e) { 
     75                        e.printStackTrace(); 
     76                } 
     77                return null; 
     78        } 
    4079         
    41         public Map<String, CronField> getFields() { 
    42                 // TODO Auto-generated method stub 
    43                 return new HashMap<String, CronField>(map); 
    44         } 
    45          
    46         public String getTaskAlias(){ 
     80        private static final int calendarTypes[] = { Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY, Calendar.MINUTE }; 
     81        private static final int upperCalendarTypes[] = { 0, Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY}; 
     82 
     83        private void adjustNextOccurence(Calendar base, NextOccurence next, int type) 
     84                        throws Exception { 
     85                int test = getNextValue(type, base); 
     86                if (test == None) { 
     87                        base.add(upperCalendarTypes[type], 1); 
     88                        if (type == DAY_TYPE && getFirstValue(DAY_TYPE, base) == None) 
     89                                base.add(upperCalendarTypes[type], 1); 
     90                         
     91                        if (type == MONTH_TYPE) 
     92                                base.set(Calendar.MONTH, getFirstValue(type, base)); 
     93                         
     94                        setAllFirstValue(base, next, type); 
     95                } else if (test > base.get(calendarTypes[type])) { 
     96                        if (type == MONTH_TYPE) 
     97                                base.set(Calendar.MONTH, test); 
     98                         
     99                        next.set(type, test); 
     100                        setAllFirstValue(base, next, type + 1); 
     101                } else { 
     102                        next.set(type, test); 
     103                } 
     104        } 
     105 
     106        private int getFirstValue(int type, Calendar base) throws Exception { 
     107                switch (type) { 
     108                case MONTH_TYPE: return map.get("Month").first(); 
     109                case DAY_TYPE: return map.get("DayOfMonth").first(base, map.get("DayOfWeek")); 
     110                case HOUR_TYPE: return map.get("Hour").first(); 
     111                case MINUTE_TYPE: return map.get("Minute").first(); 
     112                } 
     113                throw new Exception(" unsupported first value type"); 
     114        } 
     115 
     116        private int getNextValue(int type, Calendar base) throws Exception { 
     117                switch (type) { 
     118                case MONTH_TYPE: return map.get("Month").next(base.get(Calendar.MONTH)); 
     119                case DAY_TYPE: return map.get("DayOfMonth").next(base, map.get("DayOfWeek")); // merge-or 
     120                case HOUR_TYPE: return map.get("Hour").next(base.get(Calendar.HOUR_OF_DAY)); 
     121                case MINUTE_TYPE: return map.get("Minute").next(base.get(Calendar.MINUTE)); 
     122                default: 
     123                        return -2; 
     124                } 
     125        } 
     126 
     127        private Date newDate(NextOccurence next) { 
     128                Calendar result = Calendar.getInstance(); 
     129                result.set(Calendar.SECOND, 0); 
     130                result.set(Calendar.MINUTE, next.minute); 
     131                result.set(Calendar.HOUR_OF_DAY, next.hour); 
     132                result.set(Calendar.DAY_OF_MONTH, next.day + 1); // offset 
     133                result.set(Calendar.MONTH, next.month); 
     134                result.set(Calendar.YEAR, next.year); 
     135                return result.getTime(); 
     136        } 
     137 
     138        /* 
     139         * returns task alias 
     140         */ 
     141        public String getTaskAlias() { 
    47142                return this.taskAlias; 
    48143        } 
    49144 
    50          
     145        @Override 
    51146        public String toString() { 
    52147                return String.format("%8s %8s %8s %8s %8s / %8s", map.get("Minute"), 
     
    55150        } 
    56151 
     152        /* 
     153         * returns members of the given cron field 
     154         */ 
    57155        public String fieldMembers(CronField.Type type) { 
    58156                return map.get(type.toString()).debugString(); 
     157        } 
     158         
     159        /* 
     160         * returns expression string of given cron field 
     161         */ 
     162        public Object get(Type type) { 
     163                return type.toString(); 
    59164        } 
    60165 
     
    69174                return this.toString().hashCode(); 
    70175        } 
    71  
     176         
     177        /* 
     178         * builder class for schedule class 
     179         */ 
    72180        public static class Builder { 
    73181                private final Map<String, CronField> map; 
    74182                private final String taskAlias; 
    75183 
    76                 public Builder(String taskAlias) throws InvalidSyntaxException { 
     184                /* 
     185                 * create new builder object. default scheduling rule is "* * * * *"(minutely). 
     186                 */ 
     187                public Builder(String taskAlias){ 
    77188                        this.map = new HashMap<String, CronField>(); 
    78189                        this.taskAlias = taskAlias; 
     
    88199                                                CronField.Type.Day_of_Week, null)); 
    89200                        } catch (ParseException e) { 
    90                                 // TODO Auto-generated catch block 
    91                                 e.printStackTrace(); 
     201                                //must succeed. ignored. 
    92202                        } 
    93203                } 
    94204 
     205                // 
     206                 // Set cron field with given expression.  
     207                 // Following expressions are supported. 
     208                 // 1. comma(',') as list. e.g:"1,3,4,8" (space inside the list must not be used) 
     209                 // 2. dash('-') as range. e.g:"1-6", which means 1 to 6 
     210                 // 3. asterisk('*') as wild. e.g:"*", which means every~ 
     211                 // 4. slash('/') as interval. e.g:"*/5" which means every five~ 
     212                 // 
    95213                public Builder set(CronField.Type type, String exp) 
    96214                                throws ParseException { 
     
    99217                } 
    100218 
     219                /* 
     220                 * returns schedule object representing scheduling rule of current build object. 
     221                 * e.g. new Schedule.Builder("test").set(CronField.Type.Minute,"5").build(); 
     222                 * represents schedule of "5 * * * * / test" 
     223                 */ 
    101224                public Schedule build() throws Exception { 
    102225                        return new Schedule(this); 
    103226                } 
    104227 
     228                /* 
     229                 * returns schedule object representing scheduling rule of given expression. 
     230                 * cron fields previously set by set() method are ignored. 
     231                 */ 
    105232                public Schedule build(String exp) throws Exception { 
    106233                        String[] splited = exp.split(" "); 
     
    116243                } 
    117244 
    118                 public Schedule buildYearly() throws Exception { 
    119                         Schedule result = null; 
     245                /* 
     246                 * same as build("0 0 1 1 *") 
     247                 */ 
     248                public Schedule buildYearly() { 
     249                        return mustSuccessBuild("0 0 1 1 *"); 
     250                } 
     251 
     252                /* 
     253                 * same as build("0 0 1 * *") 
     254                 */ 
     255                public Schedule buildMonthly() { 
     256                        return mustSuccessBuild("0 0 1 * *"); 
     257                } 
     258 
     259                /* 
     260                 * same as build("0 0 * * 0") 
     261                 */ 
     262                public Schedule buildWeekly() { 
     263                        return mustSuccessBuild("0 0 * * 0"); 
     264                } 
     265 
     266                /* 
     267                 * same as build("0 0 * * *") 
     268                 */ 
     269                public Schedule buildDaily() { 
     270                        return mustSuccessBuild("0 0 * * *"); 
     271                } 
     272 
     273                /* 
     274                 * same as build("0 * * * *") 
     275                 */ 
     276                public Schedule buildHourly() { 
     277                        return mustSuccessBuild("0 * * * *"); 
     278                } 
     279                 
     280                private Schedule mustSuccessBuild(String expression) { 
    120281                        try { 
    121                                 result = build("0 0 1 1 *"); 
    122                         } catch (ParseException e) { 
    123                                 // TODO Auto-generated catch block 
    124                                 e.printStackTrace(); 
     282                                return build(expression); 
     283                        } catch (Exception e) { 
     284                                // ignore all, not reachable 
     285                                return null; 
    125286                        } 
    126                         return result; 
    127                 } 
    128  
    129                 public Schedule buildMonthly() throws Exception { 
    130                         Schedule result = null; 
    131                         try { 
    132                                 result = build("0 0 1 * *"); 
    133                         } catch (ParseException e) { 
    134                                 // TODO Auto-generated catch block 
    135                                 e.printStackTrace(); 
    136                         } 
    137                         return result; 
    138                 } 
    139  
    140                 public Schedule buildWeekly() throws Exception { 
    141                         Schedule result = null; 
    142                         try { 
    143                                 result = build("0 0 * * 0"); 
    144                         } catch (ParseException e) { 
    145                                 // TODO Auto-generated catch block 
    146                                 e.printStackTrace(); 
    147                         } 
    148                         return result; 
    149                 } 
    150  
    151                 public Schedule buildDaily() throws Exception { 
    152                         Schedule result = null; 
    153                         try { 
    154                                 result = build("0 0 * * *"); 
    155                         } catch (ParseException e) { 
    156                                 // TODO Auto-generated catch block 
    157                                 e.printStackTrace(); 
    158                         } 
    159                         return result; 
    160                 } 
    161  
    162                 public Schedule buildHourly() throws Exception { 
    163                         Schedule result = null; 
    164                         try { 
    165                                 result = build("0 * * * *"); 
    166                         } catch (ParseException e) { 
    167                                 // TODO Auto-generated catch block 
    168                                 e.printStackTrace(); 
    169                         } 
    170                         return result; 
    171                 } 
    172  
    173         } 
    174  
    175         public static class QueryStringGenerator { 
    176                 public static final String db_name = "cron_schedules"; 
    177                 public static final String day_of_week = "day_of_week"; 
    178                 public static final String month = "month"; 
    179                 public static final String day_of_month = "day_of_month"; 
    180                 public static final String hour = "hour"; 
    181                 public static final String minute = "minute"; 
    182                 public static final String task = "task"; 
    183  
    184                 public static String createQuery() { 
    185                         return String 
    186                                         .format( 
    187                                                         "CREATE TABLE %s  (id INTEGER IDENTITY,%s VARCHAR(256),%s VARCHAR(256),%s VARCHAR(256),%s VARCHAR(256),%s VARCHAR(256),%s VARCHAR(256))", 
    188                                                         db_name, minute, hour, day_of_month, month, 
    189                                                         day_of_week, task); 
    190                 } 
    191  
    192                 public static String selectAllQuery() { 
    193                         return String.format( 
    194                                         "select id, %s, %s, %s, %s, %s, %s from %s order by id", 
    195                                         minute, hour, day_of_month, month, day_of_week, task, 
    196                                         db_name); 
    197                 } 
    198  
    199                 public static String insertQuery(Schedule sche) { 
    200                         return String 
    201                                         .format( 
    202                                                         "insert into %s (%s, %s, %s, %s, %s, %s) values (\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\')", 
    203                                                         db_name, minute, hour, day_of_month, month, 
    204                                                         day_of_week, task, sche.map.get("Minute"), sche.map 
    205                                                                         .get("Hour"), sche.map.get("DayOfMonth"), 
    206                                                         sche.map.get("Month"), sche.map.get("DayOfWeek"), 
    207                                                         sche.taskAlias); 
    208                 } 
    209  
    210                 public static String deleteQuery(int id) { 
    211                         return String.format("delete from %s where id = %d", db_name, id); 
    212                 } 
    213         } 
    214  
    215  
    216  
    217  
    218  
    219          
     287                } 
     288        } 
     289 
    220290 
    221291} 
     292 
     293 
  • kraken-cron/src/main/java/org/krakenapps/cron/impl/CronConfig.java

    r322 r323  
    11package org.krakenapps.cron.impl; 
    22 
    3 import java.sql.Connection; 
    4 import java.sql.DriverManager; 
    5 import java.sql.PreparedStatement; 
    6 import java.sql.ResultSet; 
    7 import java.sql.SQLException; 
    8 import java.sql.Statement; 
     3import java.sql.*; 
    94import java.util.HashMap; 
    105import java.util.Map; 
     
    149import org.slf4j.LoggerFactory; 
    1510 
     11/** 
     12 * This class handles jdbc operations associated with cron schedules. 
     13 *  
     14 * @author periphery 
     15 * @since 1.0.0 
     16 */ 
    1617public class CronConfig { 
    1718        // jdbc operations 
     
    3132 
    3233        public int addEntry(Schedule schedule) { 
    33                 return update(Schedule.QueryStringGenerator.insertQuery(schedule)); 
     34                return update(QueryStringGenerator.insertQuery(schedule)); 
    3435        } 
    3536 
    3637        public void removeEntry(int id) { 
    37                 update(Schedule.QueryStringGenerator.deleteQuery(id)); 
     38                update(QueryStringGenerator.deleteQuery(id)); 
    3839        } 
    3940 
     
    4445                        connect(); 
    4546 
    46                         st = connection.prepareStatement(Schedule.QueryStringGenerator.selectAllQuery()); 
     47                        st = connection.prepareStatement(QueryStringGenerator.selectAllQuery()); 
    4748 
    4849                        ResultSet rs = st.executeQuery(); 
     
    7980 
    8081        private void initializeSchema() { 
    81                 logger.info("Cron: initializing cron config table schema."); 
     82                logger.debug("Cron: initializing cron config table schema."); 
    8283                createTable(); 
    8384        } 
     
    9091 
    9192        private void createTable() { 
    92                 update(Schedule.QueryStringGenerator.createQuery()); 
     93                update(QueryStringGenerator.createQuery()); 
    9394        } 
    9495 
     
    103104                    if (rs.next()) { 
    104105                        autoIncKeyFromApi = rs.getInt(1); 
    105                         logger.debug("CronConfig: inserted. id=" + autoIncKeyFromApi); 
     106                        logger.debug("CronConfig : " + expression); 
    106107                    }  
    107108                        rs.close(); 
    108109                        connection.commit(); 
    109110                } catch (Exception e) { 
    110                         e.printStackTrace(); 
    111                         logger.info("EXCEPTION RAISED DURING " + expression + " !! BUT WAS IGNORED."); 
     111                        logger.debug("CronConfig : exception raised during " + expression + ". IGNORED."); 
    112112                        rollback(); 
    113113                } finally { 
     
    145145                } 
    146146        } 
    147          
    148  
    149147} 
  • kraken-cron/src/main/java/org/krakenapps/cron/impl/CronField.java

    r322 r323  
    55import java.util.Calendar; 
    66 
     7/** 
     8 * component of {@link Schedule}. 
     9 *  
     10 * @author periphery 
     11 * @since 1.0.0 
     12 */ 
    713public final class CronField { 
    814        private final Type type; 
     
    142148        public int next(int start) throws Exception{ 
    143149                if(this.type.equals(CronField.Type.Day_of_Month) || this.type.equals(CronField.Type.Day_of_Week)) 
    144                         throw new Exception("not allowed for day_of_month and day_of_week. use next(Calendar, CronField) instead."); 
     150                        throw new IllegalTypeException("not allowed for day_of_month and day_of_week. use next(Calendar, CronField) instead."); 
    145151                return this.bits.nextSetBit(start); 
    146152        } 
    147153 
    148154        public int first() throws Exception { 
    149                 // TODO Auto-generated method stub 
     155                if(this.type.equals(CronField.Type.Day_of_Month) || this.type.equals(CronField.Type.Day_of_Week)) 
     156                        throw new IllegalTypeException("not allowed for day_of_month and day_of_week. use next(Calendar, CronField) instead."); 
    150157                return this.next(0);  
    151158        } 
     
    155162        public int next(Calendar base, CronField dow) throws Exception { 
    156163                if(!this.type.equals(CronField.Type.Day_of_Month)) 
    157                         throw new Exception("only for day_of_month. use next(Calendar, CronField) instead."); 
     164                        throw new IllegalTypeException("only for day_of_month. use next(Calendar, CronField) instead."); 
     165                return dow2month(base, dow).nextSetBit(base.get(Calendar.DAY_OF_MONTH) -1); 
     166        } 
     167         
     168        public int first(Calendar base, CronField dow) throws Exception{ 
     169                if(!this.type.equals(CronField.Type.Day_of_Month)) 
     170                        throw new IllegalTypeException("only for day_of_month. use next(Calendar, CronField) instead."); 
     171                return dow2month(base, dow).nextSetBit(0); 
     172        } 
     173         
     174        private BitSet dow2month(Calendar base, CronField dow){ 
    158175                Calendar clone = (Calendar) base.clone(); 
    159176                clone.set(Calendar.DAY_OF_MONTH, 1); 
     
    167184                for(int i = lastDayOfMonth; i<this.type.bitLength; i++) 
    168185                        dow2month.clear(i); 
    169                 return dow2month.nextSetBit(base.get(Calendar.DAY_OF_MONTH) -1); 
    170         } 
    171          
    172         public int first(Calendar base, CronField dow) throws Exception{ 
    173                 if(!this.type.equals(CronField.Type.Day_of_Month)) 
    174                         throw new Exception("only for day_of_month. use next(Calendar, CronField) instead."); 
    175                 Calendar clone = (Calendar) base.clone(); 
    176                 clone.set(Calendar.DAY_OF_MONTH, 1); 
    177                 int weekDayOf_1 = clone.get(Calendar.DAY_OF_WEEK) - 1; //1 for offset 
    178                 //generate 31 length bitmap by duplicating dow bitmap. 
    179                 BitSet dow2month = mergeBits(weekDayOf_1, this.bits, dow.bits); 
    180                 //turn off for surplus days of month. (e.g. turn off 30 and 31 from Feb.) 
    181                 return dow2month.nextSetBit(0); 
    182         } 
    183          
    184         public static BitSet mergeBits(int weekDayOf_1, BitSet dom, BitSet dow){ 
     186                return dow2month; 
     187        } 
     188         
     189        private static BitSet mergeBits(int weekDayOf_1, BitSet dom, BitSet dow){ 
    185190                BitSet dow2month = new BitSet(); 
    186191                for(int i=0; i<CronField.Type.Day_of_Month.getBitLength(); i++){ 
    187192                        int weekDayOf_i = (i + weekDayOf_1)%7; 
    188193                        dow2month.set(i, dow.get(weekDayOf_i)); 
    189                         //System.out.println("weekDayOPf_i = " + weekDayOf_i); 
    190194                } 
    191195                dow2month.or(dom); //dow bits and dom bits are merged by OR operation. 
     
    193197        } 
    194198 
    195  
    196199        public String toString(){ 
    197200                return this.exp; 
     
    201204                return this.bits.toString(); 
    202205        } 
    203  
    204206} 
  • kraken-cron/src/main/java/org/krakenapps/cron/impl/CronScriptFactory.java

    r316 r323  
    99import org.osgi.framework.ServiceReference; 
    1010 
     11/** 
     12 * The script factory class for cron service. 
     13 *  
     14 * @author periphery 
     15 * @since 1.0.0 
     16 */ 
    1117public class CronScriptFactory implements ScriptFactory { 
    1218        private BundleContext context; 
  • kraken-cron/src/main/java/org/krakenapps/cron/impl/CronServiceImpl.java

    r322 r323  
    1010import org.krakenapps.cron.CronService; 
    1111import org.krakenapps.cron.Schedule; 
     12 
    1213import org.osgi.framework.BundleContext; 
    1314import org.osgi.framework.InvalidSyntaxException; 
    1415import org.osgi.framework.ServiceReference; 
    1516 
     17/** 
     18 * This class provides implementation for the {@link CronService} 
     19 * interface. 
     20 *  
     21 * @author periphery 
     22 * @since 1.0.0 
     23 */ 
    1624public class CronServiceImpl implements CronService { 
    1725        private static BundleContext bundleContext; 
     
    2331                bundleContext = context; 
    2432                this.config = new CronConfig(); 
     33                refreshMap(); 
    2534        } 
    2635         
     
    2837        public void registerSchedule(Schedule schedule) throws Exception { 
    2938                int id = config.addEntry(schedule); 
    30                 refreshMap(); 
     39                this.map.put(id, schedule); 
    3140                this.scheduler.insertToQueue(id, schedule); 
    3241                //have to change implementation to only refresh the entry that had just been registered. 
     
    3746        public void unregisterSchedule(int i) throws Exception { 
    3847                config.removeEntry(i); 
    39                 refreshMap(); 
     48                this.map.remove(i); 
    4049                this.scheduler.deleteFromQueue(i); 
    4150                //have to change implementation to only remove the entry that had just been unregistered. 
     
    4453 
    4554        public void validate() throws Exception { 
    46                 refreshMap(); 
    4755                this.scheduler = new Scheduler(getMap()); 
    4856        } 
     
    7583 
    7684        public static Runnable getRef(String alias) throws InvalidSyntaxException { 
    77                 System.out.println(Runnable.class.getName()); 
     85                //System.out.println(Runnable.class.getName()); 
    7886                return getRef(bundleContext, alias); 
    7987        } 
     
    8694                        throw new NullPointerException(); 
    8795                } 
    88                  
    89                 for (ServiceReference ref : refs) { 
    90                         System.out.println(context.getService(ref).getClass() 
    91                                         .getName()); 
    92                 } 
    93                  
    9496                Runnable task = ((Runnable) context.getService(refs[0])); 
    9597                return task; 
    9698        } 
    97  
    98  
    99          
    100          
    101          
    102          
    103          
    104  
    10599} 
  • kraken-cron/src/main/java/org/krakenapps/cron/impl/Job.java

    r322 r323  
    66 
    77import org.krakenapps.cron.Schedule; 
    8 import org.osgi.framework.BundleContext; 
    98import org.osgi.framework.InvalidSyntaxException; 
    109 
     10/** 
     11 * Job class used as a component of cron scheduler 
     12 *  
     13 * @author periphery 
     14 * @since 1.0.0 
     15 */ 
    1116public class Job implements Comparable<Job> , Cloneable{ 
    1217        public final int scheduleId; 
     
    2025        } 
    2126 
    22         public boolean timeToDo() throws Exception{ 
     27        public boolean timeToDo(){ 
    2328                return timeToDo(new Date()); 
    2429        } 
    2530         
    26         public boolean timeToDo(Date now) throws Exception{ 
    27                 //System.out.println("now : " + now.getTime()/1000 + ", date : " + date.getTime()/1000); 
    28                 if(now.getTime()/1000 - date.getTime()/1000 > 60){ 
    29                         //time passed more than one minute. raise exception 
    30                         throw new Exception("time passed too much. skips this time"); 
    31                 } 
     31        //return true for jobs whose time passed 
     32        public boolean timeToDo(Date now){ 
    3233                if(now.getTime()/1000 - date.getTime()/1000 < 0){ 
    3334                        return false; 
     
    4748         
    4849        public void setNextOccurence(Date now) { 
    49                 this.date = getNextOccurence(schedule, now); 
     50                this.date = schedule.getNextOccurence(now); 
    5051        } 
    5152         
    5253        public void setNextOccurence() { 
    53                 this.date = getNextOccurence(schedule, new Date()); 
     54                this.date = schedule.getNextOccurence(new Date()); 
    5455        } 
    5556         
    56         public static Date getNextOccurence(Schedule sche, Date current){ 
    57                 try { 
    58                         Map<String, CronField> map = sche.getFields(); 
    59                         Calendar base = Calendar.getInstance(); 
    60                         base.setTime(current); 
    61                         base.set(Calendar.SECOND, 0); 
    62                         // minute 
    63                         int minute = map.get("Minute").next(base.get(Calendar.MINUTE)); 
    64                         if (minute == -1) { // wraps around 
    65                                 base.add(Calendar.HOUR_OF_DAY, 1); 
    66                                 minute = map.get("Minute").first(); 
    67                         } 
    68                         // hour 
    69                         int hour = map.get("Hour").next(base.get(Calendar.HOUR_OF_DAY)); 
    70                         if (hour == -1) { 
    71                                 base.add(Calendar.DAY_OF_MONTH, 1); 
    72                                 minute = map.get("Minute").first(); 
    73                                 hour = map.get("Hour").first(); 
    74                         } else if (hour > base.get(Calendar.HOUR_OF_DAY)) { 
    75                                 minute = map.get("Minute").first(); 
    76                         } 
    77                         // day 
    78                         int day = map.get("DayOfMonth").next(base, map.get("DayOfWeek")); // merge-or 
    79                                                                                                                                                                 // day_of_month 
    80                                                                                                                                                                 // and 
    81                                                                                                                                                                 // day_of_week 
    82                         if (day == -1) { 
    83                                 base.add(Calendar.MONTH, 1); 
    84                                 minute = map.get("Minute").first(); 
    85                                 hour = map.get("Hour").first(); 
    86                                 day = map.get("DayOfMonth").first(base, map.get("DayOfWeek")); 
    87                         }else if(day > base.get(Calendar.DAY_OF_MONTH)){ 
    88                                 minute = map.get("Minute").first(); 
    89                                 hour = map.get("Hour").first(); 
    90                         } 
    91                         //month 
    92                         int month = map.get("Month").next(base.get(Calendar.MONTH)); 
    93                         if(month == -1){ 
    94                                 base.add(Calendar.YEAR, 1); 
    95                                 minute = map.get("Minute").first(); 
    96                                 hour = map.get("Hour").first(); 
    97                                 month = map.get("Month").first(); 
    98                                 base.set(Calendar.MONTH, month); 
    99                                 day = map.get("DayOfMonth").first(base, map.get("DayOfWeek")); 
    100                         }else if(month > base.get(Calendar.MONTH)){ 
    101                                 base.set(Calendar.MONTH, month); 
    102                                 minute = map.get("Minute").first(); 
    103                                 hour = map.get("Hour").first(); 
    104                                 day = map.get("DayOfMonth").first(base, map.get("DayOfWeek")); 
    105                         } 
    106                         int year = base.get(Calendar.YEAR); 
    107                          
    108                         Calendar result = Calendar.getInstance(); 
    109                         result.set(Calendar.SECOND, 0); 
    110                         result.set(Calendar.MINUTE, minute); 
    111                         result.set(Calendar.HOUR_OF_DAY, hour); 
    112                         result.set(Calendar.DAY_OF_MONTH, day + 1); //offset 
    113                         result.set(Calendar.MONTH, month); 
    114                         result.set(Calendar.YEAR, year); 
    115                         return result.getTime(); 
    116                          
    117                 } catch (Exception e) { 
    118                         e.printStackTrace(); 
    119                 } 
    120                 return null; 
    121         } 
     57         
    12258         
    12359        public String toString(){ 
     
    14177                task.run(); 
    14278        } 
    143  
    144          
    14579} 
  • kraken-cron/src/main/java/org/krakenapps/cron/impl/Scheduler.java

    r322 r323  
    11package org.krakenapps.cron.impl; 
    22 
    3 import java.util.ArrayList; 
    4 import java.util.Arrays; 
    5 import java.util.Collections; 
    6 import java.util.Date; 
    7 import java.util.List; 
    8 import java.util.Map; 
    9 import java.util.PriorityQueue; 
     3import java.util.*; 
    104import java.util.Map.Entry; 
    11  
    12 import javax.swing.plaf.SliderUI; 
    135 
    146import org.krakenapps.cron.Schedule; 
     
    179import org.slf4j.LoggerFactory; 
    1810 
     11/** 
     12 * Cron scheduler that checks and runs registered cron jobs. 
     13 *  
     14 * @author periphery 
     15 * @since 1.0.0 
     16 */ 
    1917public final class Scheduler { 
    2018 
    21         final static Logger logger = LoggerFactory.getLogger(CronConfig.class.getName()); 
     19        final static Logger logger = LoggerFactory.getLogger(CronConfig.class 
     20                        .getName()); 
    2221        private final PriorityQueue<Job> queue; 
    2322        private final Thread loop; 
    2423        private boolean running; 
    25          
     24 
    2625        public Scheduler(Map<Integer, Schedule> map) { 
    2726                this.queue = reset(map); 
    2827                this.loop = new Thread(new Loop()); 
    29                 this.running = true; 
    3028                this.start(); 
    3129        } 
    3230 
    33         public void stop(){ 
     31        public void stop() { 
    3432                this.running = false; 
    3533        } 
    36          
    37         public void start(){ 
     34 
     35        private void start() { 
    3836                this.running = true; 
    3937                this.loop.start(); 
     
    4947        } 
    5048 
    51         public synchronized void insertToQueue(int id, Schedule sche) { 
     49        public void insertToQueue(int id, Schedule sche) { 
    5250                // have to implement 
    53                 Job job = new Job(id,sche); 
    54                 this.queue.add(job); 
     51                Job job = new Job(id, sche); 
     52                synchronized (this.queue) { 
     53                        this.queue.add(job); 
     54                } 
    5555        } 
    5656 
    57         public synchronized void deleteFromQueue(int id) { 
     57        public void deleteFromQueue(int id) { 
    5858                // have to implement 
    59                 for(Object job : this.queue.toArray()){ 
    60                         if(((Job)job).getScheduleId() == id) 
    61                                 this.queue.remove(job); 
     59                synchronized (this.queue) { 
     60                        for (Object job : this.queue.toArray()) { 
     61                                if (((Job) job).getScheduleId() == id) 
     62                                        this.queue.remove(job); 
     63                        } 
    6264                } 
    6365        } 
     
    7375        } 
    7476 
    75          
    76          
    77         private class Loop implements Runnable{ 
     77        private class Loop implements Runnable { 
    7878 
    7979                @Override 
     
    8282                        loop(); 
    8383                } 
    84                  
    85                 private void loop() {// have to implement sleeping. (may be for 10 secs?) 
     84 
     85                private void loop() {// have to implement sleeping. (may be for 10 
     86                        // secs?) 
    8687                        while (running) { 
    8788                                checkAndRun(); 
    8889                                try { 
    89                                         Thread.sleep(10 * 1000); //sleep for 10 sec 
     90                                        Thread.sleep(10 * 1000); // sleep for 10 sec 
    9091                                } catch (InterruptedException e) { 
    9192                                        // TODO Auto-generated catch block 
     
    9697                } 
    9798 
    98                 private synchronized void checkAndRun() { 
    99                         try { 
    100                                 //logger.info("Cron: scheduler wake up"); 
     99                private void checkAndRun() { 
     100                        synchronized (queue) { 
    101101                                if (itIsTime()) { 
    102102                                        Job first = queue.poll(); 
    103103                                        Thread runner = new Thread(new Runner(first.clone())); 
    104104                                        runner.start(); 
    105                                          
    106                                         //set base time as 1 min after current time 
    107                                         first 
    108                                                         .setNextOccurence(new Date( 
    109                                                                         new Date().getTime() + 60 * 1000));  
     105 
     106                                        // set base time as 1 min after current time 
     107                                        first.setNextOccurence(new Date( 
     108                                                        new Date().getTime() + 60 * 1000)); 
    110109                                        queue.add(first); 
    111                                         //recursive call to check next Job 
     110                                        // recursive call to check next Job 
    112111                                        checkAndRun(); 
    113112                                } 
    114                         } catch (Exception e) { // time passed. refresh without execution 
    115                                 Job first = queue.poll(); 
    116                                 first.setNextOccurence(); 
    117                                 queue.add(first); 
    118113                        } 
    119114                } 
    120                  
    121                 public boolean itIsTime() throws Exception { 
     115 
     116                public boolean itIsTime() { 
     117                        try{ 
    122118                        return queue.peek().timeToDo(); 
     119                        } catch (Exception e) { //empty queue 
     120                                return false; 
     121                        } 
    123122                } 
    124123        } 
     
    134133                public void run() { 
    135134                        try { 
    136                                 logger.info("Cron: run registered task " + job); 
    137135                                job.run(); 
    138                         } catch (NullPointerException e){ 
    139                                 logger.info("Cron: unable to run " + job + ". runnable " + job.schedule.getTaskAlias() + " is not active."); 
     136                                logger.debug("Cron: run registered task " + job); 
     137                        } catch (NullPointerException e) { 
     138                                logger.debug("Cron: unable to run " + job + ". runnable \'" 
     139                                                + job.schedule.getTaskAlias() + "\' is not active."); 
    140140                        } catch (InvalidSyntaxException e) { 
    141141                                // TODO Auto-generated catch block 
     
    143143                        } 
    144144                } 
    145  
    146145        } 
    147  
    148146} 
  • kraken-cron/src/main/java/org/krakenapps/cron/test/NextOccurTest.java

    r321 r323  
    11package org.krakenapps.cron.test; 
    22 
     3import java.util.Calendar; 
    34import java.util.Date; 
    45 
     6 
     7import org.junit.Test; 
    58import org.krakenapps.cron.Schedule; 
    6 import org.krakenapps.cron.impl.Job; 
    79import org.osgi.framework.InvalidSyntaxException; 
     10import static org.junit.Assert.*;  
    811 
    9 import junit.framework.TestCase; 
     12public class NextOccurTest { 
    1013 
    11 public class NextOccurTest extends TestCase { 
    12  
    13         public void setUp() { 
    14  
    15         } 
    16  
    17         // public void testDayNext() throws Exception{ 
    18         // CronField dom = new CronField(CronField.Type.Day_of_Month, "*/2"); 
    19         // CronField dow = new CronField(CronField.Type.Day_of_Week, "0"); 
    20         // BitSet a = new BitSet(); 
    21         // a.set(2); 
    22         // a.set(15); 
    23         // BitSet b = new BitSet(); 
    24         // b.set(0); 
    25         // System.out.println(CronField.mergeBits(6, a, b)); 
    26         //                       
    27         // System.out.println(dom.next(Calendar.getInstance(), dow)); 
    28         // } 
    29  
     14        @Test 
    3015        public void testNext1() throws InvalidSyntaxException, Exception { 
    3116 
     
    3520                // Schedule.Builder("test2").build("20-40 * * 4-7 *"); 
    3621                Date date = new Date(Date.parse("Feb 27, 2009 8:14 PM")); 
    37                 System.out.println("============" + date + "============="); 
     22                System.out.println("============ test1 : " + date + "============="); 
     23                Schedule sche1 = new Schedule.Builder("awef").build(); 
     24                Schedule sche2 = new Schedule.Builder("awef").buildHourly(); 
     25                Schedule sche3 = new Schedule.Builder("awef").buildDaily(); 
     26                Schedule sche4 = new Schedule.Builder("awef").buildWeekly(); 
     27                Schedule sche5 = new Schedule.Builder("awef").buildYearly(); 
     28                Schedule sche6 = new Schedule.Builder("awef").build("*/30 * * * *"); 
     29                Schedule sche7 = new Schedule.Builder("awef").build("3,17,20 * * * *"); 
     30                Schedule sche8 = new Schedule.Builder("awef") 
     31                                .build("3,17,20 */3 * * *"); 
     32                Schedule sche9 = new Schedule.Builder("awef").build("0 0 5,28 * *"); 
     33                Schedule sche10 = new Schedule.Builder("awef").build("0 0 5,28 3 *"); 
     34                Schedule sche11 = new Schedule.Builder("awef").build("0 0 5,28 3 1"); 
     35                Schedule sche12 = new Schedule.Builder("awef").build("* * 31 * 0"); 
     36 
     37                System.out.println(sche1.getNextOccurence(date)); 
     38                System.out.println(sche2.getNextOccurence(date)); 
     39                System.out.println(sche3.getNextOccurence(date)); 
     40                System.out.println(sche4.getNextOccurence( date)); 
     41                System.out.println(sche5.getNextOccurence(date)); 
     42                System.out.println(sche6.getNextOccurence(date)); 
     43                System.out.println(sche7.getNextOccurence(date)); 
     44                System.out.println(sche8.getNextOccurence(date)); 
     45                System.out.println(sche9.getNextOccurence(date)); 
     46                System.out.println(sche10.getNextOccurence(date)); 
     47                System.out.println(sche11.getNextOccurence( date)); 
     48                System.out.println(sche12.getNextOccurence( date)); 
     49                 
     50         
     51                assertEquals(sche1.getNextOccurence(date).toString(),"Fri Feb 27 20:14:00 KST 2009"); 
     52                assertEquals(sche2.getNextOccurence(date).toString(),"Fri Feb 27 21:00:00 KST 2009"); 
     53                assertEquals(sche3.getNextOccurence(date).toString(),"Sat Feb 28 00:00:00 KST 2009"); 
     54                assertEquals(sche4.getNextOccurence(date).toString(),"Sun Mar 01 00:00:00 KST 2009"); 
     55                assertEquals(sche5.getNextOccurence(date).toString(),"Fri Jan 01 00:00:00 KST 2010"); 
     56                assertEquals(sche6.getNextOccurence(date).toString(),"Fri Feb 27 20:30:00 KST 2009"); 
     57                assertEquals(sche7.getNextOccurence(date).toString(),"Fri Feb 27 20:17:00 KST 2009"); 
     58                assertEquals(sche8.getNextOccurence(date).toString(),"Fri Feb 27 21:03:00 KST 2009"); 
     59                assertEquals(sche9.getNextOccurence(date).toString(),"Sat Feb 28 00:00:00 KST 2009"); 
     60                assertEquals(sche10.getNextOccurence(date).toString(),"Thu Mar 05 00:00:00 KST 2009"); 
     61                assertEquals(sche11.getNextOccurence( date).toString(),"Mon Mar 02 00:00:00 KST 2009"); 
     62                assertEquals(sche12.getNextOccurence( date).toString(),"Sun Mar 01 00:00:00 KST 2009"); 
     63        } 
     64 
     65        @Test 
     66        public void testNext2() throws InvalidSyntaxException, Exception { 
     67 
     68                // Schedule sche1 = new 
     69                // Schedule.Builder("test1").build("*/30 * 3,18 * 0"); 
     70                // Schedule sche2 = new 
     71                // Schedule.Builder("test2").build("20-40 * * 4-7 *"); 
     72                Date date = new Date(Date.parse("Aug 6, 2009 1:11 AM")); 
     73                System.out.println("============ test2 : " + date + "============="); 
    3874                Schedule sche1 = new Schedule.Builder("awef").build(); 
    3975                Schedule sche2 = new Schedule.Builder("awef").buildHourly(); 
     
    4985                Schedule sche11 = new Schedule.Builder("awef").build("0 0 5,28 3 1"); 
    5086 
    51                 System.out.println(Job.getNextOccurence(sche1, date)); 
    52                 System.out.println(Job.getNextOccurence(sche2, date)); 
    53                 System.out.println(Job.getNextOccurence(sche3, date)); 
    54                 System.out.println(Job.getNextOccurence(sche4, date)); 
    55                 System.out.println(Job.getNextOccurence(sche5, date)); 
    56                 System.out.println(Job.getNextOccurence(sche6, date)); 
    57                 System.out.println(Job.getNextOccurence(sche7, date)); 
    58                 System.out.println(Job.getNextOccurence(sche8, date)); 
    59                 System.out.println(Job.getNextOccurence(sche9, date)); 
    60                 System.out.println(Job.getNextOccurence(sche10, date)); 
    61                 System.out.println(Job.getNextOccurence(sche11, date)); 
     87                System.out.println(sche1.getNextOccurence(date)); 
     88                System.out.println(sche2.getNextOccurence(date)); 
     89                System.out.println(sche3.getNextOccurence(date)); 
     90                System.out.println(sche4.getNextOccurence(date)); 
     91                System.out.println(sche5.getNextOccurence(date)); 
     92                System.out.println(sche6.getNextOccurence(date)); 
     93                System.out.println(sche7.getNextOccurence(date)); 
     94                System.out.println(sche8.getNextOccurence(date)); 
     95                System.out.println(sche9.getNextOccurence(date)); 
     96                System.out.println(sche10.getNextOccurence( date)); 
     97                System.out.println(sche11.getNextOccurence( date)); 
    6298                 
     99                assertEquals(sche1.getNextOccurence(date).toString(),"Thu Aug 06 01:11:00 KST 2009"); 
     100                assertEquals(sche2.getNextOccurence(date).toString(),"Thu Aug 06 02:00:00 KST 2009"); 
     101                assertEquals(sche3.getNextOccurence(date).toString(),"Fri Aug 07 00:00:00 KST 2009"); 
     102                assertEquals(sche4.getNextOccurence(date).toString(),"Sun Aug 09 00:00:00 KST 2009"); 
     103                assertEquals(sche5.getNextOccurence(date).toString(),"Fri Jan 01 00:00:00 KST 2010"); 
     104                assertEquals(sche6.getNextOccurence(date).toString(),"Thu Aug 06 01:30:00 KST 2009"); 
     105                assertEquals(sche7.getNextOccurence(date).toString(),"Thu Aug 06 01:17:00 KST 2009"); 
     106                assertEquals(sche8.getNextOccurence(date).toString(),"Thu Aug 06 03:03:00 KST 2009"); 
     107                assertEquals(sche9.getNextOccurence(date).toString(),"Fri Aug 28 00:00:00 KST 2009"); 
     108                assertEquals(sche10.getNextOccurence( date).toString(),"Fri Mar 05 00:00:00 KST 2010"); 
     109                assertEquals(sche11.getNextOccurence( date).toString(),"Mon Mar 01 00:00:00 KST 2010"); 
     110        } 
    63111         
    64                 assertEquals(Job.getNextOccurence(sche1, date).toString(),"Fri Feb 27 20:14:00 KST 2009"); 
    65                 assertEquals(Job.getNextOccurence(sche2, date).toString(),"Fri Feb 27 21:00:00 KST 2009"); 
    66                 assertEquals(Job.getNextOccurence(sche3, date).toString(),"Sat Feb 28 00:00:00 KST 2009"); 
    67                 assertEquals(Job.getNextOccurence(sche4, date).toString(),"Sun Mar 01 00:00:00 KST 2009"); 
    68                 assertEquals(Job.getNextOccurence(sche5, date).toString(),"Fri Jan 01 00:00:00 KST 2010"); 
    69                 assertEquals(Job.getNextOccurence(sche6, date).toString(),"Fri Feb 27 20:30:00 KST 2009"); 
    70                 assertEquals(Job.getNextOccurence(sche7, date).toString(),"Fri Feb 27 20:17:00 KST 2009"); 
    71                 assertEquals(Job.getNextOccurence(sche8, date).toString(),"Fri Feb 27 21:03:00 KST 2009"); 
    72                 assertEquals(Job.getNextOccurence(sche9, date).toString(),"Sat Feb 28 00:00:00 KST 2009"); 
    73                 assertEquals(Job.getNextOccurence(sche10, date).toString(),"Thu Mar 05 00:00:00 KST 2009"); 
    74                 assertEquals(Job.getNextOccurence(sche11, date).toString(),"Mon Mar 02 00:00:00 KST 2009"); 
     112        @Test 
     113        public void testDaily() throws InvalidSyntaxException, Exception{ 
     114                Date date1 = new Date(); 
     115                Schedule sche = new Schedule.Builder("aef").buildDaily(); 
     116         
     117                System.out.println("============ daily : " + date1 + "============="); 
     118                for(int i = 0 ; i < 20; i++){ 
     119                        date1 = sche.getNextOccurence( date1); 
     120                        System.out.println(date1); 
     121                        date1 = new Date(date1.getTime() + 60 * 1000); 
     122                } 
    75123        } 
    76  
    77         public void testNext2() throws InvalidSyntaxException, Exception { 
    78  
    79                  
    80                 // Schedule sche1 = new 
    81                 // Schedule.Builder("test1").build("*/30 * 3,18 * 0"); 
    82                 // Schedule sche2 = new 
    83                 // Schedule.Builder("test2").build("20-40 * * 4-7 *"); 
    84                 Date date = new Date(Date.parse("Aug 6, 2009 1:11 AM")); 
    85                 System.out.println("============" + date + "============="); 
    86                 Schedule sche1 = new Schedule.Builder("awef").build(); 
    87                 Schedule sche2 = new Schedule.Builder("awef").buildHourly(); 
    88                 Schedule sche3 = new Schedule.Builder("awef").buildDaily(); 
    89                 Schedule sche4 = new Schedule.Builder("awef").buildWeekly(); 
    90                 Schedule sche5 = new Schedule.Builder("awef").buildYearly(); 
    91                 Schedule sche6 = new Schedule.Builder("awef").build("*/30 * * * *"); 
    92                 Schedule sche7 = new Schedule.Builder("awef").build("3,17,20 * * * *"); 
    93                 Schedule sche8 = new Schedule.Builder("awef") 
    94                                 .build("3,17,20 */3 * * *"); 
    95                 Schedule sche9 = new Schedule.Builder("awef").build("0 0 5,28 * *"); 
    96                 Schedule sche10 = new Schedule.Builder("awef").build("0 0 5,28 3 *"); 
    97                 Schedule sche11 = new Schedule.Builder("awef").build("0 0 5,28 3 1"); 
    98  
    99                 System.out.println(Job.getNextOccurence(sche1, date)); 
    100                 System.out.println(Job.getNextOccurence(sche2, date)); 
    101                 System.out.println(Job.getNextOccurence(sche3, date)); 
    102                 System.out.println(Job.getNextOccurence(sche4, date)); 
    103                 System.out.println(Job.getNextOccurence(sche5, date)); 
    104                 System.out.println(Job.getNextOccurence(sche6, date)); 
    105                 System.out.println(Job.getNextOccurence(sche7, date)); 
    106                 System.out.println(Job.getNextOccurence(sche8, date)); 
    107                 System.out.println(Job.getNextOccurence(sche9, date)); 
    108                 System.out.println(Job.getNextOccurence(sche10, date)); 
    109                 System.out.println(Job.getNextOccurence(sche11, date)); 
    110                  
    111                 assertEquals(Job.getNextOccurence(sche1, date).toString(),"Thu Aug 06 01:11:00 KST 2009"); 
    112                 assertEquals(Job.getNextOccurence(sche2, date).toString(),"Thu Aug 06 02:00:00 KST 2009"); 
    113                 assertEquals(Job.getNextOccurence(sche3, date).toString(),"Fri Aug 07 00:00:00 KST 2009"); 
    114                 assertEquals(Job.getNextOccurence(sche4, date).toString(),"Sun Aug 09 00:00:00 KST 2009"); 
    115                 assertEquals(Job.getNextOccurence(sche5, date).toString(),"Fri Jan 01 00:00:00 KST 2010"); 
    116                 assertEquals(Job.getNextOccurence(sche6, date).toString(),"Thu Aug 06 01:30:00 KST 2009"); 
    117                 assertEquals(Job.getNextOccurence(sche7, date).toString(),"Thu Aug 06 01:17:00 KST 2009"); 
    118                 assertEquals(Job.getNextOccurence(sche8, date).toString(),"Thu Aug 06 03:03:00 KST 2009"); 
    119                 assertEquals(Job.getNextOccurence(sche9, date).toString(),"Fri Aug 28 00:00:00 KST 2009"); 
    120                 assertEquals(Job.getNextOccurence(sche10, date).toString(),"Fri Mar 05 00:00:00 KST 2010"); 
    121                 assertEquals(Job.getNextOccurence(sche11, date).toString(),"Mon Mar 01 00:00:00 KST 2010"); 
     124         
     125        @Test 
     126        public void testWeekly() throws InvalidSyntaxException, Exception{ 
     127                Date date1 = new Date(); 
     128                Schedule sche = new Schedule.Builder("aef").buildWeekly(); 
     129         
     130                System.out.println("============ weekly : " + date1 + "============="); 
     131                for(int i = 0 ; i < 20; i++){ 
     132                        date1 = sche.getNextOccurence( date1); 
     133                        System.out.println(date1); 
     134                        date1 = new Date(date1.getTime() + 60 * 1000); 
     135                } 
    122136        } 
    123  
     137         
     138        @Test 
     139        public void testMondayAnd10th() throws InvalidSyntaxException, Exception{ 
     140                Date date1 = new Date(); 
     141                Schedule sche = new Schedule.Builder("aef").build("0 0 */10 * 1"); 
     142         
     143                System.out.println("============ mondayAnd10th : " + date1 + "============="); 
     144                for(int i = 0 ; i < 20; i++){ 
     145                        date1 = sche.getNextOccurence( date1); 
     146                        System.out.println(date1); 
     147                        date1 = new Date(date1.getTime() + 60 * 1000); 
     148                } 
     149        } 
     150         
     151        @Test 
     152        public void test31th() throws InvalidSyntaxException, Exception{ 
     153                Date date1 = new Date(Date.parse("Feb 27, 2009 8:14 PM")); 
     154                Schedule sche = new Schedule.Builder("aef").build("0 0 29 * *"); 
     155         
     156                System.out.println("============ 31th : " + date1 + "============="); 
     157                for(int i = 0 ; i < 20; i++){ 
     158                        date1 = sche.getNextOccurence( date1); 
     159                        System.out.println(date1); 
     160                        Calendar cal = Calendar.getInstance(); 
     161                        cal.setTime(date1); 
     162                        assertTrue(cal.get(Calendar.DAY_OF_MONTH) == 29); 
     163                        date1 = new Date(date1.getTime() + 60 * 1000); 
     164                } 
     165        } 
    124166} 
  • kraken-cron/src/main/java/org/krakenapps/cron/test/ScheduleBuildTest.java

    r321 r323  
    33import java.text.ParseException; 
    44 
    5 import junit.framework.TestCase; 
     5import org.junit.Test; 
    66import org.krakenapps.cron.Schedule; 
    77import org.krakenapps.cron.impl.CronField; 
    88import org.osgi.framework.InvalidSyntaxException; 
    99 
    10 public class ScheduleBuildTest extends TestCase { 
     10import static org.junit.Assert.*; 
    1111 
     12public class ScheduleBuildTest { 
     13        @Test 
    1214        public void testBuildManual() throws Exception { 
    13                 try{ 
    14                 Schedule test = new Schedule.Builder("daily").set( 
    15                                 CronField.Type.Day_of_Week, "0").set( 
    16                                 CronField.Type.Day_of_Month, "*/10").set(CronField.Type.Hour, 
    17                                 "4-10").set(CronField.Type.Minute, "0,3,59").set( 
    18                                 CronField.Type.Month, "12").build(); 
    19                 Schedule test2 = new Schedule.Builder("daily").set( 
    20                                 CronField.Type.Day_of_Week, "3").set( 
    21                                 CronField.Type.Day_of_Month, "1").set(CronField.Type.Hour, 
    22                                 "0-23").set(CronField.Type.Minute, "2,3,3").build(); 
     15                try { 
     16                        Schedule test = new Schedule.Builder("daily").set( 
     17                                        CronField.Type.Day_of_Week, "0").set( 
     18                                        CronField.Type.Day_of_Month, "*/10").set( 
     19                                        CronField.Type.Hour, "4-10").set(CronField.Type.Minute, 
     20                                        "0,3,59").set(CronField.Type.Month, "12").build(); 
     21                        Schedule test2 = new Schedule.Builder("daily").set( 
     22                                        CronField.Type.Day_of_Week, "3").set( 
     23                                        CronField.Type.Day_of_Month, "1").set(CronField.Type.Hour, 
     24                                        "0-23").set(CronField.Type.Minute, "2,3,3").build(); 
    2325 
    24                 System.out.println("test1" + test); 
    25                 System.out.println("test2" + test2); 
     26                        System.out.println("test1" + test); 
     27                        System.out.println("test2" + test2); 
    2628 
    27                 assertEquals(test.fieldMembers(CronField.Type.Minute), "{0, 3, 59}"); 
    28                 assertEquals(test.fieldMembers(CronField.Type.Day_of_Month), 
    29                                 "{0, 10, 20, 30}"); // interval 
    30                 assertEquals(test.fieldMembers(CronField.Type.Hour), 
    31                                 "{4, 5, 6, 7, 8, 9, 10}"); // range 
    32                 assertEquals(test.fieldMembers(CronField.Type.Month), "{11}"); 
     29                        assertEquals(test.fieldMembers(CronField.Type.Minute), "{0, 3, 59}"); 
     30                        assertEquals(test.fieldMembers(CronField.Type.Day_of_Month), 
     31                                        "{0, 10, 20, 30}"); // interval 
     32                        assertEquals(test.fieldMembers(CronField.Type.Hour), 
     33                                        "{4, 5, 6, 7, 8, 9, 10}"); // range 
     34                        assertEquals(test.fieldMembers(CronField.Type.Month), "{11}"); 
    3335 
    34                 assertEquals(test2.fieldMembers(CronField.Type.Minute), "{2, 3}"); // specify 
    35                 assertEquals(test2.fieldMembers(CronField.Type.Month), 
    36                                 "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}"); // default 
     36                        assertEquals(test2.fieldMembers(CronField.Type.Minute), "{2, 3}"); // specify 
     37                        assertEquals(test2.fieldMembers(CronField.Type.Month), 
     38                                        "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}"); // default 
    3739 
    38                 assertEquals(test, new Schedule.Builder("daily") 
    39                                 .build("0,3,59 4-10 */10 12 0")); 
    40                 }catch (Exception e) { 
     40                        assertEquals(test, new Schedule.Builder("daily") 
     41                                        .build("0,3,59 4-10 */10 12 0")); 
     42                } catch (Exception e) { 
    4143                        fail(); 
    4244                        // TODO: handle exception 
    4345                } 
     46        } 
     47         
     48        @Test 
     49        public void testBuildString() throws InvalidSyntaxException, Exception { 
     50                try { 
     51                        Schedule test3 = new Schedule.Builder("daily").build("0 0 1 1 3-6"); 
     52                        System.out.println("test3" + test3); 
    4453 
    45         } 
    46  
    47         public void testBuildString() throws InvalidSyntaxException, Exception { 
    48                 try{ 
    49                 Schedule test3 = new Schedule.Builder("daily").build("0 0 1 1 3-6"); 
    50                 System.out.println("test3" + test3); 
    51  
    52                 assertEquals(test3.fieldMembers(CronField.Type.Day_of_Week), 
    53                                 "{3, 4, 5, 6}"); 
    54                 assertEquals(test3.fieldMembers(CronField.Type.Day_of_Month), "{0}"); 
    55                 assertEquals(test3.fieldMembers(CronField.Type.Month), "{0}"); 
    56                 }catch (Exception e) { 
    57                         fail(); 
    58                         // TODO: handle exception 
    59                 } 
    60  
    61         } 
    62  
    63         public void testBuildShort() throws InvalidSyntaxException, Exception { 
    64                 try{ 
    65                 Schedule test8 = new Schedule.Builder("daily").build(); 
    66                 Schedule test4 = new Schedule.Builder("daily").buildYearly(); 
    67                 Schedule test5 = new Schedule.Builder("daily").buildMonthly(); 
    68                 Schedule test6 = new Schedule.Builder("daily").buildDaily(); 
    69                 Schedule test7 = new Schedule.Builder("daily").buildWeekly(); 
    70                 System.out.println("test8" + test8); 
    71                 System.out.println("test4" + test4); 
    72                 System.out.println("test5" + test5); 
    73                 System.out.println("test6" + test6); 
    74                 System.out.println("test7" + test7); 
    75  
    76                 assertEquals(test8, new Schedule.Builder("daily").build("* * * * * ")); 
    77                 assertEquals(test4, new Schedule.Builder("daily").build("0 0 1 1 *")); 
    78                 assertEquals(test5, new Schedule.Builder("daily").build("0 0 1 * *")); 
    79                 assertEquals(test6, new Schedule.Builder("daily").build("0 0 * * *")); 
    80                 assertEquals(test7, new Schedule.Builder("daily").build("0 0 * * 0")); 
     54                        assertEquals(test3.fieldMembers(CronField.Type.Day_of_Week), 
     55                                        "{3, 4, 5, 6}"); 
     56                        assertEquals(test3.fieldMembers(CronField.Type.Day_of_Month), "{0}"); 
     57                        assertEquals(test3.fieldMembers(CronField.Type.Month), "{0}"); 
    8158                } catch (Exception e) { 
    8259                        fail(); 
     
    8663        } 
    8764 
     65        @Test 
     66        public void testBuildShort() throws InvalidSyntaxException, Exception { 
     67                try { 
     68                        Schedule test8 = new Schedule.Builder("daily").build(); 
     69                        Schedule test4 = new Schedule.Builder("daily").buildYearly(); 
     70                        Schedule test5 = new Schedule.Builder("daily").buildMonthly(); 
     71                        Schedule test6 = new Schedule.Builder("daily").buildDaily(); 
     72                        Schedule test7 = new Schedule.Builder("daily").buildWeekly(); 
     73                        System.out.println("test8" + test8); 
     74                        System.out.println("test4" + test4); 
     75                        System.out.println("test5" + test5); 
     76                        System.out.println("test6" + test6); 
     77                        System.out.println("test7" + test7); 
     78 
     79                        assertEquals(test8, new Schedule.Builder("daily") 
     80                                        .build("* * * * * ")); 
     81                        assertEquals(test4, new Schedule.Builder("daily") 
     82                                        .build("0 0 1 1 *")); 
     83                        assertEquals(test5, new Schedule.Builder("daily") 
     84                                        .build("0 0 1 * *")); 
     85                        assertEquals(test6, new Schedule.Builder("daily") 
     86                                        .build("0 0 * * *")); 
     87                        assertEquals(test7, new Schedule.Builder("daily") 
     88                                        .build("0 0 * * 0")); 
     89                } catch (Exception e) { 
     90                        fail(); 
     91                        // TODO: handle exception 
     92                } 
     93 
     94        } 
     95 
     96        @Test 
    8897        public void testSafety() throws Exception { 
    89                 try{ 
    90                 Schedule.Builder safe_builder = new Schedule.Builder("daily"); 
    91                 Schedule safe_test = safe_builder.build(); 
    92                 System.out.println("safe_test1" + safe_test); 
    93                 safe_builder.set(CronField.Type.Hour, "3"); 
    94                 System.out.println("safe_test2" + safe_test); 
    95                 assertEquals(safe_test, new Schedule.Builder("daily").build()); 
    96                 }catch (Exception e) { 
     98                try { 
     99                        Schedule.Builder safe_builder = new Schedule.Builder("daily"); 
     100                        Schedule safe_test = safe_builder.build(); 
     101                        System.out.println("safe_test1" + safe_test); 
     102                        safe_builder.set(CronField.Type.Hour, "3"); 
     103                        System.out.println("safe_test2" + safe_test); 
     104                        assertEquals(safe_test, new Schedule.Builder("daily").build()); 
     105                } catch (Exception e) { 
    97106                        fail(); 
    98107                        // TODO: handle exception 
     
    100109        } 
    101110 
     111        @Test 
    102112        public void testCollision() { 
    103113                try { 
     
    120130        } 
    121131 
     132        @Test 
    122133        public void testException() { 
    123134                try { 
     
    126137                } catch (ParseException e) { 
    127138                        // TODO Auto-generated catch block 
    128                 } catch (InvalidSyntaxException e) { 
    129                         // TODO Auto-generated catch block 
    130                 } 
     139                }  
    131140                try { 
    132141                        new Schedule.Builder("daily").set(CronField.Type.Day_of_Month, "0"); 
     
    134143                } catch (ParseException e) { 
    135144                        // TODO Auto-generated catch block 
    136                 } catch (InvalidSyntaxException e) { 
    137                         // TODO Auto-generated catch block 
    138                 } 
     145                }  
    139146                try { 
    140147                        new Schedule.Builder("daily").set(CronField.Type.Hour, "24"); 
     
    142149                } catch (ParseException e) { 
    143150                        // TODO Auto-generated catch block 
    144                 } catch (InvalidSyntaxException e) { 
    145                         // TODO Auto-generated catch block 
    146                 } 
     151                }  
    147152                try { 
    148153                        new Schedule.Builder("daily").set(CronField.Type.Hour, "-4"); 
     
    150155                } catch (ParseException e) { 
    151156                        // TODO Auto-generated catch block 
    152                 } catch (InvalidSyntaxException e) { 
    153                         // TODO Auto-generated catch block 
    154                 } 
     157                }  
    155158                try { 
    156159                        new Schedule.Builder("daily").build("0 0 0 0"); 
     
    158161                } catch (ParseException e) { 
    159162                        // TODO Auto-generated catch block 
    160                 } catch (InvalidSyntaxException e) { 
    161                         // TODO Auto-generated catch block 
    162                 } catch (Exception e) { 
     163                }catch (Exception e) { 
    163164                        // TODO Auto-generated catch block 
    164165                }