1 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 ; 10 import java.io.FileInputStream ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 14 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) throws ConfigurationSetupException { 30 File 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 out = new FileInputStream (file); 39 return out; 40 } catch (IOException ioe) { 41 throw new ConfigurationSetupException("We can't read data from the file '" + file.getAbsolutePath() + "': " 43 + ioe.getLocalizedMessage()); 44 } 45 } 46 47 public File directoryLoadedFrom() { 48 return createFile().getParentFile(); 49 } 50 51 private File createFile() { 52 File file = new File (this.path); 53 if (!file.isAbsolute()) file = new File (this.defaultDirectory, this.path); 54 return file; 55 } 56 57 public boolean isTrusted() { 58 return false; 59 } 60 61 public String toString() { 62 return "file at '" + createFile().getAbsolutePath() + "'"; 63 } 64 65 } 66 | Popular Tags |