001: package org.enhydra.snapperAdmin.business;
002:
003: import java.io.BufferedOutputStream;
004: import java.io.BufferedReader;
005: import java.io.File;
006: import java.io.FileOutputStream;
007: import java.io.FileReader;
008: import java.io.IOException;
009: import java.sql.Timestamp;
010:
011: import org.enhydra.snapper.SnapperManager;
012: import org.enhydra.snapper.api.Indexer;
013: import org.enhydra.snapperAdmin.SnapperAdmin;
014: import org.enhydra.snapperAdmin.spec.Path;
015: import org.enhydra.snapperAdmin.spec.Site;
016: import org.enhydra.snapperAdmin.spec.OptimizerSpec;
017:
018: public class Optimizer implements OptimizerSpec {
019:
020: private boolean doStop;
021:
022: public void optimizeIndex(Site site, Path[] pathArray) {
023: Indexer indexer = SnapperManager.getInstance()
024: .getIndexerFactory().newIndexer();
025: String dir = site.getINDEXDIR();
026: if (SnapperAdmin.getForceIndexUnlock()) {
027: SnapperAdmin.logInfo("Unlocking indexer!!!");
028: indexer.unlock(dir);
029: }
030: SnapperAdmin.logInfo("Setting up indexer!!!");
031: try {
032: indexer.setUpIndexer(dir, "EN", false, SnapperAdmin
033: .getmaxFieldLength(), SnapperAdmin
034: .getIndexOSspecific());
035: } catch (IOException e) {
036: // TODO Auto-generated catch block
037: e.printStackTrace();
038: }
039: SnapperAdmin.logInfo("Beginning of optimizing procedure!!!");
040: indexer.optimize();
041: SnapperAdmin.logInfo("End of optimizing procedure!!!");
042: indexer.close();
043: }
044:
045: public void setDoStop(boolean n) {
046: doStop = n;
047: }
048:
049: public void updateTimeFile(String path, Timestamp time) {
050: BufferedOutputStream resultOS = null;
051: String timeFileName = path;
052: String timeString = String.valueOf(time.getTime());
053: timeFileName = timeFileName + File.separator + "timeFile.txt";
054:
055: try {
056: File timeFile = new File(timeFileName);
057: if (timeFile.exists())
058: timeFile.delete();
059:
060: timeFile.createNewFile();
061:
062: resultOS = new BufferedOutputStream(new FileOutputStream(
063: timeFile, true));
064: resultOS.write(timeString.getBytes());
065: resultOS.flush();
066: } catch (Exception e) {
067: SnapperAdmin.logError("Couldn't update timeIndex file!!!");
068: } finally {
069: if (resultOS != null) {
070: try {
071: resultOS.close();
072: } catch (Exception e) {
073: }
074:
075: resultOS = null;
076: }
077: }
078: }
079:
080: public long readTimeFile(String path) {
081: long time = 0;
082: String timeFileName = path;
083: StringBuffer contents = new StringBuffer();
084: BufferedReader input = null;
085: String timeString = "";
086: timeFileName = timeFileName + File.separator + "timeFile.txt";
087:
088: try {
089: File timeFile = new File(timeFileName);
090: if (!timeFile.exists())
091: return 0;
092: input = new BufferedReader(new FileReader(timeFile));
093: while ((timeString = input.readLine()) != null) {
094: contents.append(timeString);
095: }
096: } catch (Exception e) {
097: SnapperAdmin.logError("Couldn't create timeIndex file!!!");
098: return 0;
099: } finally {
100: try {
101: if (input != null) {
102: //flush and close both "input" and its underlying FileReader
103: input.close();
104: }
105: } catch (Exception ex) {
106: ex.printStackTrace();
107: }
108: }
109: timeString = contents.toString();
110: time = Long.parseLong(timeString);
111: return time;
112: }
113: }
|