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;
34 import java.io.PrintWriter;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
39 import java.util.Vector;
41 import mir.log.LoggerToWriterAdapter;
42 import mir.log.LoggerWrapper;
43 import mir.producer.Producer;
44 import mir.producer.ProducerFactory;
45 import mir.util.DateToMapAdapter;
46 import mir.util.StringRoutines;
48 import multex.Failure;
50 public class ProducerEngine {
51 // private Map producers;
52 private JobQueue producerJobQueue;
53 private Thread queueThread;
54 private LoggerWrapper logger;
57 protected ProducerEngine() {
58 producerJobQueue = new JobQueue();
59 logger = new LoggerWrapper("Producer");
61 queueThread = new Thread(new ProducerJobQueueThread());
65 public void addJob(String aProducerFactory, String aVerb) {
66 producerJobQueue.appendJob(new ProducerJob(aProducerFactory, aVerb));
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 private String convertStatus(JobQueue.Job aJob) {
82 if (aJob.hasBeenProcessed())
84 if (aJob.isProcessing())
88 if (aJob.isCancelled())
90 if (aJob.hasBeenAborted())
96 private Map convertJob(JobQueue.Job aJob) {
97 Map result = new HashMap();
98 ProducerJob producerJob = (ProducerJob) aJob.getData();
100 result.put("identifier", aJob.getIdentifier());
101 result.put("factory", producerJob.getFactoryName());
102 result.put("verb", producerJob.getVerb());
103 result.put("priority", new Integer(aJob.getPriority()));
104 result.put("status", convertStatus(aJob));
105 result.put("lastchange", new DateToMapAdapter(aJob.getLastChange()));
110 private void convertJobList(List aSourceJobList, List aDestination) {
111 Iterator i = aSourceJobList.iterator();
114 aDestination.add(convertJob((JobQueue.Job) i.next()));
117 public List getQueueStatus() {
118 List result = new Vector();
119 List pendingJobs = new Vector();
120 List finishedJobs = new Vector();
122 producerJobQueue.makeJobListSnapshots(pendingJobs, finishedJobs);
124 convertJobList(pendingJobs, result);
125 convertJobList(finishedJobs, result);
130 private class ProducerJob {
131 private String factoryName;
133 private Producer producer;
135 public ProducerJob(String aFactory, String aVerb) {
136 factoryName = aFactory;
141 public String getFactoryName() {
145 public String getVerb() {
149 public void abort() {
150 if (producer!=null) {
155 public void execute() {
156 ProducerFactory factory;
159 Map startingMap = new HashMap();
161 startTime = System.currentTimeMillis();
162 logger.info("Producing job: "+factoryName+"."+verb);
165 factory = MirGlobal.localizer().producers().getFactoryForName( factoryName );
168 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
170 synchronized(factory) {
171 producer = factory.makeProducer(verb, startingMap);
173 if (producer!=null) {
174 producer.produce(logger);
178 catch (Throwable t) {
179 logger.error("Exception occurred while producing " + factoryName + "." + verb + t.getMessage());
180 t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(logger, LoggerWrapper.ERROR_MESSAGE)));
182 endTime = System.currentTimeMillis();
183 logger.info("Done producing job: " + factoryName + "." + verb + ", time elapsed:" + (endTime-startTime) + " ms");
186 boolean isAborted() {
191 private class ProducerJobQueueThread implements Runnable {
193 logger.debug("starting ProducerJobQueueThread");
196 ProducerJob job = (ProducerJob) producerJobQueue.acquirePendingJob();
200 producerJobQueue.jobAborted(job);
202 producerJobQueue.jobProcessed(job);
209 catch (InterruptedException e) {
216 public static class ProducerEngineExc extends Exc {
217 public ProducerEngineExc(String aMessage) {
222 public static class ProducerEngineRuntimeExc extends Failure {
223 public ProducerEngineRuntimeExc(String msg, Exception cause){
228 public static class ProducerTask {
229 private String producer;
232 public ProducerTask(String aProducer, String aVerb) {
233 producer = aProducer;
237 public String getVerb() {
241 public String getProducer() {
245 public static List parseProducerTaskList(String aList) throws ProducerEngineExc {
247 List result = new Vector();
249 i = StringRoutines.splitString(aList, ";").iterator();
250 while (i.hasNext()) {
251 String taskExpression = (String) i.next();
252 List parts = StringRoutines.splitString(taskExpression, ".");
255 throw new ProducerEngineExc("Invalid producer expression: '" + taskExpression + "'");
257 result.add(new ProducerEngine.ProducerTask((String) parts.get(0), (String) parts.get(1)));