Changeset 756:93286bd7acf0


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

removed Preferences dependency.

Files:
9 edited

Legend:

Unmodified
Added
Removed
  • kraken-core/src/main/java/org/krakenapps/main/Kraken.java

    r752 r756  
    6666import org.krakenapps.cron.impl.CronScriptFactory; 
    6767import org.krakenapps.cron.impl.CronServiceImpl; 
     68import org.krakenapps.cron.msgbus.CronPlugin; 
    6869import org.krakenapps.instrumentation.InstrumentationServiceImpl; 
    6970import org.krakenapps.keystore.KeyStoreScriptFactory; 
     
    281282         * @throws IOException 
    282283         */ 
    283         private void openConsolePort(BundleContext bc) throws IOException { 
     284        private void openConsolePort() throws IOException { 
    284285                InetAddress address = getConsoleBindAddress(); 
    285286                int port = getConsolePortNumber(); 
     
    288289                NioSocketAcceptor acceptor = new NioSocketAcceptor(); 
    289290                acceptor.getFilterChain().addLast("protocol", new ProtocolCodecFilter(new TelnetCodecFactory())); 
    290                 acceptor.setHandler(new TelnetHandler(bc)); 
     291                acceptor.setHandler(new TelnetHandler(context)); 
    291292                acceptor.setReuseAddress(true); 
    292293                acceptor.bind(bindSocketAddress); 
     
    349350                conf = new FileConfigService(); 
    350351                auth = new DefaultAuthService(context, conf); 
    351                 cron = new CronServiceImpl(context); 
    352  
    353                 registerScripts(context); 
     352                cron = new CronServiceImpl(context, conf); 
     353 
     354                registerScripts(); 
    354355                registerInstrumentation(); 
    355                 openConsolePort(context); 
     356                openConsolePort(); 
    356357                startSshServer(); 
    357358                logger.info("Kraken started."); 
     
    374375         *  
    375376         * @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"); 
    398395        } 
    399396 
     
    401398         * Register script factory to OSGi service registry. 
    402399         *  
    403          * @param context 
    404          *            the bundle context 
    405400         * @param scriptFactory 
    406401         *            the script factory 
     
    409404         *            command) 
    410405         */ 
    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); 
    414409                } catch (InstantiationException e) { 
    415410                        e.printStackTrace(); 
     
    419414        } 
    420415 
    421         private void registerScriptFactory(BundleContext context, ScriptFactory scriptFactory, String alias) { 
     416        private void registerScriptFactory(ScriptFactory scriptFactory, String alias) { 
    422417                Dictionary<String, Object> props = new Hashtable<String, Object>(); 
    423418                props.put("alias", alias); 
     
    427422        private void registerInstrumentation() { 
    428423                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); 
    429426        } 
    430427 
  • kraken-cron/pom.xml

    r752 r756  
    7878                <dependency> 
    7979                        <groupId>org.krakenapps</groupId> 
     80                        <artifactId>kraken-confdb</artifactId> 
     81                        <version>0.5.0</version> 
     82                </dependency> 
     83                <dependency> 
     84                        <groupId>org.krakenapps</groupId> 
    8085                        <artifactId>kraken-msgbus</artifactId> 
    8186                        <version>1.4.0</version> 
  • 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.