KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > scheduler > jobs > FileDumper


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.scheduler.jobs;
34
35 import java.lang.ref.SoftReference JavaDoc;
36
37 import java.util.HashMap JavaDoc;
38
39 import java.io.IOException JavaDoc;
40 import java.io.FileReader JavaDoc;
41 import java.io.FileWriter JavaDoc;
42 import java.io.File JavaDoc;
43
44 import com.knowgate.debug.DebugFile;
45 import com.knowgate.dataobjs.DB;
46 import com.knowgate.scheduler.Atom;
47 import com.knowgate.scheduler.Job;
48 import com.knowgate.dataxslt.FastStreamReplacer;
49
50 /**
51  * <p>Simple processor for PageSets with disk output</p>
52  * @author Sergio Montoro Ten
53  * @version 1.0
54  */

55
56 public class FileDumper extends Job {
57
58   // This flag is set if the first Job execution finds replacements of the form
59
// {#Section.Field} witch is data retrived from the database and inserted
60
// dynamically into the document final template.
61
// If the execution of this job for the first Atom find no tags of the form
62
// {#Section.Field} then the replacement subroutine can be skipped in next
63
// execution saving CPU cycles.
64
private boolean bHasReplacements;
65
66   // This is a soft reference to a String holding the base document template
67
// if virtual memory runs low the garbage collector can discard the soft
68
// reference that would be reloaded from disk later upon the next atom processing
69
private SoftReference JavaDoc oFileStr;
70
71   // A reference to the replacer class witch maps tags of the form {#Section.Field}
72
// to their corresponding database fields.
73
private FastStreamReplacer oReplacer;
74
75   // ---------------------------------------------------------------------------
76

77   public FileDumper() {
78     bHasReplacements = true;
79     oFileStr = null;
80     oReplacer = new FastStreamReplacer();
81   }
82
83   // ---------------------------------------------------------------------------
84

85   public void free() {}
86
87   // ---------------------------------------------------------------------------
88

89   /**
90    * <p>Process PageSet pointed by Atom and dumps result to disk</p>
91    * <p>Base workareas path is taken from "workareasput" property of hipergate.cnf<p>
92    * <p>Processed documents are saved under /web/workareas/apps/Mailwire/html/<i>gu_pageset</i>/</p>
93    * @param oAtm Atom holding a reference to PageSet instance to be dumped<br>
94    * Atom must have the following parameters set:<br>
95    * <table border=1 cellpadding=4>
96    * <tr><td>gu_workarea</td><td>GUID of WorkArea owner of document to be saved</td></tr>
97    * <tr><td>gu_pageset</td><td>GUID of PageSet to be saved</td></tr>
98    * <tr><td>nm_pageset</td><td>Name of PageSet document instance to be saved</td></tr>
99    * </table>
100    * @return String containing the final pos-processed document
101    * @throws IOException
102    */

103
104   public Object JavaDoc process(Atom oAtm) throws IOException JavaDoc {
105
106     File JavaDoc oFile; // Document Template File
107
FileReader JavaDoc oFileRead; // Document Template Reader
108
String JavaDoc sPathHTML; // Full Path to Document Template File
109
char cBuffer[]; // Internal Buffer for Document Template File Data
110
Object JavaDoc oReplaced; // Document Template File Data after FastStreamReplacer processing
111

112     final String JavaDoc sSep = System.getProperty("file.separator"); // Alias for file.separator
113

114     if (DebugFile.trace) {
115       DebugFile.writeln("Begin FileDumper.process([Job:" + getStringNull(DB.gu_job,"") + ", Atom:" + String.valueOf(oAtm.getInt(DB.pg_atom)) + "])");
116       DebugFile.incIdent();
117     }
118
119     if (bHasReplacements) { // Initially the document is assumed to have tags to replace
120

121       // *************************************************
122
// Compose the full path to document template file
123

124       // First get the storage base path from hipergate.cnf
125
sPathHTML = getProperty("workareasput");
126       if (!sPathHTML.endsWith(sSep)) sPathHTML += sSep;
127
128       // Concatenate PageSet workarea guid and subpath to Mailwire application directory
129
sPathHTML += getParameter("gu_workarea") + sSep + "apps" + sSep + "Mailwire" + sSep + "html" + sSep + getParameter("gu_pageset") + sSep;
130
131       // Concatenate PageSet Name
132
sPathHTML += getParameter("nm_pageset").replace(' ','_') + ".html";
133
134       if (DebugFile.trace) DebugFile.writeln("PathHTML = " + sPathHTML);
135
136       // *************************************************
137
// Call FastStreamReplacer for {#Section.Field} tags
138

139       oReplaced = oReplacer.replace(sPathHTML, oAtm.getItemMap());
140
141       // Count number of replacements done and update bHasReplacements flag accordingly
142
bHasReplacements = (oReplacer.lastReplacements()>0);
143     }
144
145     else {
146
147       oReplaced = null;
148
149       if (null!=oFileStr)
150         oReplaced = oFileStr.get();
151
152       if (null==oReplaced) {
153
154         // If document template has no database replacement tags
155
// then just cache the document template into a SoftReference String
156

157         // Compose the full path to document template file
158
sPathHTML = getProperty("workareasput");
159         if (!sPathHTML.endsWith(sSep)) sPathHTML += sSep;
160         sPathHTML += getParameter("gu_workarea") + sSep + "apps" + sSep + "Mailwire" + sSep + "html" + sSep + getParameter("gu_pageset") + sSep + getParameter("nm_pageset").replace(' ','_') + ".html";
161
162         if (DebugFile.trace) DebugFile.writeln("PathHTML = " + sPathHTML);
163
164         // ***************************
165
// Read document template file
166

167         oFile = new File JavaDoc(sPathHTML);
168
169         cBuffer = new char[new Long JavaDoc(oFile.length()).intValue()];
170
171         oFileRead = new FileReader JavaDoc(oFile);
172         oFileRead.read(cBuffer);
173         oFileRead.close();
174
175         if (DebugFile.trace) DebugFile.writeln(String.valueOf(cBuffer.length) + " characters readed");
176
177         // *********************************************************
178
// Assign SoftReference to File cached in-memory as a String
179

180         oReplaced = new String JavaDoc(cBuffer);
181         oFileStr = new SoftReference JavaDoc(oReplaced);
182
183       } // fi (oReplaced)
184

185     } // fi (bHasReplacements)
186

187     // ***********************************************
188
// Write down to disk the final replaced file data
189

190     // Compose job directory path
191
String JavaDoc sPathJobDir = getProperty("storage");
192     if (!sPathJobDir.endsWith(sSep)) sPathJobDir += sSep;
193     sPathJobDir += "jobs" + sSep + getParameter("gu_workarea") + sSep + getString(DB.gu_job) + sSep;
194
195     // Write final file data for each atom processed
196
FileWriter JavaDoc oFileWrite = new FileWriter JavaDoc(sPathJobDir + getString(DB.gu_job) + "_" + String.valueOf(oAtm.getInt(DB.pg_atom)) + ".html", true);
197     oFileWrite.write((String JavaDoc) oReplaced);
198     oFileWrite.close();
199
200     // Decrement de count of atoms peding of processing at this job
201
iPendingAtoms--;
202
203     if (DebugFile.trace) {
204       DebugFile.writeln("End FileDumper.process([Job:" + getStringNull(DB.gu_job,"") + ", Atom:" + String.valueOf(oAtm.getInt(DB.pg_atom)) + "])");
205       DebugFile.decIdent();
206     }
207
208     return oReplaced;
209   } // process
210

211 } // FileDumper
212
Popular Tags