KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > RetrieverEngine


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.retriever;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URISyntaxException JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.text.DateFormat JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Stack JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38 import javax.swing.AbstractAction JavaDoc;
39 import org.netbeans.api.progress.ProgressHandle;
40 import org.netbeans.api.progress.ProgressHandleFactory;
41 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
42 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
43 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModelFactory;
44 import org.openide.DialogDisplayer;
45 import org.openide.NotifyDescriptor;
46 import org.openide.filesystems.FileObject;
47 import org.openide.filesystems.FileUtil;
48 import org.openide.util.Cancellable;
49 import org.openide.util.NbBundle;
50 import org.openide.windows.IOProvider;
51 import org.openide.windows.InputOutput;
52 import org.openide.windows.OutputWriter;
53
54 /**
55  *
56  * @author girix
57  */

58 public class RetrieverEngine implements Runnable JavaDoc{
59     
60     private LinkedList JavaDoc<RetrieveEntry> currentRetrievalList = new LinkedList JavaDoc<RetrieveEntry>();
61     
62     private File JavaDoc currentSaveRootFile = null;
63     
64     private File JavaDoc fixedSaveRootFolder = null;
65     
66     boolean startNewThread = true;
67     
68     protected boolean showErrorPopup = true;
69     
70     public RetrieverEngine(File JavaDoc fixedSaveRootFolder){
71         this.fixedSaveRootFolder = fixedSaveRootFolder;
72         this.currentSaveRootFile = fixedSaveRootFolder;
73     }
74     
75     public RetrieverEngine(File JavaDoc fixedSaveRootFolder, boolean startNewThread){
76         this.fixedSaveRootFolder = fixedSaveRootFolder;
77         this.currentSaveRootFile = fixedSaveRootFolder;
78         this.startNewThread = startNewThread;
79     }
80     
81     public void addResourceToRetrieve(RetrieveEntry rent) {
82         currentRetrievalList.add(rent);
83     }
84     
85     Thread JavaDoc taskThread = null;
86     public void start(){
87         if(startNewThread){
88             taskThread = new Thread JavaDoc(this);
89             taskThread.start();
90         }else{
91             run();
92         }
93     }
94     
95     boolean STOP_PULL = false;
96     public void run() {
97         ProgressHandle ph = ProgressHandleFactory.createHandle(
98                 NbBundle.getMessage(RetrieverEngine.class,"LBL_PROGRESSBAR_Retrieve_XML"),
99                 new Cancellable(){
100             public boolean cancel() {
101                 synchronized(RetrieverEngine.this){
102                     if(!RetrieverEngine.this.STOP_PULL){
103                         RetrieverEngine.this.STOP_PULL = true;
104                         //taskThread.interrupt();
105
}
106                 }
107                 return true;
108             }
109         }, new AbstractAction JavaDoc(){
110             public void actionPerformed(ActionEvent JavaDoc e) {
111                 getOPWindow().setOutputVisible(true);
112                 getOPWindow().select();
113             }
114         });
115         ph.start();
116         ph.switchToIndeterminate();
117         try{
118             pullRecursively();
119         }finally{
120             ph.finish();
121         }
122     }
123     
124     boolean firstTime = true;
125     String JavaDoc firstAddressParentStr = null;
126     private void pullRecursively() {
127         synchronized(RetrieverEngine.class){
128             while(!currentRetrievalList.isEmpty() && !STOP_PULL){
129                 //System.out.println(currentRetrievalList);
130
RetrieveEntry rent =currentRetrievalList.getFirst();
131                 //System.out.println("###"+rent.toString());
132
currentRetrievalList.removeFirst();
133                 RetrieverTask rt = new RetrieverTask(rent, this);
134                 
135                 if(firstTime){
136                     firstAddressParentStr = rent.getCurrentAddress().substring(0, rent.getCurrentAddress().lastIndexOf("/"));
137                     firstTime = false;
138                 }
139                 
140                 updateDownloadingInfo(rent);
141                 
142                 HashMap JavaDoc<String JavaDoc,File JavaDoc> storedFileMap = null;;
143                 try {
144                     storedFileMap = rt.goGetIt();
145                 } catch (URISyntaxException JavaDoc ex) {
146                     //This might an error in the file. Ignore
147
//ex.printStackTrace();
148
handleException(rent, ex);
149                     continue;
150                 } catch (IOException JavaDoc ex) {
151                     //This might have been thrown to indicate a cyclic reference.
152
//ex.printStackTrace();
153
handleException(rent, ex);
154                     continue;
155                 }
156                 
157                 
158                 if(!rent.isRecursive())
159                     continue;
160                 
161                 if(storedFileMap == null){
162                     continue;
163                 }
164                 String JavaDoc effectiveSrcAddr = storedFileMap.keySet().iterator().next();
165                 File JavaDoc storedFile = storedFileMap.get(effectiveSrcAddr);
166                 
167                 rent.setSaveFile(storedFile);
168                 rent.setEffectiveAddress(effectiveSrcAddr);
169                 
170                 updateDownloadedInfo(rent);
171                 
172                 createCatalogIfRequired(rent);
173                 
174                 DocumentTypeParser dtp = DocumentParserFactory.getParser(rent.getDocType());
175                 List JavaDoc<String JavaDoc> thisFileRefs = null;;
176                 try {
177                     thisFileRefs = dtp.getAllLocationOfReferencedEntities(storedFile);
178                     //System.out.println("Parsed:"+storedFile+" Got:"+thisFileRefs);
179
} catch (Exception JavaDoc ex) {
180                     //was not able to parse the doc. Currently ignore this.
181
//ex.printStackTrace();
182
continue;
183                 }
184                 for(String JavaDoc ref: thisFileRefs){
185                     currentRetrievalList.addLast(new RetrieveEntry(effectiveSrcAddr,
186                             ref, storedFile, null, rent.getDocType(), rent.isRecursive()));
187                 }
188                 printList();
189             }
190             closeOPOuts();
191         }
192     }
193     
194     private void printList() {
195         for(RetrieveEntry rent: currentRetrievalList){
196             //System.out.println("------"+rent);
197
}
198     }
199     
200     public File JavaDoc getCurrentSaveRootFile() {
201         return currentSaveRootFile;
202     }
203     
204     public void getCurrentSaveRootFile(File JavaDoc currentSaveRootFile) {
205         this.currentSaveRootFile = currentSaveRootFile;
206     }
207     
208     private String JavaDoc getCorrectFolderName(int folderIndex){
209         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(firstAddressParentStr, "/");
210         Stack JavaDoc <String JavaDoc> stack = new Stack JavaDoc<String JavaDoc>();
211         while(stok.hasMoreTokens())
212             stack.push(stok.nextToken());
213         for(int i = 1; i < folderIndex ; i++)
214             stack.pop();
215         return stack.pop();
216     }
217     
218     int currentPushCount = 0;
219     int previousPushCount = 0;
220     void pushDownRoot(int pushCount) {
221         File JavaDoc newTmpRoot = new File JavaDoc(currentSaveRootFile.getParent()+File.separator+System.currentTimeMillis());
222         File JavaDoc leafFolder = newTmpRoot;
223         leafFolder.mkdirs();
224         for(int i = pushCount;i >= 2; i--){
225             leafFolder = new File JavaDoc(leafFolder.toString()+File.separator+getCorrectFolderName(currentPushCount+i));
226             leafFolder.mkdirs();
227         }
228         leafFolder = new File JavaDoc(leafFolder.toString()+File.separator+getCorrectFolderName(currentPushCount+1));
229         File JavaDoc movedRoot = leafFolder;
230         //String rootFolderName = saveRootFile.getName();
231
while(!currentSaveRootFile.renameTo(movedRoot)){
232             try {
233                 Thread.sleep(500);
234             } catch (InterruptedException JavaDoc ex) {
235             }
236         }
237         while(!newTmpRoot.renameTo(currentSaveRootFile)){
238             try {
239                 Thread.sleep(500);
240             } catch (InterruptedException JavaDoc ex) {
241             }
242         }
243         
244         File JavaDoc newRoot = currentSaveRootFile;
245         for(int i = pushCount;i >= 1; i--)
246             newRoot = new File JavaDoc(newRoot.toString()+File.separator+getCorrectFolderName(currentPushCount+i));
247         correctAllEntriesInTheList(newRoot);
248         previousPushCount = currentPushCount;
249         currentPushCount += pushCount;
250     }
251     
252     File JavaDoc getNewFileForOld(File JavaDoc oldFile, int pushCount) {
253         File JavaDoc newRoot = currentSaveRootFile;
254         for(int i = pushCount;i >= 1; i--)
255             newRoot = new File JavaDoc(newRoot.toString()+File.separator+getCorrectFolderName(previousPushCount+i));
256         String JavaDoc oldPath = oldFile.toString();
257         String JavaDoc newPath = new String JavaDoc(new StringBuffer JavaDoc(oldPath).replace(0, currentSaveRootFile.toString().length(),newRoot.toString()));
258         File JavaDoc newFile = new File JavaDoc(newPath);
259         return newFile;
260     }
261     
262     private void correctAllEntriesInTheList(File JavaDoc newRoot) {
263         for(RetrieveEntry rent : currentRetrievalList){
264             String JavaDoc oldPath = rent.getLocalBaseFile().toString();
265             String JavaDoc newPath = new String JavaDoc(new StringBuffer JavaDoc(oldPath).replace(0, currentSaveRootFile.toString().length(),newRoot.toString()));
266             File JavaDoc newLocalBaseFile = new File JavaDoc(newPath);
267             rent.setLocalBaseFile(newLocalBaseFile);
268         }
269         for(RetrieveEntry rent : retrievedList){
270             String JavaDoc oldPath = rent.getSaveFile().toString();
271             String JavaDoc newPath = new String JavaDoc(new StringBuffer JavaDoc(oldPath).replace(0, currentSaveRootFile.toString().length(),newRoot.toString()));
272             File JavaDoc newLocalBaseFile = new File JavaDoc(newPath);
273             rent.setSaveFile(newLocalBaseFile);
274         }
275     }
276     
277     public File JavaDoc getFixedSaveRootFolder() {
278         return fixedSaveRootFolder;
279     }
280     
281     private void handleException(RetrieveEntry rent, Exception JavaDoc ex) {
282         if(audits == null)
283             audits = new HashMap JavaDoc<RetrieveEntry, Exception JavaDoc>();
284         audits.put(rent, ex);
285         //System.out.println(ex instanceof UnknownHostException);
286
if(ex instanceof UnknownHostException JavaDoc){
287             String JavaDoc errorMess = NbBundle.getMessage(RetrieverEngine.class, "MSG_unknown_host_p1")+ex.getLocalizedMessage()+"\n"+NbBundle.getMessage(RetrieverEngine.class, "MSG_unknownhost_p2");
288             outputError(errorMess);
289             if(showErrorPopup){
290                 NotifyDescriptor.Message ndm = new NotifyDescriptor.Message(errorMess, NotifyDescriptor.Message.ERROR_MESSAGE);
291                 DialogDisplayer.getDefault().notify(ndm);
292             }
293             return;
294         }
295         if(ex instanceof URISyntaxException JavaDoc){
296             String JavaDoc errorMess = ex.getLocalizedMessage();
297             outputError(errorMess);
298             if(showErrorPopup){
299                 NotifyDescriptor.Message ndm = new NotifyDescriptor.Message(errorMess, NotifyDescriptor.Message.ERROR_MESSAGE);
300                 DialogDisplayer.getDefault().notify(ndm);
301             }
302             return;
303         }
304         if(ex instanceof FileNotFoundException JavaDoc){
305             String JavaDoc errorMess = NbBundle.getMessage(RetrieverEngine.class, "MSG_unknown_file", ex.getMessage());
306             outputError(errorMess);
307             if(showErrorPopup){
308                 NotifyDescriptor.Message ndm = new NotifyDescriptor.Message(errorMess, NotifyDescriptor.Message.ERROR_MESSAGE);
309                 DialogDisplayer.getDefault().notify(ndm);
310             }
311             return;
312         }
313         
314         if(ex instanceof IOException JavaDoc){
315             String JavaDoc exStr = NbBundle.getMessage(RetrieverEngine.class, IConstants.EXCEPTION_CYCLIC_REFERENCE_INDICATOR);
316             if(ex.getMessage().startsWith(exStr)){
317                 outputMessage(ex.getMessage()+":\n\t "+ NbBundle.getMessage(RetrieverEngine.class,
318                         "MSG_retrieving_location_found_in",rent.getCurrentAddress(),
319                         rent.getBaseAddress()));
320                 return;
321             }
322             String JavaDoc errorMess = NbBundle.getMessage(RetrieverEngine.class, "MSG_general_io_error", ex.getMessage());
323             if(showErrorPopup){
324                 NotifyDescriptor.Message ndm = new NotifyDescriptor.Message(errorMess, NotifyDescriptor.Message.ERROR_MESSAGE);
325                 DialogDisplayer.getDefault().notify(ndm);
326             }
327             outputError(errorMess);
328             return;
329         }
330         
331         outputError(ex.getMessage());
332         return;
333         
334     }
335     
336     String JavaDoc opTabTitle = NbBundle.getMessage(RetrieverEngine.class,
337             "TITLE_retriever_output_tab_title"); //NOI18N
338

339     InputOutput iop = null;
340     private InputOutput getOPWindow(){
341         if(iop == null){
342             iop = IOProvider.getDefault().getIO(opTabTitle, false);
343             iop.setErrSeparated(true);
344             iop.setFocusTaken(false);
345             /*iop.select();
346             try {
347                 iop.getOut().reset();
348             } catch (IOException ex) {
349             }*/

350             ioOut = iop.getOut();
351             DateFormat JavaDoc dtf = DateFormat.getDateTimeInstance();
352             ioOut.print("\n\n"+dtf.format(new Date JavaDoc(System.currentTimeMillis()))+" : ");
353         }
354         return iop;
355     }
356     
357     private void closeOPOuts(){
358         getErrOut().close();
359         getOPOut().close();
360     }
361     
362     OutputWriter ioOut;
363     private OutputWriter getOPOut(){
364         if(ioOut == null){
365             ioOut = getOPWindow().getOut();
366         }
367         return ioOut;
368     }
369     
370     OutputWriter ioError;
371     private OutputWriter getErrOut(){
372         if(ioError == null){
373             ioError = getOPWindow().getErr();
374         }
375         return ioError;
376     }
377     
378     private void outputError(String JavaDoc str){
379         OutputWriter err = getErrOut();
380         err.println(NbBundle.getMessage(RetrieverEngine.class, "MSG_Error_str",
381                 str)); //NOI18N
382
err.flush();
383     }
384     
385     private void outputMessage(String JavaDoc str){
386         OutputWriter err = getOPOut();
387         err.println(str); //NOI18N
388
err.flush();
389     }
390     
391     private void updateDownloadingInfo(RetrieveEntry rent) {
392         OutputWriter opt = getOPOut();
393         if(rent.getBaseAddress() != null){
394             opt.println(
395                     NbBundle.getMessage(RetrieverEngine.class,
396                     "MSG_retrieving_location_found_in",rent.getCurrentAddress(),
397                     rent.getBaseAddress())); //NOI18N
398
}else{
399             opt.println(
400                     NbBundle.getMessage(RetrieverEngine.class,
401                     "MSG_retrieving_location",rent.getCurrentAddress())); //NOI18N
402
}
403         opt.flush();
404     }
405     List JavaDoc<RetrieveEntry> retrievedList = new ArrayList JavaDoc<RetrieveEntry>();
406     private void updateDownloadedInfo(RetrieveEntry rent) {
407         retrievedList.add(rent);
408         OutputWriter opt = getOPOut();
409         String JavaDoc str = " "+rent.getEffectiveAddress();
410         opt.println(
411                 NbBundle.getMessage(RetrieverEngine.class,
412                 "MSG_retrieved_saved_at",str, rent.getSaveFile())); //NOI18N
413
opt.flush();
414     }
415     
416     public File JavaDoc getSeedFileLocation(){
417         if(retrievedList.size() > 0){
418             RetrieveEntry rent = retrievedList.get(0);
419             return rent.getSaveFile();
420         }
421         return null;
422     }
423     
424     private void createCatalogIfRequired(RetrieveEntry rent) {
425         URI JavaDoc curURI = null;
426         String JavaDoc addr = rent.getEffectiveAddress();
427         try {
428             //check if this is the first entry and the connection was redirected. If yes, then
429
//store the URI as the original URI instead of the redirected URI
430
String JavaDoc tempStr = URLResourceRetriever.resolveURL(rent.getBaseAddress(), rent.getCurrentAddress());
431             if(! (new URI JavaDoc(tempStr).equals(new URI JavaDoc(addr))) ){
432                 addr = tempStr;
433             }
434         } catch (URISyntaxException JavaDoc ex) {
435             //ignore
436
}
437         if(isSave2SingleFolder()){
438             if( !rent.getCurrentAddress().equals(rent.getEffectiveAddress()) )
439                 addr = rent.getCurrentAddress();
440         }
441         try {
442             curURI = new URI JavaDoc(addr);
443         } catch (URISyntaxException JavaDoc ex) {
444             //this is not supposed to happen. But if it does, then just return
445
return;
446         }
447         FileObject fobj = null;
448         try{
449             fobj = FileUtil.toFileObject(FileUtil.normalizeFile(rent.getSaveFile()));
450         }catch(Exception JavaDoc e){
451             return;
452         }
453         if(fobj == null)
454             return;
455         CatalogWriteModel dr = null;
456         try {
457             if(this.catalogFileObject == null)
458                 dr = CatalogWriteModelFactory.getInstance()
459                 .getCatalogWriteModelForProject(fobj);
460             else
461                 dr = CatalogWriteModelFactory.getInstance()
462                 .getCatalogWriteModelForCatalogFile(this.catalogFileObject);
463         } catch (CatalogModelException ex) {
464             //ignore this exception but return
465
return;
466         }
467         //fobj = FileUtil.toFileObject(rent.getSaveFile());
468
try {
469             dr.addURI(curURI, fobj);
470         } catch (Exception JavaDoc ex) {
471             //ignore this exception but return
472
ex = new Exception JavaDoc("Exception while writing in to catalog.", ex);
473             handleException(rent, ex);
474             return;
475         }
476     }
477     
478     boolean fileOverwrite = false;
479     
480     public void setFileOverwrite(boolean fileOverwrite){
481         this.fileOverwrite = fileOverwrite;
482     }
483     
484     public boolean getFileOverwrite() {
485         return fileOverwrite;
486     }
487     
488     Map JavaDoc<RetrieveEntry, Exception JavaDoc> audits;
489     public Map JavaDoc<RetrieveEntry, Exception JavaDoc> getRetrievedResourceExceptionMap() {
490         return audits;
491     }
492     
493     
494     FileObject catalogFileObject = null;
495     public void setCatalogFile(FileObject catalogFileObject) {
496         this.catalogFileObject = catalogFileObject;
497     }
498     
499     
500     private boolean save2SingleFolder = false;
501     public void setSave2SingleFolder(boolean save2SingleFolder) {
502         this.save2SingleFolder = save2SingleFolder;
503     }
504     
505     public boolean isSave2SingleFolder() {
506         return save2SingleFolder;
507     }
508 }
509
Popular Tags