Changeset 756:93286bd7acf0 for kraken-cron/src
- Timestamp:
- 01/03/12 17:56:54 (5 months ago)
- Branch:
- default
- Location:
- kraken-cron/src/main
- Files:
-
- 7 edited
-
java/org/krakenapps/cron/Schedule.java (modified) (2 diffs)
-
java/org/krakenapps/cron/impl/CronConfig.java (modified) (5 diffs)
-
java/org/krakenapps/cron/impl/CronField.java (modified) (1 diff)
-
java/org/krakenapps/cron/impl/CronScriptFactory.java (modified) (2 diffs)
-
java/org/krakenapps/cron/impl/CronServiceImpl.java (modified) (6 diffs)
-
java/org/krakenapps/cron/msgbus/CronPlugin.java (modified) (1 diff)
-
resources/metadata.xml (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
kraken-cron/src/main/java/org/krakenapps/cron/Schedule.java
r752 r756 43 43 private Schedule(Builder builder) { 44 44 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()); 51 51 try { 52 52 CronField.solveCollision(dom, dow); … … 54 54 // must succeed. ignore. 55 55 } 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); 58 58 this.taskName = builder.taskName; 59 59 } -
kraken-cron/src/main/java/org/krakenapps/cron/impl/CronConfig.java
r752 r756 17 17 18 18 import java.text.ParseException; 19 import java.util.Collection; 19 20 import java.util.HashMap; 20 21 import java.util.Map; 21 22 import java.util.concurrent.atomic.AtomicInteger; 22 23 24 import org.krakenapps.confdb.CollectionName; 25 import org.krakenapps.confdb.Config; 26 import org.krakenapps.confdb.ConfigDatabase; 27 import org.krakenapps.confdb.ConfigService; 28 import org.krakenapps.confdb.Predicates; 23 29 import 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;28 30 29 31 /** … … 35 37 */ 36 38 public class CronConfig { 37 private final Logger logger = LoggerFactory.getLogger(CronConfig.class.getName()); 38 private Preferences prefs; 39 private ConfigDatabase db; 39 40 private AtomicInteger maxId = new AtomicInteger(); 40 41 41 public CronConfig( Preferences prefs) {42 this. prefs = prefs;42 public CronConfig(ConfigService conf) { 43 this.db = conf.ensureDatabase("kraken-cron"); 43 44 loadMaxId(); 44 45 } 45 46 46 47 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); 54 52 } 55 } catch (BackingStoreException e) {56 logger.warn("kraken cron: fetch max id failed", e);57 53 } 58 59 54 } 60 55 … … 66 61 */ 67 62 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); 72 72 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; 88 74 } 89 75 … … 94 80 */ 95 81 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); 108 85 } 109 86 … … 116 93 */ 117 94 public Map<Integer, Schedule> getEntries() throws ParseException { 118 Preferences schedules = getScheduleRoot();119 95 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()); 135 104 } 136 return null;105 return map; 137 106 } 138 107 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; 141 117 } 142 118 } -
kraken-cron/src/main/java/org/krakenapps/cron/impl/CronField.java
r2 r756 43 43 private final int base; 44 44 45 Type(String fieldName, int bitLength, int base) {45 private Type(String fieldName, int bitLength, int base) { 46 46 this.fieldName = fieldName; 47 47 this.bitLength = bitLength; -
kraken-cron/src/main/java/org/krakenapps/cron/impl/CronScriptFactory.java
r752 r756 16 16 package org.krakenapps.cron.impl; 17 17 18 import org.apache.felix.ipojo.annotations.Component; 19 import org.apache.felix.ipojo.annotations.Provides; 20 import org.apache.felix.ipojo.annotations.Requires; 21 import org.apache.felix.ipojo.annotations.ServiceProperty; 18 22 import org.krakenapps.api.Script; 19 23 import org.krakenapps.api.ScriptFactory; … … 27 31 * @since 1.0.0 28 32 */ 33 @Component(name = "cron-script-factory") 34 @Provides 29 35 public 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 30 43 private BundleContext context; 31 private CronService cron; 44 45 public CronScriptFactory(BundleContext context) { 46 this.context = context; 47 } 32 48 33 49 public CronScriptFactory(BundleContext context, CronService cron) { -
kraken-cron/src/main/java/org/krakenapps/cron/impl/CronServiceImpl.java
r752 r756 25 25 import java.util.concurrent.ConcurrentMap; 26 26 27 import org.apache.felix.ipojo.annotations.Component; 28 import org.apache.felix.ipojo.annotations.Invalidate; 29 import org.apache.felix.ipojo.annotations.Provides; 30 import org.apache.felix.ipojo.annotations.Requires; 31 import org.apache.felix.ipojo.annotations.Validate; 32 import org.krakenapps.confdb.ConfigService; 27 33 import org.krakenapps.cron.CronService; 28 34 import org.krakenapps.cron.DuplicatedScheduleException; … … 31 37 import org.osgi.framework.InvalidSyntaxException; 32 38 import org.osgi.framework.ServiceReference; 33 import org.osgi.service.prefs.Preferences;34 import org.osgi.service.prefs.PreferencesService;35 39 36 40 /** … … 40 44 * @since 1.0.0 41 45 */ 46 @Component(name = "cron-service") 47 @Provides 42 48 public class CronServiceImpl implements CronService { 43 49 private static BundleContext bundleContext; … … 46 52 */ 47 53 private ConcurrentMap<Integer, Schedule> map; 54 55 @Requires 56 private ConfigService conf; 48 57 private final CronConfig config; 49 58 private final Scheduler scheduler = new Scheduler(); … … 53 62 tracker = new JobServiceTracker(context, this); 54 63 bundleContext = context; 55 this.config = new CronConfig( getSystemPrefs());64 this.config = new CronConfig(conf); 56 65 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() { 57 78 scheduler.start(getMap()); 58 79 tracker.open(); 80 } 81 82 @Invalidate 83 public void invalidate() { 84 tracker.close(); 85 scheduler.stop(); 59 86 } 60 87 … … 140 167 return task; 141 168 } 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 }148 169 } -
kraken-cron/src/main/java/org/krakenapps/cron/msgbus/CronPlugin.java
r2 r756 22 22 @Requires 23 23 private CronService cron; 24 25 public CronPlugin() { 26 } 27 28 public CronPlugin(CronService cron) { 29 this.cron = cron; 30 } 24 31 25 32 @MsgbusMethod -
kraken-cron/src/main/resources/metadata.xml
r752 r756 2 2 xsi:schemaLocation="org.apache.felix.ipojo http://felix.apache.org/ipojo/schemas/CURRENT/core.xsd" 3 3 xmlns="org.apache.felix.ipojo"> 4 <instance component="cron-service" /> 4 5 <instance component="cron-plugin" /> 6 <instance component="cron-script-factory" /> 5 7 </ipojo>
Note: See TracChangeset
for help on using the changeset viewer.
