use req.getContextPath to find the RootUri.. much more robust. 4.0 and 4.1 compatible...
[mir.git] / source / mircoders / producer / Producer.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
30  */
31
32 package mircoders.producer;
33
34 import java.io.*;
35
36 import freemarker.template.*;
37
38 import mir.misc.*;
39 import mir.storage.*;
40 import mir.module.*;
41
42 import mircoders.module.*;
43 import mircoders.entity.*;
44 import mircoders.storage.*;
45
46 abstract public class Producer {
47
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");;
52
53   protected static String   actionRoot = MirConfig.getProp("RootUri") + "/Mir";
54
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;
63
64   static {
65                 // init
66     try {
67
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());
75
76     }
77     catch(StorageObjectException e)
78     {
79       System.err.println("*** failed to initialize Producer " + e.toString());
80     }
81   }
82
83         public void handle(PrintWriter htmlout, EntityUsers user)
84                 throws StorageObjectException, ModuleException {
85                 handle(htmlout,user,false,false);
86         }
87
88         abstract public void handle(PrintWriter htmlout, EntityUsers user, boolean forced, boolean sync)
89                 throws StorageObjectException, ModuleException;
90
91 //
92 // Methods for producing files
93
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"));
97         }
98
99         public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, String encoding) {
100                 return _produce(template, filename, model, htmlout, false, encoding);
101         }
102
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"));
106         }
107
108         private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed, String encoding) {
109                 try {
110                         File f = new File(producerStorageRoot + filename);
111                         File dir = new File(f.getParent());
112                         dir.mkdirs();
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);
120                         } else {
121                                 outStream = new PrintWriter(outputFileStream);
122                         }
123
124                         HTMLTemplateProcessor.process(null,template, model, outStream,null);
125                         outputFileStream.close();
126                         outStream.close();
127
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());
133                         return true;
134
135                 } catch(IOException exception){
136                         logHTML(htmlout, "Producer: File could not be written " + filename);
137       System.out.println(exception.toString());
138                         return false;
139                 } catch(HTMLParseException exception){
140                         logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
141                         return false;
142                 }
143         }
144
145         //
146         // filename methods
147
148         public String indexFileNameForPageCount(int pc) {
149                 return fileNameForPageCount("/index", pc);
150         }
151
152         public String fileNameForPageCount(String stub, int pc) {
153                 String fileName = producerDocRoot + stub;
154                 if (pc>1) {
155                         fileName+=pc;
156                 }
157                 fileName += ".html";
158                 return fileName;
159         }
160
161         /**
162          * logging
163          */
164
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);
170
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>");
174   }
175
176         public void logHTML(PrintWriter out, String s) {
177                 _print(out, s, true);
178         }
179
180         public void printHTML(PrintWriter out, String s) {
181                 _print(out, s, false);
182         }
183
184         private void _print(PrintWriter out, String s, boolean log) {
185                 if (out != null) { out.println(s+"<br />");out.flush(); }
186                 if (log == true) {
187                         theLog.printInfo(s);
188                 }
189         }
190
191 }