1 17 18 19 20 package org.apache.lenya.cms.task; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import org.apache.avalon.framework.configuration.Configurable; 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 31 import org.apache.log4j.Category; 32 import org.xml.sax.SAXException ; 33 34 public class TaskManager implements Configurable { 35 private static Category log = Category.getInstance(TaskManager.class); 36 public static final String TASK_ELEMENT = "task"; 37 public static final String TASK_ID_ATTRIBUTE = "id"; 38 public static final String CONFIGURATION_FILE = 39 File.separator 40 + "config" 41 + File.separator 42 + "tasks" 43 + File.separator 44 + "tasks.xconf"; 45 46 private Map tasks = new HashMap (); 48 49 52 public TaskManager() { 53 } 54 55 63 public TaskManager(String publicationPath) 64 throws ConfigurationException, SAXException , IOException { 65 String configurationFilePath = publicationPath + CONFIGURATION_FILE; 66 log.debug("Loading tasks: " + configurationFilePath); 67 68 File configurationFile = new File (configurationFilePath); 69 70 if (configurationFile.isFile()) { 71 DefaultConfigurationBuilder builder = 72 new DefaultConfigurationBuilder(); 73 Configuration configuration = 74 builder.buildFromFile(configurationFile); 75 configure(configuration); 76 77 } else { 78 log.info( 79 "Task configuration not loaded - file [" 80 + configurationFile.getAbsolutePath() 81 + "] does not exist."); 82 } 83 tasks.put(EMTPY_TASK, new EmptyTask()); 84 tasks.put(ANT_TASK, new AntTask()); 85 } 86 87 public static final String EMTPY_TASK = "empty"; 88 public static final String ANT_TASK = "ant"; 89 90 97 public void configure(Configuration configuration) 98 throws ConfigurationException { 99 log.debug("Creating tasks:"); 100 101 Configuration[] taskConfigurations = 103 configuration.getChildren(TASK_ELEMENT); 104 105 for (int i = 0; i < taskConfigurations.length; i++) { 107 String taskId = 108 taskConfigurations[i].getAttribute(TASK_ID_ATTRIBUTE); 109 log.debug("Creating task '" + taskId + "'"); 110 111 Task task = 112 TaskFactory.getInstance().createTask(taskConfigurations[i]); 113 tasks.put(taskId, task); 114 } 115 } 116 117 122 public String [] getTaskIds() { 123 return (String []) tasks.keySet().toArray(new String [tasks.size()]); 124 } 125 126 135 public Task getTask(String taskId) throws ExecutionException { 136 if (!tasks.containsKey(taskId)) { 137 throw new ExecutionException( 138 "Task with ID '" + taskId + "' not found!"); 139 } 140 141 return (Task) tasks.get(taskId); 142 } 143 } 144 | Popular Tags |