Author: echatellier Date: 2012-05-18 19:01:18 +0200 (Fri, 18 May 2012) New Revision: 2867 Url: http://chorem.org/repositories/revision/jtimer/2867 Log: fixes #551 : Autostart task from command line Added: trunk/src/main/java/org/chorem/jtimer/JTimerActions.java trunk/src/main/resources/jtimer.properties Modified: trunk/src/main/java/org/chorem/jtimer/JTimer.java trunk/src/main/java/org/chorem/jtimer/JTimerConfig.java trunk/src/main/java/org/chorem/jtimer/data/TimerDataManager.java trunk/src/test/java/org/chorem/jtimer/data/TimerDataManagerTest.java Modified: trunk/src/main/java/org/chorem/jtimer/JTimer.java =================================================================== --- trunk/src/main/java/org/chorem/jtimer/JTimer.java 2012-05-16 14:50:43 UTC (rev 2866) +++ trunk/src/main/java/org/chorem/jtimer/JTimer.java 2012-05-18 17:01:18 UTC (rev 2867) @@ -158,6 +158,11 @@ log.info("Starting " + JTimer.class.getSimpleName() + " at " + new Date()); } + + // load configuration and run actions + loadConfiguration(args); + config.doAction(JTimerConfig.STEP_BEFORE_UI); + launch(JTimer.class, args); } @@ -173,9 +178,6 @@ // super, but does nothing super.initialize(args); - // load configuration - loadConfiguration(args); - // init resources map ApplicationContext ctxt = getContext(); resourceMap = ctxt.getResourceMap(); @@ -221,7 +223,7 @@ * * @param args args to parse command line options */ - protected void loadConfiguration(String[] args) { + protected static void loadConfiguration(String[] args) { config = new JTimerConfig(); @@ -496,6 +498,10 @@ // install icon (do it at last action) systrayManager.install(); + + // run action after ui + config.putObject(this); + config.doAction(JTimerConfig.STEP_AFTER_UI); } else { String failTitle = resourceMap.getString("startFail.title"); String failMessage = resourceMap.getString("startFail.message"); @@ -699,6 +705,24 @@ } /** + * Start task pointed by taskPath. + * + * @param taskPath task path to start (from root to task) + */ + public void startTask(String taskPath) { + TimerTask task = core.getData().getTaskForPath(taskPath); + if (task != null) { + RunTaskJob jobToRun = new RunTaskJob(this, task, core.getData()); + getContext().getTaskService().execute(jobToRun); + core.getData().startTask(task); + } else { + if (log.isWarnEnabled()) { + log.warn("Can't find task '" + taskPath + "'"); + } + } + } + + /** * Called by task job manager when task as been started. * * @param task started task Added: trunk/src/main/java/org/chorem/jtimer/JTimerActions.java =================================================================== --- trunk/src/main/java/org/chorem/jtimer/JTimerActions.java (rev 0) +++ trunk/src/main/java/org/chorem/jtimer/JTimerActions.java 2012-05-18 17:01:18 UTC (rev 2867) @@ -0,0 +1,81 @@ +/* + * #%L + * jTimer + * + * $Id: JTimer.java 2848 2012-03-28 16:37:31Z echatellier $ + * $HeadURL$ + * %% + * Copyright (C) 2012 CodeLutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +package org.chorem.jtimer; + +import org.chorem.jtimer.JTimerConfig.JTimerAction; +import org.nuiton.util.ApplicationConfig; +import org.nuiton.util.ApplicationConfig.Action.Step; + +/** + * JTimer actions class run from command line. + * + * @author chatellier + * @version $Revision: 2825 $ + * + * Last update : $Date: 2012-03-15 11:41:39 +0100 (jeu. 15 mars 2012) $ + * By : $Author: echatellier $ + */ +public class JTimerActions { + + protected ApplicationConfig config; + + public JTimerActions(ApplicationConfig config) { + this.config = config; + } + + @Step(JTimerConfig.STEP_BEFORE_UI) + public void help() { + displayVersion(); + System.out.println(); + for (JTimerAction a : JTimerAction.values()) { + for (int index = 0 ; index < a.aliases.length ; index++) { + System.out.print(a.aliases[index]); + if (index != a.aliases.length - 1) { + System.out.print(", "); + } + } + System.out.println("\t" + a.description); + } + System.exit(0); + } + + @Step(JTimerConfig.STEP_BEFORE_UI) + public void version() { + displayVersion(); + System.exit(0); + } + + protected void displayVersion() { + System.out.println("jTimer " + config.getOption("application.version")); + System.out.println(config.getOption("application.website")); + } + + @Step(JTimerConfig.STEP_AFTER_UI) + public void start(String taskPath) { + JTimer ui = config.getObject(JTimer.class); + ui.startTask(taskPath); + } +} Modified: trunk/src/main/java/org/chorem/jtimer/JTimerConfig.java =================================================================== --- trunk/src/main/java/org/chorem/jtimer/JTimerConfig.java 2012-05-16 14:50:43 UTC (rev 2866) +++ trunk/src/main/java/org/chorem/jtimer/JTimerConfig.java 2012-05-18 17:01:18 UTC (rev 2867) @@ -39,8 +39,13 @@ */ public class JTimerConfig { - private static Log log = LogFactory.getLog(JTimerConfig.class); + private static final Log log = LogFactory.getLog(JTimerConfig.class); + /** To tag action to be run before ui start. */ + protected static final int STEP_BEFORE_UI = 0; + /** To tag action to be run after ui start. */ + protected static final int STEP_AFTER_UI = 1; + protected ApplicationConfig appConfig; public JTimerConfig() { @@ -50,6 +55,11 @@ appConfig.setDefaultOption(o.key, o.defaultValue); } } + for (JTimerAction a : JTimerAction.values()) { + for (String alias : a.aliases) { + appConfig.addActionAlias(alias, a.action); + } + } } public void parse(String... args) { @@ -62,6 +72,30 @@ } } + /** + * Put new custom object into app config context. + * + * @param o object value + */ + public void putObject(Object o) { + appConfig.putObject(o); + } + + /** + * Run command line actions for step. + * + * @param step action step + */ + public void doAction(int step) { + try { + appConfig.doAction(step); + } catch (Exception ex) { + if (log.isErrorEnabled()) { + log.error("Can't run command line actions", ex); + } + } + } + public Class getServiceClass() { Class serviceClass = null; try { @@ -166,7 +200,7 @@ appConfig.saveForUser(); } - public enum JTimerOption { + protected enum JTimerOption { CONFIG_FILENAME(ApplicationConfig.CONFIG_FILE_NAME, "jtimer.properties"), SERVICE_CLASS("jtimer.service.class", null), SERVICE_ENDPOINT("jtimer.service.endpoint", null), @@ -180,7 +214,6 @@ UI_REPORT_FIRSTDAYOFWEEK("jtimer.ui.report.firstdayofweek", "0"); protected String key; - protected String defaultValue; private JTimerOption(String key, String defaultValue) { @@ -188,4 +221,20 @@ this.defaultValue = defaultValue; } } + + protected enum JTimerAction { + HELP("Show help", JTimerActions.class.getName() + "#help", "--help", "-h"), + VERSION("Display application version", JTimerActions.class.getName() + "#version", "--version", "-v"), + START("Start task", JTimerActions.class.getName() + "#start", "--start", "-s"); + + protected String description; + protected String action; + protected String[] aliases; + + private JTimerAction(String description, String action, String... aliases) { + this.description = description; + this.action = action; + this.aliases = aliases; + } + } } Modified: trunk/src/main/java/org/chorem/jtimer/data/TimerDataManager.java =================================================================== --- trunk/src/main/java/org/chorem/jtimer/data/TimerDataManager.java 2012-05-16 14:50:43 UTC (rev 2866) +++ trunk/src/main/java/org/chorem/jtimer/data/TimerDataManager.java 2012-05-18 17:01:18 UTC (rev 2867) @@ -34,11 +34,13 @@ import java.util.List; import java.util.Map.Entry; +import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.jtimer.entities.TimerAlert; import org.chorem.jtimer.entities.TimerProject; import org.chorem.jtimer.entities.TimerTask; +import org.nuiton.util.ArrayUtil; /** * Gere les donnees. Des objets peuvent s'enregistrer pour etre notifies des @@ -561,4 +563,47 @@ } } + + /** + * Find corresponding task for path. + * + * @param taskPath + * @return + */ + public TimerTask getTaskForPath(String taskPath) { + + String[] paths = taskPath.split("/"); + + // make sure can't select only project + if (paths == null || paths.length <= 1) { + throw new IllegalArgumentException("Can't parse argument '" + taskPath + "' as task"); + } + + return findTask(projectList, paths); + } + + /** + * Find task in tasks collections. + * + * @param tasks task collection + * @param paths task path to find + * @return found task + */ + protected TimerTask findTask(Collection<? extends TimerTask> tasks, String[] paths) { + String current = paths[0]; + + TimerTask foundTask = null; + for (TimerTask subtask : tasks) { + if (subtask.getName().equals(current)) { + foundTask = subtask; + } + } + + if (foundTask != null && paths.length > 1) { + String[] subPaths = ArrayUtils.subarray(paths, 1, paths.length); + foundTask = findTask(foundTask.getSubTasks(), subPaths); + } + + return foundTask; + } } Added: trunk/src/main/resources/jtimer.properties =================================================================== --- trunk/src/main/resources/jtimer.properties (rev 0) +++ trunk/src/main/resources/jtimer.properties 2012-05-18 17:01:18 UTC (rev 2867) @@ -0,0 +1,2 @@ +application.version=${project.version} +application.website=${project.url} \ No newline at end of file Modified: trunk/src/test/java/org/chorem/jtimer/data/TimerDataManagerTest.java =================================================================== --- trunk/src/test/java/org/chorem/jtimer/data/TimerDataManagerTest.java 2012-05-16 14:50:43 UTC (rev 2866) +++ trunk/src/test/java/org/chorem/jtimer/data/TimerDataManagerTest.java 2012-05-18 17:01:18 UTC (rev 2867) @@ -605,4 +605,35 @@ Assert.assertNull(task3a); Assert.assertEquals(getRecursiveAlertsCount(Collections.singleton(task2)), 4); } + + /** + * Test to find task by path. + */ + @Test + public void testGetTaskForPath() { + TimerCore core = new TimerCore(); + TimerDataManager dataManager = core.getData(); + + //core.init(); + core.load(); + + TimerTask task = dataManager.getTaskForPath("jTimer/Unit tests/UI tests"); + Assert.assertEquals(task.getName(), "UI tests"); + task = dataManager.getTaskForPath("jTimer/Unit tests/fake"); + Assert.assertNull(task); + } + + /** + * Test to find task by path. + */ + @Test(expectedExceptions=IllegalArgumentException.class) + public void testGetTaskForPathProject() { + TimerCore core = new TimerCore(); + TimerDataManager dataManager = core.getData(); + + //core.init(); + core.load(); + + TimerTask task = dataManager.getTaskForPath("jTimer"); + } }