1 19 20 package org.apache.cayenne.conf; 21 22 import java.io.File ; 23 24 import org.apache.cayenne.ConfigurationException; 25 import org.apache.cayenne.util.ResourceLocator; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 35 public class FileConfiguration extends DefaultConfiguration { 36 private static final Log logger = LogFactory.getLog(FileConfiguration.class); 37 38 41 protected File projectFile; 42 43 49 public FileConfiguration() { 50 this(Configuration.DEFAULT_DOMAIN_FILE); 51 } 52 53 62 public FileConfiguration(String domainConfigurationName) { 63 super(domainConfigurationName); 64 65 this.projectFile = new File (domainConfigurationName); 67 68 ResourceLocator locator = this.getResourceLocator(); 70 locator.setSkipAbsolutePath(false); 71 locator.setSkipClasspath(true); 72 locator.setSkipCurrentDirectory(false); 73 locator.setSkipHomeDirectory(true); 74 75 File projectDirectory = this.getProjectDirectory(); 77 if (projectDirectory != null) { 78 locator.addFilesystemPath(projectDirectory.getPath()); 79 } 80 } 81 82 89 public FileConfiguration(File domainConfigurationFile) { 90 super(); 91 92 logger.debug("using domain file: " + domainConfigurationFile); 93 94 this.setProjectFile(domainConfigurationFile); 96 97 ResourceLocator locator = this.getResourceLocator(); 99 locator.setSkipAbsolutePath(false); 100 locator.setSkipClasspath(true); 101 locator.setSkipCurrentDirectory(false); 102 locator.setSkipHomeDirectory(true); 103 104 File projectDirectory = this.getProjectDirectory(); 106 if (projectDirectory != null) { 107 locator.addFilesystemPath(projectDirectory); 108 } 109 } 110 111 130 public void addFilesystemPath(String path) { 131 this.getResourceLocator().addFilesystemPath(path); 132 } 133 134 141 public void addFilesystemPath(File path) { 142 this.getResourceLocator().addFilesystemPath(path); 143 } 144 145 149 public boolean canInitialize() { 150 return (this.getProjectFile() != null); 152 } 153 154 157 public File getProjectFile() { 158 return projectFile; 159 } 160 161 166 protected void setProjectFile(File projectFile) { 167 if (projectFile != null) { 168 if (projectFile.isFile()) { 169 this.projectFile = projectFile; 170 this.setDomainConfigurationName(projectFile.getName()); 171 } 172 else { 173 throw new ConfigurationException("Project file: " 174 + projectFile 175 + " is a directory or not readable."); 176 } 177 } 178 else { 179 throw new ConfigurationException("Cannot use null as project file."); 180 } 181 } 182 183 187 public File getProjectDirectory() { 188 File pfile = this.getProjectFile(); 189 if (pfile != null) { 190 return pfile.getParentFile(); 191 } 192 else { 193 return null; 194 } 195 } 196 } 197 | Popular Tags |