001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: TaskManager.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.cms.task;
022:
023: import java.io.File;
024: import java.io.IOException;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: import org.apache.avalon.framework.configuration.Configurable;
029: import org.apache.avalon.framework.configuration.Configuration;
030: import org.apache.avalon.framework.configuration.ConfigurationException;
031: import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
032: import org.apache.avalon.framework.service.ServiceManager;
033: import org.apache.log4j.Logger;
034: import org.xml.sax.SAXException;
035:
036: /**
037: * The task manager
038: * @deprecated Use the usecase framework instead.
039: */
040: public class TaskManager implements Configurable {
041: private static Logger log = Logger.getLogger(TaskManager.class);
042: /**
043: * <code>TASK_ELEMENT</code> The task element
044: */
045: public static final String TASK_ELEMENT = "task";
046: /**
047: * <code>TASK_ID_ATTRIBUTE</code> The task id attribute
048: */
049: public static final String TASK_ID_ATTRIBUTE = "id";
050: /**
051: * <code>CONFIGURATION_FILE</code> The path to the configuration file
052: */
053: public static final String CONFIGURATION_FILE = File.separator
054: + "config" + File.separator + "tasks" + File.separator
055: + "tasks.xconf";
056:
057: /**
058: * <code>EMTPY_TASK</code> Constant for an empty task
059: */
060: public static final String EMTPY_TASK = "empty";
061: /**
062: * <code>ANT_TASK</code> Constant for an ant task
063: */
064: public static final String ANT_TASK = "ant";
065:
066: // maps task-ids to tasks
067: private Map tasks = new HashMap();
068:
069: private ServiceManager manager;
070:
071: /**
072: * Creates a new TaskManager object.
073: * @param manager The service manager to use.
074: */
075: public TaskManager(ServiceManager manager) {
076: this .manager = manager;
077: }
078:
079: /**
080: * Creates a new instance of TaskManager
081: * @param publicationPath path to publication
082: * @param manager The service manager to use.
083: * @throws ConfigurationException if the configuration failed.
084: * @throws SAXException when parsing the config file failed.
085: * @throws IOException when an I/O error occured.
086: */
087: public TaskManager(String publicationPath, ServiceManager manager)
088: throws ConfigurationException, SAXException, IOException {
089: this (manager);
090:
091: String configurationFilePath = publicationPath
092: + CONFIGURATION_FILE;
093: log.debug("Loading tasks: " + configurationFilePath);
094:
095: File configurationFile = new File(configurationFilePath);
096:
097: if (configurationFile.isFile()) {
098: DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
099: Configuration configuration = builder
100: .buildFromFile(configurationFile);
101: configure(configuration);
102:
103: } else {
104: log.info("Task configuration not loaded - file ["
105: + configurationFile.getAbsolutePath()
106: + "] does not exist.");
107: }
108:
109: Task empty = new EmptyTask();
110: empty.service(this .manager);
111: this .tasks.put(EMTPY_TASK, empty);
112:
113: Task ant = new AntTask();
114: ant.service(this .manager);
115: this .tasks.put(ANT_TASK, ant);
116: }
117:
118: /**
119: * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
120: */
121: public void configure(Configuration configuration)
122: throws ConfigurationException {
123: log.debug("Creating tasks:");
124:
125: // create task list
126: Configuration[] taskConfigurations = configuration
127: .getChildren(TASK_ELEMENT);
128:
129: // set task IDs
130: for (int i = 0; i < taskConfigurations.length; i++) {
131: String taskId = taskConfigurations[i]
132: .getAttribute(TASK_ID_ATTRIBUTE);
133: log.debug("Creating task '" + taskId + "'");
134:
135: Task task = TaskFactory.getInstance().createTask(
136: taskConfigurations[i]);
137: task.service(this .manager);
138: this .tasks.put(taskId, task);
139: }
140: }
141:
142: /**
143: * Return all task ids
144: * @return The task ids
145: */
146: public String[] getTaskIds() {
147: return (String[]) this .tasks.keySet().toArray(
148: new String[this .tasks.size()]);
149: }
150:
151: /**
152: * Get the task with a given task-id
153: * @param taskId the task-id of the requested task
154: * @return the task
155: * @throws ExecutionException if there is no task with the given task-id.
156: */
157: public Task getTask(String taskId) throws ExecutionException {
158: if (!this .tasks.containsKey(taskId)) {
159: throw new ExecutionException("Task with ID '" + taskId
160: + "' not found!");
161: }
162:
163: return (Task) this.tasks.get(taskId);
164: }
165: }
|