svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / taskplanning / retrieving / RequestManager.java @ 40769
History | View | Annotate | Download (6.87 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.remoteclient.taskplanning.retrieving; |
25 |
|
26 |
import java.io.File; |
27 |
import java.net.MalformedURLException; |
28 |
import java.util.Hashtable; |
29 |
import java.util.TreeMap; |
30 |
|
31 |
import org.gvsig.remoteclient.taskplanning.IQueue; |
32 |
|
33 |
/**
|
34 |
* pa administrar les tasques (la hist?ria aquella
|
35 |
* de que hi haja una cola per a cada servidor)
|
36 |
* @author Luis W. Sevilla (sevilla_lui@gva.es)
|
37 |
*/
|
38 |
public class RequestManager { |
39 |
private boolean debug = true; |
40 |
|
41 |
private static RequestManager instance; |
42 |
private TreeMap serversTable = new TreeMap(); |
43 |
private RequestManager() {} // Avoid public instantiation |
44 |
|
45 |
public static RequestManager getInstance() { |
46 |
if (instance == null) |
47 |
instance = new RequestManager();
|
48 |
return instance;
|
49 |
} |
50 |
|
51 |
|
52 |
public URLRetrieveTask addURLRequest(URLRequest request, RetrieveListener listener) {
|
53 |
try {
|
54 |
|
55 |
// TODO canviar per a quetorne el Request antic, que la request guarde el
|
56 |
// seu estat aix? com la llista de listeners
|
57 |
File f = getPreviousDownloadedURLRequest(request);
|
58 |
|
59 |
if (f!=null) { |
60 |
// The file was already requested and it is in the cache or
|
61 |
// the download is in process
|
62 |
|
63 |
// Overwrite the file name with the file in the cache's one.
|
64 |
request.setFileName(f.getAbsolutePath()); |
65 |
System.out.println(request.getUrl()+" is cached at '"+f.getAbsolutePath()+"'"); |
66 |
|
67 |
|
68 |
// get this server's task queue
|
69 |
RetrieveQueue serverQueue = (RetrieveQueue) getQueue(request.getHost()); |
70 |
|
71 |
// Look up the previous cached jobs
|
72 |
|
73 |
URLRetrieveTask workingTask = serverQueue.getURLPreviousRequest(request); |
74 |
if (workingTask == null) { |
75 |
// Task already done. Notify listener
|
76 |
if (debug)
|
77 |
System.err.println("done job found: "+request.getUrl()); |
78 |
RetrieveEvent event = new RetrieveEvent();
|
79 |
event.setType(RetrieveEvent.REQUEST_FINISHED); |
80 |
listener.transferEventReceived(event); |
81 |
|
82 |
} else {
|
83 |
// The task is working yet, will register the listener
|
84 |
|
85 |
// TODO no va b?... perqu? la cola va buidant-se molt r?pidament
|
86 |
// per a fer que vaja tamb? hi hauria que registrar en cache
|
87 |
// lo que s'est? baixant al principi de l'execute();
|
88 |
if (debug)
|
89 |
System.err.println("working job found: "+request.getUrl()); |
90 |
workingTask.addRetrieveListener(listener); |
91 |
|
92 |
} |
93 |
} else {
|
94 |
// Pick an unrepeatable fileName
|
95 |
String host = request.getHost();
|
96 |
String fileName = request.getFileName();
|
97 |
File tempDir = new File(tempDirectoryPath); |
98 |
if (!tempDir.exists())
|
99 |
tempDir.mkdir(); |
100 |
String fileNamePrefix = tempDirectoryPath +
|
101 |
File.separator + host +
|
102 |
"-" ;
|
103 |
if (fileName.startsWith(fileNamePrefix))
|
104 |
fileName = fileName.substring(fileNamePrefix.length(), fileName.length()); |
105 |
|
106 |
// get this server's task queue
|
107 |
RetrieveQueue serverQueue = (RetrieveQueue) getQueue(request.getHost()); |
108 |
|
109 |
|
110 |
fileName = fileNamePrefix + fileName; |
111 |
|
112 |
request.setFileName(fileName); |
113 |
|
114 |
// TODO
|
115 |
// jo ac? comprovaria quin protocol, m?tode, host, etc... i crearia un
|
116 |
// objecte que tractara la desc?rrega segons el m?tode que li toca.
|
117 |
// algo en plan Strategy's
|
118 |
//
|
119 |
// per exemple si fem URLRetrieveTask una abstracta i tenim
|
120 |
//
|
121 |
// GET de tota la vida
|
122 |
// serverQueue.put(new HTTPGetRetrieveTask(request, listener));
|
123 |
//
|
124 |
// POST
|
125 |
// serverQueue.put(new HTTPPostRetrieveTask(request, listener));
|
126 |
//
|
127 |
// FTP
|
128 |
// serverQueue.put(new FTPRetrieveTask(request, listener));
|
129 |
//
|
130 |
// ????Xarxa local?????
|
131 |
// serverQueue.put(new SMBRetrieveTask(request, listener));
|
132 |
|
133 |
// Enqueue the request and the listener will be notified when done.
|
134 |
URLRetrieveTask task = new URLRetrieveTask(request, listener);
|
135 |
return (URLRetrieveTask) serverQueue.put(task);
|
136 |
} |
137 |
} catch (MalformedURLException e) { |
138 |
e.printStackTrace(); |
139 |
} |
140 |
return null; |
141 |
} |
142 |
|
143 |
private IQueue getQueue(String hostName) { |
144 |
RetrieveQueue queue = null;
|
145 |
if (serversTable.containsKey(hostName))
|
146 |
queue = (RetrieveQueue) serversTable.get(hostName); |
147 |
else {
|
148 |
// crea la cola
|
149 |
queue = new RetrieveQueue(hostName);
|
150 |
// pone la cola del server en marcha.
|
151 |
} |
152 |
return queue;
|
153 |
} |
154 |
|
155 |
|
156 |
private Hashtable downloadedFiles; |
157 |
private final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"tmp-andami"; |
158 |
/**
|
159 |
* Remove an URL from the system cache. The file will remain in the file
|
160 |
* system for further eventual uses.
|
161 |
* @param request
|
162 |
*/
|
163 |
public void removeURLRequest(URLRequest request) { |
164 |
if (downloadedFiles != null && downloadedFiles.containsKey(request)) |
165 |
downloadedFiles.remove(request); |
166 |
} |
167 |
/**
|
168 |
* Adds an URL to the table of downloaded files for further uses. If the URL
|
169 |
* already exists in the table its filePath value is updated to the new one and
|
170 |
* the old file itself is removed from the file system.
|
171 |
*
|
172 |
* @param url
|
173 |
* @param filePath
|
174 |
*/
|
175 |
protected void addDownloadedURLRequest(URLRequest request, String filePath){ |
176 |
if (downloadedFiles==null) |
177 |
downloadedFiles = new Hashtable(); |
178 |
String fileName = (String) downloadedFiles.put(request, filePath); |
179 |
if (fileName!=null){ |
180 |
File f = new File(fileName); |
181 |
if (f.exists())
|
182 |
f.delete(); |
183 |
} |
184 |
} |
185 |
/**
|
186 |
* Returns the content of this URL as a file from the file system.<br>
|
187 |
* <p>
|
188 |
* If the URL has been already downloaded in this session and notified
|
189 |
* to the system using the static <b>Utilities.addDownloadedURL(URL)</b>
|
190 |
* method, it can be restored faster from the file system avoiding to
|
191 |
* download it again.
|
192 |
* </p>
|
193 |
* @param url
|
194 |
* @return File containing this URL's content or null if no file was found.
|
195 |
*/
|
196 |
private File getPreviousDownloadedURLRequest(URLRequest request){ |
197 |
File f = null; |
198 |
if (downloadedFiles!=null && downloadedFiles.containsKey(request)){ |
199 |
String filePath = (String) downloadedFiles.get(request); |
200 |
f = new File(filePath); |
201 |
} |
202 |
return f;
|
203 |
} |
204 |
|
205 |
} |