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;
38 import java.util.Vector;
40 import mir.config.MirPropertiesConfiguration;
41 import mir.log.LoggerToWriterAdapter;
42 import mir.log.LoggerWrapper;
43 import mir.producer.Producer;
44 import mir.producer.ProducerFactory;
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 Vector();
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;
200 public ProducerJob(String aFactory, String aVerb) {
201 factoryName = aFactory;
206 public String getFactoryName() {
210 public String getVerb() {
214 public void abort() {
215 if (producer!=null) {
220 public boolean run() {
221 ProducerFactory factory;
224 boolean result = false;
225 Map startingMap = new HashMap();
226 Map mirMap = new HashMap();
227 mirMap.put("producer", factoryName);
228 mirMap.put("verb", verb);
230 startingMap.put("Mir", mirMap);
232 startTime = System.currentTimeMillis();
233 logger.info("Producing job: "+factoryName+"."+verb);
236 factory = getFactoryForName(factoryName);
239 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
241 synchronized(factory) {
242 producer = factory.makeProducer(verb, startingMap);
244 if (producer!=null) {
245 result = producer.produce(logger);
249 catch (Throwable t) {
250 logger.error("Exception occurred while producing " + factoryName + "." + verb + t.getMessage());
251 t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(logger, LoggerWrapper.ERROR_MESSAGE)));
253 endTime = System.currentTimeMillis();
254 logger.info("Done producing job: " + factoryName + "." + verb + ", time elapsed:" + (endTime-startTime) + " ms");
259 boolean isAborted() {
264 public static class ProducerTask {
265 private String producer;
268 public ProducerTask(String aProducer, String aVerb) {
269 producer = aProducer;
273 public String getVerb() {
277 public String getProducer() {
281 public static List parseProducerTaskList(String aList) throws MirGlobalExc {
283 List result = new Vector();
285 i = StringRoutines.splitString(aList, ";").iterator();
286 while (i.hasNext()) {
287 String taskExpression = ((String) i.next()).trim();
289 if (taskExpression.length()>0) {
290 List parts = StringRoutines.splitString(taskExpression, ".");
292 if (parts.size() != 2)
293 throw new MirGlobalExc("Invalid producer expression: '" + taskExpression + "'");
295 result.add(new ProducerEngine.ProducerTask( (String) parts.get(0), (String) parts.get(1)));