2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
32 package mircoders.global;
36 import mir.producer.*;
39 import multex.Failure;
41 public class ProducerEngine {
42 // private Map producers;
43 private JobQueue producerJobQueue;
44 private Thread queueThread;
45 private PrintWriter log;
47 protected ProducerEngine() {
48 producerJobQueue = new JobQueue();
50 RandomAccessFile raFile = (new RandomAccessFile(MirGlobal.getConfigProperty("Home") + "/" + MirGlobal.getConfigProperty("Producer.Logfile"), "rw"));
51 raFile.seek(raFile.length());
52 log = new PrintWriter(new FileWriter( raFile.getFD()));
55 log = new PrintWriter(new NullWriter());
57 queueThread = new Thread(new ProducerJobQueueThread());
61 public void addJob(String aProducerFactory, String aVerb) {
62 producerJobQueue.appendJob(new ProducerJob(aProducerFactory, aVerb));
63 log.println(aProducerFactory+"."+aVerb+" added to queue");
67 public void addTask(ProducerTask aTask) {
68 addJob(aTask.getProducer(), aTask.getVerb());
71 public void addTasks(List aTasks) {
72 Iterator i = aTasks.iterator();
75 addTask((ProducerTask) i.next());
79 private String convertStatus(JobQueue.Job aJob) {
80 if (aJob.hasBeenProcessed())
82 if (aJob.isProcessing())
86 if (aJob.isCancelled())
88 if (aJob.hasBeenAborted())
94 private Map convertJob(JobQueue.Job aJob) {
95 Map result = new HashMap();
96 ProducerJob producerJob = (ProducerJob) aJob.getData();
98 result.put("identifier", aJob.getIdentifier());
99 result.put("factory", producerJob.getFactoryName());
100 result.put("verb", producerJob.getVerb());
101 result.put("priority", new Integer(aJob.getPriority()));
102 result.put("status", convertStatus(aJob));
103 result.put("lastchange", new DateToMapAdapter(aJob.getLastChange()));
108 private void convertJobList(List aSourceJobList, List aDestination) {
109 Iterator i = aSourceJobList.iterator();
112 aDestination.add(convertJob((JobQueue.Job) i.next()));
115 public List getQueueStatus() {
116 List result = new Vector();
117 List pendingJobs = new Vector();
118 List finishedJobs = new Vector();
120 producerJobQueue.makeJobListSnapshots(pendingJobs, finishedJobs);
122 convertJobList(pendingJobs, result);
123 convertJobList(finishedJobs, result);
128 private class ProducerJob {
129 private String factoryName;
131 private Producer producer;
133 public ProducerJob(String aFactory, String aVerb) {
134 factoryName = aFactory;
139 public String getFactoryName() {
143 public String getVerb() {
147 public void abort() {
148 if (producer!=null) {
153 public void execute() {
154 ProducerFactory factory;
157 Map startingMap = new HashMap();
159 startTime = System.currentTimeMillis();
160 log.println("Producing job: "+factoryName+"."+verb);
163 factory = MirGlobal.localizer().producers().getFactoryForName( factoryName );
166 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
168 synchronized(factory) {
169 producer = factory.makeProducer(verb, startingMap);
171 if (producer!=null) {
172 producer.produce(log);
176 catch (Throwable t) {
177 log.println(" exception "+t.getMessage());
178 t.printStackTrace(log);
180 log.println("Done producing job: "+factoryName+"."+verb);
181 endTime = System.currentTimeMillis();
182 log.println("Time: " + (endTime-startTime) + " ms");
186 boolean isAborted() {
191 private class ProducerJobQueueThread implements Runnable {
193 log.println("starting ProducerJobQueueThread");
197 ProducerJob job = (ProducerJob) producerJobQueue.acquirePendingJob();
201 producerJobQueue.jobAborted(job);
203 producerJobQueue.jobProcessed(job);
210 catch (InterruptedException e) {
217 public static class ProducerEngineExc extends Exc {
218 public ProducerEngineExc(String aMessage) {
223 public static class ProducerEngineRuntimeExc extends Failure {
224 public ProducerEngineRuntimeExc(String msg, Exception cause){
229 public static class ProducerTask {
230 private String producer;
233 public ProducerTask(String aProducer, String aVerb) {
234 producer = aProducer;
238 public String getVerb() {
242 public String getProducer() {
246 public static List parseProducerTaskList(String aList) throws ProducerEngineExc {
248 List result = new Vector();
250 i = StringRoutines.splitString(aList, ";").iterator();
251 while (i.hasNext()) {
252 String taskExpression = (String) i.next();
253 List parts = StringRoutines.splitString(taskExpression, ".");
256 throw new ProducerEngineExc("Invalid producer expression: '" + taskExpression + "'");
258 result.add(new ProducerEngine.ProducerTask((String) parts.get(0), (String) parts.get(1)));