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.util.GeneratorFormatAdapters;
45 import mir.util.StringRoutines;
47 public class ProducerEngine {
48 private JobQueue producerJobQueue;
49 private LoggerWrapper logger;
50 private Map nameToFactory;
51 private List factories;
53 protected ProducerEngine() {
54 logger = new LoggerWrapper("Producer");
55 producerJobQueue = new JobQueue(new LoggerWrapper("Producer.Queue"));
57 factories = new ArrayList();
58 nameToFactory = new HashMap();
61 reloadConfiguration();
68 * Reloads the producer configuration
70 public void reloadConfiguration() throws MirGlobalExc, MirGlobalFailure {
72 factories = MirGlobal.localizer().producers().loadFactories();
74 synchronized (nameToFactory) {
75 nameToFactory.clear();
77 Iterator i = factories.iterator();
79 ProducerFactory factory = (ProducerFactory) i.next();
80 nameToFactory.put(factory.getName(), factory);
85 throw new MirGlobalFailure(t);
90 * Looks up a producer factory by name
92 private ProducerFactory getFactoryForName(String aName) {
93 synchronized (nameToFactory) {
94 return (ProducerFactory) nameToFactory.get(aName);
99 * Returns all factories
101 public List getFactories() {
105 * Adds a producer job to the queue
107 public void addJob(String aProducerFactory, String aVerb) throws MirGlobalExc, MirGlobalFailure {
108 ProducerFactory factory;
110 factory = getFactoryForName( aProducerFactory );
113 throw new MirGlobalExc("Unknown producer: " + aProducerFactory);
115 if (!factory.allowVerb(aVerb))
116 throw new MirGlobalExc("illegal producer/verb combination: " + aProducerFactory+"::"+aVerb);
118 producerJobQueue.appendJob(
119 new ProducerJob(aProducerFactory, aVerb), aProducerFactory+"."+aVerb);
123 * Cancels a list of jobs by job identifier
125 public void cancelJobs(List aJobs) {
126 producerJobQueue.cancelJobs(aJobs);
130 * Cancels all jobs in the queue
132 public void cancelAllJobs() {
133 producerJobQueue.cancelAllJobs();
136 public void addTask(ProducerTask aTask) throws MirGlobalExc, MirGlobalFailure {
137 addJob(aTask.getProducer(), aTask.getVerb());
140 private String convertStatus(JobQueue.JobInfo aJob) {
141 switch (aJob.getStatus()) {
142 case JobQueue.STATUS_ABORTED:
144 case JobQueue.STATUS_CANCELLED:
146 case JobQueue.STATUS_CREATED:
148 case JobQueue.STATUS_PENDING:
150 case JobQueue.STATUS_PROCESSED:
152 case JobQueue.STATUS_PROCESSING:
158 private Map convertJob(JobQueue.JobInfo aJob) {
160 Map result = new HashMap();
161 result.put("identifier", aJob.getIdentifier());
162 result.put("description", aJob.getDescription());
163 result.put("priority", new Integer(aJob.getPriority()));
164 result.put("runningtime", new Double( (double) aJob.getRunningTime() / 1000));
165 result.put("status", convertStatus(aJob));
166 result.put("lastchange", new GeneratorFormatAdapters.DateFormatAdapter(aJob.getLastChange(), MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone")));
167 result.put("finished", new Boolean(
168 aJob.getStatus() == JobQueue.STATUS_PROCESSED ||
169 aJob.getStatus() == JobQueue.STATUS_CANCELLED ||
170 aJob.getStatus() == JobQueue.STATUS_ABORTED));
174 catch (Throwable t) {
175 throw new RuntimeException(t.toString());
179 private List convertJobInfoList(List aJobInfoList) {
180 List result = new ArrayList();
182 Iterator i = aJobInfoList.iterator();
185 result.add(convertJob((JobQueue.JobInfo) i.next()));
190 public List getQueueStatus() {
191 return convertJobInfoList(producerJobQueue.getJobsInfo());
194 private class ProducerJob implements JobQueue.Job {
195 private String factoryName;
197 private Producer producer;
199 public ProducerJob(String aFactory, String aVerb) {
200 factoryName = aFactory;
205 public String getFactoryName() {
209 public String getVerb() {
213 public void abort() {
214 if (producer!=null) {
219 public boolean run() {
220 ProducerFactory factory;
223 boolean result = false;
224 Map startingMap = new HashMap();
225 Map mirMap = new HashMap();
226 mirMap.put("producer", factoryName);
227 mirMap.put("verb", verb);
229 startingMap.put("Mir", mirMap);
231 startTime = System.currentTimeMillis();
232 logger.info("Producing job: "+factoryName+"."+verb);
235 factory = getFactoryForName(factoryName);
238 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
240 synchronized(factory) {
241 producer = factory.makeProducer(verb, startingMap);
243 if (producer!=null) {
244 result = producer.produce(logger);
248 catch (Throwable t) {
249 logger.error("Exception occurred while producing " + factoryName + "." + verb + t.getMessage());
250 t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(logger, LoggerWrapper.ERROR_MESSAGE)));
252 endTime = System.currentTimeMillis();
253 logger.info("Done producing job: " + factoryName + "." + verb + ", time elapsed:" + (endTime-startTime) + " ms");
258 boolean isAborted() {
263 public static class ProducerTask {
264 private String producer;
267 public ProducerTask(String aProducer, String aVerb) {
268 producer = aProducer;
272 public String getVerb() {
276 public String getProducer() {
280 public static List parseProducerTaskList(String aList) throws MirGlobalExc {
282 List result = new ArrayList();
284 i = StringRoutines.splitString(aList, ";").iterator();
285 while (i.hasNext()) {
286 String taskExpression = ((String) i.next()).trim();
288 if (taskExpression.length()>0) {
289 List parts = StringRoutines.splitString(taskExpression, ".");
291 if (parts.size() != 2)
292 throw new MirGlobalExc("Invalid producer expression: '" + taskExpression + "'");
293 result.add(new ProducerEngine.ProducerTask( (String) parts.get(0), (String) parts.get(1)));