1 20 package org.apache.slide.projector.engine; 21 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 27 import org.apache.slide.projector.Context; 28 import org.apache.slide.projector.SystemContext; 29 import org.apache.slide.projector.URI; 30 import org.apache.slide.projector.descriptor.ValueFactoryManager; 31 import org.apache.slide.projector.expression.Expression; 32 import org.apache.slide.projector.value.BooleanValue; 33 import org.apache.slide.projector.value.Value; 34 35 import de.zeigermann.xml.XMLStringWriter; 36 import de.zeigermann.xml.XMLWriter; 37 38 public class Job implements Runnable { 39 private final static Logger logger = Logger.getLogger(Job.class.getName()); 40 41 protected Expression initialCondition, remainingCondition; 42 protected Map parameters; 43 private URI processorUri; 44 private boolean repeat, persistent; 45 private String id; 46 47 public Job(String id, URI processorUri, Expression initalCondition, Expression remainingCondition, Map parameters, boolean repeat, boolean persistent) { 48 this.id = id; 49 this.initialCondition = initalCondition; 50 this.remainingCondition = remainingCondition; 51 this.parameters = parameters; 52 this.processorUri = processorUri; 53 this.repeat = repeat; 54 this.persistent = persistent; 55 } 56 57 public void run() { 58 try { 59 logger.log(Level.FINE, "Launching scheduled process '"+getProcessorUri()+"'"); 60 Context context = new SystemContext(); 61 Scheduler.setContext(context); 62 ProcessorManager.getInstance().process(getProcessorUri(), getParameters(), context); 63 } catch ( Exception e ) { 64 logger.log(Level.SEVERE, "Scheduled process '"+getProcessorUri()+"' failed", e); 65 } 66 } 67 68 public Expression getInitialCondition() { 69 return initialCondition; 70 } 71 72 public Expression getRemainingCondition() { 73 return remainingCondition; 74 } 75 76 public URI getProcessorUri() { 77 return processorUri; 78 } 79 80 public Map getParameters() { 81 return parameters; 82 } 83 84 public boolean repeat() { 85 return repeat; 86 } 87 88 public boolean isPersistent() { 89 return persistent; 90 } 91 92 public void restart() { 93 remainingCondition = initialCondition; 94 } 95 96 public void activate() { 97 remainingCondition = remainingCondition.activate(this); 98 } 99 100 public void save(XMLStringWriter writer) { 101 writer.writeStartTag(XMLWriter.createStartTag("job", new String [] { "id", "processor" }, new String [] { id, ProcessorManager.THREAD.toString() })); 102 writer.writeStartTag(XMLWriter.createStartTag("load", "parameter", "parameters")); 103 writer.writeStartTag(XMLWriter.createStartTag("map")); 104 for ( Iterator i = getParameters().entrySet().iterator(); i.hasNext(); ) { 105 Map.Entry entry = (Map.Entry )i.next(); 106 String key = (String )entry.getKey(); 107 writer.writeStartTag(XMLWriter.createStartTag("entry", new String [][] { {"key", key } })); 108 ValueFactoryManager.getInstance().saveValue((Value)entry.getValue(), writer); 109 writer.writeEndTag(XMLWriter.createEndTag("entry")); 110 } 111 writer.writeEndTag(XMLWriter.createEndTag("map")); 112 writer.writeEndTag(XMLWriter.createEndTag("load")); 113 writer.writeStartTag(XMLWriter.createStartTag("load", "parameter", "condition")); 114 writer.writeStartTag(XMLWriter.createStartTag("xml")); 115 getInitialCondition().save(writer); 116 writer.writeEndTag(XMLWriter.createEndTag("xml")); 117 writer.writeEndTag(XMLWriter.createEndTag("load")); 118 writer.writeStartTag(XMLWriter.createStartTag("load", "parameter", "remainingCondition")); 119 writer.writeStartTag(XMLWriter.createStartTag("xml")); 120 getRemainingCondition().save(writer); 121 writer.writeEndTag(XMLWriter.createEndTag("xml")); 122 writer.writeEndTag(XMLWriter.createEndTag("load")); 123 writer.writeStartTag(XMLWriter.createStartTag("load", "parameter", "repeat")); 124 ValueFactoryManager.getInstance().saveValue(new BooleanValue(repeat), writer); 125 writer.writeEndTag(XMLWriter.createEndTag("load")); 126 writer.writeStartTag(XMLWriter.createStartTag("load", "parameter", "persistent")); 127 ValueFactoryManager.getInstance().saveValue(new BooleanValue(persistent), writer); 128 writer.writeEndTag(XMLWriter.createEndTag("load")); 129 writer.writeStartTag(XMLWriter.createStartTag("load", "parameter", "processor")); 130 ValueFactoryManager.getInstance().saveValue(getProcessorUri(), writer); 131 writer.writeEndTag(XMLWriter.createEndTag("load")); 132 writer.writeEndTag(XMLWriter.createEndTag("job")); 133 } 134 135 public void launch() { 136 try { 137 Thread processThread = new Thread (this); 138 processThread.setDaemon(true); 139 processThread.start(); 140 } catch ( Exception exception ) { 141 logger.log(Level.SEVERE, "Exception occured while doing job: "+this, exception); 142 } 143 } 144 }
| Popular Tags
|