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 protected static String actionRoot = MirConfig.getProp("RootUri") + "/Mir";
55 protected static Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home") + "/" + MirConfig.getProp("Producer.Logfile"));
56 protected static ModuleTopics topicsModule;
57 protected static ModuleLinksImcs linksImcsModule;
58 protected static ModuleSchwerpunkt schwerpunktModule;
59 protected static ModuleFeature featureModule;
60 protected static ModuleContent contentModule;
61 protected static ModuleImages imageModule;
62 protected static ModuleUploadedMedia uploadedMediaModule;
68 contentModule = new ModuleContent(DatabaseContent.getInstance());
69 topicsModule = new ModuleTopics(DatabaseTopics.getInstance());
70 linksImcsModule = new ModuleLinksImcs(DatabaseLinksImcs.getInstance());
71 schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
72 featureModule = new ModuleFeature(DatabaseFeature.getInstance());
73 imageModule = new ModuleImages(DatabaseImages.getInstance());
74 uploadedMediaModule = new ModuleUploadedMedia(DatabaseImages.getInstance());
77 catch(StorageObjectException e)
79 System.err.println("*** failed to initialize Producer " + e.toString());
83 public void handle(PrintWriter htmlout, EntityUsers user)
84 throws StorageObjectException, ModuleException {
85 handle(htmlout,user,false,false);
88 abstract public void handle(PrintWriter htmlout, EntityUsers user, boolean forced, boolean sync)
89 throws StorageObjectException, ModuleException;
92 // Methods for producing files
94 public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
95 return _produce(template, filename, model, htmlout, false,
96 MirConfig.getProp("Mir.DefaultEncoding"));
99 public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, String encoding) {
100 return _produce(template, filename, model, htmlout, false, encoding);
103 public boolean produce_compressed(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
104 return _produce(template, filename, model, htmlout, true,
105 MirConfig.getProp("Mir.DefaultEncoding"));
108 private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed, String encoding) {
110 File f = new File(producerStorageRoot + filename);
111 File dir = new File(f.getParent());
113 // it's important that we set the desired encoding. It should be UTF8
114 // not the platform default.
115 OutputStreamWriter outputFileStream =
116 new OutputStreamWriter(new FileOutputStream(f), encoding);
117 PrintWriter outStream;
118 if (compressed==true) {
119 outStream = new LineFilterWriter(outputFileStream);
121 outStream = new PrintWriter(outputFileStream);
124 HTMLTemplateProcessor.process(null,template, model, outStream,null);
125 outputFileStream.close();
128 printHTML(htmlout, "Produced <a href=\"" + producerProductionHost+producerDocRoot +
129 filename + "\">" + filename + "</a>");
130 //theLog.printInfo("Produced: " + producerStorageRoot + filename);
131 //theLog.printDebugInfo("free mem:" + java.lang.Runtime.getRuntime().freeMemory());
132 //theLog.printDebugInfo("total mem:" + java.lang.Runtime.getRuntime().totalMemory());
135 } catch(IOException exception){
136 logHTML(htmlout, "Producer: File could not be written " + filename);
137 System.out.println(exception.toString());
139 } catch(HTMLParseException exception){
140 logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
148 public String indexFileNameForPageCount(int pc) {
149 return fileNameForPageCount("/index", pc);
152 public String fileNameForPageCount(String stub, int pc) {
153 String fileName = producerDocRoot + stub;
165 public void logHTMLFinish(PrintWriter htmlout,String moduleName, int pageCount, long startTime, long endTime) {
166 // timing and message to browser
167 long overall = endTime - startTime;
168 int pagesPerMinute=0; float perMinute = (float)overall/(float)60000;
169 if (perMinute >0) pagesPerMinute = (int) ((float)pageCount / perMinute);
171 logHTML(htmlout, "Producer."+moduleName+" finished producing: " +
172 overall + " ms for "+ pageCount+" Pages = " +pagesPerMinute + " pages/min");
173 printHTML(htmlout, "Back to <a href=\""+actionRoot+"\">Admin-Startage</a>");
176 public void logHTML(PrintWriter out, String s) {
177 _print(out, s, true);
180 public void printHTML(PrintWriter out, String s) {
181 _print(out, s, false);
184 private void _print(PrintWriter out, String s, boolean log) {
185 if (out != null) { out.println(s+"<br />");out.flush(); }