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 // ML: TODO: should check if a similar job is already pending
65 producerJobQueue.appendJob(new ProducerJob(aProducerFactory, aVerb));
66 log.println(aProducerFactory+"."+aVerb+" added to queue");
70 public void printQueueStatus(PrintWriter aWriter) {
71 Iterator iterator = producerJobQueue.makeJobListSnapshot().iterator();
72 producerJobQueue.cleanupJobs();
75 while (iterator.hasNext()) {
76 job = (JobQueue.Job) iterator.next();
77 ProducerJob producerJob = (ProducerJob) job.getData();
79 aWriter.print(producerJob.factoryName + "." + producerJob.verb);
80 if (job.hasBeenProcessed())
81 aWriter.print(" processed");
82 else if (job.isProcessing())
83 aWriter.print(" processing");
84 else if (job.isPending())
85 aWriter.print(" pending");
87 aWriter.print(" unknown status");
89 aWriter.println("<br/>");
93 private void produceNow(String aProducerFactory, String aVerb, PrintWriter aLogger) {
97 Map startingMap = new HashMap();
99 startTime = System.currentTimeMillis();
101 aLogger.println("Producing (" + aProducerFactory + "," + aVerb + ")");
103 ProducerFactory factory = (ProducerFactory) MirGlobal.localizer().producers().factories().get(aProducerFactory);
105 if (factory == null )
106 throw new Exception("No producer factory '"+aProducerFactory+"' present.");
108 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
109 Producer producer = factory.makeProducer(aVerb, startingMap);
111 producer.produce(aLogger);
113 endTime = System.currentTimeMillis();
115 aLogger.println("Time: " + (endTime-startTime) + " ms<br>");
117 catch (Throwable e) {
119 aLogger.println("exception occurred:<br>");
120 aLogger.println(e.getMessage());
121 e.printStackTrace(aLogger);
123 catch (Throwable f) {
128 private class ProducerJob {
129 private String factoryName;
132 public ProducerJob(String aFactory, String aVerb) {
133 factoryName = aFactory;
137 public void execute() {
138 ProducerFactory factory;
142 Map startingMap = new HashMap();
144 startTime = System.currentTimeMillis();
145 log.println("Producing job: "+factoryName+"."+verb);
148 factory = (ProducerFactory) MirGlobal.localizer().producers().factories().get( factoryName );
151 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
153 synchronized(factory) {
154 producer = factory.makeProducer(verb, startingMap);
156 if (producer!=null) {
157 producer.produce(log);
161 catch (Throwable t) {
162 log.println(" exception "+t.getMessage());
163 t.printStackTrace(log);
165 log.println("Done producing job: "+factoryName+"."+verb);
166 endTime = System.currentTimeMillis();
168 log.println("Time: " + (endTime-startTime) + " ms");
173 private class ProducerJobQueueThread implements Runnable {
175 log.println("starting ProducerJobQueueThread");
179 ProducerJob job = (ProducerJob) producerJobQueue.acquirePendingJob();
182 producerJobQueue.flagOffJob(job);
189 catch (InterruptedException e) {
197 public static class ProducerEngineRuntimeExc extends Failure {
198 public ProducerEngineRuntimeExc(String msg, Exception cause){