Ignore:
Timestamp:
01/03/12 17:56:54 (5 months ago)
Author:
delmitz@delmitz-PC.office.nchovy.net
Branch:
default
Message:

removed Preferences dependency.

Location:
kraken-cron/src/main
Files:
7 edited

Legend:

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

    r752 r756  
    4343        private Schedule(Builder builder) { 
    4444                this.map = new HashMap<String, CronField>(); 
    45                 this.map.put("Minute", builder.map.get("Minute")); 
    46                 this.map.put("Hour", builder.map.get("Hour")); 
    47                 this.map.put("Month", builder.map.get("Month")); 
    48  
    49                 CronField dom = builder.map.get("DayOfMonth"); 
    50                 CronField dow = builder.map.get("DayOfWeek"); 
     45                this.map.put(Type.MINUTE.toString(), builder.map.get(Type.MINUTE.toString())); 
     46                this.map.put(Type.HOUR.toString(), builder.map.get(Type.HOUR.toString())); 
     47                this.map.put(Type.MONTH.toString(), builder.map.get(Type.MONTH.toString())); 
     48 
     49                CronField dom = builder.map.get(Type.DAY_OF_MONTH.toString()); 
     50                CronField dow = builder.map.get(Type.DAY_OF_WEEK.toString()); 
    5151                try { 
    5252                        CronField.solveCollision(dom, dow); 
     
    5454                        // must succeed. ignore. 
    5555                } 
    56                 this.map.put("DayOfMonth", dom); 
    57                 this.map.put("DayOfWeek", dow); 
     56                this.map.put(Type.DAY_OF_MONTH.toString(), dom); 
     57                this.map.put(Type.DAY_OF_WEEK.toString(), dow); 
    5858                this.taskName = builder.taskName; 
    5959        } 
  • kraken-cron/src/main/java/org/krakenapps/cron/impl/CronConfig.java

    r752 r756  
    1717 
    1818import java.text.ParseException; 
     19import java.util.Collection; 
    1920import java.util.HashMap; 
    2021import java.util.Map; 
    2122import java.util.concurrent.atomic.AtomicInteger; 
    2223 
     24import org.krakenapps.confdb.CollectionName; 
     25import org.krakenapps.confdb.Config; 
     26import org.krakenapps.confdb.ConfigDatabase; 
     27import org.krakenapps.confdb.ConfigService; 
     28import org.krakenapps.confdb.Predicates; 
    2329import org.krakenapps.cron.Schedule; 
    24 import org.osgi.service.prefs.BackingStoreException; 
    25 import org.osgi.service.prefs.Preferences; 
    26 import org.slf4j.Logger; 
    27 import org.slf4j.LoggerFactory; 
    2830 
    2931/** 
     
    3537 */ 
    3638public class CronConfig { 
    37         private final Logger logger = LoggerFactory.getLogger(CronConfig.class.getName()); 
    38         private Preferences prefs; 
     39        private ConfigDatabase db; 
    3940        private AtomicInteger maxId = new AtomicInteger(); 
    4041 
    41         public CronConfig(Preferences prefs) { 
    42                 this.prefs = prefs; 
     42        public CronConfig(ConfigService conf) { 
     43                this.db = conf.ensureDatabase("kraken-cron"); 
    4344                loadMaxId(); 
    4445        } 
    4546 
    4647        private void loadMaxId() { 
    47                 try { 
    48                         Preferences schedules = getScheduleRoot(); 
    49                         for (String name : schedules.childrenNames()) { 
    50                                 int id = Integer.parseInt(name); 
    51                                 if (maxId.get() < id) { 
    52                                         maxId.set(id); 
    53                                 } 
     48                Collection<ScheduleInfo> schedules = db.findAll(ScheduleInfo.class).getDocuments(ScheduleInfo.class); 
     49                for (ScheduleInfo schedule : schedules) { 
     50                        if (maxId.get() < schedule.id) { 
     51                                maxId.set(schedule.id); 
    5452                        } 
    55                 } catch (BackingStoreException e) { 
    56                         logger.warn("kraken cron: fetch max id failed", e); 
    5753                } 
    58  
    5954        } 
    6055 
     
    6661         */ 
    6762        public int addEntry(Schedule schedule) { 
    68                 try { 
    69                         Preferences schedules = getScheduleRoot(); 
    70                         int nextId = maxId.incrementAndGet(); 
    71                         Preferences entry = schedules.node(Integer.toString(nextId)); 
     63                ScheduleInfo info = new ScheduleInfo(); 
     64                info.id = maxId.incrementAndGet(); 
     65                info.task = schedule.getTaskName(); 
     66                info.minute = schedule.get(CronField.Type.MINUTE).toString(); 
     67                info.hour = schedule.get(CronField.Type.HOUR).toString(); 
     68                info.dayOfMonth = schedule.get(CronField.Type.DAY_OF_MONTH).toString(); 
     69                info.month = schedule.get(CronField.Type.MONTH).toString(); 
     70                info.dayOfWeek = schedule.get(CronField.Type.DAY_OF_WEEK).toString(); 
     71                db.add(info); 
    7272 
    73                         entry.put("minute", schedule.get(CronField.Type.MINUTE).toString()); 
    74                         entry.put("hour", schedule.get(CronField.Type.HOUR).toString()); 
    75                         entry.put("day_of_month", schedule.get(CronField.Type.DAY_OF_MONTH).toString()); 
    76                         entry.put("month", schedule.get(CronField.Type.MONTH).toString()); 
    77                         entry.put("day_of_week", schedule.get(CronField.Type.DAY_OF_WEEK).toString()); 
    78                         entry.put("task", schedule.getTaskName()); 
    79  
    80                         schedules.flush(); 
    81                         schedules.sync(); 
    82  
    83                         return nextId; 
    84                 } catch (BackingStoreException e) { 
    85                         logger.warn("kraken cron: add entry failed", e); 
    86                         return -1; 
    87                 } 
     73                return info.id; 
    8874        } 
    8975 
     
    9480         */ 
    9581        public void removeEntry(int id) { 
    96                 try { 
    97                         Preferences schedules = getScheduleRoot(); 
    98                         String name = Integer.toString(id); 
    99                         if (!schedules.nodeExists(name)) 
    100                                 return; 
    101  
    102                         schedules.node(name).removeNode(); 
    103                         schedules.flush(); 
    104                         schedules.sync(); 
    105                 } catch (BackingStoreException e) { 
    106                         logger.warn("kraken cron: remove entry failed", e); 
    107                 } 
     82                Config c = db.findOne(ScheduleInfo.class, Predicates.field("id", id)); 
     83                if (c != null) 
     84                        db.remove(c); 
    10885        } 
    10986 
     
    11693         */ 
    11794        public Map<Integer, Schedule> getEntries() throws ParseException { 
    118                 Preferences schedules = getScheduleRoot(); 
    11995                Map<Integer, Schedule> map = new HashMap<Integer, Schedule>(); 
    120                 try { 
    121                         for (String id : schedules.childrenNames()) { 
    122                                 Preferences p = schedules.node(id); 
    123  
    124                                 Schedule.Builder builder = new Schedule.Builder(p.get("task", null)); 
    125                                 builder.set(CronField.Type.MINUTE, p.get("minute", null)); 
    126                                 builder.set(CronField.Type.HOUR, p.get("hour", null)); 
    127                                 builder.set(CronField.Type.DAY_OF_MONTH, p.get("day_of_month", null)); 
    128                                 builder.set(CronField.Type.MONTH, p.get("month", null)); 
    129                                 builder.set(CronField.Type.DAY_OF_WEEK, p.get("day_of_week", null)); 
    130                                 map.put(Integer.parseInt(id), builder.build()); 
    131                         } 
    132                         return map; 
    133                 } catch (BackingStoreException e) { 
    134                         logger.error("kraken cron: load schedule instances error", e); 
     96                for (ScheduleInfo schedule : db.findAll(ScheduleInfo.class).getDocuments(ScheduleInfo.class)) { 
     97                        Schedule.Builder builder = new Schedule.Builder(schedule.task); 
     98                        builder.set(CronField.Type.MINUTE, schedule.minute); 
     99                        builder.set(CronField.Type.HOUR, schedule.hour); 
     100                        builder.set(CronField.Type.DAY_OF_MONTH, schedule.dayOfMonth); 
     101                        builder.set(CronField.Type.MONTH, schedule.month); 
     102                        builder.set(CronField.Type.DAY_OF_WEEK, schedule.dayOfWeek); 
     103                        map.put(schedule.id, builder.build()); 
    135104                } 
    136                 return null; 
     105                return map; 
    137106        } 
    138107 
    139         private Preferences getScheduleRoot() { 
    140                 return prefs.node("/kraken_cron/schedules"); 
     108        @CollectionName("schedule") 
     109        private static class ScheduleInfo { 
     110                private int id; 
     111                private String task; 
     112                private String minute; 
     113                private String hour; 
     114                private String dayOfMonth; 
     115                private String month; 
     116                private String dayOfWeek; 
    141117        } 
    142118} 
  • kraken-cron/src/main/java/org/krakenapps/cron/impl/CronField.java

    r2 r756  
    4343                private final int base; 
    4444 
    45                 Type(String fieldName, int bitLength, int base) { 
     45                private Type(String fieldName, int bitLength, int base) { 
    4646                        this.fieldName = fieldName; 
    4747                        this.bitLength = bitLength; 
  • kraken-cron/src/main/java/org/krakenapps/cron/impl/CronScriptFactory.java

    r752 r756  
    1616package org.krakenapps.cron.impl; 
    1717 
     18import org.apache.felix.ipojo.annotations.Component; 
     19import org.apache.felix.ipojo.annotations.Provides; 
     20import org.apache.felix.ipojo.annotations.Requires; 
     21import org.apache.felix.ipojo.annotations.ServiceProperty; 
    1822import org.krakenapps.api.Script; 
    1923import org.krakenapps.api.ScriptFactory; 
     
    2731 * @since 1.0.0 
    2832 */ 
     33@Component(name = "cron-script-factory") 
     34@Provides 
    2935public class CronScriptFactory implements ScriptFactory { 
     36        @SuppressWarnings("unused") 
     37        @ServiceProperty(name = "alias", value = "cron") 
     38        private String alias; 
     39 
     40        @Requires 
     41        private CronService cron; 
     42 
    3043        private BundleContext context; 
    31         private CronService cron; 
     44 
     45        public CronScriptFactory(BundleContext context) { 
     46                this.context = context; 
     47        } 
    3248 
    3349        public CronScriptFactory(BundleContext context, CronService cron) { 
  • kraken-cron/src/main/java/org/krakenapps/cron/impl/CronServiceImpl.java

    r752 r756  
    2525import java.util.concurrent.ConcurrentMap; 
    2626 
     27import org.apache.felix.ipojo.annotations.Component; 
     28import org.apache.felix.ipojo.annotations.Invalidate; 
     29import org.apache.felix.ipojo.annotations.Provides; 
     30import org.apache.felix.ipojo.annotations.Requires; 
     31import org.apache.felix.ipojo.annotations.Validate; 
     32import org.krakenapps.confdb.ConfigService; 
    2733import org.krakenapps.cron.CronService; 
    2834import org.krakenapps.cron.DuplicatedScheduleException; 
     
    3137import org.osgi.framework.InvalidSyntaxException; 
    3238import org.osgi.framework.ServiceReference; 
    33 import org.osgi.service.prefs.Preferences; 
    34 import org.osgi.service.prefs.PreferencesService; 
    3539 
    3640/** 
     
    4044 * @since 1.0.0 
    4145 */ 
     46@Component(name = "cron-service") 
     47@Provides 
    4248public class CronServiceImpl implements CronService { 
    4349        private static BundleContext bundleContext; 
     
    4652         */ 
    4753        private ConcurrentMap<Integer, Schedule> map; 
     54 
     55        @Requires 
     56        private ConfigService conf; 
    4857        private final CronConfig config; 
    4958        private final Scheduler scheduler = new Scheduler(); 
     
    5362                tracker = new JobServiceTracker(context, this); 
    5463                bundleContext = context; 
    55                 this.config = new CronConfig(getSystemPrefs()); 
     64                this.config = new CronConfig(conf); 
    5665                refreshMap(); 
     66        } 
     67 
     68        public CronServiceImpl(BundleContext context, ConfigService conf) throws ParseException { 
     69                tracker = new JobServiceTracker(context, this); 
     70                bundleContext = context; 
     71                this.config = new CronConfig(conf); 
     72                refreshMap(); 
     73                validate(); 
     74        } 
     75 
     76        @Validate 
     77        public void validate() { 
    5778                scheduler.start(getMap()); 
    5879                tracker.open(); 
     80        } 
     81 
     82        @Invalidate 
     83        public void invalidate() { 
     84                tracker.close(); 
     85                scheduler.stop(); 
    5986        } 
    6087 
     
    140167                return task; 
    141168        } 
    142  
    143         private Preferences getSystemPrefs() { 
    144                 ServiceReference ref = bundleContext.getServiceReference(PreferencesService.class.getName()); 
    145                 PreferencesService prefsService = (PreferencesService) bundleContext.getService(ref); 
    146                 return prefsService.getSystemPreferences(); 
    147         } 
    148169} 
  • kraken-cron/src/main/java/org/krakenapps/cron/msgbus/CronPlugin.java

    r2 r756  
    2222        @Requires 
    2323        private CronService cron; 
     24 
     25        public CronPlugin() { 
     26        } 
     27 
     28        public CronPlugin(CronService cron) { 
     29                this.cron = cron; 
     30        } 
    2431 
    2532        @MsgbusMethod 
  • kraken-cron/src/main/resources/metadata.xml

    r752 r756  
    22        xsi:schemaLocation="org.apache.felix.ipojo http://felix.apache.org/ipojo/schemas/CURRENT/core.xsd" 
    33        xmlns="org.apache.felix.ipojo"> 
     4        <instance component="cron-service" /> 
    45        <instance component="cron-plugin" /> 
     6        <instance component="cron-script-factory" /> 
    57</ipojo> 
Note: See TracChangeset for help on using the changeset viewer.