tuning full production of content
[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   protected static String   actionRoot = null;
22
23   protected static Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home") + "/" + MirConfig.getProp("Producer.Logfile"));
24   protected static ModuleTopics         topicsModule;
25   protected static ModuleLinksImcs      linksImcsModule;
26   protected static ModuleSchwerpunkt    schwerpunktModule;
27   protected static ModuleFeature        featureModule;
28   protected static ModuleContent        contentModule;
29   protected static ModuleImages         imageModule;
30   protected static ModuleUploadedMedia  uploadedMediaModule;
31
32   static {
33                 // init
34     try {
35       contentModule = new ModuleContent(DatabaseContent.getInstance());
36       topicsModule = new ModuleTopics(DatabaseTopics.getInstance());
37       linksImcsModule = new ModuleLinksImcs(DatabaseLinksImcs.getInstance());
38       schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
39       featureModule = new ModuleFeature(DatabaseFeature.getInstance());
40       imageModule = new ModuleImages(DatabaseImages.getInstance());
41       uploadedMediaModule = new ModuleUploadedMedia(DatabaseImages.getInstance());
42       /** @todo same as in HTMLTemplateProcessor, this should be dynamically set */
43       actionRoot = producerDocRoot + "/servlet/Mir";
44     }
45     catch(StorageObjectException e)
46     {
47       System.err.println("*** failed to initialize Producer " + e.toString());
48     }
49   }
50
51         public void handle(PrintWriter htmlout, EntityUsers user)
52                 throws StorageObjectException, ModuleException {
53                 handle(htmlout,user,false,false);
54         }
55
56         abstract public void handle(PrintWriter htmlout, EntityUsers user, boolean forced, boolean sync)
57                 throws StorageObjectException, ModuleException;
58
59 //
60 // Methods for producing files
61
62         public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
63                 return _produce(template, filename, model, htmlout, false);
64         }
65
66         public boolean produce_compressed(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
67                 return _produce(template, filename, model, htmlout, true);
68         }
69
70         private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed) {
71                 try {
72                         File f = new File(producerStorageRoot + filename);
73                         File dir = new File(f.getParent());
74                         dir.mkdirs();
75                         FileWriter outputFile = new FileWriter(f);
76                         PrintWriter outStream;
77                         if (compressed==true) {
78                                 outStream = new LineFilterWriter(outputFile);
79                         } else {
80                                 outStream = new PrintWriter(outputFile);
81                         }
82
83                         HTMLTemplateProcessor.process(null,template, model, outStream);
84                         outputFile.close();
85                         outStream.close();
86
87                         printHTML(htmlout, "Produced <a href=\"" + producerProductionHost+  filename + "\">" + filename + "</a>");
88                         //theLog.printInfo("Produced: " + producerStorageRoot + filename);
89                         //theLog.printDebugInfo("free mem:" + java.lang.Runtime.getRuntime().freeMemory());
90       //theLog.printDebugInfo("total mem:" + java.lang.Runtime.getRuntime().totalMemory());
91                         return true;
92
93                 } catch(IOException exception){
94                         logHTML(htmlout, "Producer: File could not be written " + filename);
95                         return false;
96                 } catch(HTMLParseException exception){
97                         logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
98                         return false;
99                 }
100         }
101
102         public boolean produceFile(String filename, byte[] in, PrintWriter htmlout, boolean icon) {
103
104                 boolean retVal = false;
105
106                 if (in!=null) {
107                         try {
108         File f = null;
109         if(icon==false){
110           f = new File(filename);
111           theLog.printDebugInfo("image: " + filename);
112         } else {
113           f = new File(producerStorageRoot + filename);
114           theLog.printDebugInfo("icon: " + filename);
115         }
116                                 File dir = new File(f.getParent());
117                                 dir.mkdirs();
118
119                                 FileOutputStream outStream;
120                                 outStream = new FileOutputStream(f);
121                                 outStream.write(in);
122                                 outStream.close();
123                                 retVal = true;
124                         } catch(IOException exception) {
125         logHTML(htmlout, "Producer: File could not be written: " + filename);
126       }
127                 }
128                 return retVal;
129         }
130
131
132
133         //
134         // filename methods
135
136         public String indexFileNameForPageCount(int pc) {
137                 return fileNameForPageCount("/index", pc);
138         }
139
140         public String fileNameForPageCount(String stub, int pc) {
141                 String fileName = producerDocRoot + stub;
142                 if (pc>1) {
143                         fileName+=pc;
144                 }
145                 fileName += ".html";
146                 return fileName;
147         }
148
149         /**
150          * logging
151          */
152         public void logHTML(PrintWriter out, String s) {
153                 _print(out, s, true);
154         }
155
156         public void printHTML(PrintWriter out, String s) {
157                 _print(out, s, false);
158         }
159
160         private void _print(PrintWriter out, String s, boolean log) {
161                 if (out != null) { out.println(s+"<br>");out.flush(); }
162                 if (log == true) {
163                         theLog.printInfo(s);
164                 }
165         }
166
167 }