1
2
3
4
5
6 package org.astrogrid.mySpace.delegate.mySpaceManager;
7
8 import java.io.*;
9
10 import java.lang.UnsupportedOperationException;
11 import java.net.MalformedURLException;
12 import java.net.URL;
13 import java.util.Arrays;
14 import java.util.Vector;
15
16
17 /***
18 * This is a dummy myspace delegate that can be used to test applications
19 * against myspace delegates but without actually needing access to a myspace
20 * service. It basically stores files locally, in a directory
21 * off the working one, with a name given by the endpoint,
22 *
23 * @author: M Hill
24 */
25
26
27 public class MySpaceDummyDelegate extends MySpaceManagerDelegate
28 {
29 File dir = null;
30
31 public static final String OVERWRITE = "Overwrite";
32 public static final String APPEND = "Append";
33
34 /***
35 * Class for filtering filenames based on the given criteria
36 */
37 private class CriteriaFilenameFilter implements FilenameFilter
38 {
39 String criteria = null;
40
41 public CriteriaFilenameFilter(String givenCriteria)
42 {
43 this.criteria = givenCriteria;
44 }
45
46 public boolean accept(File dir, String name)
47 {
48 if (criteria.equals("*"))
49 {
50 return true;
51 }
52 else
53 {
54 throw new UnsupportedOperationException();
55 }
56 }
57 }
58
59 /***
60 * Opens (creates if it doesn't exist) a subdirectory off the working one
61 * given by the given end point
62 */
63 public MySpaceDummyDelegate(String givenEndPoint) {
64
65 super(givenEndPoint);
66
67
68 String dirName = givenEndPoint.replace(':', '_');
69 dirName = givenEndPoint.replace('/', '_');
70
71 try {
72 dir = File.createTempFile("MySpaceDummy" + dirName,"");
73
74
75 dir.delete();
76 dir.mkdir();
77
78
79
80
81
82
83
84
85
86
87 } catch (IOException e) {
88 throw new RuntimeException("Couldn't create dummy myspace ",e);
89 }
90 }
91
92 /***
93 * Returns a list of filenames that match the given criteria
94 *
95 * @param: userid: userid
96 * @param: communityid
97 * @param: criteria: eg./userid/communityid/workflows/A*
98 * @return: Vector of String of fileNames
99 */
100 public Vector listDataHoldings(String userid, String communityid, String criteria) {
101
102 String[] matchingFilenames = dir.list(new CriteriaFilenameFilter(criteria));
103
104 return new Vector(Arrays.asList(matchingFilenames));
105 }
106
107 /***
108 * A generic method which returns a list of xml strings, each of which
109 * holds the full file path as well as other info such as expired date etc.
110 * @param userid
111 * @param communityid
112 * @param criteria
113 * @return Vector of strings, each one an XML snippet
114 */
115 public Vector listDataHoldingsGen(String userid, String communityid, String criteria) {
116
117 Vector matchingFilesXml = new Vector();
118
119 File[] matchedFiles = dir.listFiles(new CriteriaFilenameFilter(criteria));
120
121
122 for (int f=0;f<matchedFiles.length;f++)
123 {
124 matchingFilesXml.add(getXmlSnippet(matchedFiles[f]));
125 }
126
127 return matchingFilesXml;
128 }
129
130 /***
131 * Returns the full xml snippet for the given file
132 */
133 private String getXmlSnippet(File givenFile)
134 {
135 return "<filename>"+givenFile.getName()+"<filename>";
136 }
137
138 /***
139 * Returns the path to access the given file for the given user, etc
140 */
141 private String getPath(String userid, String communityid)
142 {
143 return dir.getPath()+File.separator;
144 }
145
146 /***
147 * Returns the full xml description of the given file
148 * @param userid
149 * @param communityid
150 * @param serverFileName: full file name eg: /clq/serv1/File1.xml
151 * @return
152 */
153 public String listDataHolding(String userid, String communityid, String serverFileName) {
154
155 File file = new File(getPath(userid, communityid)+serverFileName);
156
157
158
159 return getXmlSnippet(file);
160 }
161
162 /***
163 * Copies one file to another on the same server
164 *
165 * @param userid
166 * @param communityid
167 * @param sourceFileName: full file name copy from
168 * @param destFileName: full file name copy to
169 * @return
170 */
171 public String copyDataHolding(String userid, String communityid, String sourceFileName, String destFileName)
172 throws IOException {
173
174 File source = new File(getPath(userid, communityid)+sourceFileName);
175 File dest = new File(getPath(userid, communityid)+destFileName);
176
177
178
179 InputStream in = new BufferedInputStream(new FileInputStream(source));
180 OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
181
182 byte[] block = new byte[100];
183 int bytesRead = 0;
184
185 while (in.available() > 0)
186 {
187 bytesRead = in.read(block);
188 out.write(block, 0, bytesRead);
189 }
190
191 in.close();
192 out.close();
193
194 return "done";
195 }
196
197 /***
198 * Renames a file on a server
199 *
200 * @todo broken at the moment - possibly because the rename shouldn't
201 * have the path specified.
202 *
203 * @param userid
204 * @param communityid
205 * @param oldFileName: ole file full name
206 * @param newFileName: new file full name
207 * @return
208 */
209
210 public String renameDataHolding(String userid, String communityid, String oldFileName, String newFileName) throws IOException {
211
212 File file = new File(getPath(userid, communityid)+oldFileName);
213 File dest = new File(getPath(userid, communityid)+newFileName);
214
215
216
217
218 boolean success = file.renameTo(dest);
219
220 if (!success)
221 {
222 throw new IOException("Failed to rename "+file+" to "+dest+" (don't know why)");
223 }
224
225 return "done";
226 }
227
228 /***
229 * Deletes a file on the server
230 *
231 * @param userid
232 * @param communityid
233 * @param serverFileName: Full file name which you want to delete
234 * @return
235 * @throws Exception
236 */
237 public String deleteDataHolding(String userid, String communityid, String serverFileName) throws IOException {
238
239 File file = new File(getPath(userid, communityid)+serverFileName);
240
241
242
243 boolean success = file.delete();
244
245 if (!success) {
246 throw new IOException("Could not delete file '"+file+"' (don't know why)");
247 }
248
249 return "done";
250 }
251
252 /***
253 * Saves the given fileContent string to the given filename on the server.
254 * Used as a 'push' save
255 *
256 * @param: userid: userid
257 * @param: communityid
258 * @param: fileName: unique file name for Workflow or Query you want to store.
259 * @param: fileContent: content of workflow or data query
260 * @param: category "WF" or "QUERY", if not set, default is "VOTable"
261 * @param: action "Overwrite" or "Append", if not set, default is "Overwrite"
262 * @return: boolean true if file successfully stored in MySapce false otherwise.
263 */
264
265 public boolean saveDataHolding(String userid, String communityid, String fileName, String fileContent,
266 String category, String action) throws IOException
267 {
268 boolean append = false;
269
270 if (action.equals(OVERWRITE)) {
271 append = false;
272 }
273 else if (action.equals(APPEND)) {
274 append = true;
275 }
276 else {
277 throw new IllegalArgumentException("Illegal Action '"+action+"'");
278 }
279
280 File dest = new File(getPath(userid, communityid)+fileName);
281 OutputStream out = new FileOutputStream(dest, append);
282 out.write(fileContent.getBytes());
283 out.close();
284
285 return true;
286 }
287
288 /***
289 * Saves the file at the given URL to the server filespace
290 * Used to 'pull' data
291 * @param userid
292 * @param communityid
293 * @param fileName
294 * @param importURI - url that save the dataholding from
295 * @param category
296 * @param action
297 * @return
298 * @throws Exception
299 */
300
301 public boolean saveDataHoldingURL(String userid, String communityid, String fileName, String importURL,
302 String category, String action) throws IOException {
303
304 boolean append = false;
305
306 if (action.toLowerCase().equals(OVERWRITE)) {
307 append = false;
308 }
309 else if (action.toLowerCase().equals(APPEND)) {
310 append = true;
311 }
312 else {
313 throw new IllegalArgumentException("Illegal Action '"+action+"'");
314 }
315
316 URL source = new URL(importURL);
317 File dest = new File(getPath(userid, communityid)+fileName);
318
319 InputStream in = new BufferedInputStream(source.openStream());
320 OutputStream out = new BufferedOutputStream(new FileOutputStream(dest, append));
321
322 byte[] block = new byte[100];
323 int bytesRead = 0;
324
325 while (in.available() > 0)
326 {
327 bytesRead = in.read(block);
328 out.write(block, 0, bytesRead);
329 }
330
331 in.close();
332 out.close();
333
334 return true;
335 }
336
337 /***
338 * Returns the URL of the given file name
339 *
340 * @param: userid: userid
341 * @param: communityid: communityid
342 * @param: fullFileName: full file name in mySpace for Workflow or Query you want to find
343 * @return: URL of file
344 */
345 public URL getDataHoldingUrl(String userid, String communityid, String fullFileName) throws IOException {
346
347 File file = new File(getPath(userid, communityid)+fullFileName);
348
349
350
351 String fullpath = file.getCanonicalPath();
352
353 return new URL("file://"+fullpath);
354 }
355
356 /***
357 * Returns an inputstream to the given file name
358 *
359 * @param: userid: userid
360 * @param: communityid: communityid
361 * @param: fullFileName: full file name in mySpace for Workflow or Query you want be downloaded.
362 * @return: file content in String format
363 */
364 public InputStream getDataHoldingStream(String userid, String communityid, String fullFileName) throws IOException {
365
366 URL url = getDataHoldingUrl(userid, communityid, fullFileName);
367
368 return url.openStream();
369 }
370
371 /***
372 * Returns the contents of the given file namen as a string. Not sure about
373 * the encoding methods used to convert between bytes and chars.
374 *
375 * @param: userid: userid
376 * @param: communityid: communityid
377 * @param: fullFileName: full file name in mySpace for Workflow or Query you want be downloaded.
378 * @return: file content in String format
379 */
380 public String getDataHoldingContents(String userid, String communityid, String fullFileName) throws IOException {
381
382 InputStream in = new BufferedInputStream(getDataHoldingStream(userid, communityid, fullFileName));
383 ByteArrayOutputStream out = new ByteArrayOutputStream();
384
385 byte[] block = new byte[100];
386 int bytesRead = 0;
387
388 while (in.available() > 0)
389 {
390 bytesRead = in.read(block);
391 out.write(block, 0, bytesRead);
392 }
393
394 in.close();
395 out.close();
396
397 return out.toString();
398 }
399
400 /***
401 * Release is permanent on dummy. Does nothing
402 */
403 public String extendLease(String userid, String communityid, String serverFileName, int extentionPeriod)
404 {
405 return "done";
406 }
407
408
409
410 /***
411 * Don't know what this is supposed to do
412 * @param jobDetails: use mySpace/configFiles/MSManagerRequestTemplate.xml to create an xml String by filling in userID/communityID/jobID/serverFileName
413 * @return
414 * @throws Exception
415 */
416
417 public String publish(String jobDetails) {
418 throw new UnsupportedOperationException();
419 }
420
421 /***
422 * What is a container?
423 * @param userid
424 * @param communityid
425 * @param newContainerName
426 * @return
427 */
428 public String createContainer(String userid, String communityid, String newContainerName)
429 {
430 throw new UnsupportedOperationException();
431 }
432
433 /***
434 * @todo
435 *
436 * @param userID: userid@communityid
437 * @param servers: server names user wants to create
438 * @return
439 * @throws Exception
440 */
441 public boolean createUser(String userID, Vector servers) {
442 throw new UnsupportedOperationException();
443 }
444
445 /***
446 * @todo
447 *
448 * @param userID: userid@communityid
449 * @param servers: server names user wants to delete
450 * @return
451 * @throws Exception
452 */
453
454
455 public boolean deleteUser(String userID) {
456 throw new UnsupportedOperationException();
457 }
458
459 /***
460 * @todo
461 *
462 * @param dataHolderName: file working on
463 * @param newOwnerID: userID changing to
464 * @return
465 * @throws Exception
466 */
467 public String changeOwner(String userid, String communityid, String dataHolderName,String newOwnerID)
468 {
469 throw new UnsupportedOperationException();
470 }
471
472 }