KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > setup > sources > FileConfigurationSource


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.config.schema.setup.sources;
5
6 import com.tc.config.schema.setup.ConfigurationSetupException;
7 import com.tc.util.Assert;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13
14 /**
15  * A {@link ConfigurationSource} that reads from a file.
16  */

17 public class FileConfigurationSource implements ConfigurationSource {
18
19   private final String JavaDoc path;
20   private final File JavaDoc defaultDirectory;
21
22   public FileConfigurationSource(String JavaDoc path, File JavaDoc defaultDirectory) {
23     Assert.assertNotBlank(path);
24
25     this.path = path;
26     this.defaultDirectory = defaultDirectory;
27   }
28
29   public InputStream JavaDoc getInputStream(long maxTimeoutMillis) throws ConfigurationSetupException {
30     File JavaDoc file = createFile();
31
32     if (!file.exists()) throw new ConfigurationSetupException("The file '" + file.getAbsolutePath()
33                                                               + "' does not exist");
34     if (file.isDirectory()) throw new ConfigurationSetupException("The \"file\" '" + file.getAbsolutePath()
35                                                                   + "' is actually a directory");
36
37     try {
38       FileInputStream JavaDoc out = new FileInputStream JavaDoc(file);
39       return out;
40     } catch (IOException JavaDoc ioe) {
41       // We need this to be a ConfigurationSetupException so that we don't keep 'retrying' this file.
42
throw new ConfigurationSetupException("We can't read data from the file '" + file.getAbsolutePath() + "': "
43                                             + ioe.getLocalizedMessage());
44     }
45   }
46
47   public File JavaDoc directoryLoadedFrom() {
48     return createFile().getParentFile();
49   }
50
51   private File JavaDoc createFile() {
52     File JavaDoc file = new File JavaDoc(this.path);
53     if (!file.isAbsolute()) file = new File JavaDoc(this.defaultDirectory, this.path);
54     return file;
55   }
56
57   public boolean isTrusted() {
58     return false;
59   }
60   
61   public String JavaDoc toString() {
62     return "file at '" + createFile().getAbsolutePath() + "'";
63   }
64
65 }
66
Popular Tags