2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
32 package mircoders.producer;
36 import freemarker.template.*;
42 import mircoders.module.*;
43 import mircoders.entity.*;
44 import mircoders.storage.*;
46 abstract public class Producer {
48 protected static String producerDocRoot = MirConfig.getProp("Producer.DocRoot");
49 protected static String producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
50 protected static String producerProductionHost = MirConfig.getProp("Producer.ProductionHost");
51 protected static String producerOpenAction = MirConfig.getProp("Producer.OpenAction");;
53 /** @todo same as in HTMLTemplateProcessor, this should be dynamically set */
54 protected static String actionRoot = MirConfig.getProp("RootUri") + "/servlet/Mir";
56 protected static Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home") + "/" + MirConfig.getProp("Producer.Logfile"));
57 protected static ModuleTopics topicsModule;
58 protected static ModuleLinksImcs linksImcsModule;
59 protected static ModuleSchwerpunkt schwerpunktModule;
60 protected static ModuleFeature featureModule;
61 protected static ModuleContent contentModule;
62 protected static ModuleImages imageModule;
63 protected static ModuleUploadedMedia uploadedMediaModule;
69 contentModule = new ModuleContent(DatabaseContent.getInstance());
70 topicsModule = new ModuleTopics(DatabaseTopics.getInstance());
71 linksImcsModule = new ModuleLinksImcs(DatabaseLinksImcs.getInstance());
72 schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
73 featureModule = new ModuleFeature(DatabaseFeature.getInstance());
74 imageModule = new ModuleImages(DatabaseImages.getInstance());
75 uploadedMediaModule = new ModuleUploadedMedia(DatabaseImages.getInstance());
78 catch(StorageObjectException e)
80 System.err.println("*** failed to initialize Producer " + e.toString());
84 public void handle(PrintWriter htmlout, EntityUsers user)
85 throws StorageObjectException, ModuleException {
86 handle(htmlout,user,false,false);
89 abstract public void handle(PrintWriter htmlout, EntityUsers user, boolean forced, boolean sync)
90 throws StorageObjectException, ModuleException;
93 // Methods for producing files
95 public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
96 return _produce(template, filename, model, htmlout, false,
97 MirConfig.getProp("Mir.DefaultEncoding"));
100 public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, String encoding) {
101 return _produce(template, filename, model, htmlout, false, encoding);
104 public boolean produce_compressed(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
105 return _produce(template, filename, model, htmlout, true,
106 MirConfig.getProp("Mir.DefaultEncoding"));
109 private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed, String encoding) {
111 File f = new File(producerStorageRoot + filename);
112 File dir = new File(f.getParent());
114 // it's important that we set the desired encoding. It should be UTF8
115 // not the platform default.
116 OutputStreamWriter outputFileStream =
117 new OutputStreamWriter(new FileOutputStream(f), encoding);
118 PrintWriter outStream;
119 if (compressed==true) {
120 outStream = new LineFilterWriter(outputFileStream);
122 outStream = new PrintWriter(outputFileStream);
125 HTMLTemplateProcessor.process(null,template, model, outStream,null);
126 outputFileStream.close();
129 printHTML(htmlout, "Produced <a href=\"" + producerProductionHost+producerDocRoot +
130 filename + "\">" + filename + "</a>");
131 //theLog.printInfo("Produced: " + producerStorageRoot + filename);
132 //theLog.printDebugInfo("free mem:" + java.lang.Runtime.getRuntime().freeMemory());
133 //theLog.printDebugInfo("total mem:" + java.lang.Runtime.getRuntime().totalMemory());
136 } catch(IOException exception){
137 logHTML(htmlout, "Producer: File could not be written " + filename);
138 System.out.println(exception.toString());
140 } catch(HTMLParseException exception){
141 logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
149 public String indexFileNameForPageCount(int pc) {
150 return fileNameForPageCount("/index", pc);
153 public String fileNameForPageCount(String stub, int pc) {
154 String fileName = producerDocRoot + stub;
166 public void logHTMLFinish(PrintWriter htmlout,String moduleName, int pageCount, long startTime, long endTime) {
167 // timing and message to browser
168 long overall = endTime - startTime;
169 int pagesPerMinute=0; float perMinute = (float)overall/(float)60000;
170 if (perMinute >0) pagesPerMinute = (int) ((float)pageCount / perMinute);
172 logHTML(htmlout, "Producer."+moduleName+" finished producing: " +
173 overall + " ms for "+ pageCount+" Pages = " +pagesPerMinute + " pages/min");
174 printHTML(htmlout, "Back to <a href=\""+actionRoot+"\">Admin-Startage</a>");
177 public void logHTML(PrintWriter out, String s) {
178 _print(out, s, true);
181 public void printHTML(PrintWriter out, String s) {
182 _print(out, s, false);
185 private void _print(PrintWriter out, String s, boolean log) {
186 if (out != null) { out.println(s+"<br />");out.flush(); }