001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.scheduler.jobs;
034:
035: import java.lang.ref.SoftReference;
036:
037: import java.util.HashMap;
038:
039: import java.io.IOException;
040: import java.io.FileReader;
041: import java.io.FileWriter;
042: import java.io.File;
043:
044: import com.knowgate.debug.DebugFile;
045: import com.knowgate.dataobjs.DB;
046: import com.knowgate.scheduler.Atom;
047: import com.knowgate.scheduler.Job;
048: import com.knowgate.dataxslt.FastStreamReplacer;
049:
050: /**
051: * <p>Simple processor for PageSets with disk output</p>
052: * @author Sergio Montoro Ten
053: * @version 1.0
054: */
055:
056: public class FileDumper extends Job {
057:
058: // This flag is set if the first Job execution finds replacements of the form
059: // {#Section.Field} witch is data retrived from the database and inserted
060: // dynamically into the document final template.
061: // If the execution of this job for the first Atom find no tags of the form
062: // {#Section.Field} then the replacement subroutine can be skipped in next
063: // execution saving CPU cycles.
064: private boolean bHasReplacements;
065:
066: // This is a soft reference to a String holding the base document template
067: // if virtual memory runs low the garbage collector can discard the soft
068: // reference that would be reloaded from disk later upon the next atom processing
069: private SoftReference oFileStr;
070:
071: // A reference to the replacer class witch maps tags of the form {#Section.Field}
072: // to their corresponding database fields.
073: private FastStreamReplacer oReplacer;
074:
075: // ---------------------------------------------------------------------------
076:
077: public FileDumper() {
078: bHasReplacements = true;
079: oFileStr = null;
080: oReplacer = new FastStreamReplacer();
081: }
082:
083: // ---------------------------------------------------------------------------
084:
085: public void free() {
086: }
087:
088: // ---------------------------------------------------------------------------
089:
090: /**
091: * <p>Process PageSet pointed by Atom and dumps result to disk</p>
092: * <p>Base workareas path is taken from "workareasput" property of hipergate.cnf<p>
093: * <p>Processed documents are saved under /web/workareas/apps/Mailwire/html/<i>gu_pageset</i>/</p>
094: * @param oAtm Atom holding a reference to PageSet instance to be dumped<br>
095: * Atom must have the following parameters set:<br>
096: * <table border=1 cellpadding=4>
097: * <tr><td>gu_workarea</td><td>GUID of WorkArea owner of document to be saved</td></tr>
098: * <tr><td>gu_pageset</td><td>GUID of PageSet to be saved</td></tr>
099: * <tr><td>nm_pageset</td><td>Name of PageSet document instance to be saved</td></tr>
100: * </table>
101: * @return String containing the final pos-processed document
102: * @throws IOException
103: */
104:
105: public Object process(Atom oAtm) throws IOException {
106:
107: File oFile; // Document Template File
108: FileReader oFileRead; // Document Template Reader
109: String sPathHTML; // Full Path to Document Template File
110: char cBuffer[]; // Internal Buffer for Document Template File Data
111: Object oReplaced; // Document Template File Data after FastStreamReplacer processing
112:
113: final String sSep = System.getProperty("file.separator"); // Alias for file.separator
114:
115: if (DebugFile.trace) {
116: DebugFile.writeln("Begin FileDumper.process([Job:"
117: + getStringNull(DB.gu_job, "") + ", Atom:"
118: + String.valueOf(oAtm.getInt(DB.pg_atom)) + "])");
119: DebugFile.incIdent();
120: }
121:
122: if (bHasReplacements) { // Initially the document is assumed to have tags to replace
123:
124: // *************************************************
125: // Compose the full path to document template file
126:
127: // First get the storage base path from hipergate.cnf
128: sPathHTML = getProperty("workareasput");
129: if (!sPathHTML.endsWith(sSep))
130: sPathHTML += sSep;
131:
132: // Concatenate PageSet workarea guid and subpath to Mailwire application directory
133: sPathHTML += getParameter("gu_workarea") + sSep + "apps"
134: + sSep + "Mailwire" + sSep + "html" + sSep
135: + getParameter("gu_pageset") + sSep;
136:
137: // Concatenate PageSet Name
138: sPathHTML += getParameter("nm_pageset").replace(' ', '_')
139: + ".html";
140:
141: if (DebugFile.trace)
142: DebugFile.writeln("PathHTML = " + sPathHTML);
143:
144: // *************************************************
145: // Call FastStreamReplacer for {#Section.Field} tags
146:
147: oReplaced = oReplacer.replace(sPathHTML, oAtm.getItemMap());
148:
149: // Count number of replacements done and update bHasReplacements flag accordingly
150: bHasReplacements = (oReplacer.lastReplacements() > 0);
151: }
152:
153: else {
154:
155: oReplaced = null;
156:
157: if (null != oFileStr)
158: oReplaced = oFileStr.get();
159:
160: if (null == oReplaced) {
161:
162: // If document template has no database replacement tags
163: // then just cache the document template into a SoftReference String
164:
165: // Compose the full path to document template file
166: sPathHTML = getProperty("workareasput");
167: if (!sPathHTML.endsWith(sSep))
168: sPathHTML += sSep;
169: sPathHTML += getParameter("gu_workarea") + sSep
170: + "apps" + sSep + "Mailwire" + sSep + "html"
171: + sSep + getParameter("gu_pageset") + sSep
172: + getParameter("nm_pageset").replace(' ', '_')
173: + ".html";
174:
175: if (DebugFile.trace)
176: DebugFile.writeln("PathHTML = " + sPathHTML);
177:
178: // ***************************
179: // Read document template file
180:
181: oFile = new File(sPathHTML);
182:
183: cBuffer = new char[new Long(oFile.length()).intValue()];
184:
185: oFileRead = new FileReader(oFile);
186: oFileRead.read(cBuffer);
187: oFileRead.close();
188:
189: if (DebugFile.trace)
190: DebugFile.writeln(String.valueOf(cBuffer.length)
191: + " characters readed");
192:
193: // *********************************************************
194: // Assign SoftReference to File cached in-memory as a String
195:
196: oReplaced = new String(cBuffer);
197: oFileStr = new SoftReference(oReplaced);
198:
199: } // fi (oReplaced)
200:
201: } // fi (bHasReplacements)
202:
203: // ***********************************************
204: // Write down to disk the final replaced file data
205:
206: // Compose job directory path
207: String sPathJobDir = getProperty("storage");
208: if (!sPathJobDir.endsWith(sSep))
209: sPathJobDir += sSep;
210: sPathJobDir += "jobs" + sSep + getParameter("gu_workarea")
211: + sSep + getString(DB.gu_job) + sSep;
212:
213: // Write final file data for each atom processed
214: FileWriter oFileWrite = new FileWriter(sPathJobDir
215: + getString(DB.gu_job) + "_"
216: + String.valueOf(oAtm.getInt(DB.pg_atom)) + ".html",
217: true);
218: oFileWrite.write((String) oReplaced);
219: oFileWrite.close();
220:
221: // Decrement de count of atoms peding of processing at this job
222: iPendingAtoms--;
223:
224: if (DebugFile.trace) {
225: DebugFile.writeln("End FileDumper.process([Job:"
226: + getStringNull(DB.gu_job, "") + ", Atom:"
227: + String.valueOf(oAtm.getInt(DB.pg_atom)) + "])");
228: DebugFile.decIdent();
229: }
230:
231: return oReplaced;
232: } // process
233:
234: } // FileDumper
|