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,
66 MirConfig.getProp("Mir.DefaultEncoding"));
69 public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, String encoding) {
70 return _produce(template, filename, model, htmlout, false, encoding);
73 public boolean produce_compressed(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
74 return _produce(template, filename, model, htmlout, true,
75 MirConfig.getProp("Mir.DefaultEncoding"));
78 private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed, String encoding) {
80 File f = new File(producerStorageRoot + filename);
81 File dir = new File(f.getParent());
83 // it's important that we set the desired encoding. It should be UTF8
84 // not the platform default.
85 OutputStreamWriter outputFileStream =
86 new OutputStreamWriter(new FileOutputStream(f), encoding);
87 PrintWriter outStream;
88 if (compressed==true) {
89 outStream = new LineFilterWriter(outputFileStream);
91 outStream = new PrintWriter(outputFileStream);
94 HTMLTemplateProcessor.process(null,template, model, outStream,null);
95 outputFileStream.close();
98 printHTML(htmlout, "Produced <a href=\"" + producerProductionHost+producerDocRoot +
99 filename + "\">" + filename + "</a>");
100 //theLog.printInfo("Produced: " + producerStorageRoot + filename);
101 //theLog.printDebugInfo("free mem:" + java.lang.Runtime.getRuntime().freeMemory());
102 //theLog.printDebugInfo("total mem:" + java.lang.Runtime.getRuntime().totalMemory());
105 } catch(IOException exception){
106 logHTML(htmlout, "Producer: File could not be written " + filename);
107 System.out.println(exception.toString());
109 } catch(HTMLParseException exception){
110 logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
118 public String indexFileNameForPageCount(int pc) {
119 return fileNameForPageCount("/index", pc);
122 public String fileNameForPageCount(String stub, int pc) {
123 String fileName = producerDocRoot + stub;
135 public void logHTMLFinish(PrintWriter htmlout,String moduleName, int pageCount, long startTime, long endTime) {
136 // timing and message to browser
137 long overall = endTime - startTime;
138 int pagesPerMinute=0; float perMinute = (float)overall/(float)60000;
139 if (perMinute >0) pagesPerMinute = (int) ((float)pageCount / perMinute);
141 logHTML(htmlout, "Producer."+moduleName+" finished producing: " +
142 overall + " ms for "+ pageCount+" Pages = " +pagesPerMinute + " pages/min");
143 printHTML(htmlout, "Back to <a href=\""+actionRoot+"\">Admin-Startage</a>");
146 public void logHTML(PrintWriter out, String s) {
147 _print(out, s, true);
150 public void printHTML(PrintWriter out, String s) {
151 _print(out, s, false);
154 private void _print(PrintWriter out, String s, boolean log) {
155 if (out != null) { out.println(s+"<br />");out.flush(); }