b641371dd0e4e425eb35209df65c65fbd4bf16c0
[mir.git] / source / mircoders / producer / Producer.java
1 package mircoders.producer;
2
3 import java.io.*;
4
5 import freemarker.template.*;
6
7 import mir.misc.*;
8 import mir.storage.*;
9 import mir.module.*;
10
11 import mircoders.module.*;
12 import mircoders.entity.*;
13 import mircoders.storage.*;
14
15 abstract public class Producer {
16
17   protected static String   producerDocRoot = MirConfig.getProp("Producer.DocRoot");
18   protected static String   producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
19   protected static String   producerProductionHost = MirConfig.getProp("Producer.ProductionHost");
20   protected static String   producerOpenAction = MirConfig.getProp("Producer.OpenAction");;
21
22   /** @todo same as in HTMLTemplateProcessor, this should be dynamically set */
23   protected static String   actionRoot = MirConfig.getProp("RootUri") + "/servlet/Mir";
24
25   protected static Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home") + "/" + MirConfig.getProp("Producer.Logfile"));
26   protected static ModuleTopics         topicsModule;
27   protected static ModuleLinksImcs      linksImcsModule;
28   protected static ModuleSchwerpunkt    schwerpunktModule;
29   protected static ModuleFeature        featureModule;
30   protected static ModuleContent        contentModule;
31   protected static ModuleImages         imageModule;
32   protected static ModuleUploadedMedia  uploadedMediaModule;
33
34   static {
35                 // init
36     try {
37
38       contentModule = new ModuleContent(DatabaseContent.getInstance());
39       topicsModule = new ModuleTopics(DatabaseTopics.getInstance());
40       linksImcsModule = new ModuleLinksImcs(DatabaseLinksImcs.getInstance());
41       schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
42       featureModule = new ModuleFeature(DatabaseFeature.getInstance());
43       imageModule = new ModuleImages(DatabaseImages.getInstance());
44       uploadedMediaModule = new ModuleUploadedMedia(DatabaseImages.getInstance());
45
46     }
47     catch(StorageObjectException e)
48     {
49       System.err.println("*** failed to initialize Producer " + e.toString());
50     }
51   }
52
53         public void handle(PrintWriter htmlout, EntityUsers user)
54                 throws StorageObjectException, ModuleException {
55                 handle(htmlout,user,false,false);
56         }
57
58         abstract public void handle(PrintWriter htmlout, EntityUsers user, boolean forced, boolean sync)
59                 throws StorageObjectException, ModuleException;
60
61 //
62 // Methods for producing files
63
64         public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
65                 return _produce(template, filename, model, htmlout, false);
66         }
67
68         public boolean produce_compressed(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
69                 return _produce(template, filename, model, htmlout, true);
70         }
71
72         private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed) {
73                 try {
74                         File f = new File(producerStorageRoot + filename);
75                         File dir = new File(f.getParent());
76                         dir.mkdirs();
77                         FileWriter outputFile = new FileWriter(f);
78                         PrintWriter outStream;
79                         if (compressed==true) {
80                                 outStream = new LineFilterWriter(outputFile);
81                         } else {
82                                 outStream = new PrintWriter(outputFile);
83                         }
84
85                         HTMLTemplateProcessor.process(null,template, model, outStream,null);
86                         outputFile.close();
87                         outStream.close();
88
89                         printHTML(htmlout, "Produced <a href=\"" + producerProductionHost+
90                         filename + "\">" + filename + "</a>");
91                         //theLog.printInfo("Produced: " + producerStorageRoot + filename);
92                         //theLog.printDebugInfo("free mem:" + java.lang.Runtime.getRuntime().freeMemory());
93       //theLog.printDebugInfo("total mem:" + java.lang.Runtime.getRuntime().totalMemory());
94                         return true;
95
96                 } catch(IOException exception){
97                         logHTML(htmlout, "Producer: File could not be written " + filename);
98                         return false;
99                 } catch(HTMLParseException exception){
100                         logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
101                         return false;
102                 }
103         }
104
105         //
106         // filename methods
107
108         public String indexFileNameForPageCount(int pc) {
109                 return fileNameForPageCount("/index", pc);
110         }
111
112         public String fileNameForPageCount(String stub, int pc) {
113                 String fileName = producerDocRoot + stub;
114                 if (pc>1) {
115                         fileName+=pc;
116                 }
117                 fileName += ".html";
118                 return fileName;
119         }
120
121         /**
122          * logging
123          */
124
125   public void logHTMLFinish(PrintWriter htmlout,String moduleName, int pageCount, long startTime, long endTime) {
126     // timing and message to browser
127     long overall = endTime - startTime;
128     int pagesPerMinute=0; float perMinute = (float)overall/(float)60000;
129     if (perMinute >0) pagesPerMinute = (int) ((float)pageCount / perMinute);
130
131     logHTML(htmlout, "Producer."+moduleName+" finished producing: " +
132             overall + " ms for "+ pageCount+" Pages = " +pagesPerMinute + " pages/min");
133     printHTML(htmlout, "Back to <a href=\""+actionRoot+"\">Admin-Startage</a>");
134   }
135
136         public void logHTML(PrintWriter out, String s) {
137                 _print(out, s, true);
138         }
139
140         public void printHTML(PrintWriter out, String s) {
141                 _print(out, s, false);
142         }
143
144         private void _print(PrintWriter out, String s, boolean log) {
145                 if (out != null) { out.println(s+"<br />");out.flush(); }
146                 if (log == true) {
147                         theLog.printInfo(s);
148                 }
149         }
150
151 }