01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.updates;
04:
05: import java.io.*;
06: import java.net.URL;
07:
08: public class FileUpdate implements Update {
09: private static final String slash = "/";
10:
11: protected String destination;
12: protected String source;
13: protected File destinationDir;
14: protected String rootDir;
15: protected String filename;
16:
17: public FileUpdate(Updater updater, String source, String destination)
18: throws Exception {
19: this .destination = destination;
20: this .source = source;
21: rootDir = updater.context.rootPagePath;
22: destinationDir = new File(new File(rootDir), destination);
23:
24: filename = new File(source).getName();
25: }
26:
27: public void doUpdate() throws Exception {
28: makeSureDirectoriesExist();
29: copyResource();
30: }
31:
32: private void makeSureDirectoriesExist() {
33: String[] subDirectories = destination.split(slash);
34: String currentDirPath = rootDir;
35:
36: for (int i = 0; i < subDirectories.length; i++) {
37: String subDirectory = subDirectories[i];
38: currentDirPath = currentDirPath + slash + subDirectory;
39: File directory = new File(currentDirPath);
40: directory.mkdir();
41: }
42: }
43:
44: private void copyResource() throws Exception {
45: URL url = getResource(source);
46: if (url != null) {
47: InputStream input = url.openStream();
48: OutputStream output = new FileOutputStream(
49: destinationFile());
50:
51: int b;
52: while ((b = input.read()) != -1)
53: output.write(b);
54:
55: input.close();
56: output.close();
57: } else
58: throw new Exception("Could not load resource: " + source);
59: }
60:
61: protected URL getResource(String resource) {
62: return ClassLoader.getSystemResource(resource);
63: }
64:
65: public String getMessage() {
66: return "Installing file: " + destinationFile();
67: }
68:
69: protected File destinationFile() {
70: return new File(destinationDir, filename);
71: }
72:
73: public String getName() {
74: return "FileUpdate(" + filename + ")";
75: }
76:
77: public boolean shouldBeApplied() throws Exception {
78: return !destinationFile().exists();
79: }
80: }
|