128a947b0168ef5e502dd7d0d53e74aba0b030b8
[mir.git] / source / mircoders / global / ProducerEngine.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.global;
33
34 import java.util.*;
35 import java.io.*;
36 import mir.producer.*;
37 import mir.util.*;
38 import multex.Exc;
39 import multex.Failure;
40
41 public class ProducerEngine {
42 //  private Map producers;
43   private JobQueue producerJobQueue;
44   private Thread queueThread;
45   private PrintWriter log;
46
47   protected ProducerEngine() {
48 //    producers = MirGlobal.localizer().producers().factories();
49     producerJobQueue = new JobQueue();
50     try {
51       RandomAccessFile raFile = (new RandomAccessFile(MirGlobal.getConfigProperty("Home") + "/" + MirGlobal.getConfigProperty("Producer.Logfile"), "rw"));
52                         raFile.seek(raFile.length());
53                 log = new PrintWriter(new FileWriter( raFile.getFD()));
54     }
55     catch (Exception e) {
56 //      throw new ProducerEngineRuntimeExc("Creating PrintWriter log failed",e);
57       log = new PrintWriter(new NullWriter());
58     }
59     queueThread = new Thread(new ProducerJobQueueThread());
60     queueThread.start();
61   }
62
63   public void addJob(String aProducerFactory, String aVerb) {
64     producerJobQueue.appendJob(new ProducerJob(aProducerFactory, aVerb));
65     log.println(aProducerFactory+"."+aVerb+" added to queue");
66     log.flush();
67   }
68
69   public void addTask(ProducerTask aTask) {
70     addJob(aTask.getProducer(), aTask.getVerb());
71   }
72
73   public void addTasks(List aTasks) {
74     Iterator i = aTasks.iterator();
75
76     while (i.hasNext()) {
77       addTask((ProducerTask) i.next());
78     }
79   }
80
81   public void printQueueStatus(PrintWriter aWriter) {
82     Iterator iterator = producerJobQueue.makeJobListSnapshot().iterator();
83     producerJobQueue.cleanupJobs();
84     JobQueue.Job job;
85
86     while (iterator.hasNext()) {
87       job = (JobQueue.Job) iterator.next();
88       ProducerJob producerJob = (ProducerJob) job.getData();
89
90       aWriter.print(producerJob.factoryName + "." + producerJob.verb);
91       if (job.hasBeenProcessed())
92         aWriter.print(" processed");
93       else if (job.isProcessing())
94         aWriter.print(" processing");
95       else if (job.isPending())
96         aWriter.print(" pending");
97       else
98         aWriter.print(" unknown status");
99
100       aWriter.println("<br/>");
101     }
102   }
103
104   private void produceNow(String aProducerFactory, String aVerb, PrintWriter aLogger) {
105     try {
106       long startTime;
107       long endTime;
108       Map startingMap = new HashMap();
109
110       startTime = System.currentTimeMillis();
111
112       aLogger.println("Producing (" + aProducerFactory + "," + aVerb + ")");
113
114       ProducerFactory factory = (ProducerFactory) MirGlobal.localizer().producers().factories().get(aProducerFactory);
115
116       if (factory == null )
117         throw new Exception("No producer factory '"+aProducerFactory+"' present.");
118
119       MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
120       Producer producer = factory.makeProducer(aVerb, startingMap);
121
122       producer.produce(aLogger);
123
124       endTime = System.currentTimeMillis();
125
126       aLogger.println("Time: " + (endTime-startTime) + " ms<br>");
127     }
128     catch (Throwable e) {
129       try {
130         aLogger.println("exception occurred:<br>");
131         aLogger.println(e.getMessage());
132         e.printStackTrace(aLogger);
133       }
134       catch (Throwable f) {
135       }
136     }
137   }
138
139   private class ProducerJob {
140     private String factoryName;
141     private String verb;
142
143     public ProducerJob(String aFactory, String aVerb) {
144       factoryName = aFactory;
145       verb = aVerb;
146     }
147
148     public void execute() {
149       ProducerFactory factory;
150       Producer producer;
151       long startTime;
152       long endTime;
153       Map startingMap = new HashMap();
154
155       startTime = System.currentTimeMillis();
156       log.println("Producing job: "+factoryName+"."+verb);
157
158       try {
159         factory = (ProducerFactory) MirGlobal.localizer().producers().factories().get( factoryName );
160
161         if (factory!=null) {
162           MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
163
164           synchronized(factory) {
165             producer = factory.makeProducer(verb, startingMap);
166           }
167           if (producer!=null) {
168             producer.produce(log);
169           }
170         }
171       }
172       catch (Throwable t) {
173         log.println("  exception "+t.getMessage());
174         t.printStackTrace(log);
175       }
176       log.println("Done producing job: "+factoryName+"."+verb);
177                   endTime = System.currentTimeMillis();
178
179                   log.println("Time: " + (endTime-startTime) + " ms");
180                   log.flush();
181     }
182   }
183
184   private class ProducerJobQueueThread implements Runnable {
185     public void run() {
186       log.println("starting ProducerJobQueueThread");
187       log.flush();
188
189       while (true) {
190         ProducerJob job = (ProducerJob) producerJobQueue.acquirePendingJob();
191         if (job!=null) {
192           job.execute();
193           producerJobQueue.flagOffJob(job);
194         }
195         else
196         {
197           try {
198             Thread.sleep(1500);
199           }
200           catch (InterruptedException e) {
201           }
202         }
203       }
204     }
205   }
206
207
208   public static class ProducerEngineRuntimeExc extends Failure {
209     public ProducerEngineRuntimeExc(String msg, Exception cause){
210       super(msg,cause);
211     }
212   }
213
214   public static class ProducerTask {
215     private String producer;
216     private String verb;
217
218     public ProducerTask(String aProducer, String aVerb) {
219       producer = aProducer;
220       verb = aVerb;
221     }
222
223     public String getVerb() {
224       return verb;
225     }
226
227     public String getProducer() {
228       return producer;
229     }
230   }
231 }