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