1 package mircoders.producer;
5 import freemarker.template.*;
11 import mircoders.module.*;
12 import mircoders.entity.*;
13 import mircoders.storage.*;
15 abstract public class Producer {
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");;
22 /** @todo same as in HTMLTemplateProcessor, this should be dynamically set */
23 protected static String actionRoot = MirConfig.getProp("RootUri") + "/servlet/Mir";
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;
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());
47 catch(StorageObjectException e)
49 System.err.println("*** failed to initialize Producer " + e.toString());
53 public void handle(PrintWriter htmlout, EntityUsers user)
54 throws StorageObjectException, ModuleException {
55 handle(htmlout,user,false,false);
58 abstract public void handle(PrintWriter htmlout, EntityUsers user, boolean forced, boolean sync)
59 throws StorageObjectException, ModuleException;
62 // Methods for producing files
64 public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
65 return _produce(template, filename, model, htmlout, false);
68 public boolean produce_compressed(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
69 return _produce(template, filename, model, htmlout, true);
72 private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed) {
74 File f = new File(producerStorageRoot + filename);
75 File dir = new File(f.getParent());
77 FileWriter outputFile = new FileWriter(f);
78 PrintWriter outStream;
79 if (compressed==true) {
80 outStream = new LineFilterWriter(outputFile);
82 outStream = new PrintWriter(outputFile);
85 HTMLTemplateProcessor.process(null,template, model, outStream,null);
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());
96 } catch(IOException exception){
97 logHTML(htmlout, "Producer: File could not be written " + filename);
99 } catch(HTMLParseException exception){
100 logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
105 public boolean produceFile(String filename, byte[] in, PrintWriter htmlout, boolean icon) {
107 boolean retVal = false;
113 f = new File(filename);
114 theLog.printDebugInfo("image: " + filename);
116 f = new File(producerStorageRoot + filename);
117 theLog.printDebugInfo("icon: " + filename);
119 File dir = new File(f.getParent());
122 FileOutputStream outStream;
123 outStream = new FileOutputStream(f);
127 } catch(IOException exception) {
128 logHTML(htmlout, "Producer: File could not be written: " + filename);
139 public String indexFileNameForPageCount(int pc) {
140 return fileNameForPageCount("/index", pc);
143 public String fileNameForPageCount(String stub, int pc) {
144 String fileName = producerDocRoot + stub;
156 public void logHTMLFinish(PrintWriter htmlout,String moduleName, int pageCount, long startTime, long endTime) {
157 // timing and message to browser
158 long overall = endTime - startTime;
159 int pagesPerMinute=0; float perMinute = (float)overall/(float)60000;
160 if (perMinute >0) pagesPerMinute = (int) ((float)pageCount / perMinute);
162 logHTML(htmlout, "Producer."+moduleName+" finished producing: " +
163 overall + " ms for "+ pageCount+" Pages = " +pagesPerMinute + " pages/min");
164 logHTML(htmlout, "Back to <a href=\""+actionRoot+"\">Admin-Startage</a>");
167 public void logHTML(PrintWriter out, String s) {
168 _print(out, s, true);
171 public void printHTML(PrintWriter out, String s) {
172 _print(out, s, false);
175 private void _print(PrintWriter out, String s, boolean log) {
176 if (out != null) { out.println(s+"<br>");out.flush(); }