Hauptseite   Packages   Klassenhierarchie   ?bersicht   Auflistung der Dateien   Datenstruktur-Elemente  

Batch.java

gehe zur Dokumentation dieser Datei
00001 /*
00002  * $Source: /shared/cvsroot/diplom/app/src/java/de/picana/control/Batch.java,v $
00003  * $Author: mstolpe $
00004  * $Date: 2003/02/24 23:08:29 $
00005  * $Revision: 1.3 $
00006  * $Release$
00007  *
00008  * Created on 18. August 2002, 19:14
00009  *
00010  * Copyright 2002 by Marco Stolpe
00011  */
00012 
00013 package de.picana.control;
00014 
00015 import org.jdom.*;
00016 import org.jdom.input.SAXBuilder;
00017 import java.io.*;
00018 import java.util.*;
00019 
00020 
00027 public class Batch extends Task {
00028 
00029     private Random rand;
00030     private Hashtable taskinfo_hash;
00031     private Vector taskid_list;
00032     private Task act_task;
00033     
00034     
00036     public Batch() {
00037         LOGSRC = "control.Batch";
00038         taskinfo_hash = new Hashtable();
00039         taskid_list = new Vector();
00040         rand = new Random();
00041     }
00042 
00047     public void load(String filename) throws TaskException {
00048         try {
00049             File file = new File(filename);
00050             FileInputStream in = new FileInputStream(file);
00051             load(in);
00052 
00053         } catch (Exception e) {
00054             logger.error(LOGSRC, e.toString());
00055             throw new TaskException(e.toString());    
00056         }
00057     }
00058     
00063     public void load(InputStream in) throws TaskException {
00064         
00065         try {
00066             taskinfo_hash = new Hashtable();
00067             taskid_list = new Vector();
00068             
00069             SAXBuilder builder = new SAXBuilder(false);
00070             Document doc = builder.build(in);
00071             Element root = doc.getRootElement();
00072             
00073             List tasks = root.getChildren("task");
00074             
00075             Iterator i = tasks.iterator();
00076             while (i.hasNext()) {
00077                 Element currentTask = (Element)i.next();
00078                 
00079                 String taskid = currentTask.getAttributeValue("id");
00080                 if (taskid == null) {
00081                     String newid = String.valueOf(rand.nextInt());
00082                     while (taskinfo_hash.containsKey(newid))
00083                         newid = String.valueOf(rand.nextInt());
00084                     taskid = newid;
00085                 }
00086                 String taskname = currentTask.getAttributeValue("name");
00087                 String classname = currentTask.getAttributeValue("class");
00088                 ParameterSet set = constructParameterSet(currentTask);
00089                 
00090                 TaskInfo ti = new TaskInfo(taskid, taskname, classname, set);
00091                 addTaskInfo(ti);
00092             }
00093             
00094         } catch (Exception e) {
00095             logger.error(LOGSRC, e.toString());
00096             throw new TaskException(e.toString());
00097         }
00098     }
00099     
00100     private ParameterSet constructParameterSet(Element setElem) {
00101         
00102         ParameterSet set = new ParameterSet();
00103         
00104         set.setName(setElem.getAttributeValue("name"));
00105         
00106         List parameterElements = setElem.getChildren("parameter");
00107             
00108         // Add parameters to this ParameterSet
00109             
00110         Iterator i = parameterElements.iterator();
00111         while (i.hasNext()) {
00112             Element currentParameter = (Element)i.next();
00113                 
00114             String paramName = currentParameter.getAttributeValue("name");
00115                 
00116             if (currentParameter.getAttributeValue("value") != null) {
00117                     
00118                 // Add a single parameter value to ParameterSet
00119                     
00120                 set.setParameter(paramName,
00121                     currentParameter.getAttributeValue("value"));
00122                 
00123             } else {
00124                     
00125                 // Add list of parameter values to ParameterSet
00126                 
00127                 Element elem = currentParameter.getChild("parameter");
00128                 
00129                 if (elem.getAttributeValue("name") != null) {
00130                     
00131                     // construct Hashtable
00132                     
00133                     Hashtable valueHash = new Hashtable();
00134                     
00135                     List valueElements = currentParameter.getChildren("parameter");
00136                     
00137                     Iterator j = valueElements.iterator();
00138                     while (j.hasNext()) {
00139                         Element currentParam = (Element)j.next();
00140                         valueHash.put(currentParam.getAttributeValue("name"),
00141                             currentParam.getAttributeValue("value"));
00142                     }
00143                     
00144                     set.setParameter(paramName, valueHash);
00145                     
00146                 } else {
00147                 
00148                     // construct Vector
00149                     
00150                     Vector valueList = new Vector();
00151                     
00152                     List valueElements = currentParameter.getChildren("parameter");
00153                     
00154                     Iterator j = valueElements.iterator();
00155                     while (j.hasNext()) {
00156                         Element currentValue = (Element)j.next();
00157                         valueList.addElement(
00158                             currentValue.getAttributeValue("value"));
00159                     }
00160                     
00161                     set.setParameter(paramName, valueList);
00162                 }
00163             }
00164         }
00165             
00166         // Add parameter sets to this ParameterSet
00167            
00168         List paramSetElements = setElem.getChildren("parameter-set");
00169          
00170         i = paramSetElements.iterator();
00171         while (i.hasNext()) {
00172             Element currentParameterSet = (Element)i.next();
00173                 
00174             ParameterSet cset = constructParameterSet(currentParameterSet);
00175             set.setParameter(cset.getName(), cset);
00176         }
00177             
00178         return set;
00179     }
00180     
00181     private void addTaskInfo(TaskInfo ti) {
00182         taskinfo_hash.put(ti.getId(), ti);
00183         taskid_list.addElement(ti.getId());
00184     }
00185     
00186     private TaskInfo getTaskInfo(String taskid) {
00187         return (TaskInfo)taskinfo_hash.get(taskid);
00188     }
00189     
00190     private Enumeration getTaskIds() {
00191         return taskid_list.elements();    
00192     }
00193     
00194     private Task getTask(String taskid) throws TaskException {
00195         Task task = null;
00196         try {
00197             TaskInfo ti = getTaskInfo(taskid);
00198             task = (Task)ti.getJavaClass().newInstance();
00199             task.init(ti.getParameterSet(), logger);
00200             
00201         } catch (Exception e) {
00202             throw new TaskException(e.toString());    
00203         }
00204         return task;
00205     }
00206     
00208     public void start() throws TaskException {
00209         
00210         try {
00211             logger.info(LOGSRC, "Started.");
00212             
00213             Enumeration taskids = getTaskIds();
00214             while (taskids.hasMoreElements()) {
00215                 String taskid = (String)taskids.nextElement();
00216                 act_task = getTask(taskid);
00217                 act_task.start();
00218             }
00219             
00220             logger.info(LOGSRC, "Stopped.");
00221             
00222         } catch (Exception e) {
00223             throw new TaskException(e.toString());    
00224         }
00225     }
00226     
00228     public void stop() {
00229         act_task.stop();
00230     }
00231     
00233     public void pause() {
00234         act_task.pause();
00235     }
00236     
00238     public void resume() {
00239         act_task.resume();
00240     }
00241 }

Erzeugt am Tue Apr 22 11:22:55 2003 f?r Picana von doxygen1.2.18