X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fglobal%2FProducerEngine.java;h=60a399ea38180d144b7491cb031988872c061872;hb=06b9a796be74c75d1297bfa6959a4ce193932a57;hp=ecccfdbd054bfff0b27c688b95ed316608234d4f;hpb=d90c1bbdd5e1823253436f24dce80de4f0abbfcb;p=mir.git diff --git a/source/mircoders/global/ProducerEngine.java b/source/mircoders/global/ProducerEngine.java index ecccfdbd..60a399ea 100755 --- a/source/mircoders/global/ProducerEngine.java +++ b/source/mircoders/global/ProducerEngine.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 The Mir-coders group + * Copyright (C) 2001, 2002 The Mir-coders group * * This file is part of Mir. * @@ -18,17 +18,15 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with the com.oreilly.servlet library, any library - * licensed under the Apache Software License, The Sun (tm) Java Advanced - * Imaging library (JAI), The Sun JIMI library (or with modified versions of - * the above that use the same license as the above), and distribute linked - * combinations including the two. You must obey the GNU General Public - * License in all respects for all of the code used other than the above - * mentioned libraries. If you modify this file, you may extend this exception - * to your version of the file, but you are not obligated to do so. If you do - * not wish to do so, delete this exception statement from your version. + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. */ - package mircoders.global; import java.io.PrintWriter; @@ -38,34 +36,38 @@ import java.util.List; import java.util.Map; import java.util.Vector; +import mir.config.MirPropertiesConfiguration; import mir.log.LoggerToWriterAdapter; import mir.log.LoggerWrapper; import mir.producer.Producer; import mir.producer.ProducerFactory; -import mir.util.DateToMapAdapter; +import mir.util.GeneratorFormatAdapters; import mir.util.StringRoutines; import multex.Exc; import multex.Failure; public class ProducerEngine { -// private Map producers; private JobQueue producerJobQueue; - private Thread queueThread; private LoggerWrapper logger; protected ProducerEngine() { - producerJobQueue = new JobQueue(); logger = new LoggerWrapper("Producer"); - - queueThread = new Thread(new ProducerJobQueueThread()); - queueThread.start(); + producerJobQueue = new JobQueue(new LoggerWrapper("Producer.Queue")); } public void addJob(String aProducerFactory, String aVerb) { - producerJobQueue.appendJob(new ProducerJob(aProducerFactory, aVerb)); + producerJobQueue.appendJob(new ProducerJob(aProducerFactory, aVerb), aProducerFactory+"."+aVerb); } + public void cancelJobs(List aJobs) { + producerJobQueue.cancelJobs(aJobs); + }; + + public void cancelAllJobs() { + producerJobQueue.cancelAllJobs(); + }; + public void addTask(ProducerTask aTask) { addJob(aTask.getProducer(), aTask.getVerb()); } @@ -78,56 +80,61 @@ public class ProducerEngine { } } - private String convertStatus(JobQueue.Job aJob) { - if (aJob.hasBeenProcessed()) - return "processed"; - if (aJob.isProcessing()) - return "processing"; - if (aJob.isPending()) - return "pending"; - if (aJob.isCancelled()) - return "cancelled"; - if (aJob.hasBeenAborted()) - return "aborted"; - + private String convertStatus(JobQueue.JobInfo aJob) { + switch (aJob.getStatus()) { + case JobQueue.STATUS_ABORTED: + return "aborted"; + case JobQueue.STATUS_CANCELLED: + return "cancelled"; + case JobQueue.STATUS_CREATED: + return "created"; + case JobQueue.STATUS_PENDING: + return "pending"; + case JobQueue.STATUS_PROCESSED: + return "processed"; + case JobQueue.STATUS_PROCESSING: + return "processing"; + } return "unknown"; } - private Map convertJob(JobQueue.Job aJob) { - Map result = new HashMap(); - ProducerJob producerJob = (ProducerJob) aJob.getData(); - - result.put("identifier", aJob.getIdentifier()); - result.put("factory", producerJob.getFactoryName()); - result.put("verb", producerJob.getVerb()); - result.put("priority", new Integer(aJob.getPriority())); - result.put("status", convertStatus(aJob)); - result.put("lastchange", new DateToMapAdapter(aJob.getLastChange())); - - return result; - } - - private void convertJobList(List aSourceJobList, List aDestination) { - Iterator i = aSourceJobList.iterator(); + private Map convertJob(JobQueue.JobInfo aJob) { + try { + Map result = new HashMap(); + result.put("identifier", aJob.getIdentifier()); + result.put("description", aJob.getDescription()); + result.put("priority", new Integer(aJob.getPriority())); + result.put("runningtime", new Double( (double) aJob.getRunningTime() / 1000)); + result.put("status", convertStatus(aJob)); + result.put("lastchange", new GeneratorFormatAdapters.DateFormatAdapter(aJob.getLastChange(), MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone"))); + result.put("finished", new Boolean( + aJob.getStatus() == JobQueue.STATUS_PROCESSED || + aJob.getStatus() == JobQueue.STATUS_CANCELLED || + aJob.getStatus() == JobQueue.STATUS_ABORTED)); - while (i.hasNext()) - aDestination.add(convertJob((JobQueue.Job) i.next())); + return result; + } + catch (Throwable t) { + throw new RuntimeException(t.toString()); + } } - public List getQueueStatus() { + private List convertJobInfoList(List aJobInfoList) { List result = new Vector(); - List pendingJobs = new Vector(); - List finishedJobs = new Vector(); - producerJobQueue.makeJobListSnapshots(pendingJobs, finishedJobs); + Iterator i = aJobInfoList.iterator(); - convertJobList(pendingJobs, result); - convertJobList(finishedJobs, result); + while (i.hasNext()) + result.add(convertJob((JobQueue.JobInfo) i.next())); return result; } -private class ProducerJob { + public List getQueueStatus() { + return convertJobInfoList(producerJobQueue.getJobsInfo()); + } + + private class ProducerJob implements JobQueue.Job { private String factoryName; private String verb; private Producer producer; @@ -152,11 +159,17 @@ private class ProducerJob { } } - public void execute() { + public boolean run() { ProducerFactory factory; long startTime; long endTime; + boolean result = false; Map startingMap = new HashMap(); + Map mirMap = new HashMap(); + mirMap.put("producer", factoryName); + mirMap.put("verb", verb); + + startingMap.put("Mir", mirMap); startTime = System.currentTimeMillis(); logger.info("Producing job: "+factoryName+"."+verb); @@ -171,7 +184,7 @@ private class ProducerJob { producer = factory.makeProducer(verb, startingMap); } if (producer!=null) { - producer.produce(logger); + result = producer.produce(logger); } } } @@ -181,6 +194,8 @@ private class ProducerJob { } endTime = System.currentTimeMillis(); logger.info("Done producing job: " + factoryName + "." + verb + ", time elapsed:" + (endTime-startTime) + " ms"); + + return result; } boolean isAborted() { @@ -188,31 +203,6 @@ private class ProducerJob { } } - private class ProducerJobQueueThread implements Runnable { - public void run() { - logger.debug("starting ProducerJobQueueThread"); - - while (true) { - ProducerJob job = (ProducerJob) producerJobQueue.acquirePendingJob(); - if (job!=null) { - job.execute(); - if (job.isAborted()) - producerJobQueue.jobAborted(job); - else - producerJobQueue.jobProcessed(job); - } - else - { - try { - Thread.sleep(1500); - } - catch (InterruptedException e) { - } - } - } - } - } - public static class ProducerEngineExc extends Exc { public ProducerEngineExc(String aMessage) { super(aMessage); @@ -248,13 +238,16 @@ private class ProducerJob { i = StringRoutines.splitString(aList, ";").iterator(); while (i.hasNext()) { - String taskExpression = (String) i.next(); - List parts = StringRoutines.splitString(taskExpression, "."); + String taskExpression = ((String) i.next()).trim(); + + if (taskExpression.length()>0) { + List parts = StringRoutines.splitString(taskExpression, "."); - if (parts.size()!=2) - throw new ProducerEngineExc("Invalid producer expression: '" + taskExpression + "'"); - else - result.add(new ProducerEngine.ProducerTask((String) parts.get(0), (String) parts.get(1))); + if (parts.size() != 2) + throw new ProducerEngineExc("Invalid producer expression: '" + taskExpression + "'"); + else + result.add(new ProducerEngine.ProducerTask( (String) parts.get(0), (String) parts.get(1))); + } } return result;