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 any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
30 package mircoders.global;
32 import java.io.PrintWriter;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.Vector;
39 import mir.config.MirPropertiesConfiguration;
40 import mir.log.LoggerToWriterAdapter;
41 import mir.log.LoggerWrapper;
42 import mir.producer.Producer;
43 import mir.producer.ProducerFactory;
44 import mir.util.GeneratorFormatAdapters;
45 import mir.util.StringRoutines;
47 import multex.Failure;
48 import mircoders.localizer.MirLocalizerExc;
50 public class ProducerEngine {
51 private JobQueue producerJobQueue;
52 private LoggerWrapper logger;
55 protected ProducerEngine() {
56 logger = new LoggerWrapper("Producer");
57 producerJobQueue = new JobQueue(new LoggerWrapper("Producer.Queue"));
60 public void addJob(String aProducerFactory, String aVerb) throws ProducerEngineExc, ProducerEngineFailure {
61 ProducerFactory factory;
64 factory = MirGlobal.localizer().producers().getFactoryForName( aProducerFactory );
66 catch (MirLocalizerExc e) {
67 throw new ProducerEngineFailure(e);
71 throw new ProducerEngineExc("Unknown producer: " + aProducerFactory);
73 if (!factory.allowVerb(aVerb))
74 throw new ProducerEngineExc("illegal producer/verb combination: " + aProducerFactory+"::"+aVerb);
76 producerJobQueue.appendJob(
77 new ProducerJob(aProducerFactory, aVerb), aProducerFactory+"."+aVerb);
80 public void cancelJobs(List aJobs) {
81 producerJobQueue.cancelJobs(aJobs);
84 public void cancelAllJobs() {
85 producerJobQueue.cancelAllJobs();
88 public void addTask(ProducerTask aTask) throws ProducerEngineExc, ProducerEngineFailure {
89 addJob(aTask.getProducer(), aTask.getVerb());
92 private String convertStatus(JobQueue.JobInfo aJob) {
93 switch (aJob.getStatus()) {
94 case JobQueue.STATUS_ABORTED:
96 case JobQueue.STATUS_CANCELLED:
98 case JobQueue.STATUS_CREATED:
100 case JobQueue.STATUS_PENDING:
102 case JobQueue.STATUS_PROCESSED:
104 case JobQueue.STATUS_PROCESSING:
110 private Map convertJob(JobQueue.JobInfo aJob) {
112 Map result = new HashMap();
113 result.put("identifier", aJob.getIdentifier());
114 result.put("description", aJob.getDescription());
115 result.put("priority", new Integer(aJob.getPriority()));
116 result.put("runningtime", new Double( (double) aJob.getRunningTime() / 1000));
117 result.put("status", convertStatus(aJob));
118 result.put("lastchange", new GeneratorFormatAdapters.DateFormatAdapter(aJob.getLastChange(), MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone")));
119 result.put("finished", new Boolean(
120 aJob.getStatus() == JobQueue.STATUS_PROCESSED ||
121 aJob.getStatus() == JobQueue.STATUS_CANCELLED ||
122 aJob.getStatus() == JobQueue.STATUS_ABORTED));
126 catch (Throwable t) {
127 throw new RuntimeException(t.toString());
131 private List convertJobInfoList(List aJobInfoList) {
132 List result = new Vector();
134 Iterator i = aJobInfoList.iterator();
137 result.add(convertJob((JobQueue.JobInfo) i.next()));
142 public List getQueueStatus() {
143 return convertJobInfoList(producerJobQueue.getJobsInfo());
146 private class ProducerJob implements JobQueue.Job {
147 private String factoryName;
149 private Producer producer;
151 public ProducerJob(String aFactory, String aVerb) {
152 factoryName = aFactory;
157 public String getFactoryName() {
161 public String getVerb() {
165 public void abort() {
166 if (producer!=null) {
171 public boolean run() {
172 ProducerFactory factory;
175 boolean result = false;
176 Map startingMap = new HashMap();
177 Map mirMap = new HashMap();
178 mirMap.put("producer", factoryName);
179 mirMap.put("verb", verb);
181 startingMap.put("Mir", mirMap);
183 startTime = System.currentTimeMillis();
184 logger.info("Producing job: "+factoryName+"."+verb);
187 factory = MirGlobal.localizer().producers().getFactoryForName( factoryName );
190 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
192 synchronized(factory) {
193 producer = factory.makeProducer(verb, startingMap);
195 if (producer!=null) {
196 result = producer.produce(logger);
200 catch (Throwable t) {
201 logger.error("Exception occurred while producing " + factoryName + "." + verb + t.getMessage());
202 t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(logger, LoggerWrapper.ERROR_MESSAGE)));
204 endTime = System.currentTimeMillis();
205 logger.info("Done producing job: " + factoryName + "." + verb + ", time elapsed:" + (endTime-startTime) + " ms");
210 boolean isAborted() {
215 public static class ProducerEngineExc extends Exc {
216 public ProducerEngineExc(String aMessage) {
221 public static class ProducerEngineFailure extends Failure {
222 public ProducerEngineFailure(Throwable aCause){
223 super(aCause.getMessage(), aCause);
226 public ProducerEngineFailure(String msg, Exception cause){
231 public static class ProducerTask {
232 private String producer;
235 public ProducerTask(String aProducer, String aVerb) {
236 producer = aProducer;
240 public String getVerb() {
244 public String getProducer() {
248 public static List parseProducerTaskList(String aList) throws ProducerEngineExc {
250 List result = new Vector();
252 i = StringRoutines.splitString(aList, ";").iterator();
253 while (i.hasNext()) {
254 String taskExpression = ((String) i.next()).trim();
256 if (taskExpression.length()>0) {
257 List parts = StringRoutines.splitString(taskExpression, ".");
259 if (parts.size() != 2)
260 throw new ProducerEngineExc("Invalid producer expression: '" + taskExpression + "'");
262 result.add(new ProducerEngine.ProducerTask( (String) parts.get(0), (String) parts.get(1)));