Changeset 756:93286bd7acf0
- Timestamp:
- 01/03/12 17:56:54 (5 months ago)
- Branch:
- default
- Files:
-
- 9 edited
-
kraken-core/src/main/java/org/krakenapps/main/Kraken.java (modified) (9 diffs)
-
kraken-cron/pom.xml (modified) (1 diff)
-
kraken-cron/src/main/java/org/krakenapps/cron/Schedule.java (modified) (2 diffs)
-
kraken-cron/src/main/java/org/krakenapps/cron/impl/CronConfig.java (modified) (5 diffs)
-
kraken-cron/src/main/java/org/krakenapps/cron/impl/CronField.java (modified) (1 diff)
-
kraken-cron/src/main/java/org/krakenapps/cron/impl/CronScriptFactory.java (modified) (2 diffs)
-
kraken-cron/src/main/java/org/krakenapps/cron/impl/CronServiceImpl.java (modified) (6 diffs)
-
kraken-cron/src/main/java/org/krakenapps/cron/msgbus/CronPlugin.java (modified) (1 diff)
-
kraken-cron/src/main/resources/metadata.xml (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
kraken-core/src/main/java/org/krakenapps/main/Kraken.java
r752 r756 66 66 import org.krakenapps.cron.impl.CronScriptFactory; 67 67 import org.krakenapps.cron.impl.CronServiceImpl; 68 import org.krakenapps.cron.msgbus.CronPlugin; 68 69 import org.krakenapps.instrumentation.InstrumentationServiceImpl; 69 70 import org.krakenapps.keystore.KeyStoreScriptFactory; … … 281 282 * @throws IOException 282 283 */ 283 private void openConsolePort( BundleContext bc) throws IOException {284 private void openConsolePort() throws IOException { 284 285 InetAddress address = getConsoleBindAddress(); 285 286 int port = getConsolePortNumber(); … … 288 289 NioSocketAcceptor acceptor = new NioSocketAcceptor(); 289 290 acceptor.getFilterChain().addLast("protocol", new ProtocolCodecFilter(new TelnetCodecFactory())); 290 acceptor.setHandler(new TelnetHandler( bc));291 acceptor.setHandler(new TelnetHandler(context)); 291 292 acceptor.setReuseAddress(true); 292 293 acceptor.bind(bindSocketAddress); … … 349 350 conf = new FileConfigService(); 350 351 auth = new DefaultAuthService(context, conf); 351 cron = new CronServiceImpl(context );352 353 registerScripts( context);352 cron = new CronServiceImpl(context, conf); 353 354 registerScripts(); 354 355 registerInstrumentation(); 355 openConsolePort( context);356 openConsolePort(); 356 357 startSshServer(); 357 358 logger.info("Kraken started."); … … 374 375 * 375 376 * @see Kraken API documentation 376 * @param context 377 * the bundle context 378 */ 379 private void registerScripts(BundleContext context) { 380 registerScriptFactory(context, CoreScriptFactory.class, "core"); 381 registerScriptFactory(context, BundleScriptFactory.class, "bundle"); 382 registerScriptFactory(context, LoggerScriptFactory.class, "logger"); 383 registerScriptFactory(context, OsgiScriptFactory.class, "osgi"); 384 registerScriptFactory(context, PackageScriptFactory.class, "pkg"); 385 registerScriptFactory(context, HistoryScriptFactory.class, "history"); 386 registerScriptFactory(context, ThreadScriptFactory.class, "thread"); 387 registerScriptFactory(context, PerfScriptFactory.class, "perf"); 388 registerScriptFactory(context, RegistryScriptFactory.class, "registry"); 389 registerScriptFactory(context, KeyStoreScriptFactory.class, "keystore"); 390 registerScriptFactory(context, new AccountScriptFactory(context, conf), "account"); 391 registerScriptFactory(context, SunPerfScriptFactory.class, "sunperf"); 392 registerScriptFactory(context, new ConfScriptFactory(conf), "conf"); 393 registerScriptFactory(context, new AuthScriptFactory(auth), "auth"); 394 registerScriptFactory(context, new CronScriptFactory(context, cron), "cron"); 395 registerScriptFactory(context, BatchScriptFactory.class, "batch"); 396 397 Kraken.getContext().registerService(CronService.class.getName(), cron, null); 377 */ 378 private void registerScripts() { 379 registerScriptFactory(CoreScriptFactory.class, "core"); 380 registerScriptFactory(BundleScriptFactory.class, "bundle"); 381 registerScriptFactory(LoggerScriptFactory.class, "logger"); 382 registerScriptFactory(OsgiScriptFactory.class, "osgi"); 383 registerScriptFactory(PackageScriptFactory.class, "pkg"); 384 registerScriptFactory(HistoryScriptFactory.class, "history"); 385 registerScriptFactory(ThreadScriptFactory.class, "thread"); 386 registerScriptFactory(PerfScriptFactory.class, "perf"); 387 registerScriptFactory(RegistryScriptFactory.class, "registry"); 388 registerScriptFactory(KeyStoreScriptFactory.class, "keystore"); 389 registerScriptFactory(new AccountScriptFactory(context, conf), "account"); 390 registerScriptFactory(SunPerfScriptFactory.class, "sunperf"); 391 registerScriptFactory(new ConfScriptFactory(conf), "conf"); 392 registerScriptFactory(new AuthScriptFactory(auth), "auth"); 393 registerScriptFactory(new CronScriptFactory(context, cron), "cron"); 394 registerScriptFactory(BatchScriptFactory.class, "batch"); 398 395 } 399 396 … … 401 398 * Register script factory to OSGi service registry. 402 399 * 403 * @param context404 * the bundle context405 400 * @param scriptFactory 406 401 * the script factory … … 409 404 * command) 410 405 */ 411 private void registerScriptFactory( BundleContext context,Class<? extends ScriptFactory> scriptFactory, String alias) {412 try { 413 registerScriptFactory( context,scriptFactory.newInstance(), alias);406 private void registerScriptFactory(Class<? extends ScriptFactory> scriptFactory, String alias) { 407 try { 408 registerScriptFactory(scriptFactory.newInstance(), alias); 414 409 } catch (InstantiationException e) { 415 410 e.printStackTrace(); … … 419 414 } 420 415 421 private void registerScriptFactory( BundleContext context,ScriptFactory scriptFactory, String alias) {416 private void registerScriptFactory(ScriptFactory scriptFactory, String alias) { 422 417 Dictionary<String, Object> props = new Hashtable<String, Object>(); 423 418 props.put("alias", alias); … … 427 422 private void registerInstrumentation() { 428 423 context.registerService(InstrumentationService.class.getName(), new InstrumentationServiceImpl(), null); 424 context.registerService(CronService.class.getName(), cron, null); 425 context.registerService(CronPlugin.class.getName(), new CronPlugin(cron), null); 429 426 } 430 427 -
kraken-cron/pom.xml
r752 r756 78 78 <dependency> 79 79 <groupId>org.krakenapps</groupId> 80 <artifactId>kraken-confdb</artifactId> 81 <version>0.5.0</version> 82 </dependency> 83 <dependency> 84 <groupId>org.krakenapps</groupId> 80 85 <artifactId>kraken-msgbus</artifactId> 81 86 <version>1.4.0</version> -
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.
