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 mir.config.MirPropertiesConfiguration;
33 import mir.log.LoggerWrapper;
34 import mir.producer.Producer;
35 import mir.producer.ProducerFactory;
36 import mir.producer.ProductionContext;
37 import mir.util.GeneratorFormatAdapters;
38 import mir.util.StringRoutines;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.List;
45 import java.util.Collections;
47 public class ProducerEngine {
48 private final JobQueue producerJobQueue;
49 private final LoggerWrapper logger = new LoggerWrapper("Producer");
50 private final Map nameToFactory = new HashMap();;
51 private List factories = new ArrayList();
53 protected ProducerEngine() {
54 producerJobQueue = new JobQueue(new LoggerWrapper("Producer.Queue"));
55 reloadConfiguration();
59 * Reloads the producer configuration
61 public void reloadConfiguration() {
63 factories = new ArrayList(MirGlobal.localizer().producers().loadFactories());
65 synchronized (nameToFactory) {
66 nameToFactory.clear();
68 Iterator i = factories.iterator();
70 ProducerFactory factory = (ProducerFactory) i.next();
71 nameToFactory.put(factory.getName(), factory);
76 throw new MirGlobalFailure(t);
81 * Looks up a producer factory by name
83 private ProducerFactory getFactoryForName(String aName) {
84 synchronized (nameToFactory) {
85 return (ProducerFactory) nameToFactory.get(aName);
90 * Returns all factories
92 public List getFactories() {
93 return Collections.unmodifiableList(factories);
96 * Adds a producer job to the queue
98 public void addJob(String aProducerFactory, String aVerb) throws MirGlobalExc, MirGlobalFailure {
99 ProducerFactory factory = getFactoryForName( aProducerFactory );
102 throw new MirGlobalExc("Unknown producer: " + aProducerFactory);
105 if (!factory.allowVerb(aVerb)) {
106 throw new MirGlobalExc("illegal producer/verb combination: " + aProducerFactory+"::"+aVerb);
109 producerJobQueue.appendJob(
110 new ProducerJob(aProducerFactory, aVerb), aProducerFactory+"."+aVerb);
114 * Cancels a list of jobs by job identifier
116 public void cancelJobs(List aJobs) {
117 producerJobQueue.cancelJobs(aJobs);
121 * Cancels all jobs in the queue
123 public void cancelAllJobs() {
124 producerJobQueue.cancelAllJobs();
127 public void addTask(ProducerTask aTask) throws MirGlobalExc, MirGlobalFailure {
128 addJob(aTask.getProducer(), aTask.getVerb());
131 private String convertStatus(JobQueue.JobInfo aJob) {
133 switch (aJob.getStatus()) {
134 case JobQueue.STATUS_ABORTED:
137 case JobQueue.STATUS_CANCELLED:
140 case JobQueue.STATUS_CREATED:
143 case JobQueue.STATUS_PENDING:
146 case JobQueue.STATUS_PROCESSED:
149 case JobQueue.STATUS_PROCESSING:
156 private Map convertJob(JobQueue.JobInfo aJob) {
158 Map result = new HashMap();
160 result.put("identifier", aJob.getIdentifier());
161 result.put("description", aJob.getDescription());
162 result.put("priority", new Integer(aJob.getPriority()));
163 result.put("runningtime", new Double( (double) aJob.getRunningTime() / 1000));
164 result.put("status", convertStatus(aJob));
165 result.put("lastchange", new GeneratorFormatAdapters.DateFormatAdapter(aJob.getLastChange(), MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone")));
166 result.put("finished", new Boolean(
167 aJob.getStatus() == JobQueue.STATUS_PROCESSED ||
168 aJob.getStatus() == JobQueue.STATUS_CANCELLED ||
169 aJob.getStatus() == JobQueue.STATUS_ABORTED));
173 catch (Throwable t) {
174 throw new RuntimeException(t.toString());
178 private List convertJobInfoList(List aJobInfoList) {
179 List result = new ArrayList();
181 Iterator i = aJobInfoList.iterator();
184 result.add(convertJob((JobQueue.JobInfo) i.next()));
189 public List getQueueStatus() {
190 return convertJobInfoList(producerJobQueue.getJobsInfo());
193 private class ProducerJob implements JobQueue.Job {
194 private String factoryName;
196 private Producer producer;
197 private ProductionContext productionContext;
199 ProducerJob(String aFactory, String aVerb) {
200 factoryName = aFactory;
205 public String getFactoryName() {
209 public String getVerb() {
213 public void abort() {
214 if (producer!=null && productionContext!=null) {
215 producer.abort(productionContext);
219 public boolean run() {
220 final Map startingMap = new HashMap();
222 long startTime = System.currentTimeMillis();
223 logger.info("Producing job: "+factoryName+"."+verb);
224 boolean result = false;
227 final ProducerFactory factory = getFactoryForName(factoryName);
230 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
232 synchronized(factory) {
233 producer = factory.makeProducer(verb, startingMap);
236 if (producer!=null) {
238 MirGlobal.localizer().producers().beforeProducerTask(factoryName, verb);
240 catch (Throwable t) {
241 logger.warn("Misbehaving beforeProducerTask",t );
244 productionContext = new ProductionContext() {
245 public ProducerFactory getFactory() {
249 public Producer getProducer() {
253 public String getVerb() {
257 public LoggerWrapper getLogger() {
261 public Map getValueSet() {
266 result = producer.execute(productionContext);
267 productionContext = null;
271 MirGlobal.localizer().producers().afterProducerTask(factoryName, verb);
273 catch (Throwable t) {
274 logger.warn("Misbehaving afterProducerTask", t);
279 catch (Throwable t) {
280 logger.error("Exception occurred while producing " + factoryName + "." + verb, t);
282 long endTime = System.currentTimeMillis();
284 logger.info("Done producing job: " + factoryName + "." + verb + ", time elapsed:" + (endTime-startTime) + " ms");
289 boolean isAborted() {
294 public static class ProducerTask {
295 private String producer;
298 public ProducerTask(String aProducer, String aVerb) {
299 producer = aProducer;
303 public String getVerb() {
307 public String getProducer() {
311 public static List parseProducerTaskList(String aList) throws MirGlobalExc {
312 List result = new ArrayList();
314 Iterator i = StringRoutines.splitString(aList, ";").iterator();
315 while (i.hasNext()) {
316 String taskExpression = ((String) i.next()).trim();
318 if (taskExpression.length()>0) {
319 List parts = StringRoutines.splitString(taskExpression, ".");
321 if (parts.size() != 2) {
322 throw new MirGlobalExc("Invalid producer expression: '" + taskExpression + "'");
324 result.add(new ProducerEngine.ProducerTask( (String) parts.get(0), (String) parts.get(1)));