
package org.objectstyle.cayenne.conf;

import java.io.InputStream;

import org.objectstyle.cayenne.CayenneRuntimeException;

public class PackageConfiguration extends DefaultConfiguration
{
	private String _packagePath;

	public PackageConfiguration() {
		super();
		_packagePath = this.getClass().getPackage().getName().replace('.', '/');

		// this could go away if DriverDataSourceFactory would have the
		// new Constructor and would automatically set its parentConfig
		// (see PackageDriverDataSourceFactory.java)
		try {
			DataSourceFactory f = new PackageDriverDataSourceFactory(this);
			this.setOverrideFactory(f);
		}
		catch (Throwable t) {
			throw new CayenneRuntimeException(t);
		}

	}

	// these could return URLs as well, String just worked for the prototype;
	// in any case it's better than File (which makes the assumption of file loading)

	public String domainConfigName() {
		String configPath = this.domainConfigPath();
		if (configPath != null) {
			return configPath + '/' + DOMAIN_FILE;
		}
		else {
			return DOMAIN_FILE;
		}
	}

	public String domainConfigPath() {
		return _packagePath;
	}


	// if domainConfigPath() (or -URL) were in Configuration (and therefore used by
	// DefaultConfiguration, like shown below), all this would magically go away

	public InputStream getDomainConfig()
	{
		return locator.findResourceStream(domainConfigName());
	}

	public InputStream getMapConfig(String location)
	{
		return locator.findResourceStream(this.domainConfigPath() + '/' + location);
	}

}

