throws MappingException
{
Unmarshaller unm;
Mapping mapping;
Enumeration mappings;
Database database;
DatabaseRegistry dbs;
PersistenceFactory factory;
unm = new Unmarshaller( Database.class );
try {
// Load the JDO database configuration file from the specified
// input source. If the database was already configured, ignore
// this file (allowing multiple loadings).
if ( resolver == null )
unm.setEntityResolver( new DTDResolver() );
else
unm.setEntityResolver( new DTDResolver( resolver ) );
database = (Database) unm.unmarshal( source );
if ( _databases.get( database.getName() ) != null )
return;
// Complain if no database engine was specified, otherwise get
// a persistence factory for that database engine.
if ( database.getEngine() == null )
factory = PersistenceFactoryRegistry.getPersistenceFactory( GenericEngine );
else
factory = PersistenceFactoryRegistry.getPersistenceFactory( database.getEngine() );
if ( factory == null )
throw new MappingException( "jdo.noSuchEngine", database.getEngine() );
// Load the mapping file from the URL specified in the database
// configuration file, relative to the configuration file.
// Fail if cannot load the mapping for whatever reason.
mapping = new Mapping( loader );
if ( resolver != null )
mapping.setEntityResolver( resolver );
if ( source.getSystemId() != null )
mapping.setBaseURL( source.getSystemId() );
mappings = database.enumerateMapping();
while ( mappings.hasMoreElements() )
mapping.loadMapping( ( (org.exolab.castor.jdo.conf.Mapping) mappings.nextElement() ).getHref() );
if ( database.getDriver() != null ) {
// JDO configuration file specifies a driver, use the driver
// properties to create a new registry object.
Properties props;
Enumeration params;
Param param;
if ( database.getDriver().getClassName() != null ) {
try {
Class.forName( database.getDriver().getClassName() ).newInstance();
} catch ( Exception except ) {
throw new MappingException( except );
}
}
if ( DriverManager.getDriver( database.getDriver().getUrl() ) == null )
throw new MappingException( "jdo.missingDriver", database.getDriver().getUrl() );
props = new Properties();
params = database.getDriver().enumerateParam();
while ( params.hasMoreElements() ) {
param = (Param) params.nextElement();
props.put( param.getName(), param.getValue() );
}
dbs = new DatabaseRegistry( database.getName(), mapping.getResolver( Mapping.JDO, factory ), factory,
database.getDriver().getUrl(), props, logInterceptor );
} else if ( database.getDataSource() != null ) {
// JDO configuration file specifies a DataSource object, use the
// DataSource which was configured from the JDO configuration file
// to create a new registry object.
DataSource ds;
ds = (DataSource) database.getDataSource().getParams();
if ( ds == null )
throw new MappingException( "jdo.missingDataSource", database.getName() );
dbs = new DatabaseRegistry( database.getName(), mapping.getResolver( Mapping.JDO, factory ), factory,
ds, logInterceptor );
} else if ( database.getJndi() != null ) {
// JDO configuration file specifies a DataSource lookup through JNDI,
// locate the DataSource object frome the JNDI namespace and use it.
Object ds;
try {
ds = new InitialContext().lookup( database.getJndi().getName() );
} catch ( NameNotFoundException except ) {
throw new MappingException( "jdo.jndiNameNotFound", database.getJndi().getName() );
} catch ( NamingException except ) {
throw new MappingException( except );
}
if ( ! ( ds instanceof DataSource ) )
throw new MappingException( "jdo.jndiNameNotFound", database.getJndi().getName() );
dbs = new DatabaseRegistry( database.getName(), mapping.getResolver( Mapping.JDO, factory ), factory,
(DataSource) ds, logInterceptor );
} else {
throw new MappingException( "jdo.missingDataSource", database.getName() );
}
// Register the new registry object for the given database name.
_databases.put( database.getName(), dbs );
} catch ( MappingException except ) {
throw except;
} catch ( Exception except ) {
throw new MappingException( except );