KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > business > Updater


1 /*
2  * Created on Mar 21, 2005
3  */

4 package org.enhydra.snapper.business;
5
6 /**
7  * @author Igor Smirnov
8  *
9  */

10
11 import java.io.File JavaDoc;
12 import java.sql.Timestamp JavaDoc;
13 import java.util.Date JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.TreeSet JavaDoc;
19 import java.util.Vector JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 import org.enhydra.snapper.SnapperAdmin;
23 import org.enhydra.snapper.SnapperManager;
24 import org.enhydra.snapper.api.*;
25 import org.enhydra.snapper.parsers.Parser;
26 import org.enhydra.snapper.spec.*;
27
28 import com.lutris.appserver.server.sql.DBTransaction;
29 import com.lutris.appserver.server.sql.DatabaseManager;
30
31
32
33 // searcher.
34

35
36 // check if files in Site paths exist in FSDirectory,
37
//
38
// FSDirectory dir = IndexReader.directory()
39
//
40
//
41
//
42
// 1. if doesnt't: parse, add to a Document
43
// dir.fileExists(String name);
44
// 2. if exists
45
// 2a. if check modified later than Site.getLASTUPDATE()
46
// dir.fileModified(String name)
47
// 2b. if modified later:
48
// Reader.delete(new Term("path", file.getPath()
49
// save a file to be added in a docToAdd Vector
50
// reader.close();
51

52
53 // init indexer (writer)
54
// indexer.setUpIndexer(siteName, language, false);
55

56 // for (int i = 0; i< docToAdd; docToAdd++)
57
// 1. parse file
58
// 2. indexer.indexDoc(file, contents, type, title);
59
//
60
// optimize
61
// close
62

