KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > task > TaskManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: TaskManager.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.task;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
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 JavaDoc;
33
34 public class TaskManager implements Configurable {
35     private static Category log = Category.getInstance(TaskManager.class);
36     public static final String JavaDoc TASK_ELEMENT = "task";
37     public static final String JavaDoc TASK_ID_ATTRIBUTE = "id";
38     public static final String JavaDoc CONFIGURATION_FILE =
39         File.separator
40             + "config"
41             + File.separator
42             + "tasks"
43             + File.separator
44             + "tasks.xconf";
45
46     // maps task-ids to tasks
47
private Map JavaDoc tasks = new HashMap JavaDoc();
48
49     /**
50      * Creates a new TaskManager object.
51      */

52     public TaskManager() {
53     }
54
55     /**
56      * Creates a new instance of TaskManager
57      *
58      * @param publicationPath path to publication
59      * @throws ConfigurationException if the configuration failed.
60      * @throws SAXException when parsing the config file failed.
61      * @throws IOException when an I/O error occured.
62      */

63     public TaskManager(String JavaDoc publicationPath)
64         throws ConfigurationException, SAXException JavaDoc, IOException JavaDoc {
65         String JavaDoc configurationFilePath = publicationPath + CONFIGURATION_FILE;
66         log.debug("Loading tasks: " + configurationFilePath);
67
68         File JavaDoc configurationFile = new File JavaDoc(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 JavaDoc EMTPY_TASK = "empty";
88     public static final String JavaDoc ANT_TASK = "ant";
89
90     /**
91      * DOCUMENT ME!
92      *
93      * @param configuration DOCUMENT ME!
94      *
95      * @throws ConfigurationException DOCUMENT ME!
96      */

97     public void configure(Configuration configuration)
98         throws ConfigurationException {
99         log.debug("Creating tasks:");
100
101         // create task list
102
Configuration[] taskConfigurations =
103             configuration.getChildren(TASK_ELEMENT);
104
105         // set task IDs
106
for (int i = 0; i < taskConfigurations.length; i++) {
107             String JavaDoc 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     /**
118      * DOCUMENT ME!
119      *
120      * @return DOCUMENT ME!
121      */

122     public String JavaDoc[] getTaskIds() {
123         return (String JavaDoc[]) tasks.keySet().toArray(new String JavaDoc[tasks.size()]);
124     }
125
126     /**
127      * Get the task with a given task-id
128      *
129      * @param taskId the task-id of the requested task
130      *
131      * @return the task
132      *
133      * @throws ExecutionException if there is no task with the given task-id.
134      */

135     public Task getTask(String JavaDoc 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