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;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.OutputStreamWriter;
38 import java.io.PrintWriter;
40 import mir.config.MirPropertiesConfiguration;
41 import mir.misc.HTMLParseException;
42 import mir.misc.HTMLTemplateProcessor;
43 import mir.misc.LineFilterWriter;
44 import mir.misc.Logfile;
45 import mir.module.ModuleException;
46 import mir.storage.StorageObjectFailure;
47 import mircoders.entity.EntityUsers;
48 import mircoders.module.ModuleContent;
49 import mircoders.module.ModuleFeature;
50 import mircoders.module.ModuleImages;
51 import mircoders.module.ModuleLinksImcs;
52 import mircoders.module.ModuleSchwerpunkt;
53 import mircoders.module.ModuleTopics;
54 import mircoders.module.ModuleUploadedMedia;
55 import mircoders.storage.DatabaseContent;
56 import mircoders.storage.DatabaseFeature;
57 import mircoders.storage.DatabaseImages;
58 import mircoders.storage.DatabaseLinksImcs;
59 import mircoders.storage.DatabaseTopics;
60 import freemarker.template.TemplateModelRoot;
62 abstract public class Producer {
64 protected static MirPropertiesConfiguration configuration;
65 protected static String producerDocRoot;
66 protected static String producerStorageRoot;
67 protected static String producerProductionHost;
68 protected static String producerOpenAction;
69 protected static String actionRoot;
70 protected static Logfile theLog;
71 protected static ModuleTopics topicsModule;
72 protected static ModuleLinksImcs linksImcsModule;
73 protected static ModuleSchwerpunkt schwerpunktModule;
74 protected static ModuleFeature featureModule;
75 protected static ModuleContent contentModule;
76 protected static ModuleImages imageModule;
77 protected static ModuleUploadedMedia uploadedMediaModule;
82 configuration = MirPropertiesConfiguration.instance();
83 producerDocRoot = configuration.getString("Producer.DocRoot");
84 producerStorageRoot = configuration.getString("Producer.StorageRoot");
85 producerProductionHost = configuration.getString("Producer.ProductionHost");
86 producerOpenAction = configuration.getString("Producer.OpenAction");
87 actionRoot = configuration.getString("RootUri") + "/Mir";
88 theLog = Logfile.getInstance(configuration.getStringWithHome("Producer.Logfile"));
89 contentModule = new ModuleContent(DatabaseContent.getInstance());
90 topicsModule = new ModuleTopics(DatabaseTopics.getInstance());
91 linksImcsModule = new ModuleLinksImcs(DatabaseLinksImcs.getInstance());
92 schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
93 featureModule = new ModuleFeature(DatabaseFeature.getInstance());
94 imageModule = new ModuleImages(DatabaseImages.getInstance());
95 uploadedMediaModule = new ModuleUploadedMedia(DatabaseImages.getInstance());
100 System.err.println("*** failed to initialize Producer " + e.toString());
104 public void handle(PrintWriter htmlout, EntityUsers user)
105 throws StorageObjectFailure, ModuleException {
106 handle(htmlout,user,false,false);
109 abstract public void handle(PrintWriter htmlout, EntityUsers user, boolean forced, boolean sync)
110 throws StorageObjectFailure, ModuleException;
113 // Methods for producing files
115 public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
116 return _produce(template, filename, model, htmlout, false,
117 configuration.getString("Mir.DefaultEncoding"));
120 public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, String encoding) {
121 return _produce(template, filename, model, htmlout, false, encoding);
124 public boolean produce_compressed(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
125 return _produce(template, filename, model, htmlout, true,
126 configuration.getString("Mir.DefaultEncoding"));
129 private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed, String encoding) {
131 File f = new File(producerStorageRoot + filename);
132 File dir = new File(f.getParent());
134 // it's important that we set the desired encoding. It should be UTF8
135 // not the platform default.
136 OutputStreamWriter outputFileStream =
137 new OutputStreamWriter(new FileOutputStream(f), encoding);
138 PrintWriter outStream;
139 if (compressed==true) {
140 outStream = new LineFilterWriter(outputFileStream);
142 outStream = new PrintWriter(outputFileStream);
145 HTMLTemplateProcessor.process(null,template, model, outStream,null);
146 outputFileStream.close();
149 printHTML(htmlout, "Produced <a href=\"" + producerProductionHost+producerDocRoot +
150 filename + "\">" + filename + "</a>");
151 //theLog.printInfo("Produced: " + producerStorageRoot + filename);
152 //theLog.printDebugInfo("free mem:" + java.lang.Runtime.getRuntime().freeMemory());
153 //theLog.printDebugInfo("total mem:" + java.lang.Runtime.getRuntime().totalMemory());
156 } catch(IOException exception){
157 logHTML(htmlout, "Producer: File could not be written " + filename);
158 System.out.println(exception.toString());
160 } catch(HTMLParseException exception){
161 logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
169 public String indexFileNameForPageCount(int pc) {
170 return fileNameForPageCount("/index", pc);
173 public String fileNameForPageCount(String stub, int pc) {
174 String fileName = producerDocRoot + stub;
186 public void logHTMLFinish(PrintWriter htmlout,String moduleName, int pageCount, long startTime, long endTime) {
187 // timing and message to browser
188 long overall = endTime - startTime;
189 int pagesPerMinute=0; float perMinute = (float)overall/(float)60000;
190 if (perMinute >0) pagesPerMinute = (int) ((float)pageCount / perMinute);
192 logHTML(htmlout, "Producer."+moduleName+" finished producing: " +
193 overall + " ms for "+ pageCount+" Pages = " +pagesPerMinute + " pages/min");
194 printHTML(htmlout, "Back to <a href=\""+actionRoot+"\">Admin-Startage</a>");
197 public void logHTML(PrintWriter out, String s) {
198 _print(out, s, true);
201 public void printHTML(PrintWriter out, String s) {
202 _print(out, s, false);
205 private void _print(PrintWriter out, String s, boolean log) {
206 if (out != null) { out.println(s+"<br />");out.flush(); }