1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.astrogrid.filestore.common.transfer.mock ;
22
23 import java.io.InputStream ;
24 import java.io.OutputStream ;
25 import java.io.IOException ;
26 import java.io.FileNotFoundException ;
27
28 import java.util.Map ;
29 import java.util.HashMap ;
30
31 import java.net.URL ;
32 import java.net.URLConnection ;
33 import java.net.URLStreamHandler ;
34
35 import org.apache.commons.logging.Log ;
36 import org.apache.commons.logging.LogFactory ;
37
38 import org.apache.axis.client.Call ;
39
40 /***
41 * A mock stream URL handler, so that we can create mock URLs.
42 *
43 */
44 public class Handler
45 extends URLStreamHandler
46 {
47 /***
48 * Our debug logger.
49 *
50 */
51 private static Log log = LogFactory.getLog(Handler.class);
52
53 /***
54 * Our mock URL protocol.
55 *
56 */
57 public static final String PROTOCOL = "mock" ;
58
59 /***
60 * Public constructor.
61 *
62 */
63 public Handler()
64 {
65 super();
66 log.debug("");
67 log.debug("Handler.Handler()");
68 }
69
70 /***
71 * Add our transport handler to the system protocol search path.
72 *
73 */
74 public static void register()
75 {
76 log.debug("");
77 log.debug("Handler.register()");
78
79
80 Call.addTransportPackage(
81 "org.astrogrid.filestore.common.transfer"
82 );
83 }
84
85 /***
86 * Our map of registered URLs.
87 *
88 */
89 private static Map map = new HashMap() ;
90
91 /***
92 * Register a connector for a URL.
93 *
94 */
95 public static void addConnector(URL url, Connector connector)
96 {
97 log.debug("");
98 log.debug("Handler.addConnector()");
99 log.debug(" URL : " + url.toString());
100 map.put(
101 url.toString(),
102 connector
103 );
104 }
105
106 /***
107 * get a connector for a URL.
108 *
109 */
110 public Connector getConnector(URL url)
111 {
112 log.debug("");
113 log.debug("Handler.getConnector()");
114 log.debug(" URL : " + url.toString());
115 return (Connector) map.get(
116 url.toString()
117 );
118 }
119
120 /***
121 * Open a connection to a mock URL.
122 *
123 */
124 public URLConnection openConnection(URL url)
125 {
126 log.debug("");
127 log.debug("Handler.openConnection()");
128 log.debug(" URL : " + url.toString());
129 return new Connection(url) ;
130 }
131
132 /***
133 * Inner class to handle a URL connection.
134 *
135 */
136 public class Connection
137 extends URLConnection
138 {
139
140 /***
141 * Our connection URL.
142 *
143 */
144 protected URL url ;
145
146 /***
147 * Public constructor.
148 *
149 */
150 public Connection(URL url)
151 {
152 super(url);
153 this.url = url ;
154 log.debug("");
155 log.debug("Handler.Connection()");
156 log.debug(" URL : " + url.toString());
157 }
158
159 /***
160 * Our connector.
161 *
162 */
163 protected Connector connector ;
164
165 /***
166 * Abstract connect() in URLConnection.
167 *
168 */
169 public void connect()
170 {
171 log.debug("");
172 log.debug("Handler.Connection.connect()");
173 log.debug(" URL : " + url.toString());
174 connector = getConnector(url) ;
175 if (null != connector)
176 {
177 log.debug("PASS : found connector");
178 }
179 else {
180 log.debug("FAIL : missing connector");
181 }
182 }
183
184 /***
185 * Get an InputStream to the URL content.
186 *
187 */
188 public InputStream getInputStream()
189 throws IOException
190 {
191 log.debug("");
192 log.debug("Handler.Connection.getInputStream()");
193 log.debug(" URL : " + url.toString());
194
195
196 if (null == this.connector)
197 {
198 this.connect() ;
199 }
200
201
202 if (null != this.connector)
203 {
204 return connector.getInputStream() ;
205 }
206
207
208 else {
209 throw new FileNotFoundException(
210 "Unable to open input stream to " + url.toString()
211 );
212 }
213 }
214
215 /***
216 * Get an OutputStream to the URL content.
217 *
218 */
219 public OutputStream getOutputStream()
220 throws IOException
221 {
222 log.debug("");
223 log.debug("Handler.Connection.getOutputStream()");
224 log.debug(" URL : " + url.toString());
225
226
227 if (null == this.connector)
228 {
229 this.connect() ;
230 }
231
232
233 if (null != this.connector)
234 {
235 return connector.getOutputStream() ;
236 }
237
238
239 else {
240 throw new FileNotFoundException(
241 "Unable to open output stream to " + url.toString()
242 );
243 }
244 }
245 }
246 }