KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
21  * RetrieverTask.java
22  *
23  * Created on January 9, 2006, 6:50 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.retriever;
30
31 import java.io.BufferedInputStream JavaDoc;
32 import java.io.BufferedOutputStream JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.net.URI JavaDoc;
38 import java.net.URISyntaxException JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import org.netbeans.modules.xml.retriever.catalog.Utilities;
41 import org.openide.filesystems.FileLock;
42 import org.openide.filesystems.FileObject;
43 import org.openide.filesystems.FileUtil;
44 import org.openide.util.NbBundle;
45
46 /**
47  *
48  * @author girix
49  */

50 public class RetrieverTask {
51     
52     
53     
54     private File JavaDoc saveFile;
55     
56     private String JavaDoc baseAddress;
57     
58     private String JavaDoc sourceToBeGot;
59     
60     private RetrieveEntry rent;
61     
62     private RetrieverEngine retEngine = null;
63     
64     /** Creates a new instance of RetrieverTask */
65     public RetrieverTask(RetrieveEntry rent, RetrieverEngine retEngine){
66         this.retEngine = retEngine;
67         this.sourceToBeGot = rent.getCurrentAddress();
68         this.baseAddress = rent.getBaseAddress();
69         this.saveFile = rent.getSaveFile();
70         this.rent = rent;
71     }
72     
73     
74     public HashMap JavaDoc<String JavaDoc,File JavaDoc> goGetIt() throws IOException JavaDoc, URISyntaxException JavaDoc{
75         synchronized(RetrieverTask.class){
76             if(( saveFile != null ) && (saveFile.isFile()) && (saveFile.length() != 0)){
77                 //String newfile = saveFile.getParentFile().toString()+File.pathSeparator+saveFile.getName()+System.currentTimeMillis();
78
//saveFile = new File(newfile);
79

80                 //this will prevent a cycle in recursion.
81
throw new IOException JavaDoc("File already exists"); //NOI18N
82
}
83             ResourceRetriever rr = ResourceRetrieverFactory.getResourceRetriever(baseAddress, sourceToBeGot);
84             if(rr == null )
85                 throw new RuntimeException JavaDoc("No Retriever for this Resource address :"+sourceToBeGot); //NOI18N
86

87             if(isAlreadyDownloadedInThisSession(rr.getEffectiveAddress(baseAddress , sourceToBeGot))){
88                 String JavaDoc fileExists = NbBundle.getMessage(RetrieverTask.class,
89                         IConstants.EXCEPTION_CYCLIC_REFERENCE_INDICATOR);
90                 throw new IOException JavaDoc(fileExists);
91             }
92             
93             HashMap JavaDoc<String JavaDoc, InputStream JavaDoc> srcAddrNContent = rr.retrieveDocument(baseAddress , sourceToBeGot);
94             if(srcAddrNContent == null)
95                 return null;
96             String JavaDoc effectiveSrcAddr = srcAddrNContent.keySet().iterator().next();
97             InputStream JavaDoc is = srcAddrNContent.get(effectiveSrcAddr);
98             rent.setEffectiveAddress(effectiveSrcAddr);
99             if(saveFile == null)
100                 saveFile = guessSaveFile(rent);
101             if(saveFile == null)
102                 throw new IOException JavaDoc("Could not determine the save file."); //NOI18N
103

104             checkForCycle(saveFile, rr.getStreamLength(), is);
105             
106             if(retEngine.isSave2SingleFolder() && !retEngine.getFileOverwrite() ){
107                 //this stream is diff but same file name so give another name
108
int i = 0;
109                 File JavaDoc curFile = saveFile;
110                 String JavaDoc fileName = saveFile.getName();
111                 while(curFile.isFile())
112                     curFile = new File JavaDoc(retEngine.getCurrentSaveRootFile()+File.separator+"new"+i+++fileName);
113                 saveFile = curFile;
114             }
115             
116             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(is, 1024);
117             FileObject saveFileObject = FileUtil.toFileObject(FileUtil.normalizeFile(saveFile));
118             //create datafile and also all the parents folders
119
saveFile.getParentFile().mkdirs();
120             FileObject parent = FileUtil.toFileObject(FileUtil.normalizeFile(saveFile.getParentFile()));
121             saveFileObject = FileUtil.createData(parent, saveFile.getName());
122             FileLock saveFileLock = saveFileObject.lock();
123             try{
124                 OutputStream JavaDoc saveFileOutputStream = saveFileObject.getOutputStream(saveFileLock);
125                 BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(saveFileOutputStream, 1024);
126                 byte[] buffer = new byte[1024];
127                 int len = 0;
128                 while((len = bis.read(buffer)) != -1){
129                     bos.write(buffer, 0, len);
130                 }
131                 bos.close();
132                 bis.close();
133             }finally{
134                 //release the lock at any cost
135
saveFileLock.releaseLock();
136             }
137             HashMap JavaDoc<String JavaDoc, File JavaDoc> result = new HashMap JavaDoc<String JavaDoc, File JavaDoc>();
138             String JavaDoc modifiedFileExtn = null;
139             try {
140                 modifiedFileExtn = new DocumentTypeSchemaWsdlParser().getFileExtensionByParsing(saveFile);
141                 if(modifiedFileExtn != null){
142                     if(!saveFileObject.getNameExt().endsWith("."+modifiedFileExtn)){
143                         String JavaDoc fileName = saveFileObject.getNameExt();
144                         if(saveFileObject.getNameExt().endsWith("_"+modifiedFileExtn)){
145                             fileName = fileName.substring(0, fileName.length() -
146                                     ("_"+modifiedFileExtn).length());
147                         }
148                         
149                         File JavaDoc newFile = new File JavaDoc(saveFile.getParent()+File.separator+fileName+"."+modifiedFileExtn);
150                         if(newFile.isFile() && retEngine.getFileOverwrite())
151                             newFile.delete();
152                         
153                         FileObject newsaveFileObject = FileUtil.copyFile(saveFileObject, saveFileObject.getParent(),
154                                 fileName, modifiedFileExtn);
155                         saveFileObject.delete();
156                         saveFileObject = newsaveFileObject;
157                     }
158                 }
159             } catch (Exception JavaDoc ex) {
160                 //this is just a rename. So, just ignore any exceptions.
161
}
162             
163             result.put(effectiveSrcAddr, FileUtil.toFile(saveFileObject));
164             //commented out the ref file generation
165
//createReferenceFile(effectiveSrcAddr, saveFile);
166
return result;
167         }
168     }
169     
170     
171     private File JavaDoc guessSaveFile(RetrieveEntry rent) throws URISyntaxException JavaDoc, IOException JavaDoc{
172         if(rent.getSaveFile() != null)
173             return rent.getSaveFile();
174         URI JavaDoc curUri = new URI JavaDoc(rent.getEffectiveAddress());
175         //get file name
176
String JavaDoc curAddr = rent.getEffectiveAddress();
177         String JavaDoc curFileName = null;
178         int index = curAddr.lastIndexOf('/'); //NOI18N
179
if(index != -1){
180             curFileName = curAddr.substring(index+1);
181         }else{
182             curFileName = curAddr;
183         }
184         
185         if(retEngine.isSave2SingleFolder()){
186             curFileName = convertAllSpecialChars(curFileName);
187             return new File JavaDoc(retEngine.getCurrentSaveRootFile()+File.separator+curFileName);
188         }
189         
190         File JavaDoc result = null;
191         
192         //get directory to be stored
193
if(curUri.isAbsolute()){
194             if("http".equalsIgnoreCase(curUri.getScheme()) || "https".equalsIgnoreCase(curUri.getScheme())) { //NOI18N
195
//treat URLs differently
196
result = getSaveFileForURL(curUri);
197             } else{
198                 result = new File JavaDoc(new URI JavaDoc(retEngine.getFixedSaveRootFolder().toURI().toString()+"/"+curFileName));
199             }
200         }else{
201             File JavaDoc newFile = new File JavaDoc(new URI JavaDoc(rent.getLocalBaseFile().getParentFile().toURI().normalize().toString()+"/"+rent.getCurrentAddress())).getCanonicalFile();
202             File JavaDoc newParentFile = getModifiedParentFile(rent.getLocalBaseFile(), newFile);
203             if(rent.getLocalBaseFile() != newParentFile)
204                 result = new File JavaDoc(new URI JavaDoc(newParentFile.getParentFile().toURI().toString()+"/"+rent.getCurrentAddress()));
205             else
206                 result = newFile;
207         }
208         return result;
209     }
210     
211     private File JavaDoc getModifiedParentFile(File JavaDoc parentFile, File JavaDoc curSaveFile) {
212         File JavaDoc result = parentFile;
213         
214         String JavaDoc curSaveStr = curSaveFile.toURI().toString();
215         String JavaDoc saveRootStr = retEngine.getFixedSaveRootFolder().toURI().toString();
216         
217         if(curSaveStr.startsWith(saveRootStr)){
218             return result;
219         }
220         int pushCount = Utilities.countPushdownFolders(curSaveFile.toURI(), retEngine.getFixedSaveRootFolder().toURI());
221         retEngine.pushDownRoot(pushCount);
222         result = retEngine.getNewFileForOld(parentFile, pushCount);
223         return result;
224     }
225     
226     private File JavaDoc getSaveFileForURL(URI JavaDoc absURI) {
227         String JavaDoc rootFolderStr = retEngine.getFixedSaveRootFolder().toURI().toString();
228         //replace http:// with the saverootfolder
229
String JavaDoc resultStr = absURI.getSchemeSpecificPart().replace(':','_');
230         resultStr = resultStr.replace('?', '.');
231         if(resultStr.contains(".")){
232             String JavaDoc fileExtension = resultStr.substring(resultStr.lastIndexOf('.'), resultStr.length());
233             
234             if(!fileExtension.equals(fileExtension.toLowerCase())){
235                 resultStr = resultStr.substring(0, resultStr.lastIndexOf('.'))+fileExtension.toLowerCase();
236             }
237         }
238         resultStr = convertAllSpecialChars(resultStr);
239         resultStr = rootFolderStr+"/"+resultStr;
240         try {
241             return new File JavaDoc(new URI JavaDoc(resultStr).normalize());
242         } catch (URISyntaxException JavaDoc ex) {
243             return null;
244         }
245     }
246     
247     
248     public String JavaDoc convertAllSpecialChars(String JavaDoc resultStr){
249         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(resultStr);
250         for(int i = 0; i < sb.length(); i++){
251             char c = sb.charAt(i);
252             if( Character.isLetterOrDigit(c) ||
253                     (c == '/') ||
254                     (c == '.') ||
255                     (c == '_') ||
256                     (c == ' ') ||
257                     (c == '-')){
258                 continue;
259             }else{
260                 sb.setCharAt(i, '_');
261             }
262         }
263         return sb.toString();
264     }
265     
266     
267     private void checkForCycle(File JavaDoc saveFile, long l, InputStream JavaDoc is) throws IOException JavaDoc {
268         String JavaDoc fileExists = NbBundle.getMessage(RetrieverTask.class,
269                 IConstants.EXCEPTION_CYCLIC_REFERENCE_INDICATOR);
270         if(saveFile.isFile()){
271             if( (isAlreadyDownloadedInThisSession(saveFile)) ||
272                     ((saveFile.length() == l) && !retEngine.getFileOverwrite()) ) {
273                 //file is already there...Breaks cyclic link traversals
274
is.close();
275                 throw new IOException JavaDoc(fileExists+" : "+saveFile);
276             }
277             if(retEngine.getFileOverwrite()){
278                 //let the retriever overwrite
279
return;
280             } else{
281                 //dont overwrite.
282
is.close();
283                 throw new IOException JavaDoc(fileExists+" : "+saveFile);
284             }
285         }
286         if(saveFile.isDirectory()){
287             is.close();
288             String JavaDoc dirExists = NbBundle.getMessage(RetrieverTask.class,
289                     IConstants.EXCEPTION_DIRECTORY_ALREADY_EXISTS);
290             throw new IOException JavaDoc(dirExists + " : "+saveFile.getCanonicalPath()); //NOI18N
291
}
292     }
293     
294     private boolean isAlreadyDownloadedInThisSession(File JavaDoc thisFile){
295         for(RetrieveEntry rent : retEngine.retrievedList){
296             if(rent.getSaveFile().equals(thisFile))
297                 return true;
298         }
299         return false;
300     }
301     
302     private boolean isAlreadyDownloadedInThisSession(String JavaDoc uri){
303         for(RetrieveEntry rent : retEngine.retrievedList){
304             if(rent.getEffectiveAddress().equals(uri))
305                 return true;
306         }
307         return false;
308     }
309 }
310
Popular Tags