1 19 20 package org.apache.cayenne.conf; 21 22 import java.io.InputStream ; 23 24 import org.apache.cayenne.ConfigurationException; 25 import org.apache.cayenne.util.ResourceLocator; 26 import org.apache.cayenne.util.Util; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 35 public class DefaultConfiguration extends Configuration { 36 37 private static Log logger = LogFactory.getLog(DefaultConfiguration.class); 38 39 42 private ResourceLocator locator; 43 44 51 public DefaultConfiguration() { 52 this(Configuration.DEFAULT_DOMAIN_FILE); 53 } 54 55 63 public DefaultConfiguration(String domainConfigurationName) { 64 super(domainConfigurationName); 65 66 if (domainConfigurationName == null) { 67 throw new ConfigurationException("cannot use null as domain file name."); 68 } 69 70 logger.debug("using domain file name: " + domainConfigurationName); 71 72 ResourceLocator locator = new ResourceLocator(); 74 locator.setSkipAbsolutePath(true); 75 locator.setSkipClasspath(false); 76 locator.setSkipCurrentDirectory(true); 77 locator.setSkipHomeDirectory(true); 78 79 if (!(this.getClass().equals(DefaultConfiguration.class))) { 81 locator.addClassPath(Util.getPackagePath(this.getClass().getName())); 82 } 83 84 setResourceLocator(locator); 85 } 86 87 93 public DefaultConfiguration(String domainConfigurationName, ResourceLocator locator) { 94 super(domainConfigurationName); 95 setResourceLocator(locator); 96 } 97 98 109 public void addClassPath(String customPath) { 110 this.getResourceLocator().addClassPath(customPath); 111 } 112 113 122 public void addResourcePath(String path) { 123 this.getResourceLocator().addFilesystemPath(path); 124 } 125 126 131 public boolean canInitialize() { 132 logger.debug("canInitialize started."); 133 return true; 135 } 136 137 141 public void initialize() throws Exception { 142 logger.debug("initialize starting."); 143 144 InputStream in = this.getDomainConfiguration(); 145 if (in == null) { 146 StringBuffer msg = new StringBuffer (); 147 msg.append("[").append(this.getClass().getName()).append( 148 "] : Domain configuration file \"").append( 149 this.getDomainConfigurationName()).append("\" is not found."); 150 151 throw new ConfigurationException(msg.toString()); 152 } 153 154 ConfigLoaderDelegate delegate = this.getLoaderDelegate(); 155 if (delegate == null) { 156 delegate = new RuntimeLoadDelegate(this, this.getLoadStatus()); 157 } 158 159 ConfigLoader loader = new ConfigLoader(delegate); 160 161 try { 162 loader.loadDomains(in); 163 } 164 finally { 165 this.setLoadStatus(delegate.getStatus()); 166 in.close(); 167 } 168 169 logger.debug("initialize finished."); 171 } 172 173 177 public void didInitialize() { 178 logger.debug("didInitialize finished."); 180 } 181 182 185 protected ResourceLocator getResourceLocator() { 186 return this.locator; 187 } 188 189 193 protected void setResourceLocator(ResourceLocator locator) { 194 this.locator = locator; 195 } 196 197 201 protected InputStream getDomainConfiguration() { 202 return locator.findResourceStream(this.getDomainConfigurationName()); 203 } 204 205 210 protected InputStream getMapConfiguration(String location) { 211 return locator.findResourceStream(location); 212 } 213 214 protected InputStream getViewConfiguration(String location) { 215 return locator.findResourceStream(location); 216 } 217 218 221 public String toString() { 222 StringBuffer buf = new StringBuffer (); 223 buf 224 .append('[') 225 .append(this.getClass().getName()) 226 .append(": classloader=") 227 .append(locator.getClassLoader()) 228 .append(']'); 229 return buf.toString(); 230 } 231 232 } 233 | Popular Tags |