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.ArrayList;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
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.producer.ProductionContext;
45 import mir.util.GeneratorFormatAdapters;
46 import mir.util.StringRoutines;
48 public class ProducerEngine {
49 private JobQueue producerJobQueue;
50 private LoggerWrapper logger;
51 private Map nameToFactory;
52 private List factories;
54 protected ProducerEngine() {
55 logger = new LoggerWrapper("Producer");
56 producerJobQueue = new JobQueue(new LoggerWrapper("Producer.Queue"));
58 factories = new ArrayList();
59 nameToFactory = new HashMap();
62 reloadConfiguration();
69 * Reloads the producer configuration
71 public void reloadConfiguration() throws MirGlobalExc, MirGlobalFailure {
73 factories = MirGlobal.localizer().producers().loadFactories();
75 synchronized (nameToFactory) {
76 nameToFactory.clear();
78 Iterator i = factories.iterator();
80 ProducerFactory factory = (ProducerFactory) i.next();
81 nameToFactory.put(factory.getName(), factory);
86 throw new MirGlobalFailure(t);
91 * Looks up a producer factory by name
93 private ProducerFactory getFactoryForName(String aName) {
94 synchronized (nameToFactory) {
95 return (ProducerFactory) nameToFactory.get(aName);
100 * Returns all factories
102 public List getFactories() {
106 * Adds a producer job to the queue
108 public void addJob(String aProducerFactory, String aVerb) throws MirGlobalExc, MirGlobalFailure {
109 ProducerFactory factory;
111 factory = getFactoryForName( aProducerFactory );
114 throw new MirGlobalExc("Unknown producer: " + aProducerFactory);
116 if (!factory.allowVerb(aVerb))
117 throw new MirGlobalExc("illegal producer/verb combination: " + aProducerFactory+"::"+aVerb);
119 producerJobQueue.appendJob(
120 new ProducerJob(aProducerFactory, aVerb), aProducerFactory+"."+aVerb);
124 * Cancels a list of jobs by job identifier
126 public void cancelJobs(List aJobs) {
127 producerJobQueue.cancelJobs(aJobs);
131 * Cancels all jobs in the queue
133 public void cancelAllJobs() {
134 producerJobQueue.cancelAllJobs();
137 public void addTask(ProducerTask aTask) throws MirGlobalExc, MirGlobalFailure {
138 addJob(aTask.getProducer(), aTask.getVerb());
141 private String convertStatus(JobQueue.JobInfo aJob) {
142 switch (aJob.getStatus()) {
143 case JobQueue.STATUS_ABORTED:
145 case JobQueue.STATUS_CANCELLED:
147 case JobQueue.STATUS_CREATED:
149 case JobQueue.STATUS_PENDING:
151 case JobQueue.STATUS_PROCESSED:
153 case JobQueue.STATUS_PROCESSING:
159 private Map convertJob(JobQueue.JobInfo aJob) {
161 Map result = new HashMap();
162 result.put("identifier", aJob.getIdentifier());
163 result.put("description", aJob.getDescription());
164 result.put("priority", new Integer(aJob.getPriority()));
165 result.put("runningtime", new Double( (double) aJob.getRunningTime() / 1000));
166 result.put("status", convertStatus(aJob));
167 result.put("lastchange", new GeneratorFormatAdapters.DateFormatAdapter(aJob.getLastChange(), MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone")));
168 result.put("finished", new Boolean(
169 aJob.getStatus() == JobQueue.STATUS_PROCESSED ||
170 aJob.getStatus() == JobQueue.STATUS_CANCELLED ||
171 aJob.getStatus() == JobQueue.STATUS_ABORTED));
175 catch (Throwable t) {
176 throw new RuntimeException(t.toString());
180 private List convertJobInfoList(List aJobInfoList) {
181 List result = new ArrayList();
183 Iterator i = aJobInfoList.iterator();
186 result.add(convertJob((JobQueue.JobInfo) i.next()));
191 public List getQueueStatus() {
192 return convertJobInfoList(producerJobQueue.getJobsInfo());
195 private class ProducerJob implements JobQueue.Job {
196 private String factoryName;
198 private Producer producer;
199 private ProductionContext productionContext;
201 public ProducerJob(String aFactory, String aVerb) {
202 factoryName = aFactory;
207 public String getFactoryName() {
211 public String getVerb() {
215 public void abort() {
216 if (producer!=null && productionContext!=null) {
217 producer.abort(productionContext);
221 public boolean run() {
222 final ProducerFactory factory;
225 boolean result = false;
226 final Map startingMap = new HashMap();
227 Map mirMap = new HashMap();
228 mirMap.put("producer", factoryName);
229 mirMap.put("verb", verb);
231 startingMap.put("Mir", mirMap);
233 startTime = System.currentTimeMillis();
234 logger.info("Producing job: "+factoryName+"."+verb);
237 factory = getFactoryForName(factoryName);
240 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
242 synchronized(factory) {
243 producer = factory.makeProducer(verb, startingMap);
246 if (producer!=null) {
248 MirGlobal.localizer().producers().beforeProducerTask(factoryName, verb);
250 catch (Throwable t) {
251 logger.warn("Misbehaving beforeProducerTask",t );
254 productionContext = new ProductionContext() {
255 public ProducerFactory getFactory() {
259 public Producer getProducer() {
263 public String getVerb() {
267 public LoggerWrapper getLogger() {
271 public Map getValueSet() {
275 result = producer.execute(productionContext);
277 MirGlobal.localizer().producers().afterProducerTask(factoryName, verb);
279 catch (Throwable t) {
280 logger.warn("Misbehaving afterProducerTask", t);
285 catch (Throwable t) {
286 logger.error("Exception occurred while producing " + factoryName + "." + verb + t.getMessage());
287 t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(logger, LoggerWrapper.ERROR_MESSAGE)));
289 endTime = System.currentTimeMillis();
290 logger.info("Done producing job: " + factoryName + "." + verb + ", time elapsed:" + (endTime-startTime) + " ms");
295 boolean isAborted() {
300 public static class ProducerTask {
301 private String producer;
304 public ProducerTask(String aProducer, String aVerb) {
305 producer = aProducer;
309 public String getVerb() {
313 public String getProducer() {
317 public static List parseProducerTaskList(String aList) throws MirGlobalExc {
319 List result = new ArrayList();
321 i = StringRoutines.splitString(aList, ";").iterator();
322 while (i.hasNext()) {
323 String taskExpression = ((String) i.next()).trim();
325 if (taskExpression.length()>0) {
326 List parts = StringRoutines.splitString(taskExpression, ".");
328 if (parts.size() != 2)
329 throw new MirGlobalExc("Invalid producer expression: '" + taskExpression + "'");
330 result.add(new ProducerEngine.ProducerTask( (String) parts.get(0), (String) parts.get(1)));