svn-gvsig-desktop / branches / MULTITHREADING_DEVELOPMENT / libraries / libRemoteServices / src / org / gvsig / remoteClient / taskplanning / retrieving / URLRequest.java @ 5378
History | View | Annotate | Download (4.86 KB)
1 | 5156 | jaume | /*
|
---|---|---|---|
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 | 5245 | jaume | import java.util.Iterator; |
9 | import java.util.Vector; |
||
10 | 5156 | jaume | |
11 | 5378 | jaume | import org.gvsig.remoteClient.ICancellable; |
12 | |||
13 | 5156 | jaume | /**
|
14 | * @author Luis W. Sevilla (sevilla_lui@gva.es)
|
||
15 | */
|
||
16 | 5180 | jaume | public class URLRequest{ |
17 | 5230 | jaume | public static final String HTTP = "http"; |
18 | 5180 | jaume | private volatile int hashCode = 0; |
19 | 5230 | jaume | public static final int GET = 1; |
20 | public static final int POST = 2; |
||
21 | 5156 | jaume | |
22 | private int requestType = GET; |
||
23 | private String protocol; |
||
24 | private String host; |
||
25 | 5230 | jaume | private int port = -1; |
26 | private String file; |
||
27 | 5245 | jaume | private boolean locked; |
28 | private Vector listeners = new Vector(); |
||
29 | private RetrieveEvent event = new RetrieveEvent(); |
||
30 | 5262 | jaume | private URLRetrieveTask worker;
|
31 | 5378 | jaume | private ICancellable cancel;
|
32 | 5156 | jaume | |
33 | public URL getUrl() throws MalformedURLException { |
||
34 | String u = protocol;
|
||
35 | u += "://"+host;
|
||
36 | 5230 | jaume | if (port != -1) |
37 | 5156 | jaume | u += ":"+port;
|
38 | 5246 | jaume | u += (file.charAt(0) == '/') ? file : "/"+file; |
39 | 5156 | jaume | return new URL(u); |
40 | } |
||
41 | 5269 | jaume | |
42 | 5156 | jaume | /**
|
43 | * @return Returns the fileName.
|
||
44 | */
|
||
45 | public String getFileName() { |
||
46 | 5273 | jaume | return event.getFileName();
|
47 | 5156 | jaume | } |
48 | 5269 | jaume | |
49 | 5156 | jaume | /**
|
50 | * @param fileName The fileName to set.
|
||
51 | */
|
||
52 | public void setFileName(String fileName) { |
||
53 | 5273 | jaume | event.setFileName(fileName); |
54 | 5156 | jaume | } |
55 | 5269 | jaume | |
56 | 5156 | jaume | /**
|
57 | * @return Returns the host.
|
||
58 | */
|
||
59 | public String getHost() { |
||
60 | return host;
|
||
61 | } |
||
62 | 5269 | jaume | |
63 | 5156 | jaume | /**
|
64 | * @param host The host to set.
|
||
65 | */
|
||
66 | public void setHost(String host) { |
||
67 | this.host = host;
|
||
68 | } |
||
69 | 5269 | jaume | |
70 | 5156 | jaume | /**
|
71 | 5230 | jaume | * @return Returns the file.
|
72 | 5156 | jaume | */
|
73 | 5230 | jaume | public String getFile() { |
74 | return file;
|
||
75 | 5156 | jaume | } |
76 | 5269 | jaume | |
77 | 5156 | jaume | /**
|
78 | 5230 | jaume | * @param page The file to set.
|
79 | 5156 | jaume | */
|
80 | 5230 | jaume | public void setFile(String page) { |
81 | this.file = page;
|
||
82 | 5156 | jaume | } |
83 | 5269 | jaume | |
84 | 5156 | jaume | /**
|
85 | * @return Returns the protocol.
|
||
86 | */
|
||
87 | public String getProtocol() { |
||
88 | return protocol;
|
||
89 | } |
||
90 | 5269 | jaume | |
91 | 5156 | jaume | /**
|
92 | * @param protocol The protocol to set.
|
||
93 | */
|
||
94 | public void setProtocol(String protocol) { |
||
95 | this.protocol = protocol;
|
||
96 | } |
||
97 | 5269 | jaume | |
98 | 5156 | jaume | /**
|
99 | * @return Returns the requestType.
|
||
100 | */
|
||
101 | public int getRequestType() { |
||
102 | return requestType;
|
||
103 | } |
||
104 | 5269 | jaume | |
105 | 5156 | jaume | /**
|
106 | * @param requestType The requestType to set.
|
||
107 | */
|
||
108 | public void setRequestType(int requestType) { |
||
109 | this.requestType = requestType;
|
||
110 | } |
||
111 | 5269 | jaume | |
112 | 5156 | jaume | /**
|
113 | * @return Returns the port.
|
||
114 | */
|
||
115 | 5230 | jaume | public int getPort() { |
116 | 5156 | jaume | return port;
|
117 | } |
||
118 | 5269 | jaume | |
119 | 5156 | jaume | /**
|
120 | * @param port The port to set.
|
||
121 | */
|
||
122 | 5230 | jaume | public void setPort(int port) { |
123 | 5156 | jaume | this.port = port;
|
124 | } |
||
125 | 5180 | jaume | |
126 | 5269 | jaume | 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 | 5378 | jaume | event.setType(RetrieveEvent.REQUEST_CANCELED); |
154 | 5269 | jaume | 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 | 5180 | jaume | public int hashCode() { |
174 | if (hashCode == 0) { |
||
175 | int result = 17; |
||
176 | String[] stringFields = new String[] { |
||
177 | 5273 | jaume | // fileName,
|
178 | 5180 | jaume | host, |
179 | 5230 | jaume | file, |
180 | 5180 | jaume | 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 | 5230 | jaume | result = 37*result + port;
|
189 | 5180 | jaume | result = 37*result + requestType;
|
190 | 5273 | jaume | |
191 | 5180 | jaume | } |
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 | 5230 | jaume | this.file,
|
207 | 5180 | jaume | this.protocol
|
208 | }; |
||
209 | String[] othersStringField = new String[] { |
||
210 | // other.fileName, NO!!! Do not check file name
|
||
211 | other.host, |
||
212 | 5230 | jaume | other.file, |
213 | 5180 | jaume | 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 | 5230 | jaume | |
222 | if (this.port != other.port) return false; |
||
223 | if (this.requestType != other.requestType) return false; |
||
224 | 5180 | jaume | return true; |
225 | } |
||
226 | 5378 | jaume | |
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 | 5269 | jaume | } |