63
64 public class Updater implements UpdaterSpec{
65     
66     Reader reader;
67     Vector JavaDoc notIndexed = new Vector JavaDoc();
68     
69     TreeSet JavaDoc filesToCheck = new TreeSet JavaDoc();
70     Vector JavaDoc filesToAdd = new Vector JavaDoc();
71     
72     Vector JavaDoc timestamps = new Vector JavaDoc();
73     Vector JavaDoc tempFiles = new Vector JavaDoc();
74     Hashtable JavaDoc ftpFilesToAdd = new Hashtable JavaDoc();
75     Vector JavaDoc origfiles = new Vector JavaDoc();
76     DocumentStore ds = null;
77     int documentNo = 0;
78     Vector JavaDoc filtered = new Vector JavaDoc();
79     
80     FileChecker fc;
81     Site site;
82     Path[] pathArray;
83     String JavaDoc metaDB ="", metaTable="", metaFileColumn="", metaKeyColumn="", metaValueColumn="";
84    private boolean doStop;
85    String JavaDoc siteName;
86    private DatabaseManager dbm;
87   
88    private String JavaDoc originalHost = "";
89     
90     public Updater(){
91     }
92     
93     public Updater(Site site, Path[] pathArray) {
94         this.site = site;
95         this.pathArray = pathArray;
96     }
97     
98     private void setUpReader() {
99         try {
100                 siteName = site.getName();
101                 reader = ReaderFactory.createReader("org.enhydra.snapper.wrapper.lucene.LuceneReader");
102                 if (site.getINDEXDIR().length()==0)
103                     reader.setUpReader(SnapperAdmin.getIndexDir() + File.separator + siteName);
104                 else reader.setUpReader(site.getINDEXDIR());
105                 
106             }
107             catch (Exception JavaDoc e) {
108                     SnapperManager.getInstance().getLoggingManager().error(
109                             "Could not setup reader");
110                     SnapperManager.getInstance().getLoggingManager().debug(e.toString());
111                     
112                 }
113       }
114     
115     public void updateIndex(Site site, Path[] pathArray){
116         this.site = site;
117         this.pathArray = pathArray;
118         // init Reader
119

120         try{
121             setUpReader();
122             initChecker(this.site.getAge(), this.site.getSize(), this.site);
123             documentNo = this.site.getFILENR();
124         }
125             catch(Exception JavaDoc ex) {
126                 
127                     SnapperManager.getInstance().getLoggingManager().error(
128                             "Could not setup reader");
129                     SnapperManager.getInstance().getLoggingManager().debug(ex.toString());
130                }
131             
132         // If Include list exists, consider only the files in the list
133
try{
134             if ( site.getINCLUDEDB() != null && site.getINCLUDEDB().length() != 0
135                     && site.getINCLUDETABLE() != null && site.getINCLUDETABLE().length() != 0
136                     && site.getINCLUDECOLUMN() != null && site.getINCLUDECOLUMN().length() != 0
137                     && site.getINCLUDECOLUMNMODIFIED() != null && site.getINCLUDECOLUMNMODIFIED().length() !=0) {
138                 
139                 BusinessUtil bu = new BusinessUtil(dbm);
140                 Timestamp JavaDoc start = new Timestamp JavaDoc(System.currentTimeMillis());
141                 TreeSet JavaDoc filesToCheck = bu.getIncludedFiles(site.getINCLUDEDB(), site.getINCLUDETABLE(), site.getINCLUDECOLUMN());
142                 Timestamp JavaDoc end = new Timestamp JavaDoc(System.currentTimeMillis());
143                 SnapperManager.getInstance().getLoggingManager().debug("Getting included files trajalo: " + (end.getTime() - start.getTime())
144                         + " total milliseconds");
145
146                 start = new Timestamp JavaDoc(System.currentTimeMillis());
147                 Map JavaDoc data = new HashMap JavaDoc(bu.getData(site.getINCLUDEDB(), site.getINCLUDETABLE(), site.getINCLUDECOLUMN(), site.getINCLUDECOLUMNMODIFIED(), reader.lastModified()));
148                 //Map data = new HashMap(bu.getData(site.getINCLUDEDB(), site.getINCLUDETABLE(), site.getINCLUDECOLUMN(), site.getINCLUDECOLUMNMODIFIED()));
149
end = new Timestamp JavaDoc(System.currentTimeMillis());
150                 SnapperManager.getInstance().getLoggingManager().debug("Getting metadata took: " + (end.getTime() - start.getTime())
151                         + " total milliseconds");
152                 site.setLOCKED(null);
153                 site.save();
154                 start = new Timestamp JavaDoc(System.currentTimeMillis());
155                 reader.checkDeleted(filesToCheck);
156                 end = new Timestamp JavaDoc(System.currentTimeMillis());
157                 SnapperManager.getInstance().getLoggingManager().debug("Check deleted took: " + (end.getTime() - start.getTime())
158                         + " total milliseconds");
159                 start = new Timestamp JavaDoc(System.currentTimeMillis());
160                 for (Iterator JavaDoc iterator = data.entrySet().iterator(); iterator.hasNext();){
161                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
162                         String JavaDoc path = (String JavaDoc)entry.getKey();
163                         filesToAdd.add(new File JavaDoc((String JavaDoc)entry.getKey()));
164                     
165                 }
166                 
167                 reader.closeReader();
168                 
169                 setUpReader();
170                 reader.removeDocuments(data);
171                 end = new Timestamp JavaDoc(System.currentTimeMillis());
172                 SnapperManager.getInstance().getLoggingManager().debug("Check timestamps took: " + (end.getTime() - start.getTime())
173                         + " total milliseconds");
174                 reader.closeReader();
175                 
176                 reader = null;
177                 Indexer indexer = SnapperManager.getInstance().getIndexerFactory().newIndexer();
178                 if (site.getINDEXDIR().length() == 0) {
179                 indexer.setUpIndexer(SnapperAdmin.getIndexDir() + File.separator + site.getName(), "EN", false, SnapperAdmin.getmaxFieldLength());
180             
181                 }
182                 else {
183                     indexer.setUpIndexer(site.getINDEXDIR(), "EN", false, SnapperAdmin.getmaxFieldLength());
184                 }
185                 indexer.optimize();
186                 indexer.close();
187                 
188                 if (filesToAdd.size() > 0)
189                     parseAndAdd("","","");
190                 //filesToCheck.removeAllElements();
191
filesToCheck = null;
192                 filesToAdd.removeAllElements();
193                 filesToAdd = null;
194                 tempFiles.removeAllElements();
195                 tempFiles = null;
196                 filtered.removeAllElements();
197                 filtered = null;
198                 bu.gc();
199                 bu = null;
200                 return;
201                 }
202             }
203             catch (Exception JavaDoc e) {
204                     SnapperManager.getInstance().getLoggingManager().error(
205                             "Could not update include list");
206                 }
207             
208         // list files in Site paths and store them in a Vector
209
for (int i = 0 ; i < pathArray.length; i++)
210             { if (doStop)
211                 {
212                     try{
213                     reader.closeReader();
214                     }
215                     catch (Exception JavaDoc e) {
216                         SnapperManager.getInstance().getLoggingManager().error(
217                         "Could not close reader files");
218                         }
219                     return;
220                 }
221                 try{
222                     PathType pathType;
223                     String JavaDoc pType;
224                     pathType = PathTypeFactory.getPathType("org.enhydra.snapper.business.PathTypeImpl");
225                     DBTransaction dbt = dbm.createTransaction();
226                     pType = (pathType.findPathTypeByID(pathArray[i].getType(), dbt)).getName();
227                     dbt.release();
228                 if (pType.equals("FTP"))
229                     {
230                     getFTPFiles(pathArray[i]);
231                     }
232                 else{
233                     getFiles(pathArray[i]);
234                     }
235                    }catch (Exception JavaDoc e) {
236                     SnapperManager.getInstance().getLoggingManager().error(
237                             "Could not get files");
238                }
239                 }
240             try{
241                 reader.checkDeleted(filesToCheck);
242                 Iterator JavaDoc itr = origfiles.iterator();
243                 while (itr.hasNext())
244                 {
245                     Object JavaDoc obj = itr.next();
246                     filesToCheck.remove(obj);
247                 }
248                 checkFTPFiles();
249                 checkFiles();
250             }
251             catch (Exception JavaDoc e) {
252                         SnapperManager.getInstance().getLoggingManager().error(
253                                 "Could not check files");
254                    }
255             try{
256                 reader.closeReader();
257                 parseAndAdd(site.getFILTERDB(), site.getFILTERTABLE(), site.getFILTERCOLUMN());
258                 
259                 filesToCheck = null;
260                 filesToAdd.removeAllElements();
261                 filesToAdd = null;
262                 origfiles.clear();
263                 origfiles = null;
264                 filtered.removeAllElements();
265                 filtered = null;
266                 ftpFilesToAdd.clear();
267                 ftpFilesToAdd = null;
268                 
269                 if(ds!=null)
270                 {
271                  ds.delTempFiles(tempFiles);
272                 }
273                 ds = null;
274               }
275             catch(Exception JavaDoc ex) {
276                 try {
277                     SnapperManager.getInstance().getLoggingManager().error(
278                             "Could not parse files");
279                     SnapperManager.getInstance().getLoggingManager().debug(ex.toString());
280                     
281                 } catch (Exception JavaDoc e) {
282                     System.out.println("Could not parse files");
283                     
284                 }}
285
286     }
287         
288
289     // list files in Site paths and store them in a Vector
290
public void getFiles(Path path){
291         
292
293         try {
294                 originalHost = path.getRoot();
295                 File JavaDoc loc = new File JavaDoc(path.getRoot());
296                 
297                 if (loc.canRead()) {
298                     if (loc.isDirectory()) {
299                         String JavaDoc[] files = loc.list();
300                         if (files != null) {
301                             for (int p = 0; p < files.length; p++) {
302                                 if (doStop) return;
303                                 
304                                 File JavaDoc fl = new File JavaDoc(loc, files[p]);
305                                     if (fl.isDirectory()) {
306                                         String JavaDoc[] fls = fl.list();
307                                         subfolders(fl, fls);
308                                     }
309                                 
310                                 if (fileOK(fl))
311                                     filesToCheck.add(fl.getPath());
312                             }
313                         }
314                     }
315                 }
316                 
317
318          } catch (Exception JavaDoc e) {
319
320                     SnapperManager.getInstance().getLoggingManager().error(
321                             "Could not get files");
322                     SnapperManager.getInstance().getLoggingManager().debug(e.toString());
323                 }
324         
325     }
326     public void getFTPFiles(Path path){
327     
328         try {
329             originalHost = path.getRoot();
330             ds = new DocumentStore(path.getUser(), path.getRoot(), "FTP", path.getHost(), "", path.getUser(), path.getPass());
331             ds.connect();
332             ds.retrieveFile(path.getRoot());
333
334                         timestamps = ds.getTimeStamps();
335                         tempFiles = ds.getTempFiles();
336                         origfiles = ds.getOriginalFiles();
337                         for (int i=0; i< origfiles.size(); i++){
338                             if (doStop){ds.disconnect();
339                                 return;
340                             }
341                             
342                        if (fileOK(new File JavaDoc(origfiles.elementAt(i).toString())))
343                              filesToCheck.add(origfiles.elementAt(i));
344                         }
345                         
346             
347             //checkFTPFiles();
348
ds.disconnect();
349          } catch (Exception JavaDoc e) {
350                 try {
351                     SnapperManager.getInstance().getLoggingManager().error(
352                             "Could not get files");
353                     SnapperManager.getInstance().getLoggingManager().debug(e.toString());
354                     
355                 } catch (Exception JavaDoc ex) {
356                     System.out.println("Could not get files");
357                     
358                 }}
359     }
360     
361         private void subfolders(File JavaDoc dir, String JavaDoc[] files){
362             if (files != null) {
363                 for (int q = 0; q < files.length; q++) {
364                     if (doStop) return;
365                     File JavaDoc fl = new File JavaDoc(dir, files[q]);
366                      if (fl.isDirectory()) {
367                         String JavaDoc[] fls = fl.list();
368                         subfolders(fl, fls);
369                     }
370                     
371                     if (fileOK(new File JavaDoc(dir, files[q])))
372                     {
373                          filesToCheck.add((new File JavaDoc(dir, files[q])).getPath());
374                     }
375                 }
376             }
377             
378         }
379         
380         
381         public boolean fileOK(File JavaDoc file){
382             return fc.check(file);
383         }
384         
385         public void initChecker(int age, int size, Site site){
386             try{
387                 fc = new FileChecker(age, size, site.getDOC(), site.getHTML(), site.getMSG(),
388                         site.getPDF(), site.getRTF(), site.getTXT(), site.getXLS(), site.getPPT(),
389                         site.getPPS(), site.getEML(), site.getZIP(), site.getOO(), site.getOO(), site.getOTHER());
390                 } catch (Exception JavaDoc e) {
391                     try {
392                         SnapperManager.getInstance().getLoggingManager().error(
393                                 "Could not setup FileChecker");
394                         SnapperManager.getInstance().getLoggingManager().debug(
395                         e.toString());
396                     } catch (Exception JavaDoc ex) {
397                         System.out.println("Could not setup FileChecker");
398                     }
399                 }
400         }
401         /*public void removeDocuments(Map data){
402             for (Iterator iterator = data.entrySet().iterator(); iterator.hasNext();){
403                 Map.Entry entry = (Map.Entry) iterator.next();
404                 
405                 try{
406                     String path = (String)entry.getKey();
407                     reader.removeDocuments(path);
408                     filesToAdd.add(new File(path));
409                 }
410                 catch (Exception ex) {
411                     SnapperManager.getInstance().getLoggingManager().error(
412                     "Could not remove document while updating");
413                 }
414             }
415         }*/

416         
417         public void checkFiles(){
418             boolean modified = false;
419             boolean exists = false;
420             String JavaDoc fileToCheck = "";
421
422             Iterator JavaDoc itr = filesToCheck.iterator();
423             while (itr.hasNext()){
424                 Object JavaDoc obj = itr.next();
425                 
426                 try{
427                 
428                         fileToCheck = String.valueOf(obj.toString());
429                         //String path = fileToCheck;
430
exists = reader.fileExists(fileToCheck);
431                 
432                 }catch (Exception JavaDoc ex){
433                     try {
434                         SnapperManager.getInstance().getLoggingManager().error(
435                                 "Could not check files");
436                         SnapperManager.getInstance().getLoggingManager().debug(ex.toString());
437                         
438                     } catch (Exception JavaDoc e) {
439                         System.out.println("Could not check files");
440                         
441                     }}
442
443                 if (exists){
444                     try{
445                         modified = reader.checkLastModified(new File JavaDoc(String.valueOf(obj.toString())));
446                     } catch (Exception JavaDoc ex){
447                         try {
448                             SnapperManager.getInstance().getLoggingManager().error(
449                                     "Could not check files");
450                             SnapperManager.getInstance().getLoggingManager().debug(ex.toString());
451                             
452                         } catch (Exception JavaDoc e) {
453                             System.out.println("Could not check files");
454                             
455                         }}
456                     if (modified)
457                         filesToAdd.add(new String JavaDoc(String.valueOf(obj.toString())));
458                 }
459                 else
460                     filesToAdd.add(new String JavaDoc(String.valueOf(obj.toString())));
461             }
462             
463         }
464         
465         public void checkFiles(Map JavaDoc data){
466             boolean modified = false;
467             boolean exists = false;
468             File JavaDoc fileToCheck = null;
469
470             
471             for (Iterator JavaDoc iterator = data.entrySet().iterator(); iterator.hasNext();){
472                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
473                 
474                 try{
475                     String JavaDoc path = (String JavaDoc)entry.getKey();
476                     exists = reader.fileExists(path);
477                 
478                 }catch (Exception JavaDoc ex){
479                     try {
480                         SnapperManager.getInstance().getLoggingManager().error(
481                                 "Could not check files");
482                         SnapperManager.getInstance().getLoggingManager().debug(ex.toString());
483                         
484                     } catch (Exception JavaDoc e) {
485                         System.out.println("Could not check files");
486                         
487                     }}
488
489                 if (exists){
490                     try{
491                         String JavaDoc path = (String JavaDoc)entry.getKey();
492                         long mod = ((Date JavaDoc)entry.getValue()).getTime();
493                         modified = reader.checkLastModified(path, mod);
494                     } catch (Exception JavaDoc ex){
495                         try {
496                             SnapperManager.getInstance().getLoggingManager().error(
497                                     "Could not check files");
498                             SnapperManager.getInstance().getLoggingManager().debug(ex.toString());
499                             
500                         } catch (Exception JavaDoc e) {
501                             System.out.println("Could not check files");
502                             
503                         }}
504                     if (modified)
505                         filesToAdd.add(new File JavaDoc((String JavaDoc)entry.getKey()));
506                 }
507                 else
508                     filesToAdd.add(new File JavaDoc((String JavaDoc)entry.getKey()));
509             }
510             
511         }
512         /////
513

514         public void checkFTPFiles(){
515             boolean modified = false;
516             boolean exists = false;
517             File JavaDoc fileToCheck = null;
518
519             Iterator JavaDoc itr = origfiles.iterator();
520             int i=-1;
521             while (itr.hasNext()){
522                 i++;
523                 Object JavaDoc obj = itr.next();
524                 try{
525                         if (((String JavaDoc)obj).endsWith("zip")
526                             || ((String JavaDoc)obj).endsWith("ZIP")) {
527                         continue;
528                     }
529                     exists = reader.fileExists((String JavaDoc)obj);
530                 } catch (Exception JavaDoc ex){
531                     try {
532                         SnapperManager.getInstance().getLoggingManager().error(
533                                 "Could not check files");
534                         SnapperManager.getInstance().getLoggingManager().debug(ex.toString());
535                         
536                     } catch (Exception JavaDoc e) {
537                         System.out.println("Could not check files");
538                         
539                     }}
540
541                 if (exists){
542                     try{
543                         modified = reader.checkLastModified((File JavaDoc)tempFiles.elementAt(i), ((Long JavaDoc) timestamps.elementAt(i)).longValue() );
544                     } catch (Exception JavaDoc ex){
545                         try {
546                             SnapperManager.getInstance().getLoggingManager().error(
547                                     "Could not check files");
548                             SnapperManager.getInstance().getLoggingManager().debug(ex.toString());
549                             
550                         } catch (Exception JavaDoc e) {
551                             System.out.println("Could not check files");
552                             
553                         }}
554                     if (modified){
555                     if(!((File JavaDoc)tempFiles.elementAt(i)).getAbsolutePath().endsWith("zip"))
556                         ftpFilesToAdd.put((String JavaDoc)(origfiles.elementAt(i)),(File JavaDoc)tempFiles.elementAt(i));
557                     }
558
559                 }
560                 else
561                 {
562                     if(!((File JavaDoc)tempFiles.elementAt(i)).getAbsolutePath().endsWith("zip"))
563                         ftpFilesToAdd.put((String JavaDoc)(origfiles.elementAt(i)),(File JavaDoc)tempFiles.elementAt(i));
564                 }
565             }
566         }
567         
568         ////
569
public void parseAndAdd(String JavaDoc db, String JavaDoc tb, String JavaDoc co){
570             // parse filesToAdd
571
//addFile (...)
572
Parser parser = new Parser();
573             Indexer indexer = null;
574             filtered = new Vector JavaDoc();
575             try{
576                 if (db.length()!=0 && tb.length()!= 0 && co.length() != 0) {
577                 BusinessUtil bu = new BusinessUtil(dbm);
578                 filtered = bu.getFilteredFiles(db, tb, co);
579                 }
580                 //bu.gc();
581
indexer = SnapperManager.getInstance().getIndexerFactory().newIndexer();
582                 if (site.getINDEXDIR().length() == 0) {
583                 indexer.setUpIndexer(SnapperAdmin.getIndexDir() + File.separator + site.getName(), "EN", false, SnapperAdmin.getmaxFieldLength());
584             
585                 }
586                 else {
587                     indexer.setUpIndexer(site.getINDEXDIR(), "EN", false, SnapperAdmin.getmaxFieldLength());
588                 }
589             }
590             catch (Exception JavaDoc ex) {
591                
592                     SnapperManager.getInstance().getLoggingManager().error(
593                             "Could not setup indexer");
594                     SnapperManager.getInstance().getLoggingManager().debug(ex.toString());
595                     
596                }
597             boolean error = false;
598             for (int i = 0; i < filesToAdd.size(); i++){
599                 error = false;
600                 if (doStop){
601                     indexer.optimize();
602                     indexer.close();
603                     return;
604                 }
605                     
606                 if (isFiltered(String.valueOf(filesToAdd.elementAt(i)))) continue;
607                 
608
609                 
610                 Vector JavaDoc data = parser.parse(new File JavaDoc(String.valueOf(filesToAdd.elementAt(i))));
611                 
612                 if (String.valueOf(filesToAdd.elementAt(i)).endsWith("zip")
613                         || String.valueOf(filesToAdd.elementAt(i)).endsWith("ZIP")) {
614                       
615                     indexUnZippedFiles(data);
616                     continue;
617                 }
618                 
619                 
620                 if ((data == null) || (data.size() == 0)){
621                     try{
622                         SnapperManager.getInstance().getLoggingManager().error("Could not index file: " + String.valueOf(filesToAdd.elementAt(i)));
623                     } catch (Exception JavaDoc ex) {
624                           System.out.println("Could not index file: " + String.valueOf(filesToAdd.elementAt(i)));
625                     }
626                 // System.out.println();
627
// notIndexed.add(new File(String.valueOf(filesToAdd.elementAt(i))));
628
error = true;
629                 }
630                 for (int p = 0; p< data.size(); p++){
631                     if (data.elementAt(p) == null){
632                         try{
633                             SnapperManager.getInstance().getLoggingManager().debug("Could not index file: " + String.valueOf(filesToAdd.elementAt(i)));
634                         } catch (Exception JavaDoc ex) {
635                               System.out.println("Could not index file: " + String.valueOf(filesToAdd.elementAt(i)));
636                         }
637                     // notIndexed.add(new File(String.valueOf(filesToAdd.elementAt(i))));
638
error = true;
639                     }
640                 }
641                 
642                 if (error){
643                     
644                      String JavaDoc filePath = String.valueOf(filesToAdd.elementAt(i));
645                      
646                      if(SnapperManager.getInstance().getFileSeparatorConvention()!=null &&
647                              SnapperManager.getInstance().getFileSeparatorConvention().equalsIgnoreCase("unix")) {
648                         filePath= filePath.replaceAll("\\\\","/");
649                         }
650                         
651                     notIndexed.add(filePath);
652                   // if (i == (filesToAdd.size() -1)){
653
// indexer.optimize();
654
// indexer.close();
655
// }
656
error = false;
657                     continue;
658                 }
659                 try{
660                     try{
661                         SnapperManager.getInstance().getLoggingManager().debug("Updating " + (new File JavaDoc(String.valueOf(filesToAdd.elementAt(i)))));
662                     } catch (Exception JavaDoc ex) {
663                           System.out.println("Updating " + (new File JavaDoc(String.valueOf(filesToAdd.elementAt(i)))));
664                     }
665                     
666                     BusinessUtil bu = new BusinessUtil(dbm);
667                     String JavaDoc metadata = new String JavaDoc(bu.getMetadata(String.valueOf(filesToAdd.elementAt(i)), metaDB, metaTable, metaFileColumn, metaKeyColumn, metaValueColumn));
668                     
669                     String JavaDoc title = "";
670                     if (SnapperManager.getInstance().getLogicalNameFromDatabase().equals("1"))
671                         title = new String JavaDoc(bu.title);
672                     
673                     if (title.length() == 0)
674                         title = new String JavaDoc(data.elementAt(4).toString());
675                  String JavaDoc confPath = data.elementAt(1).toString();
676                
677                  if(SnapperManager.getInstance().getFileSeparatorConvention()!=null &&
678                           SnapperManager.getInstance().getFileSeparatorConvention().equalsIgnoreCase("unix")) {
679            
680                            confPath = confPath.replaceAll("\\\\","/");
681                            originalHost = originalHost.replaceAll("\\\\","/");
682                        }
683                      
684                 if(SnapperManager.getInstance().getRelativeIndexPaths() !=null &&
685                      SnapperManager.getInstance().getRelativeIndexPaths().equalsIgnoreCase("true")) {
686                
687                    if(confPath.startsWith(originalHost)){
688                      confPath=confPath.substring(originalHost.length());
689                      if(confPath.startsWith("/"))
690                         confPath=confPath.substring(1);
691                      }
692                   }
693                
694                     indexer.indexDoc(((Long JavaDoc) data.elementAt(0)).longValue(), confPath,
695                               data.elementAt(2).toString(), data.elementAt(3).toString(), title,
696                            data.elementAt(5).toString(), metadata, new File JavaDoc(String.valueOf(filesToAdd.elementAt(i))).getName());
697                     
698                     try{
699                         SnapperManager.getInstance().getLoggingManager().debug("Updated " + String.valueOf(filesToAdd.elementAt(i)));
700                     } catch (Exception JavaDoc ex) {
701                           System.out.println("Updated " + String.valueOf(filesToAdd.elementAt(i)));
702                     }
703                 }
704                 catch (Exception JavaDoc ex) {
705                         SnapperManager.getInstance().getLoggingManager().error(
706                                 "Could not index files");
707                         SnapperManager.getInstance().getLoggingManager().debug(ex.toString());
708                     }
709                 
710             }
711         // if (!error){
712
// indexer.optimize();
713
// indexer.close();
714
// }
715
for (Enumeration JavaDoc e = ftpFilesToAdd.keys() ; e.hasMoreElements() ;) {
716                  
717                 Object JavaDoc path =null;
718                 File JavaDoc tempFile =null;
719                 path = e.nextElement();
720                 tempFile = (File JavaDoc)ftpFilesToAdd.get(path);
721                 error = false;
722         
723                 if (doStop){
724                     indexer.optimize();
725                     indexer.close();
726                     return;
727                 }
728                     
729                 if (isFiltered(String.valueOf((String JavaDoc)path))) continue;
730     
731                 Vector JavaDoc data = parser.parse(tempFile);
732     
733                 if(((String JavaDoc)path).endsWith("zip") || ((String JavaDoc)path).endsWith("ZIP")){
734                     continue;
735                 }
736                 
737                 if ((data == null) || (data.size() == 0)){
738                     try{
739                         SnapperManager.getInstance().getLoggingManager().error("Could not index file: " + path);
740                     } catch (Exception JavaDoc ex) {
741                           System.out.println("Could not index file: " + path);
742                     }
743                     error = true;
744                 }
745         
746                 for (int p = 0; p< data.size(); p++){
747                     if (data.elementAt(p) == null){
748                         try{
749                             SnapperManager.getInstance().getLoggingManager().debug("Could not index file: " + path);
750                         } catch (Exception JavaDoc ex) {
751                               System.out.println("Could not index file: " + path);
752                         }
753                         error = true;
754                     }
755                 }
756                 if (error){
757                     
758                       String JavaDoc filePath = (String JavaDoc)path;
759                       
760                       if(SnapperManager.getInstance().getFileSeparatorConvention()!=null &&
761                               SnapperManager.getInstance().getFileSeparatorConvention().equalsIgnoreCase("unix")) {
762                         filePath= filePath.replaceAll("\\\\","/");
763                          }
764                     
765                     notIndexed.add(filePath);
766                     error = false;
767                     continue;
768                 }
769                 try{
770                     try{
771                         SnapperManager.getInstance().getLoggingManager().debug("Updating " + path);
772                     } catch (Exception JavaDoc ex) {
773                           System.out.println("Updating " + path);
774                     }
775                     
776                     BusinessUtil bu = new BusinessUtil(dbm);
777                     String JavaDoc metadata = new String JavaDoc(bu.getMetadata(((String JavaDoc)path), metaDB, metaTable, metaFileColumn, metaKeyColumn, metaValueColumn));
778                     
779                     String JavaDoc title = "";
780                     if (SnapperManager.getInstance().getLogicalNameFromDatabase().equals("1"))
781                         title = new String JavaDoc(bu.title);
782                     
783                     if (title.length() == 0)
784                         title = new String JavaDoc(data.elementAt(4).toString());
785             
786                 String JavaDoc confPath = (String JavaDoc)path;
787             
788                 if(SnapperManager.getInstance().getFileSeparatorConvention()!=null &&
789                           SnapperManager.getInstance().getFileSeparatorConvention().equalsIgnoreCase("unix")) {
790            
791                            confPath = confPath.replaceAll("\\\\","/");
792                            originalHost = originalHost.replaceAll("\\\\","/");
793                        }
794                      
795                 if(SnapperManager.getInstance().getRelativeIndexPaths() !=null &&
796                    SnapperManager.getInstance().getRelativeIndexPaths().equalsIgnoreCase("true")) {
797              
798                  if(confPath.startsWith(originalHost)){
799                    confPath=confPath.substring(originalHost.length());
800                    if(confPath.startsWith("/"))
801                       confPath=confPath.substring(1);
802                    }
803                 }
804                 
805                     indexer.indexDoc(((Long JavaDoc) data.elementAt(0)).longValue(), confPath,
806                               data.elementAt(2).toString(), data.elementAt(3).toString(), title,
807                            data.elementAt(5).toString(), metadata, tempFile.getName());
808                     
809                     try{
810                         SnapperManager.getInstance().getLoggingManager().debug("Updated " + path);
811                     } catch (Exception JavaDoc ex) {
812                           System.out.println("Updated " + path);
813                     }
814                     
815                 }
816                 catch (Exception JavaDoc ex) {
817                     
818                         SnapperManager.getInstance().getLoggingManager().error(
819                                 "Could not index files");
820                         SnapperManager.getInstance().getLoggingManager().debug(ex.toString());
821                     }
822                 path =null;
823                 tempFile =null;
824             }
825             if (!error){
826             indexer.optimize();
827             indexer.close();
828             }
829     }
830         
831         public void addFile(Vector JavaDoc data){ // indexer.index....
832

833             }
834         
835         public int getDocumentNo(){
836             return documentNo;
837         }
838         
839         public Vector JavaDoc getNotIndexed(){
840             return notIndexed;
841         }
842         
843         private boolean isFiltered(String JavaDoc filePath){
844             String JavaDoc path = filePath;
845             path = path.replaceAll("\\\\","/");
846             //path = path.toLowerCase();
847
if (filtered.contains(path)){
848                 try{
849                     SnapperManager.getInstance().getLoggingManager().debug("File filtered! ---> " + filePath);
850                 } catch (Exception JavaDoc ex) {
851                       System.out.println("File filtered! ---> " + filePath);
852                 }
853                 return true;
854             }
855             return false;
856         }
857         
858         public void setMeta (String JavaDoc db, String JavaDoc table, String JavaDoc file, String JavaDoc key, String JavaDoc value){
859             this.metaDB=db;
860             this.metaTable=table;
861             this.metaFileColumn=file;
862             this.metaKeyColumn=key;
863             this.metaValueColumn=value;
864             
865             /*if (SnapperManager.getInstance().getMetaIndexing() == 1){
866                 BusinessUtil bu = new BusinessUtil();
867                 bu.indexMeta(siteName, db, table, file, key, value);
868             }*/

869         }
870         
871         public void setDoStop(boolean n) {
872             doStop = n;
873         }
874         
875         public void setDBM(DatabaseManager dbm){
876             this.dbm = dbm;
877         }
878         
879         public void indexUnZippedFiles(Vector JavaDoc files) {
880             Parser parser = new Parser();
881             Indexer indexer = null;
882             filtered = new Vector JavaDoc();
883             try{
884                 indexer = SnapperManager.getInstance().getIndexerFactory().getIndexer();
885                 
886             }
887             catch (Exception JavaDoc ex) {
888                     SnapperManager.getInstance().getLoggingManager().error(
889                             "Could not setup indexer");
890                     SnapperManager.getInstance().getLoggingManager().debug(ex.toString());
891                     
892                }
893             for (int i = 0; i < files.size(); i++) {
894                 
895                 Vector JavaDoc data = (Vector JavaDoc) files.elementAt(i);
896                 if (!checkData(data)) {
897                     continue;
898                 }
899                 try {
900                     SnapperManager.getInstance().getLoggingManager().debug(
901                             "adding : " + data.elementAt(4).toString());
902
903                     String JavaDoc title = new String JavaDoc(data.elementAt(4).toString());
904                     //
905
String JavaDoc confPath = data.elementAt(1).toString();
906                  
907                  if(SnapperManager.getInstance().getFileSeparatorConvention()!=null &&
908                          SnapperManager.getInstance().getFileSeparatorConvention().equalsIgnoreCase("unix")) {
909                        confPath = confPath.replaceAll("\\\\","/");
910                        originalHost = originalHost.replaceAll("\\\\","/");
911                      }
912                  
913                 if(SnapperManager.getInstance().getRelativeIndexPaths() !=null &&
914                      SnapperManager.getInstance().getRelativeIndexPaths().equalsIgnoreCase("true")) {
915
916                 
917                   if(confPath.startsWith(originalHost)){
918                      confPath=confPath.substring(originalHost.length());
919                      if(confPath.startsWith("/"))
920                         confPath=confPath.substring(1);
921                      }
922                    }
923                     
924                     indexer.indexDoc(((Long JavaDoc) data.elementAt(0)).longValue(), confPath,
925                             data.elementAt(2).toString(),
926                             data.elementAt(3).toString(), data.elementAt(4)
927                                     .toString(), data.elementAt(5).toString(), "",
928                             data.elementAt(4).toString());
929                     SnapperManager.getInstance().getLoggingManager().debug(
930                             "done adding : " + data.elementAt(4).toString());
931                 }
932
933                 catch (Exception JavaDoc ex) {
934                     parser = null;
935                     data.removeAllElements();
936                     data = null;
937                     try {
938                         SnapperManager.getInstance().getLoggingManager().error(
939                                 "Could not remove site-lock!");
940                     } catch (Exception JavaDoc e) {
941                         System.out.println("Could not remove site-lock!");
942                     }
943                 }
944                 finally {
945                     if (data != null)
946                         data.removeAllElements();
947                     data = null;
948                     parser = null;
949                 }
950
951                 
952
953             }
954
955         }
956         
957         private boolean checkData(Vector JavaDoc data) {
958             if ((data == null) || (data.size() == 0)) {
959                        return false;
960                }
961                for (int i = 0; i < data.size(); i++) {
962                    if (data.elementAt(i) == null) {
963                        try {
964                            SnapperManager.getInstance().getLoggingManager().debug(
965                                    "Could not index file: "
966                                 + data.elementAt(1).toString());
967                        
968                            String JavaDoc filePath = data.elementAt(1).toString();
969                            
970                            if(SnapperManager.getInstance().getFileSeparatorConvention()!=null &&
971                                    SnapperManager.getInstance().getFileSeparatorConvention().equalsIgnoreCase("unix")) {
972                             filePath= filePath.replaceAll("\\\\","/");
973                               }
974                            
975                            
976                            notIndexed.add(new String JavaDoc(filePath));
977                            
978                        
979                        } catch (Exception JavaDoc ex) {
980                            System.out.println("Could not index file: "
981                                    + data.elementAt(1).toString());
982                        }
983                  return false;
984                    }
985
986             }
987             return true;
988         }
989     
990     
991
992 }
993
Popular Tags