001: /*
002: * snapper Enhydra super-servlet business object
003: */
004:
005: package org.enhydra.snapperAdmin.business;
006:
007: import java.io.File;
008: import java.sql.Timestamp;
009: import java.util.Properties;
010: import java.util.Vector;
011:
012: import org.enhydra.snapper.SnapperManager;
013: import org.enhydra.snapper.api.Reader;
014: import org.enhydra.snapperAdmin.SnapperAdmin;
015: import org.enhydra.snapperAdmin.spec.FileType;
016: import org.enhydra.snapperAdmin.spec.IndexerBaseSpec;
017: import org.enhydra.snapperAdmin.spec.Path;
018: import org.enhydra.snapperAdmin.spec.ReaderFactory;
019: import org.enhydra.snapperAdmin.spec.Site;
020: import org.enhydra.snapperAdmin.spec.SiteList;
021: import org.enhydra.snapperAdmin.spec.SiteListFactory;
022: import org.enhydra.snapperAdmin.spec.Optimize;
023: import org.enhydra.snapperAdmin.spec.OptimizerSpec;
024: import org.enhydra.snapperAdmin.spec.OptimizerSpecFactory;
025:
026: public class OptimizeThread implements Optimize, Runnable {
027: String siteID, siteName;
028:
029: Site site = null;
030:
031: Path[] pathArray = null;
032:
033: OptimizerSpec optimizer = null;
034: IndexerBaseSpec indexerBase = null;
035:
036: String indexdir = "";
037:
038: public void setDoStop(boolean n) throws Exception {
039: optimizer.setDoStop(n);
040: SnapperManager.getInstance().removeThread(siteID);
041: optimizer = null;
042: }
043:
044: public void run() {
045: try {
046:
047: Timestamp sts = new Timestamp(System.currentTimeMillis());
048:
049: try {
050: SiteList sl = SiteListFactory
051: .getSiteList("org.enhydra.snapperAdmin.business.SiteListImpl");
052: site = sl.findSiteByName(siteID);
053:
054: if (site == null) {
055: SnapperAdmin
056: .logWarrning("Error getting site : "
057: + siteID
058: + " aborting optimize process !!!");
059: return;
060: }
061:
062: pathArray = site.getPathList();
063: Properties prop = new Properties();
064:
065: try {
066:
067: if (site.getDocumentGroup() != null) {
068: Vector listDocGrop = (Vector) site
069: .getDocumentGroup()
070: .getListOfDocumentGroup();
071:
072: if (listDocGrop != null) {
073: for (int j = 0; j < listDocGrop.size(); j++) {
074: FileType[] listPT = site
075: .getDocumentGroup()
076: .getListOfFILETYPES(
077: (String) listDocGrop
078: .elementAt(j));
079: if (listPT != null) {
080: for (int k = 0; k < listPT.length; k++) {
081: if (!prop.containsKey(listPT[k]
082: .getEXTENSION()))
083: prop
084: .setProperty(
085: listPT[k]
086: .getEXTENSION(),
087: listPT[k]
088: .getPARSER());
089: }
090: }
091:
092: }
093: }
094: }
095: } catch (Exception e) {
096: SnapperAdmin
097: .logError("Error getting site parameters!");
098: }
099:
100: siteName = site.getNAME();
101: optimizer = OptimizerSpecFactory
102: .getOptimizer("org.enhydra.snapperAdmin.business.Optimizer");
103: long temp1 = optimizer.readTimeFile(site.getINDEXDIR());
104: Timestamp temp = new Timestamp(temp1);
105: if (temp1 == 0)
106: temp = sts;
107: site.setStartTime(temp);
108: optimizer.updateTimeFile(site.getINDEXDIR(), sts);
109: } catch (Exception ex) {
110: }
111:
112: try {
113: SnapperAdmin.logInfo("OPTIMIZER VERSION 1.1 STARTED");
114: SnapperAdmin.logInfo("Optimizing index for site "
115: + site.getNAME() + " started");
116: } catch (Exception e) {
117: System.out.println("Optimizing index for site "
118: + siteName + " started");
119: }
120:
121: try {
122: optimizer.optimizeIndex(site, pathArray);
123: } catch (Exception ex) {
124: SnapperAdmin.logError("Could not lock site");
125: }
126:
127: Timestamp ts = new Timestamp(System.currentTimeMillis());
128:
129: try {
130: Reader reader = ReaderFactory
131: .createReader("org.enhydra.snapper.wrapper.lucene.LuceneReader");
132: /* if (SnapperAdmin.getIndexType().equals("DataBase")) {
133: LogicalDatabase ldb = dbm.findLogicalDatabase("luceneDB");
134: reader.setUpDBReader(ldb, siteName);
135: } else {*/
136: if (site.getINDEXDIR().length() == 0) {
137: reader.setUpReader(SnapperAdmin.getIndexDir()
138: + File.separator + site.getNAME());
139: if (SnapperAdmin.getForceIndexUnlock())
140: reader.unlock();
141: } else {
142: reader.setUpReader(site.getINDEXDIR());
143: if (SnapperAdmin.getForceIndexUnlock())
144: reader.unlock();
145: }
146: // }
147:
148: // sites table are accessed from snapperAdmin and snapper application simultaneously so...
149: boolean allOk = false;
150:
151: SnapperManager.getInstance().getIndexerFactory()
152: .removeIndexer();
153:
154: reader.closeReader();
155: reader = null;
156: } catch (Exception e) {
157: SnapperAdmin.logDebug("Could not save NotIndexed");
158: }
159:
160: try {
161: String path = SnapperManager.getInstance().getTempDir()
162: + File.separator
163: + Thread.currentThread().getName();
164: File siteTempDir = new File(path);
165: FinalDeleter.deleteDir(siteTempDir);
166: } catch (Exception e) {
167: SnapperAdmin
168: .logError("Problem occured while delete Snapper Temp directory");
169: }
170:
171: SnapperAdmin.logInfo("Optimizing index for site "
172: + siteName + " finished");
173: SnapperAdmin.logInfo("Total time = "
174: + (ts.getTime() - sts.getTime()) + " milliseconds");
175:
176: SnapperManager.getInstance().removeThread(this .siteID);
177:
178: } finally {
179:
180: }
181: }
182:
183: public void stop() {
184: }
185:
186: public OptimizeThread() {
187: }
188:
189: public void optimize(String siteOID) {
190: this.siteID = siteOID;
191: }
192:
193: }
|