Package org.exolab.castor.jdo.conf

Examples of org.exolab.castor.jdo.conf.Database


     private static synchronized void loadDatabase(Database[] databases, EntityResolver resolver, ClassLoader loader, String baseURI)
        throws MappingException
    {
        Mapping            mapping;
        Enumeration        mappings;
        Database           database;
        // Database[]         databases;
        DatabaseRegistry   dbs;
        PersistenceFactory factory;

        try {
            // Load the JDO configuration file from the specified input source.
            // databases = JDOConfLoader.getDatabases (baseURI, resolver);
           
          for (int i = 0; i < databases.length ;i++) {
           
            database = databases[i];
           
            // If the database was already configured, ignore
            // this database configuration (allowing multiple loadings).
            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 ( baseURI != null )
              mapping.setBaseURL( baseURI );
           
            mappings = database.enumerateMapping();
            while ( mappings.hasMoreElements() )
            {
              String mappingUrl = ( (org.exolab.castor.jdo.conf.Mapping) mappings.nextElement() ).getHref();
              _log.debug( "Loading the mapping descriptor: " + mappingUrl );
             
              if ( mappingUrl != null )
              {
                mapping.loadMapping( mappingUrl );
              }
            }
           
            if (database.getDatabaseChoice() == null) {
              throw new MappingException( "jdo.missingDataSource", database.getName() );
            }
           
                if ( database.getDatabaseChoice().getDriver() != null ) {
            // JDO configuration file specifies a driver, use the driver
            // properties to create a new registry object.
                    dbs = initFromDriver(mapping, database, factory);
          } else if ( database.getDatabaseChoice().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.
            dbs = initFromDataSource(mapping, database, factory, loader);
          } else if ( database.getDatabaseChoice().getJndi() != null ) {
            // JDO configuration file specifies a DataSource lookup through JNDI,
            // locate the DataSource object frome the JNDI namespace and use it.
            dbs = initFromJNDI(mapping, database, factory);
                } 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 ) {
View Full Code Here


    }
   
    public static Database getDatabase (String databaseName, InputSource source, EntityResolver resolver)
        throws MappingException
    {
        Database database = null;
        loadConfiguration (source, resolver);
        Database[] databases = getDatabases (source, resolver);
        for (int i = 0; i < databases.length ;i++) {
            if (databases[i].getName().equals (databaseName)) {
                database =databases[i];
View Full Code Here

   * @param engine name of the database engine
   * @return Database configuration
   */
  public static Database createJdoDbConf(String name, String engine)
  {
    Database dbConf = new Database();

    dbConf.setName(name);
    dbConf.setEngine(engine);

    return dbConf;
  }
View Full Code Here

   * @param dsConf JDO datasource configuration
   * @return Database configuration
   */
  public static Database createJdoDbConf(String db_name, String engine, DataSource dsConf)
  {
    Database dbConf = createJdoDbConf(db_name, engine);
    DatabaseChoice dbChoice = new DatabaseChoice();

    dbChoice.setDataSource(dsConf);
    dbConf.setDatabaseChoice(dbChoice);

    return dbConf;
  }
View Full Code Here

   * @param driverConf JDO driver configuration
   * @return Database configuration
   */
  public static Database createJdoDbConf(String db_name, String engine, Driver driverConf)
  {
    Database dbConf = createJdoDbConf(db_name, engine);
    DatabaseChoice dbChoice = new DatabaseChoice();

    dbChoice.setDriver(driverConf);
    dbConf.setDatabaseChoice(dbChoice);

    return dbConf;
  }
View Full Code Here

  public void testGetDatabase()
    throws Exception
  {
   
    Database database = JDOConfLoader.getDatabase (DATABASE_NAME, source, resolver);
    writer.println (database);
  }
View Full Code Here

        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 );
View Full Code Here

TOP

Related Classes of org.exolab.castor.jdo.conf.Database

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.