1 2 package org.enhydra.snapper.business; 3 4 import java.io.File ; 5 import java.sql.Timestamp ; 6 import java.util.Vector ; 7 8 import org.enhydra.dods.DODS; 9 import org.enhydra.dods.DODSException; 10 import org.enhydra.snapper.SnapperAdmin; 11 import org.enhydra.snapper.SnapperManager; 12 import org.enhydra.snapper.api.Reader; 13 import org.enhydra.snapper.spec.Index; 14 import org.enhydra.snapper.spec.Indexed; 15 import org.enhydra.snapper.spec.IndexedFactory; 16 import org.enhydra.snapper.spec.IndexerBaseSpec; 17 import org.enhydra.snapper.spec.IndexerBaseSpecFactory; 18 import org.enhydra.snapper.spec.NotIndexed; 19 import org.enhydra.snapper.spec.NotIndexedFactory; 20 import org.enhydra.snapper.spec.Path; 21 import org.enhydra.snapper.spec.PathFactory; 22 import org.enhydra.snapper.spec.PathType; 23 import org.enhydra.snapper.spec.PathTypeFactory; 24 import org.enhydra.snapper.spec.ReaderFactory; 25 import org.enhydra.snapper.spec.Site; 26 import org.enhydra.snapper.spec.SiteFactory; 27 28 import com.lutris.appserver.server.sql.DBTransaction; 29 import com.lutris.appserver.server.sql.DatabaseManager; 30 31 32 36 37 38 public class IndexImpl implements Index, Runnable { 39 40 String siteOID; 41 42 Site site = null; 43 44 Path[] pathArray = null; 45 46 PathType pathType = null; 47 48 IndexerBaseSpec indexerBase = null; 49 50 String indexdir = ""; 51 52 DBTransaction dbTransaction = null; 53 54 private boolean doStop, indexed; 55 56 private DatabaseManager dbm; 57 58 public void setDoStop(boolean n) throws Exception { 59 doStop = n; 60 indexerBase.setDoStop(n); 61 site.setLOCKED(null); 62 site.save(); 63 dbTransaction.commit(); 64 SnapperManager.getInstance().removeThread(siteOID); 65 dbTransaction.release(); 66 indexerBase = null; 67 68 } 69 70 public void run() { 71 try { 72 Timestamp sts = new Timestamp (System.currentTimeMillis()); 73 74 try { 75 dbTransaction = dbm.createTransaction(); 76 } catch (Exception ex) { 77 System.out.println("Could not init transaction"); 78 } 79 80 try { 82 Path pt = PathFactory.getPath("org.enhydra.snapper.business.PathImpl"); 83 pathArray = pt.getListForID(dbTransaction, siteOID); 84 pathType = PathTypeFactory.getPathType("org.enhydra.snapper.business.PathTypeImpl"); 85 86 } catch (Exception ex) { 87 System.out.println("Exception: " + ex); 88 pathArray = null; 89 } 90 91 try { 93 site = (SiteFactory.getSite("org.enhydra.snapper.business.SiteImpl")).findSiteByID(siteOID, 94 dbTransaction); 95 } catch (Exception ex) { 96 System.out.println("Could not findSiteByID"); 97 } 98 99 try { 100 if (site.getLOCKED() == true) { 101 102 dbTransaction.release(); 103 indexerBase = null; 104 105 SnapperManager.getInstance().getLoggingManager().info("Site locked! Indexing Aborted."); 106 return; 107 } 108 String indexer = SnapperAdmin.getIdexerClassName(); 109 indexerBase = IndexerBaseSpecFactory.getIndexerBase("org.enhydra.snapper.business.IndexerBase"); 110 indexerBase.initChecker(site.getAge(), site.getSize(), site); 111 indexerBase.setDBM(dbm); 112 113 SnapperManager.getInstance().getLoggingManager().info("Indexing site: " + site.getName() + " started"); 114 115 site.setLOCKED("true"); 116 site.save(); 117 dbTransaction.commit(); 118 String filterORinclude = ""; 119 String db = "", table = "", column = ""; 120 121 if (site.getFILTERDB() != null && site.getFILTERTABLE() != null && site.getFILTERCOLUMN() != null 122 && site.getFILTERDB().length() != 0 && site.getFILTERTABLE().length() != 0 123 && site.getFILTERCOLUMN().length() != 0) { 124 indexerBase.setExclude(site.getFILTERDB(), site.getFILTERTABLE(), site.getFILTERCOLUMN()); 125 } 126 ; 127 128 if (site.getMETADB().length() != 0 && site.getMETATABLE().length() != 0 129 && site.getMETAFILE().length() != 0 && site.getMETAKEY().length() != 0 130 && site.getMETAVALUE().length() != 0) { 131 indexerBase.setMeta(site.getMETADB(), site.getMETATABLE(), site.getMETAFILE(), site.getMETAKEY(), 132 site.getMETAVALUE()); 133 } 134 135 if (site.getINDEXDIR().length() == 0) { 136 indexerBase.setUpIndexer(true, SnapperAdmin.getIndexDir() + File.separator + site.getName(), site 137 .getLanguage(), true, indexer); 138 indexdir = SnapperAdmin.getIndexDir() + File.separator + site.getName(); 139 } else { 140 indexerBase.setUpIndexer(true, site.getINDEXDIR(), site.getLanguage(), true, indexer); 141 indexdir = site.getINDEXDIR(); 142 } 143 if (site.getINCLUDEDB() != null && site.getINCLUDEDB().length() != 0 && site.getINCLUDETABLE() != null 144 && site.getINCLUDETABLE().length() != 0 && site.getINCLUDECOLUMN() != null 145 && site.getINCLUDECOLUMN().length() != 0 && site.getINCLUDECOLUMNMODIFIED() != null 146 && site.getINCLUDECOLUMNMODIFIED().length() != 0) { 147 148 indexerBase.setInclude(site.getINCLUDEDB(), site.getINCLUDETABLE(), site.getINCLUDECOLUMN(), site 149 .getINCLUDECOLUMNMODIFIED()); 150 indexerBase.indexInclude(); 151 site.setLOCKED(null); 152 site.save(); 153 dbTransaction.commit(); 154 indexed = true; 157 158 } 160 161 } catch (Exception ex) { 162 try { 163 site.setLOCKED(null); 164 site.save(); 165 dbTransaction.commit(); 166 } catch (Exception e) { 167 ex.printStackTrace(); 168 e.printStackTrace(); 169 } 170 } 173 174 if (!indexed) { 176 177 Vector pv = new Vector (); 178 for (int i = 0; i < pathArray.length; i++) { 179 if (doStop) 180 return; 181 try { 182 if ((pathType.findPathTypeByID(pathArray[i].getType(), dbTransaction)).getName().equals( 183 "FileSystem")) { 184 indexerBase.indexDocs(site.getID(), site.getName(), "", "", pathArray[i].getRoot(), 185 "FileSystem", "", "", true); 186 if (doStop) 187 return; 188 } 189 if ((pathType.findPathTypeByID(pathArray[i].getType(), dbTransaction)).getName().equals("FTP")) { 190 191 indexerBase.indexDocs(site.getID(), site.getName(), pathArray[i].getHost(), "", 192 pathArray[i].getRoot(), "FTP", pathArray[i].getUser(), pathArray[i] 193 .getPass(), true); 194 if (doStop) 195 return; 196 } 197 if ((pathType.findPathTypeByID(pathArray[i].getType(), dbTransaction)).getName().equals("UNC")) { 198 199 indexerBase.indexDocs(site.getID(), site.getName(), pathArray[i].getHost(), "", 200 pathArray[i].getRoot(), "UNC", pathArray[i].getUser(), pathArray[i] 201 .getPass(), true); 202 if (doStop) 203 return; 204 } 205 206 } catch (Exception e) { 207 e.printStackTrace(); 208 } 209 } 210 } 211 212 indexerBase.optimize(); 213 214 Timestamp ts = new Timestamp (System.currentTimeMillis()); 215 Vector vni = new Vector (); 216 NotIndexed ni = null; 217 Indexed in = null; 218 Reader reader = null; 219 220 try { 221 site.setLASTUPDATE(ts); 222 reader = ReaderFactory.createReader("org.enhydra.snapper.wrapper.lucene.LuceneReader"); 223 reader.setUpReader(indexdir); 224 225 site.setFILENR(reader.getSize()); 226 site.setLOCKED(null); 227 site.save(); 228 SnapperManager.getInstance().getLoggingManager().info("Indexing site: " + site.getName() + " finished"); 229 dbTransaction.commit(); 230 reader.closeReader(); 231 reader = null; 232 233 in = IndexedFactory.getIndexed("org.enhydra.snapper.business.IndexedImpl"); 234 in.createNew(dbTransaction); 235 in.setName(site.getName()); 236 in.setLASTSTART(sts); 237 in.setLASTTYPE("INDEX"); 238 in.setTime(ts); 239 in.setDoc(String.valueOf(site.getFILENR())); 240 in.save(); 241 242 dbTransaction.commit(); 243 244 ni = NotIndexedFactory.getNotIndexed("org.enhydra.snapper.business.NotIndexedImpl"); 245 246 vni = indexerBase.getNotIndexed(); 247 for (int i = 0; i < vni.size(); i++) { 248 ni.createNew(dbTransaction); 249 ni.setIndexed(in.getID()); 250 ni.setLink(vni.elementAt(i).toString()); 251 ni.save(); 252 253 } 254 indexerBase.close(); 255 dbTransaction.commit(); 256 dbTransaction.release(); 257 indexerBase = null; 258 259 vni.removeAllElements(); 260 vni = null; 261 ni = null; 262 263 SnapperManager.getInstance().removeThread(siteOID); 264 } catch (Exception e) { 265 e.printStackTrace(); 266 } 267 268 finally { 269 if (indexerBase != null) { 270 indexerBase = null; 271 } 272 if (vni != null) { 273 vni.removeAllElements(); 274 vni = null; 275 } 276 277 if (ni != null) { 278 ni = null; 279 } 280 281 if (in != null) { 282 in = null; 283 } 284 if (reader != null) { 285 reader = null; 286 } 287 if (site != null) { 288 site = null; 289 } 290 } 291 } finally { 292 try { 293 DODS.unregister(Thread.currentThread()); 294 } catch (DODSException e) { 295 e.printStackTrace(); 296 } 297 } 298 299 } 300 301 public void stop() { 302 } 303 304 public IndexImpl() { 305 } 306 307 public void index(String siteOID, DatabaseManager dbm) { 308 this.siteOID = siteOID; 309 this.dbm = dbm; 310 } 311 312 } | Popular Tags |