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.LoggerToWriterAdapter;
34 import mir.log.LoggerWrapper;
35 import mir.producer.Producer;
36 import mir.producer.ProducerFactory;
37 import mir.util.GeneratorFormatAdapters;
38 import mir.util.StringRoutines;
40 import java.io.PrintWriter;
43 public class ProducerEngine {
44 private JobQueue producerJobQueue;
45 private LoggerWrapper logger;
46 private Map nameToFactory;
47 private List factories;
49 protected ProducerEngine() {
50 logger = new LoggerWrapper("Producer");
51 producerJobQueue = new JobQueue(new LoggerWrapper("Producer.Queue"));
53 factories = new ArrayList();
54 nameToFactory = new HashMap();
57 reloadConfiguration();
64 * Reloads the producer configuration
66 public void reloadConfiguration() throws MirGlobalExc, MirGlobalFailure {
68 factories = MirGlobal.localizer().producers().loadFactories();
70 synchronized (nameToFactory) {
71 nameToFactory.clear();
73 Iterator i = factories.iterator();
75 ProducerFactory factory = (ProducerFactory) i.next();
76 nameToFactory.put(factory.getName(), factory);
81 throw new MirGlobalFailure(t);
86 * Looks up a producer factory by name
88 private ProducerFactory getFactoryForName(String aName) {
89 synchronized (nameToFactory) {
90 return (ProducerFactory) nameToFactory.get(aName);
95 * Returns all factories
97 public List getFactories() {
101 * Adds a producer job to the queue
103 public void addJob(String aProducerFactory, String aVerb) throws MirGlobalExc, MirGlobalFailure {
104 ProducerFactory factory;
106 factory = getFactoryForName( aProducerFactory );
109 throw new MirGlobalExc("Unknown producer: " + aProducerFactory);
111 if (!factory.allowVerb(aVerb))
112 throw new MirGlobalExc("illegal producer/verb combination: " + aProducerFactory+"::"+aVerb);
114 producerJobQueue.appendJob(
115 new ProducerJob(aProducerFactory, aVerb), aProducerFactory+"."+aVerb);
119 * Cancels a list of jobs by job identifier
121 public void cancelJobs(List aJobs) {
122 producerJobQueue.cancelJobs(aJobs);
126 * Cancels all jobs in the queue
128 public void cancelAllJobs() {
129 producerJobQueue.cancelAllJobs();
132 public void addTask(ProducerTask aTask) throws MirGlobalExc, MirGlobalFailure {
133 addJob(aTask.getProducer(), aTask.getVerb());
136 private String convertStatus(JobQueue.JobInfo aJob) {
137 switch (aJob.getStatus()) {
138 case JobQueue.STATUS_ABORTED:
140 case JobQueue.STATUS_CANCELLED:
142 case JobQueue.STATUS_CREATED:
144 case JobQueue.STATUS_PENDING:
146 case JobQueue.STATUS_PROCESSED:
148 case JobQueue.STATUS_PROCESSING:
154 private Map convertJob(JobQueue.JobInfo aJob) {
156 Map result = new HashMap();
157 result.put("identifier", aJob.getIdentifier());
158 result.put("description", aJob.getDescription());
159 result.put("priority", new Integer(aJob.getPriority()));
160 result.put("runningtime", new Double( (double) aJob.getRunningTime() / 1000));
161 result.put("status", convertStatus(aJob));
162 result.put("lastchange", new GeneratorFormatAdapters.DateFormatAdapter(aJob.getLastChange(), MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone")));
163 result.put("finished", new Boolean(
164 aJob.getStatus() == JobQueue.STATUS_PROCESSED ||
165 aJob.getStatus() == JobQueue.STATUS_CANCELLED ||
166 aJob.getStatus() == JobQueue.STATUS_ABORTED));
170 catch (Throwable t) {
171 throw new RuntimeException(t.toString());
175 private List convertJobInfoList(List aJobInfoList) {
176 List result = new ArrayList();
178 Iterator i = aJobInfoList.iterator();
181 result.add(convertJob((JobQueue.JobInfo) i.next()));
186 public List getQueueStatus() {
187 return convertJobInfoList(producerJobQueue.getJobsInfo());
190 private class ProducerJob implements JobQueue.Job {
191 private String factoryName;
193 private Producer producer;
195 public ProducerJob(String aFactory, String aVerb) {
196 factoryName = aFactory;
201 public String getFactoryName() {
205 public String getVerb() {
209 public void abort() {
210 if (producer!=null) {
215 public boolean run() {
216 ProducerFactory factory;
219 boolean result = false;
220 Map startingMap = new HashMap();
221 Map mirMap = new HashMap();
222 mirMap.put("producer", factoryName);
223 mirMap.put("verb", verb);
225 startingMap.put("Mir", mirMap);
227 startTime = System.currentTimeMillis();
228 logger.info("Producing job: "+factoryName+"."+verb);
231 factory = getFactoryForName(factoryName);
234 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
236 synchronized(factory) {
237 producer = factory.makeProducer(verb, startingMap);
239 if (producer!=null) {
240 result = producer.produce(logger);
244 catch (Throwable t) {
245 logger.error("Exception occurred while producing " + factoryName + "." + verb + t.getMessage());
246 t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(logger, LoggerWrapper.ERROR_MESSAGE)));
248 endTime = System.currentTimeMillis();
249 logger.info("Done producing job: " + factoryName + "." + verb + ", time elapsed:" + (endTime-startTime) + " ms");
254 boolean isAborted() {
259 public static class ProducerTask {
260 private String producer;
263 public ProducerTask(String aProducer, String aVerb) {
264 producer = aProducer;
268 public String getVerb() {
272 public String getProducer() {
276 public static List parseProducerTaskList(String aList) throws MirGlobalExc {
278 List result = new ArrayList();
280 i = StringRoutines.splitString(aList, ";").iterator();
281 while (i.hasNext()) {
282 String taskExpression = ((String) i.next()).trim();
284 if (taskExpression.length()>0) {
285 List parts = StringRoutines.splitString(taskExpression, ".");
287 if (parts.size() != 2)
288 throw new MirGlobalExc("Invalid producer expression: '" + taskExpression + "'");
290 result.add(new ProducerEngine.ProducerTask( (String) parts.get(0), (String) parts.get(1)));