001: /*
002: * snapper
003: *
004: * Enhydra super-servlet business object
005: *
006: */
007:
008: package org.enhydra.snapperAdmin.business;
009:
010: // Enhydra SuperServlet specification imports
011:
012: import java.io.BufferedOutputStream;
013: import java.io.BufferedReader;
014: import java.io.File;
015: import java.io.FileInputStream;
016: import java.io.FileOutputStream;
017: import java.io.FileReader;
018: import java.io.InputStreamReader;
019: import java.sql.Timestamp;
020: import java.util.StringTokenizer;
021: import java.util.Vector;
022:
023: import org.enhydra.snapperAdmin.SnapperAdmin;
024: import org.enhydra.snapperAdmin.spec.Indexed;
025:
026: public class IndexedImpl implements Indexed {
027:
028: String id = "";
029:
030: String doc = "";
031:
032: String name = "";
033:
034: long time = 0;
035:
036: long lastStart = 0;
037:
038: String lastType = "";
039:
040: public String getID() {
041: return id;
042: }
043:
044: public void setID(String str) {
045: id = str;
046: }
047:
048: public String getDoc() {
049: return doc;
050: }
051:
052: public void setDoc(String num) {
053: doc = num;
054: }
055:
056: public String getName() {
057: return name;
058: }
059:
060: public void setName(String str) {
061: name = str;
062: }
063:
064: public void save() {
065: //site name , Document No. , Type , Started , Finished , Length
066: String line = name + "|" + doc + "|" + lastType + "|"
067: + lastStart + "|" + time + "\n";
068: writeNotIndexedDocumentToFile(line);
069:
070: }
071:
072: public void clear() {
073: BufferedOutputStream resultOS = null;
074:
075: try {
076: String outputFileS = SnapperAdmin.getLogDirectory();
077: outputFileS = outputFileS + "Indexed.txt";
078: File outputFile = new File(outputFileS);
079:
080: if (outputFile.exists()) {
081:
082: resultOS = new BufferedOutputStream(
083: new FileOutputStream(outputFile, false));
084: String empty = "";
085: resultOS.write(empty.getBytes());
086: resultOS.flush();
087: }
088:
089: } catch (Exception e) {
090: SnapperAdmin
091: .logError("Problem ocured while trying to clear Indexed log file "
092: + e.getMessage());
093: } finally {
094:
095: if (resultOS != null) {
096: try {
097: resultOS.close();
098: } catch (Exception e) {
099: }
100:
101: resultOS = null;
102: }
103: }
104: }
105:
106: public long getTime() {
107: return time;
108: }
109:
110: public void setTime(long str) {
111: time = str;
112: }
113:
114: public Indexed[] getList() {
115:
116: BufferedReader d = null;
117:
118: try {
119: Indexed[] iArray = null;
120:
121: Vector result = new Vector();
122:
123: String inputFileS = SnapperAdmin.getLogDirectory();
124: inputFileS = inputFileS + "Indexed.txt";
125: File inputFile = new File(inputFileS);
126:
127: if (!inputFile.exists())
128: return null;
129:
130: FileInputStream fis = new FileInputStream(inputFile);
131: d = new BufferedReader(new InputStreamReader(fis));
132:
133: String curentObject = null;
134:
135: while ((curentObject = d.readLine()) != null) {
136: Indexed current = new IndexedImpl();
137:
138: StringTokenizer st = new StringTokenizer(curentObject,
139: "|", false);
140: //site name , Document No. , Type , Started , Finished , Length
141: if (st.hasMoreTokens()) {
142: String temp = st.nextToken();
143: current.setName(temp);
144: } else {
145: current.setName("");
146: }
147:
148: if (st.hasMoreTokens()) {
149: String temp = st.nextToken();
150: current.setDoc(temp);
151: } else {
152: current.setDoc("");
153: }
154:
155: if (st.hasMoreTokens()) {
156: String temp = st.nextToken();
157: current.setLASTTYPE(temp);
158: } else {
159: current.setLASTTYPE("");
160: }
161:
162: if (st.hasMoreTokens()) {
163: String temp = st.nextToken();
164: long temp1 = 0;
165: try {
166: temp1 = (new Long(temp)).longValue();
167: } catch (Exception e) {
168: temp1 = 0;
169: }
170: current.setLASTSTART(temp1);
171: } else {
172: current.setLASTSTART(0);
173: }
174:
175: if (st.hasMoreTokens()) {
176: String temp = st.nextToken();
177: long temp1 = 0;
178: try {
179: temp1 = (new Long(temp)).longValue();
180: } catch (Exception e) {
181: temp1 = 0;
182: }
183: current.setTime(temp1);
184: } else {
185: current.setTime(0);
186: }
187:
188: result.add(current);
189: }
190:
191: iArray = new IndexedImpl[result.size()];
192:
193: for (int k = 0; k < result.size(); k++) {
194:
195: iArray[k] = (IndexedImpl) result.elementAt(k);
196:
197: }
198:
199: return iArray;
200:
201: } catch (Exception e) {
202: SnapperAdmin
203: .logError("Problem ocured while trying to read log not indexed file "
204: + e.getMessage());
205: return null;
206: } finally {
207: if (d != null) {
208: try {
209: d.close();
210: } catch (Exception e) {
211: }
212:
213: d = null;
214: }
215: }
216:
217: }
218:
219: public long getLASTSTART() {
220: return lastStart;
221: }
222:
223: public void setLASTSTART(long str) {
224: lastStart = str;
225: }
226:
227: public String getLASTTYPE() {
228: return lastType;
229: }
230:
231: public void setLASTTYPE(String str) {
232: lastType = str;
233: }
234:
235: private void writeNotIndexedDocumentToFile(String line) {
236: BufferedOutputStream resultOS = null;
237:
238: try {
239: String outputFileS = SnapperAdmin.getLogDirectory();
240: outputFileS = outputFileS + "Indexed.txt";
241: File outputFile = new File(outputFileS);
242:
243: if (!outputFile.exists())
244: outputFile.createNewFile();
245:
246: resultOS = new BufferedOutputStream(new FileOutputStream(
247: outputFile, true));
248: resultOS.write(line.getBytes());
249: resultOS.flush();
250:
251: } catch (Exception e) {
252: SnapperAdmin
253: .logError("Problem ocured while trying to log not indexed file "
254: + e.getMessage());
255: } finally {
256: if (resultOS != null) {
257: try {
258: resultOS.close();
259: } catch (Exception e) {
260: }
261:
262: resultOS = null;
263: }
264: }
265: }
266:
267: public void createTimeFile(String path, Timestamp time) {
268: BufferedOutputStream resultOS = null;
269: String timeFileName = path;
270: String timeString = String.valueOf(time.getTime());
271: timeFileName = timeFileName + File.separator + "timeFile.txt";
272:
273: try {
274: File timeFile = new File(timeFileName);
275: if (timeFile.exists())
276: timeFile.delete();
277:
278: timeFile.createNewFile();
279:
280: resultOS = new BufferedOutputStream(new FileOutputStream(
281: timeFile, true));
282: resultOS.write(timeString.getBytes());
283: resultOS.flush();
284: } catch (Exception e) {
285: SnapperAdmin.logError("Couldn't create timeIndex file!!!");
286: } finally {
287: if (resultOS != null) {
288: try {
289: resultOS.close();
290: } catch (Exception e) {
291: }
292:
293: resultOS = null;
294: }
295: }
296: }
297:
298: public long readTimeFile(String path) {
299: long time = 0;
300: String timeFileName = path;
301: StringBuffer contents = new StringBuffer();
302: BufferedReader input = null;
303: String timeString = "";
304: timeFileName = timeFileName + File.separator + "timeFile.txt";
305:
306: try {
307: File timeFile = new File(timeFileName);
308: if (!timeFile.exists())
309: return 0;
310: input = new BufferedReader(new FileReader(timeFile));
311: while ((timeString = input.readLine()) != null) {
312: contents.append(timeString);
313: }
314: } catch (Exception e) {
315: SnapperAdmin.logError("Couldn't create timeIndex file!!!");
316: return 0;
317: } finally {
318: try {
319: if (input != null) {
320: //flush and close both "input" and its underlying FileReader
321: input.close();
322: }
323: } catch (Exception ex) {
324: ex.printStackTrace();
325: }
326: }
327: timeString = contents.toString();
328: time = Long.parseLong(timeString);
329: return time;
330: }
331:
332: }
|