01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.config.schema.setup.sources;
05:
06: import com.tc.config.schema.setup.ConfigurationSetupException;
07: import com.tc.util.Assert;
08:
09: import java.io.File;
10: import java.io.FileInputStream;
11: import java.io.IOException;
12: import java.io.InputStream;
13:
14: /**
15: * A {@link ConfigurationSource} that reads from a file.
16: */
17: public class FileConfigurationSource implements ConfigurationSource {
18:
19: private final String path;
20: private final File defaultDirectory;
21:
22: public FileConfigurationSource(String path, File defaultDirectory) {
23: Assert.assertNotBlank(path);
24:
25: this .path = path;
26: this .defaultDirectory = defaultDirectory;
27: }
28:
29: public InputStream getInputStream(long maxTimeoutMillis)
30: throws ConfigurationSetupException {
31: File file = createFile();
32:
33: if (!file.exists())
34: throw new ConfigurationSetupException("The file '"
35: + file.getAbsolutePath() + "' does not exist");
36: if (file.isDirectory())
37: throw new ConfigurationSetupException("The \"file\" '"
38: + file.getAbsolutePath()
39: + "' is actually a directory");
40:
41: try {
42: FileInputStream out = new FileInputStream(file);
43: return out;
44: } catch (IOException ioe) {
45: // We need this to be a ConfigurationSetupException so that we don't keep 'retrying' this file.
46: throw new ConfigurationSetupException(
47: "We can't read data from the file '"
48: + file.getAbsolutePath() + "': "
49: + ioe.getLocalizedMessage());
50: }
51: }
52:
53: public File directoryLoadedFrom() {
54: return createFile().getParentFile();
55: }
56:
57: private File createFile() {
58: File file = new File(this .path);
59: if (!file.isAbsolute())
60: file = new File(this .defaultDirectory, this .path);
61: return file;
62: }
63:
64: public boolean isTrusted() {
65: return false;
66: }
67:
68: public String toString() {
69: return "file at '" + createFile().getAbsolutePath() + "'";
70: }
71:
72: }
|