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 // producers = MirGlobal.localizer().producers().factories();
49 producerJobQueue = new JobQueue();
51 RandomAccessFile raFile = (new RandomAccessFile(MirGlobal.getConfigProperty("Home") + "/" + MirGlobal.getConfigProperty("Producer.Logfile"), "rw"));
52 raFile.seek(raFile.length());
53 log = new PrintWriter(new FileWriter( raFile.getFD()));
56 // throw new ProducerEngineRuntimeExc("Creating PrintWriter log failed",e);
57 log = new PrintWriter(new NullWriter());
59 queueThread = new Thread(new ProducerJobQueueThread());
63 public void addJob(String aProducerFactory, String aVerb) {
64 producerJobQueue.appendJob(new ProducerJob(aProducerFactory, aVerb));
65 log.println(aProducerFactory+"."+aVerb+" added to queue");
69 public void addTask(ProducerTask aTask) {
70 addJob(aTask.getProducer(), aTask.getVerb());
73 public void addTasks(List aTasks) {
74 Iterator i = aTasks.iterator();
77 addTask((ProducerTask) i.next());
81 public void printQueueStatus(PrintWriter aWriter) {
82 Iterator iterator = producerJobQueue.makeJobListSnapshot().iterator();
83 producerJobQueue.cleanupJobs();
86 while (iterator.hasNext()) {
87 job = (JobQueue.Job) iterator.next();
88 ProducerJob producerJob = (ProducerJob) job.getData();
90 aWriter.print(producerJob.factoryName + "." + producerJob.verb);
91 if (job.hasBeenProcessed())
92 aWriter.print(" processed");
93 else if (job.isProcessing())
94 aWriter.print(" processing");
95 else if (job.isPending())
96 aWriter.print(" pending");
98 aWriter.print(" unknown status");
100 aWriter.println("<br/>");
104 private void produceNow(String aProducerFactory, String aVerb, PrintWriter aLogger) {
108 Map startingMap = new HashMap();
110 startTime = System.currentTimeMillis();
112 aLogger.println("Producing (" + aProducerFactory + "," + aVerb + ")");
114 ProducerFactory factory = (ProducerFactory) MirGlobal.localizer().producers().factories().get(aProducerFactory);
116 if (factory == null )
117 throw new Exception("No producer factory '"+aProducerFactory+"' present.");
119 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
120 Producer producer = factory.makeProducer(aVerb, startingMap);
122 producer.produce(aLogger);
124 endTime = System.currentTimeMillis();
126 aLogger.println("Time: " + (endTime-startTime) + " ms<br>");
128 catch (Throwable e) {
130 aLogger.println("exception occurred:<br>");
131 aLogger.println(e.getMessage());
132 e.printStackTrace(aLogger);
134 catch (Throwable f) {
139 private class ProducerJob {
140 private String factoryName;
143 public ProducerJob(String aFactory, String aVerb) {
144 factoryName = aFactory;
148 public void execute() {
149 ProducerFactory factory;
153 Map startingMap = new HashMap();
155 startTime = System.currentTimeMillis();
156 log.println("Producing job: "+factoryName+"."+verb);
159 factory = (ProducerFactory) MirGlobal.localizer().producers().factories().get( factoryName );
162 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
164 synchronized(factory) {
165 producer = factory.makeProducer(verb, startingMap);
167 if (producer!=null) {
168 producer.produce(log);
172 catch (Throwable t) {
173 log.println(" exception "+t.getMessage());
174 t.printStackTrace(log);
176 log.println("Done producing job: "+factoryName+"."+verb);
177 endTime = System.currentTimeMillis();
179 log.println("Time: " + (endTime-startTime) + " ms");
184 private class ProducerJobQueueThread implements Runnable {
186 log.println("starting ProducerJobQueueThread");
190 ProducerJob job = (ProducerJob) producerJobQueue.acquirePendingJob();
193 producerJobQueue.flagOffJob(job);
200 catch (InterruptedException e) {
208 public static class ProducerEngineRuntimeExc extends Failure {
209 public ProducerEngineRuntimeExc(String msg, Exception cause){
214 public static class ProducerTask {
215 private String producer;
218 public ProducerTask(String aProducer, String aVerb) {
219 producer = aProducer;
223 public String getVerb() {
227 public String getProducer() {