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 @ 40559
History | View | Annotate | Download (6.9 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | /*
|
25 | * Created on 01-oct-2005
|
||
26 | */
|
||
27 | package org.gvsig.remoteclient.taskplanning.retrieving; |
||
28 | |||
29 | import java.io.File; |
||
30 | import java.net.MalformedURLException; |
||
31 | import java.util.Hashtable; |
||
32 | import java.util.TreeMap; |
||
33 | |||
34 | import org.gvsig.remoteclient.taskplanning.IQueue; |
||
35 | |||
36 | /**
|
||
37 | * pa administrar les tasques (la hist?ria aquella
|
||
38 | * de que hi haja una cola per a cada servidor)
|
||
39 | * @author Luis W. Sevilla (sevilla_lui@gva.es)
|
||
40 | */
|
||
41 | public class RequestManager { |
||
42 | private boolean debug = true; |
||
43 | |||
44 | private static RequestManager instance; |
||
45 | private TreeMap serversTable = new TreeMap(); |
||
46 | private RequestManager() {} // Avoid public instantiation |
||
47 | |||
48 | public static RequestManager getInstance() { |
||
49 | if (instance == null) |
||
50 | instance = new RequestManager();
|
||
51 | return instance;
|
||
52 | } |
||
53 | |||
54 | |||
55 | public URLRetrieveTask addURLRequest(URLRequest request, RetrieveListener listener) {
|
||
56 | try {
|
||
57 | |||
58 | // TODO canviar per a quetorne el Request antic, que la request guarde el
|
||
59 | // seu estat aix? com la llista de listeners
|
||
60 | File f = getPreviousDownloadedURLRequest(request);
|
||
61 | |||
62 | if (f!=null) { |
||
63 | // The file was already requested and it is in the cache or
|
||
64 | // the download is in process
|
||
65 | |||
66 | // Overwrite the file name with the file in the cache's one.
|
||
67 | request.setFileName(f.getAbsolutePath()); |
||
68 | System.out.println(request.getUrl()+" is cached at '"+f.getAbsolutePath()+"'"); |
||
69 | |||
70 | |||
71 | // get this server's task queue
|
||
72 | RetrieveQueue serverQueue = (RetrieveQueue) getQueue(request.getHost()); |
||
73 | |||
74 | // Look up the previous cached jobs
|
||
75 | |||
76 | URLRetrieveTask workingTask = serverQueue.getURLPreviousRequest(request); |
||
77 | if (workingTask == null) { |
||
78 | // Task already done. Notify listener
|
||
79 | if (debug)
|
||
80 | System.err.println("done job found: "+request.getUrl()); |
||
81 | RetrieveEvent event = new RetrieveEvent();
|
||
82 | event.setType(RetrieveEvent.REQUEST_FINISHED); |
||
83 | listener.transferEventReceived(event); |
||
84 | |||
85 | } else {
|
||
86 | // The task is working yet, will register the listener
|
||
87 | |||
88 | // TODO no va b?... perqu? la cola va buidant-se molt r?pidament
|
||
89 | // per a fer que vaja tamb? hi hauria que registrar en cache
|
||
90 | // lo que s'est? baixant al principi de l'execute();
|
||
91 | if (debug)
|
||
92 | System.err.println("working job found: "+request.getUrl()); |
||
93 | workingTask.addRetrieveListener(listener); |
||
94 | |||
95 | } |
||
96 | } else {
|
||
97 | // Pick an unrepeatable fileName
|
||
98 | String host = request.getHost();
|
||
99 | String fileName = request.getFileName();
|
||
100 | File tempDir = new File(tempDirectoryPath); |
||
101 | if (!tempDir.exists())
|
||
102 | tempDir.mkdir(); |
||
103 | String fileNamePrefix = tempDirectoryPath +
|
||
104 | File.separator + host +
|
||
105 | "-" ;
|
||
106 | if (fileName.startsWith(fileNamePrefix))
|
||
107 | fileName = fileName.substring(fileNamePrefix.length(), fileName.length()); |
||
108 | |||
109 | // get this server's task queue
|
||
110 | RetrieveQueue serverQueue = (RetrieveQueue) getQueue(request.getHost()); |
||
111 | |||
112 | |||
113 | fileName = fileNamePrefix + fileName; |
||
114 | |||
115 | request.setFileName(fileName); |
||
116 | |||
117 | // TODO
|
||
118 | // jo ac? comprovaria quin protocol, m?tode, host, etc... i crearia un
|
||
119 | // objecte que tractara la desc?rrega segons el m?tode que li toca.
|
||
120 | // algo en plan Strategy's
|
||
121 | //
|
||
122 | // per exemple si fem URLRetrieveTask una abstracta i tenim
|
||
123 | //
|
||
124 | // GET de tota la vida
|
||
125 | // serverQueue.put(new HTTPGetRetrieveTask(request, listener));
|
||
126 | //
|
||
127 | // POST
|
||
128 | // serverQueue.put(new HTTPPostRetrieveTask(request, listener));
|
||
129 | //
|
||
130 | // FTP
|
||
131 | // serverQueue.put(new FTPRetrieveTask(request, listener));
|
||
132 | //
|
||
133 | // ????Xarxa local?????
|
||
134 | // serverQueue.put(new SMBRetrieveTask(request, listener));
|
||
135 | |||
136 | // Enqueue the request and the listener will be notified when done.
|
||
137 | URLRetrieveTask task = new URLRetrieveTask(request, listener);
|
||
138 | return (URLRetrieveTask) serverQueue.put(task);
|
||
139 | } |
||
140 | } catch (MalformedURLException e) { |
||
141 | e.printStackTrace(); |
||
142 | } |
||
143 | return null; |
||
144 | } |
||
145 | |||
146 | private IQueue getQueue(String hostName) { |
||
147 | RetrieveQueue queue = null;
|
||
148 | if (serversTable.containsKey(hostName))
|
||
149 | queue = (RetrieveQueue) serversTable.get(hostName); |
||
150 | else {
|
||
151 | // crea la cola
|
||
152 | queue = new RetrieveQueue(hostName);
|
||
153 | // pone la cola del server en marcha.
|
||
154 | } |
||
155 | return queue;
|
||
156 | } |
||
157 | |||
158 | |||
159 | private Hashtable downloadedFiles; |
||
160 | private final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"tmp-andami"; |
||
161 | /**
|
||
162 | * Remove an URL from the system cache. The file will remain in the file
|
||
163 | * system for further eventual uses.
|
||
164 | * @param request
|
||
165 | */
|
||
166 | public void removeURLRequest(URLRequest request) { |
||
167 | if (downloadedFiles != null && downloadedFiles.containsKey(request)) |
||
168 | downloadedFiles.remove(request); |
||
169 | } |
||
170 | /**
|
||
171 | * Adds an URL to the table of downloaded files for further uses. If the URL
|
||
172 | * already exists in the table its filePath value is updated to the new one and
|
||
173 | * the old file itself is removed from the file system.
|
||
174 | *
|
||
175 | * @param url
|
||
176 | * @param filePath
|
||
177 | */
|
||
178 | protected void addDownloadedURLRequest(URLRequest request, String filePath){ |
||
179 | if (downloadedFiles==null) |
||
180 | downloadedFiles = new Hashtable(); |
||
181 | String fileName = (String) downloadedFiles.put(request, filePath); |
||
182 | if (fileName!=null){ |
||
183 | File f = new File(fileName); |
||
184 | if (f.exists())
|
||
185 | f.delete(); |
||
186 | } |
||
187 | } |
||
188 | /**
|
||
189 | * Returns the content of this URL as a file from the file system.<br>
|
||
190 | * <p>
|
||
191 | * If the URL has been already downloaded in this session and notified
|
||
192 | * to the system using the static <b>Utilities.addDownloadedURL(URL)</b>
|
||
193 | * method, it can be restored faster from the file system avoiding to
|
||
194 | * download it again.
|
||
195 | * </p>
|
||
196 | * @param url
|
||
197 | * @return File containing this URL's content or null if no file was found.
|
||
198 | */
|
||
199 | private File getPreviousDownloadedURLRequest(URLRequest request){ |
||
200 | File f = null; |
||
201 | if (downloadedFiles!=null && downloadedFiles.containsKey(request)){ |
||
202 | String filePath = (String) downloadedFiles.get(request); |
||
203 | f = new File(filePath); |
||
204 | } |
||
205 | return f;
|
||
206 | } |
||
207 | |||
208 | } |