package vicazh.hyperpool.stream.net.http.html;
import java.util.*;
import vicazh.hyperpool.*;
import vicazh.hyperpool.stream.Connection;
/**
* The task service
*
* @author Victor Zhigunov
* @version 0.4.11
*/
public class TaskService extends Service implements TaskServiceMBean {
public TaskService() {
}
Reader reader;
Writer writer;
String cache;
String base;
/**
* @param reader
* base source transformer
* @param writer
* base result transformer
* @param cache
* cache folder name
* @param base
* base folder name
*/
public TaskService(Reader reader, Writer writer, String cache, String base) {
this.reader = reader;
this.writer = writer;
this.cache = cache;
this.base = base;
}
public Connection getConnection() {
return new Connection(this);
}
private List<Task> tasks = new ArrayList<Task>();
public List<Task> getTasks() {
return tasks;
}
public void setTasks(List<Task> tasks) {
this.tasks = tasks;
}
public Task getTask() {
return new Task(this);
}
public void start() throws Exception {
super.start();
for (Task t : tasks)
if (t.isAutorun())
t.go();
}
public void stop() throws Exception {
for (Task t : tasks)
t.close();
super.stop();
}
public void setAttribute(String name, Object value) throws Exception {
if (name.equals(TaskServiceMBean.BREAK))
tasks.get(tasks.indexOf(value)).close();
else if (name.equals(TaskServiceMBean.STOP))
tasks.get(tasks.indexOf(value)).cancel();
else if (name.equals(TaskServiceMBean.EDIT)) {
Task t = tasks.get(tasks.indexOf(value));
t.set((TaskItem) value);
t.path2dir(((TaskItem) value).getDir());
} else if (name.equals(TaskServiceMBean.REMOVE)) {
Task t = tasks.get(tasks.indexOf(value));
t.close();
tasks.remove(t);
} else if (name.equals(TaskServiceMBean.GO))
tasks.get(tasks.indexOf(value)).go();
else if (name.equals(TaskServiceMBean.ADD)) {
Task t = getTask();
t.set((TaskItem) value);
t.path2dir(((TaskItem) value).getDir());
tasks.add(t);
}
}
public List<TaskItem> getItems() {
List<TaskItem> list = new ArrayList<TaskItem>();
for (Task t : tasks)
list.add(new TaskItem(t));
return list;
}
}