svn-gvsig-desktop / branches / MULTITHREADING_DEVELOPMENT / libraries / libRemoteServices / src / org / gvsig / remoteClient / taskplanning / retrieving / RequestManager.java @ 5262
History | View | Annotate | Download (5.15 KB)
1 |
/*
|
---|---|
2 |
* Created on 01-oct-2005
|
3 |
*/
|
4 |
package org.gvsig.remoteClient.taskplanning.retrieving; |
5 |
|
6 |
import java.io.File; |
7 |
import java.net.MalformedURLException; |
8 |
import java.util.ArrayList; |
9 |
import java.util.TreeMap; |
10 |
import java.util.Vector; |
11 |
|
12 |
import org.gvsig.remoteClient.taskplanning.IQueue; |
13 |
|
14 |
/**
|
15 |
* pa administrar les tasques (la hist?ria aquella
|
16 |
* de que hi haja una cola per a cada servidor)
|
17 |
* @author Luis W. Sevilla (sevilla_lui@gva.es)
|
18 |
*/
|
19 |
public class RequestManager { |
20 |
private boolean debug = true; |
21 |
|
22 |
private static RequestManager instance; |
23 |
private TreeMap serversTable = new TreeMap(); |
24 |
private RequestManager() {} // Avoid public instantiation |
25 |
|
26 |
public static RequestManager getInstance() { |
27 |
if (instance == null) |
28 |
instance = new RequestManager();
|
29 |
return instance;
|
30 |
} |
31 |
|
32 |
|
33 |
public URLRequest addURLRequest(URLRequest request, RetrieveListener listener) {
|
34 |
try {
|
35 |
if (downloadedFiles.contains(request)) {
|
36 |
for (int i = 0; i < downloadedFiles.size(); i++) { |
37 |
URLRequest r = (URLRequest) downloadedFiles.get(i); |
38 |
if (r.equals(request)) {
|
39 |
System.out.println(request.getUrl()+" is cached at '"+r.getFileName()+"'"); |
40 |
request.setFileName(r.getFileName()); /* Uaaah!
|
41 |
* Si no pose a?? l'event es
|
42 |
* dispara abans que haja
|
43 |
* retornat el request
|
44 |
* reasignat i confon el
|
45 |
* sufixe.
|
46 |
*/
|
47 |
request = r; |
48 |
break;
|
49 |
} |
50 |
} |
51 |
|
52 |
|
53 |
synchronized (this) { |
54 |
if (request.getStatus() == RetrieveEvent.REQUEST_CANCELLED ||
|
55 |
request.getStatus() == RetrieveEvent.REQUEST_FAILED) { |
56 |
// The last requested URL was cancelled or it failed but now it is required again
|
57 |
// so, I have to remove it and add again.
|
58 |
removeURLRequest(request); |
59 |
return addURLRequest(request, listener);
|
60 |
} else if (request.getStatus() == RetrieveEvent.REQUEST_FINISHED) { |
61 |
if (debug)
|
62 |
System.err.println("done job found ("+request.getFileName()+")"); |
63 |
RetrieveEvent event = new RetrieveEvent();
|
64 |
event.setType(RetrieveEvent.REQUEST_FINISHED); |
65 |
listener.transferEventReceived(event); |
66 |
} else {
|
67 |
if (debug)
|
68 |
System.err.println("working job found ("+request.getFileName()+")"); |
69 |
request.lock(); |
70 |
request.addRetrieveListener(listener); |
71 |
request.unlock(); |
72 |
} |
73 |
|
74 |
} |
75 |
} else {
|
76 |
// Pick an unrepeatable fileName
|
77 |
String host = request.getHost();
|
78 |
String fileName = request.getFileName();
|
79 |
File tempDir = new File(tempDirectoryPath); |
80 |
if (!tempDir.exists())
|
81 |
tempDir.mkdir(); |
82 |
String fileNamePrefix = tempDirectoryPath +
|
83 |
File.separator + host +
|
84 |
"-" ;
|
85 |
if (fileName.startsWith(fileNamePrefix))
|
86 |
fileName = fileName.substring(fileNamePrefix.length(), fileName.length()); |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
// get this server's task queue
|
92 |
RetrieveQueue serverQueue = (RetrieveQueue) getQueue(request.getHost()); |
93 |
|
94 |
|
95 |
fileName = fileNamePrefix + fileName; |
96 |
|
97 |
request.setFileName(fileName); |
98 |
synchronized (this) { |
99 |
addDownloadedURLRequest(request); |
100 |
} |
101 |
// TODO
|
102 |
// jo ac? comprovaria quin protocol, m?tode, host, etc... i crearia un
|
103 |
// objecte que tractara la desc?rrega segons el m?tode que li toca.
|
104 |
// algo en plan Strategy's
|
105 |
//
|
106 |
// per exemple si fem URLRetrieveTask una abstracta i tenim
|
107 |
//
|
108 |
// GET de tota la vida
|
109 |
// serverQueue.put(new HTTPGetRetrieveTask(request, listener));
|
110 |
//
|
111 |
// POST
|
112 |
// serverQueue.put(new HTTPPostRetrieveTask(request, listener));
|
113 |
//
|
114 |
// FTP
|
115 |
// serverQueue.put(new FTPRetrieveTask(request, listener));
|
116 |
//
|
117 |
// ????Xarxa local?????
|
118 |
// serverQueue.put(new SMBRetrieveTask(request, listener));
|
119 |
|
120 |
// Enqueue the request and the listener will be notified when done.
|
121 |
request.addRetrieveListener(listener); |
122 |
URLRetrieveTask task = new URLRetrieveTask(request);
|
123 |
serverQueue.put(task); |
124 |
} |
125 |
} catch (MalformedURLException e) { |
126 |
e.printStackTrace(); |
127 |
} |
128 |
return request;
|
129 |
} |
130 |
|
131 |
private IQueue getQueue(String hostName) { |
132 |
RetrieveQueue queue = null;
|
133 |
if (serversTable.containsKey(hostName))
|
134 |
queue = (RetrieveQueue) serversTable.get(hostName); |
135 |
else {
|
136 |
// crea la cola
|
137 |
queue = new RetrieveQueue(hostName);
|
138 |
// pone la cola del server en marcha.
|
139 |
} |
140 |
return queue;
|
141 |
} |
142 |
|
143 |
|
144 |
private Vector downloadedFiles = new Vector(); |
145 |
private final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"tmp-andami"; |
146 |
/**
|
147 |
* Remove an URL from the system cache. The file will remain in the file
|
148 |
* system for further eventual uses.
|
149 |
* @param request
|
150 |
*/
|
151 |
public void removeURLRequest(URLRequest request) { |
152 |
if (downloadedFiles != null && downloadedFiles.contains(request)) |
153 |
downloadedFiles.remove(request); |
154 |
} |
155 |
/**
|
156 |
* Adds an URL to the table of downloaded files for further uses. If the URL
|
157 |
* already exists in the table its filePath value is updated to the new one and
|
158 |
* the old file itself is removed from the file system.
|
159 |
*
|
160 |
* @param url
|
161 |
* @param filePath
|
162 |
*/
|
163 |
private void addDownloadedURLRequest(URLRequest request){ |
164 |
if (!downloadedFiles.contains(request)) {
|
165 |
downloadedFiles.add(request); |
166 |
} |
167 |
} |
168 |
|
169 |
} |