Mir goes GPL
[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   /** @todo same as in HTMLTemplateProcessor, this should be dynamically set */
54   protected static String   actionRoot = MirConfig.getProp("RootUri") + "/servlet/Mir";
55
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;
64
65   static {
66                 // init
67     try {
68
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());
76
77     }
78     catch(StorageObjectException e)
79     {
80       System.err.println("*** failed to initialize Producer " + e.toString());
81     }
82   }
83
84         public void handle(PrintWriter htmlout, EntityUsers user)
85                 throws StorageObjectException, ModuleException {
86                 handle(htmlout,user,false,false);
87         }
88
89         abstract public void handle(PrintWriter htmlout, EntityUsers user, boolean forced, boolean sync)
90                 throws StorageObjectException, ModuleException;
91
92 //
93 // Methods for producing files
94
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"));
98         }
99
100         public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, String encoding) {
101                 return _produce(template, filename, model, htmlout, false, encoding);
102         }
103
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"));
107         }
108
109         private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed, String encoding) {
110                 try {
111                         File f = new File(producerStorageRoot + filename);
112                         File dir = new File(f.getParent());
113                         dir.mkdirs();
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);
121                         } else {
122                                 outStream = new PrintWriter(outputFileStream);
123                         }
124
125                         HTMLTemplateProcessor.process(null,template, model, outStream,null);
126                         outputFileStream.close();
127                         outStream.close();
128
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());
134                         return true;
135
136                 } catch(IOException exception){
137                         logHTML(htmlout, "Producer: File could not be written " + filename);
138       System.out.println(exception.toString());
139                         return false;
140                 } catch(HTMLParseException exception){
141                         logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
142                         return false;
143                 }
144         }
145
146         //
147         // filename methods
148
149         public String indexFileNameForPageCount(int pc) {
150                 return fileNameForPageCount("/index", pc);
151         }
152
153         public String fileNameForPageCount(String stub, int pc) {
154                 String fileName = producerDocRoot + stub;
155                 if (pc>1) {
156                         fileName+=pc;
157                 }
158                 fileName += ".html";
159                 return fileName;
160         }
161
162         /**
163          * logging
164          */
165
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);
171
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>");
175   }
176
177         public void logHTML(PrintWriter out, String s) {
178                 _print(out, s, true);
179         }
180
181         public void printHTML(PrintWriter out, String s) {
182                 _print(out, s, false);
183         }
184
185         private void _print(PrintWriter out, String s, boolean log) {
186                 if (out != null) { out.println(s+"<br />");out.flush(); }
187                 if (log == true) {
188                         theLog.printInfo(s);
189                 }
190         }
191
192 }