root / branches / MULTITHREADING_DEVELOPMENT / libraries / libRemoteServices / src / org / gvsig / remoteClient / taskplanning / retrieving / URLRequest.java @ 5393
History | View | Annotate | Download (4.87 KB)
1 |
/*
|
---|---|
2 |
* Created on 01-oct-2005
|
3 |
*/
|
4 |
package org.gvsig.remoteClient.taskplanning.retrieving; |
5 |
|
6 |
import java.net.MalformedURLException; |
7 |
import java.net.URL; |
8 |
import java.util.Iterator; |
9 |
import java.util.Vector; |
10 |
|
11 |
import org.gvsig.remoteClient.taskplanning.ICancellable; |
12 |
|
13 |
/**
|
14 |
* @author Luis W. Sevilla (sevilla_lui@gva.es)
|
15 |
*/
|
16 |
public class URLRequest{ |
17 |
public static final String HTTP = "http"; |
18 |
private volatile int hashCode = 0; |
19 |
public static final int GET = 1; |
20 |
public static final int POST = 2; |
21 |
|
22 |
private int requestType = GET; |
23 |
private String protocol; |
24 |
private String host; |
25 |
private int port = -1; |
26 |
private String file; |
27 |
private boolean locked; |
28 |
private Vector listeners = new Vector(); |
29 |
private RetrieveEvent event = new RetrieveEvent(); |
30 |
private URLRetrieveTask worker;
|
31 |
private ICancellable cancel;
|
32 |
|
33 |
public URL getUrl() throws MalformedURLException { |
34 |
String u = protocol;
|
35 |
u += "://"+host;
|
36 |
if (port != -1) |
37 |
u += ":"+port;
|
38 |
u += (file.charAt(0) == '/') ? file : "/"+file; |
39 |
return new URL(u); |
40 |
} |
41 |
|
42 |
/**
|
43 |
* @return Returns the fileName.
|
44 |
*/
|
45 |
public String getFileName() { |
46 |
return event.getFileName();
|
47 |
} |
48 |
|
49 |
/**
|
50 |
* @param fileName The fileName to set.
|
51 |
*/
|
52 |
public void setFileName(String fileName) { |
53 |
event.setFileName(fileName); |
54 |
} |
55 |
|
56 |
/**
|
57 |
* @return Returns the host.
|
58 |
*/
|
59 |
public String getHost() { |
60 |
return host;
|
61 |
} |
62 |
|
63 |
/**
|
64 |
* @param host The host to set.
|
65 |
*/
|
66 |
public void setHost(String host) { |
67 |
this.host = host;
|
68 |
} |
69 |
|
70 |
/**
|
71 |
* @return Returns the file.
|
72 |
*/
|
73 |
public String getFile() { |
74 |
return file;
|
75 |
} |
76 |
|
77 |
/**
|
78 |
* @param page The file to set.
|
79 |
*/
|
80 |
public void setFile(String page) { |
81 |
this.file = page;
|
82 |
} |
83 |
|
84 |
/**
|
85 |
* @return Returns the protocol.
|
86 |
*/
|
87 |
public String getProtocol() { |
88 |
return protocol;
|
89 |
} |
90 |
|
91 |
/**
|
92 |
* @param protocol The protocol to set.
|
93 |
*/
|
94 |
public void setProtocol(String protocol) { |
95 |
this.protocol = protocol;
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* @return Returns the requestType.
|
100 |
*/
|
101 |
public int getRequestType() { |
102 |
return requestType;
|
103 |
} |
104 |
|
105 |
/**
|
106 |
* @param requestType The requestType to set.
|
107 |
*/
|
108 |
public void setRequestType(int requestType) { |
109 |
this.requestType = requestType;
|
110 |
} |
111 |
|
112 |
/**
|
113 |
* @return Returns the port.
|
114 |
*/
|
115 |
public int getPort() { |
116 |
return port;
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* @param port The port to set.
|
121 |
*/
|
122 |
public void setPort(int port) { |
123 |
this.port = port;
|
124 |
} |
125 |
|
126 |
protected void lock() { |
127 |
locked = true;
|
128 |
} |
129 |
|
130 |
protected void unlock() { |
131 |
locked = false;
|
132 |
} |
133 |
|
134 |
protected synchronized void setStatus(int status) { |
135 |
event.setType(status); |
136 |
fireEvent(); |
137 |
} |
138 |
|
139 |
protected synchronized int getStatus() { |
140 |
return event.getType();
|
141 |
} |
142 |
|
143 |
public void addRetrieveListener(RetrieveListener l) { |
144 |
if (l!=null) |
145 |
listeners.add(l); |
146 |
} |
147 |
|
148 |
public void removeRetrieveListener(RetrieveListener l) { |
149 |
listeners.remove(l); |
150 |
if (listeners.size()==0) { |
151 |
// If no body listens at, then no body cares about the result;
|
152 |
// cancel the job
|
153 |
event.setType(RetrieveEvent.REQUEST_CANCELED); |
154 |
worker.cancel(); |
155 |
worker = null;
|
156 |
} |
157 |
} |
158 |
|
159 |
private void fireEvent() { |
160 |
while (locked) { /* wait*/System.out.println("I'm locked"); } |
161 |
|
162 |
Iterator it = listeners.iterator();
|
163 |
while (it.hasNext()) {
|
164 |
RetrieveListener listener = (RetrieveListener) it.next(); |
165 |
listener.transferEventReceived(event); |
166 |
} |
167 |
} |
168 |
|
169 |
protected void setWorker(URLRetrieveTask worker) { |
170 |
this.worker = worker;
|
171 |
} |
172 |
|
173 |
public int hashCode() { |
174 |
if (hashCode == 0) { |
175 |
int result = 17; |
176 |
String[] stringFields = new String[] { |
177 |
// fileName,
|
178 |
host, |
179 |
file, |
180 |
protocol |
181 |
}; |
182 |
for (int i = 0; i < stringFields.length; i++) { |
183 |
if (stringFields[i] != null) |
184 |
for (int j = 0; j < stringFields[i].length(); j++) |
185 |
result = 37*result + (int) stringFields[i].charAt(j); |
186 |
|
187 |
} |
188 |
result = 37*result + port;
|
189 |
result = 37*result + requestType;
|
190 |
|
191 |
} |
192 |
return hashCode;
|
193 |
} |
194 |
|
195 |
public boolean equals(Object o) { |
196 |
if (this == o) |
197 |
return true; |
198 |
if (!(o instanceof URLRequest)) |
199 |
return false; |
200 |
|
201 |
URLRequest other = (URLRequest) o; |
202 |
|
203 |
String[] stringFields = new String[] { |
204 |
// this.fileName, NO!!! Do not check file name
|
205 |
this.host,
|
206 |
this.file,
|
207 |
this.protocol
|
208 |
}; |
209 |
String[] othersStringField = new String[] { |
210 |
// other.fileName, NO!!! Do not check file name
|
211 |
other.host, |
212 |
other.file, |
213 |
other.protocol |
214 |
}; |
215 |
for (int i = 0; i < stringFields.length; i++) { |
216 |
if (stringFields[i] == null && othersStringField[i]!=null) return false; |
217 |
if (stringFields[i] == null && othersStringField[i]!=null) return false; |
218 |
if (stringFields[i] != null && othersStringField[i]!=null) |
219 |
if (!stringFields[i].equals(othersStringField[i])) return false; |
220 |
} |
221 |
|
222 |
if (this.port != other.port) return false; |
223 |
if (this.requestType != other.requestType) return false; |
224 |
return true; |
225 |
} |
226 |
|
227 |
public void setCancel(ICancellable cancel) { |
228 |
this.cancel = cancel;
|
229 |
} |
230 |
|
231 |
public boolean isCanceled() { |
232 |
if (cancel!=null) |
233 |
return cancel.isCanceled() || event.getType() == RetrieveEvent.REQUEST_CANCELED;
|
234 |
else
|
235 |
return event.getType() == RetrieveEvent.REQUEST_CANCELED;
|
236 |
} |
237 |
} |