Mir goes GPL
[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 // ML: TODO: should check if a similar job is already pending
65     producerJobQueue.appendJob(new ProducerJob(aProducerFactory, aVerb));
66     log.println(aProducerFactory+"."+aVerb+" added to queue");
67     log.flush();
68   }
69
70   public void printQueueStatus(PrintWriter aWriter) {
71     Iterator iterator = producerJobQueue.makeJobListSnapshot().iterator();
72     producerJobQueue.cleanupJobs();
73     JobQueue.Job job;
74
75     while (iterator.hasNext()) {
76       job = (JobQueue.Job) iterator.next();
77       ProducerJob producerJob = (ProducerJob) job.getData();
78
79       aWriter.print(producerJob.factoryName + "." + producerJob.verb);
80       if (job.hasBeenProcessed())
81         aWriter.print(" processed");
82       else if (job.isProcessing())
83         aWriter.print(" processing");
84       else if (job.isPending())
85         aWriter.print(" pending");
86       else
87         aWriter.print(" unknown status");
88
89       aWriter.println("<br/>");
90     }
91   }
92
93   private void produceNow(String aProducerFactory, String aVerb, PrintWriter aLogger) {
94     try {
95       long startTime;
96       long endTime;
97       Map startingMap = new HashMap();
98
99       startTime = System.currentTimeMillis();
100
101       aLogger.println("Producing (" + aProducerFactory + "," + aVerb + ")");
102
103       ProducerFactory factory = (ProducerFactory) MirGlobal.localizer().producers().factories().get(aProducerFactory);
104
105       if (factory == null )
106         throw new Exception("No producer factory '"+aProducerFactory+"' present.");
107
108       MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
109       Producer producer = factory.makeProducer(aVerb, startingMap);
110
111       producer.produce(aLogger);
112
113       endTime = System.currentTimeMillis();
114
115       aLogger.println("Time: " + (endTime-startTime) + " ms<br>");
116     }
117     catch (Throwable e) {
118       try {
119         aLogger.println("exception occurred:<br>");
120         aLogger.println(e.getMessage());
121         e.printStackTrace(aLogger);
122       }
123       catch (Throwable f) {
124       }
125     }
126   }
127
128   private class ProducerJob {
129     private String factoryName;
130     private String verb;
131
132     public ProducerJob(String aFactory, String aVerb) {
133       factoryName = aFactory;
134       verb = aVerb;
135     }
136
137     public void execute() {
138       ProducerFactory factory;
139       Producer producer;
140       long startTime;
141       long endTime;
142       Map startingMap = new HashMap();
143
144       startTime = System.currentTimeMillis();
145       log.println("Producing job: "+factoryName+"."+verb);
146
147       try {
148         factory = (ProducerFactory) MirGlobal.localizer().producers().factories().get( factoryName );
149
150         if (factory!=null) {
151           MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(startingMap);
152
153           synchronized(factory) {
154             producer = factory.makeProducer(verb, startingMap);
155           }
156           if (producer!=null) {
157             producer.produce(log);
158           }
159         }
160       }
161       catch (Throwable t) {
162         log.println("  exception "+t.getMessage());
163         t.printStackTrace(log);
164       }
165       log.println("Done producing job: "+factoryName+"."+verb);
166                   endTime = System.currentTimeMillis();
167
168                   log.println("Time: " + (endTime-startTime) + " ms");
169                   log.flush();
170     }
171   }
172
173   private class ProducerJobQueueThread implements Runnable {
174     public void run() {
175       log.println("starting ProducerJobQueueThread");
176       log.flush();
177
178       while (true) {
179         ProducerJob job = (ProducerJob) producerJobQueue.acquirePendingJob();
180         if (job!=null) {
181           job.execute();
182           producerJobQueue.flagOffJob(job);
183         }
184         else
185         {
186           try {
187             Thread.sleep(1500);
188           }
189           catch (InterruptedException e) {
190           }
191         }
192       }
193     }
194   }
195
196
197   public static class ProducerEngineRuntimeExc extends Failure {
198     public ProducerEngineRuntimeExc(String msg, Exception cause){
199       super(msg,cause);
200     }
201   }
202
203 }