1 56 package org.objectstyle.cayenne.conf; 57 58 import java.io.InputStream ; 59 60 import org.apache.log4j.Logger; 61 import org.objectstyle.cayenne.ConfigurationException; 62 import org.objectstyle.cayenne.util.ResourceLocator; 63 import org.objectstyle.cayenne.util.Util; 64 65 73 public class DefaultConfiguration extends Configuration { 74 private static Logger logObj = Logger.getLogger(DefaultConfiguration.class); 75 76 79 private ResourceLocator locator; 80 81 87 public DefaultConfiguration() { 88 this(Configuration.DEFAULT_DOMAIN_FILE); 89 } 90 91 98 public DefaultConfiguration(String domainConfigurationName) { 99 super(domainConfigurationName); 100 101 if (domainConfigurationName == null) { 102 throw new ConfigurationException("cannot use null as domain file name."); 103 } 104 105 logObj.debug("using domain file name: " + domainConfigurationName); 106 107 ResourceLocator locator = new ResourceLocator(); 109 locator.setSkipAbsolutePath(true); 110 locator.setSkipClasspath(false); 111 locator.setSkipCurrentDirectory(true); 112 locator.setSkipHomeDirectory(true); 113 114 if (!(this.getClass().equals(DefaultConfiguration.class))) { 116 locator.addClassPath(Util.getPackagePath(this.getClass().getName())); 117 } 118 119 locator.setClassLoader(Configuration.getResourceLoader()); 123 124 setResourceLocator(locator); 125 } 126 127 133 public DefaultConfiguration(String domainConfigurationName, ResourceLocator locator) { 134 super(domainConfigurationName); 135 setResourceLocator(locator); 136 } 137 138 151 public void addClassPath(String customPath) { 152 this.getResourceLocator().addClassPath(customPath); 153 } 154 155 156 165 public void addResourcePath(String path) { 166 this.getResourceLocator().addFilesystemPath(path); 167 } 168 169 175 public boolean canInitialize() { 176 logObj.debug("canInitialize started."); 177 return true; 179 } 180 181 185 public void initialize() throws Exception { 186 logObj.debug("initialize starting."); 187 188 InputStream in = this.getDomainConfiguration(); 189 if (in == null) { 190 StringBuffer msg = new StringBuffer (); 191 msg 192 .append("[") 193 .append(this.getClass().getName()) 194 .append("] : Domain configuration file \"") 195 .append(this.getDomainConfigurationName()) 196 .append("\" is not found."); 197 198 throw new ConfigurationException(msg.toString()); 199 } 200 201 ConfigLoaderDelegate delegate = this.getLoaderDelegate(); 202 if (delegate == null) { 203 delegate = new RuntimeLoadDelegate(this, this.getLoadStatus(), Configuration.getLoggingLevel()); 204 } 205 206 ConfigLoader loader = new ConfigLoader(delegate); 207 208 try { 209 loader.loadDomains(in); 210 } finally { 211 this.setLoadStatus(delegate.getStatus()); 212 in.close(); 213 } 214 215 logObj.debug("initialize finished."); 217 } 218 219 223 public void didInitialize() { 224 logObj.debug("didInitialize finished."); 226 } 227 228 231 protected ResourceLocator getResourceLocator() { 232 return this.locator; 233 } 234 235 239 protected void setResourceLocator(ResourceLocator locator) { 240 this.locator = locator; 241 } 242 243 248 protected InputStream getDomainConfiguration() { 249 return locator.findResourceStream(this.getDomainConfigurationName()); 250 } 251 252 257 protected InputStream getMapConfiguration(String location) { 258 return locator.findResourceStream(location); 259 } 260 261 protected InputStream getViewConfiguration(String location) { 262 return locator.findResourceStream(location); 263 } 264 265 268 public String toString() { 269 StringBuffer buf = new StringBuffer (); 270 buf 271 .append('[') 272 .append(this.getClass().getName()) 273 .append(": classloader=") 274 .append(locator.getClassLoader()) 275 .append(']'); 276 return buf.toString(); 277 } 278 279 } | Popular Tags |