use of org.onebusaway.transit_data_federation.bundle.model.TaskDefinition in project onebusaway-application-modules by camsys.
the class FederatedTransitDataBundleCreator method getTaskDefinitionsByName.
private Map<String, TaskDefinition> getTaskDefinitionsByName(Map<String, TaskDefinition> taskDefinitions) {
Map<String, TaskDefinition> taskDefinitionsByTaskName = new HashMap<String, TaskDefinition>();
for (TaskDefinition taskDefinition : taskDefinitions.values()) {
String taskName = taskDefinition.getTaskName();
if (taskName == null || taskName.trim().length() == 0) {
throw new IllegalStateException("no taskName property defined for task definition: " + taskDefinition);
}
taskDefinitionsByTaskName.put(taskName, taskDefinition);
}
return taskDefinitionsByTaskName;
}
use of org.onebusaway.transit_data_federation.bundle.model.TaskDefinition in project onebusaway-application-modules by camsys.
the class FederatedTransitDataBundleCreator method getTaskList.
private List<TaskDefinition> getTaskList(ApplicationContext context) throws UnknownTaskException {
Map<String, TaskDefinition> taskDefinitions = context.getBeansOfType(TaskDefinition.class);
Map<String, TaskDefinition> taskDefinitionsByTaskName = getTaskDefinitionsByName(taskDefinitions);
DirectedGraph<String> graph = new DirectedGraph<String>();
for (TaskDefinition taskDefinition : taskDefinitions.values()) {
String taskName = taskDefinition.getTaskName();
graph.addNode(taskName);
String before = taskDefinition.getBeforeTaskName();
if (before != null) {
if (!taskDefinitionsByTaskName.containsKey(before))
throw new UnknownTaskException(before);
graph.addEdge(taskName, before);
}
List<String> afters = taskDefinition.getAfterTaskNames();
if (!CollectionsLibrary.isEmpty(afters)) {
for (String after : afters) {
if (!taskDefinitionsByTaskName.containsKey(after))
throw new UnknownTaskException(after);
graph.addEdge(after, taskName);
}
}
}
List<String> taskNames = graph.getTopologicalSort(null);
List<TaskDefinition> taskDefinitionsInOrder = new ArrayList<TaskDefinition>();
for (String taskName : taskNames) {
TaskDefinition task = taskDefinitionsByTaskName.get(taskName);
taskDefinitionsInOrder.add(task);
}
return taskDefinitionsInOrder;
}
use of org.onebusaway.transit_data_federation.bundle.model.TaskDefinition in project onebusaway-application-modules by camsys.
the class FederatedTransitDataBundleCreator method run.
/**
* Build the bundle!
*
* @throws IOException
* @throws ClassNotFoundException
* @throws UnknownTaskException
*/
public void run() throws IOException, ClassNotFoundException, UnknownTaskException {
_outputPath.mkdirs();
setSystemProperties();
ConfigurableApplicationContext context = _context;
boolean closeContextOnCompletion = false;
if (context == null) {
List<String> contextPaths = getPrimaryApplicatonContextPaths();
Map<String, BeanDefinition> contextBeans = getPrimaryBeanDefintions();
context = ContainerLibrary.createContext(contextPaths, contextBeans);
closeContextOnCompletion = true;
}
List<TaskDefinition> taskDefinitions = getTaskList(context);
Set<String> taskNames = getReducedTaskList(taskDefinitions);
// Clear cache files
FederatedTransitDataBundle bundle = context.getBean(FederatedTransitDataBundle.class);
clearExistingCacheFiles(bundle);
int taskSize = taskNames.size();
int i = 0;
for (TaskDefinition def : taskDefinitions) {
String taskName = def.getTaskName();
if (taskNames.contains(taskName)) {
i++;
System.out.println("== " + taskName + " =====>");
_status.addMessage("running task " + taskName + " (" + i + "/" + taskSize + ")");
Runnable task = getTask(context, def.getTask(), def.getTaskBeanName());
if (task == null)
throw new IllegalStateException("unknown task bean with name: " + taskName);
task.run();
} else {
Runnable task = getTask(context, def.getTaskWhenSkipped(), def.getTaskWhenSkippedBeanName());
if (task != null) {
System.out.println("== skipping " + taskName + " =====>");
_status.addMessage("skipping task " + taskName);
task.run();
}
}
}
// We don't need this context anymore
if (closeContextOnCompletion) {
context.stop();
context.close();
context = null;
}
}
use of org.onebusaway.transit_data_federation.bundle.model.TaskDefinition in project onebusaway-application-modules by camsys.
the class FederatedTransitDataBundleCreator method getReducedTaskList.
private Set<String> getReducedTaskList(List<TaskDefinition> taskDefinitions) throws UnknownTaskException {
// Check task names first
List<String> tasks = new ArrayList<String>();
for (TaskDefinition taskDef : taskDefinitions) {
if (!isTaskEnabled(taskDef))
continue;
String taskName = taskDef.getTaskName();
tasks.add(taskName);
}
for (String task : _onlyTasks) {
if (!tasks.contains(task))
throw new UnknownTaskException(task);
}
for (String task : _skipTasks) {
if (!tasks.contains(task))
throw new UnknownTaskException(task);
}
if (_skipToTask != null) {
int index = tasks.indexOf(_skipToTask);
if (index == -1)
throw new UnknownTaskException(_skipToTask);
for (int i = 0; i < index; i++) tasks.remove(0);
}
if (!_onlyTasks.isEmpty())
tasks.retainAll(_onlyTasks);
tasks.removeAll(_skipTasks);
return new HashSet<String>(tasks);
}
Aggregations