You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by to...@apache.org on 2004/01/26 20:15:30 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/platforms DBHandlingTask.java DBHandling.java TorqueDBHandling.java

tomdz       2004/01/26 11:15:30

  Modified:    src/java/org/apache/ojb/broker/platforms
                        TorqueDBHandling.java
  Added:       src/java/org/apache/ojb/broker/platforms DBHandlingTask.java
                        DBHandling.java
  Log:
  - Enhanced interface of dbhandling
  - Added "create database" details for postgresql
  - Added base interface of dbhandling classes
  - Added ant task for using the dbhandling classes
  
  Revision  Changes    Path
  1.2       +210 -56   db-ojb/src/java/org/apache/ojb/broker/platforms/TorqueDBHandling.java
  
  Index: TorqueDBHandling.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/platforms/TorqueDBHandling.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TorqueDBHandling.java	21 Jan 2004 13:47:03 -0000	1.1
  +++ TorqueDBHandling.java	26 Jan 2004 19:15:30 -0000	1.2
  @@ -57,6 +57,7 @@
   import java.io.*;
   import java.util.HashMap;
   import java.util.Iterator;
  +import java.util.StringTokenizer;
   import java.util.zip.GZIPInputStream;
   import java.util.zip.GZIPOutputStream;
   
  @@ -73,7 +74,7 @@
    * 
    * @author Thomas Dudziak
    */
  -public class TorqueDBHandling
  +public class TorqueDBHandling implements DBHandling
   {
       /** Torque db platforms */
       protected static final String TORQUE_PLATFORM_DB2        = "db2";
  @@ -90,7 +91,7 @@
       private static final String CREATION_SCRIPT_NAME = "create-db.sql";
       /** The name of the torque database mapping file */
       private static final String SQL_DB_MAP_NAME      = "sqldb.map";
  -    
  +
       /** Mapping from ojb dbms to torque database setting */
       private static HashMap _dbmsToTorqueDb = new HashMap();
       
  @@ -113,34 +114,44 @@
       /** The jdbc connection for communicating with the db */
       private JdbcConnectionDescriptor _jcd;
       /** The target database */
  -    private String _targetDatabase; 
  -    /** The source directory */
  -    private String _srcDir; 
  -    /** The schema files (relative to the source directory) */
  -    private String _schemaFiles;
  +    private String _targetDatabase;
  +    /** The directory where we work in */
  +    private File _workDir;
  +    /** The compressed contents of the torque schemata */
  +    private HashMap _torqueSchemata = new HashMap();
       /** The compressed content of the creation script */
       private byte[] _creationScript;
       /** The compressed contents of the db initialization scripts */
       private HashMap _initScripts = new HashMap();
  -    
  +
       /**
        * Creates a new handling object.
  +     */
  +    public TorqueDBHandling()
  +    {}
  +
  +    /**
  +     * Sets the jdbc connection to use.
        * 
  -     * @param jcd         The connection to use
  -     * @param srcDir      The source directory
  -     * @param schemaFiles The schema file to use (relative to the source directory)
  -     * @throws PlatformException If some error occurred
  +     * @param jcd The connection to use
  +     * @throws PlatformException If the target database cannot be handled with torque
        */
  -    public TorqueDBHandling(JdbcConnectionDescriptor jcd, String srcDir, String schemaFiles) throws PlatformException
  +    public void setConnection(JdbcConnectionDescriptor jcd) throws PlatformException
       {
  -        _jcd            = jcd;
  -        _srcDir         = srcDir;
  -        _schemaFiles    = schemaFiles;
  -        _targetDatabase = (String)_dbmsToTorqueDb.get(_jcd.getDbms().toLowerCase());
  -        if (_targetDatabase == null)
  +        _jcd = jcd;
  +
  +        String targetDatabase = (String)_dbmsToTorqueDb.get(_jcd.getDbms().toLowerCase());
  +
  +        if (targetDatabase == null)
           {
               throw new PlatformException("Database "+_jcd.getDbms()+" is not supported by torque");
           }
  +        if (!targetDatabase.equals(_targetDatabase))
  +        {
  +            _targetDatabase = targetDatabase;
  +            _creationScript = null;
  +            _initScripts.clear();
  +        }
       }
   
       /**
  @@ -148,7 +159,7 @@
        * 
        * @return The connection descriptor
        */
  -    public JdbcConnectionDescriptor getConnectionDescriptor()
  +    public JdbcConnectionDescriptor getConnection()
       {
           return _jcd;
       }
  @@ -164,6 +175,65 @@
       }
   
       /**
  +     * Adds the input files (in our case torque schema files) to use.
  +     * 
  +     * @param srcDir          The directory containing the files
  +     * @param listOfFilenames The filenames in a comma-separated list
  +     */
  +    public void addDBDefinitionFiles(String srcDir, String listOfFilenames) throws IOException
  +    {
  +        StringTokenizer tokenizer = new StringTokenizer(listOfFilenames, ",");
  +        File            dir       = new File(srcDir);
  +        String          filename;
  +        
  +        while (tokenizer.hasMoreTokens())
  +        {
  +            filename = tokenizer.nextToken().trim();
  +            if (filename.length() > 0)
  +            {    
  +                 _torqueSchemata.put("schema"+_torqueSchemata.size()+".xml",
  +                                     readTextCompressed(new File(dir, filename)));
  +            }
  +        }
  +    }
  +
  +    /**
  +     * Adds an input stream of a db definition (in our case of a torque schema file).
  +     * 
  +     * @param schemataStream The input stream
  +     */
  +    public void addDBDefinitionFile(InputStream schemaStream) throws IOException
  +    {
  +        _torqueSchemata.put("schema"+_torqueSchemata.size()+".xml",
  +                            readStreamCompressed(schemaStream));
  +    }
  +
  +    /**
  +     * Writes the torque schemata to files in the given directory and returns
  +     * a comma-separated list of the filenames.
  +     * 
  +     * @param dir The directory to write the files to
  +     * @return The list of filenames
  +     * @throws IOException If an error occurred
  +     */
  +    private String writeSchemata(File dir) throws IOException
  +    {
  +        writeCompressedTexts(dir, _torqueSchemata);
  +
  +        StringBuffer includes = new StringBuffer();
  +
  +        for (Iterator it = _torqueSchemata.keySet().iterator(); it.hasNext();)
  +        {
  +            includes.append((String)it.next());
  +            if (it.hasNext())
  +            {
  +                includes.append(",");
  +            }
  +        }
  +        return includes.toString();
  +    }
  +    
  +    /**
        * Creates the db-creation sql script (but does not perform it).
        * 
        * @throws PlatformException If some error occurred
  @@ -172,13 +242,17 @@
       {
           Project             project    = new Project();
           TorqueDataModelTask modelTask  = new TorqueDataModelTask();
  +        File                tmpDir     = null;
           File                scriptFile = null;
           
           _creationScript = null;
           try
           {
  -            File tmpDir = getTmpDir();
  +            tmpDir = new File(getWorkDir(), "schemas");
  +            tmpDir.mkdir();
   
  +            String includes = writeSchemata(tmpDir);
  +            
               scriptFile = new File(tmpDir, CREATION_SCRIPT_NAME);
   
               project.setBasedir(tmpDir.getAbsolutePath());
  @@ -192,22 +266,22 @@
               modelTask.setTargetDatabase(_targetDatabase);
   
               FileSet files = new FileSet();
  -            
  -            files.setDir(new File(_srcDir));
  -            files.setIncludes(_schemaFiles);
  +
  +            files.setDir(tmpDir);
  +            files.setIncludes(includes);
               modelTask.addFileset(files);
               modelTask.execute();
   
               _creationScript = readTextCompressed(scriptFile);
   
  -            scriptFile.delete();
  +            deleteDir(tmpDir);
           }
           catch (Exception ex)
           {
               // clean-up
  -            if ((scriptFile != null) && scriptFile.exists())
  +            if ((tmpDir != null) && tmpDir.exists())
               {
  -                scriptFile.delete();
  +                deleteDir(tmpDir);
               }
               throw new PlatformException(ex);
           }
  @@ -227,11 +301,13 @@
   
           Project             project    = new Project();
           TorqueDataModelTask modelTask  = new TorqueDataModelTask();
  +        File                tmpDir     = null;
           File                scriptFile = null;
           
           try
           {
  -            File tmpDir = getTmpDir();
  +            tmpDir = new File(getWorkDir(), "schemas");
  +            tmpDir.mkdir();
   
               scriptFile = new File(tmpDir, CREATION_SCRIPT_NAME);
   
  @@ -254,12 +330,12 @@
   	        sqlTask.setSrc(scriptFile);
   	        sqlTask.execute();
   
  -	        scriptFile.delete();
  +	        deleteDir(tmpDir);
           }
           catch (Exception ex)
           {
               // clean-up
  -            if ((scriptFile != null) && scriptFile.exists())
  +            if ((tmpDir != null) && tmpDir.exists())
               {
                   scriptFile.delete();
               }
  @@ -277,25 +353,31 @@
       {
           Project       project   = new Project();
           TorqueSQLTask sqlTask   = new TorqueSQLTask(); 
  -        File          outputDir = null;
  +        File          schemaDir = null;
  +        File          sqlDir    = null;
           
           _initScripts.clear();
           try
           {
  -            outputDir = new File(getTmpDir(), "sql");
  +            File tmpDir = getWorkDir();
   
  -            File sqlDbMapFile = new File(outputDir, SQL_DB_MAP_NAME);
  +            schemaDir = new File(tmpDir, "schemas");
  +            sqlDir    = new File(tmpDir, "sql");
  +            schemaDir.mkdir();
  +            sqlDir.mkdir();
  +
  +            String includes     = writeSchemata(schemaDir);
  +            File   sqlDbMapFile = new File(sqlDir, SQL_DB_MAP_NAME);
   
  -            outputDir.mkdir();
               sqlDbMapFile.createNewFile();
  -            project.setBasedir(outputDir.getAbsolutePath());
  +            project.setBasedir(sqlDir.getAbsolutePath());
               
               // populating with defaults
               sqlTask.setProject(project);
               sqlTask.setUseClasspath(true);
               sqlTask.setBasePathToDbProps("sql/base/");
               sqlTask.setControlTemplate("sql/base/Control.vm");
  -            sqlTask.setOutputDirectory(outputDir);
  +            sqlTask.setOutputDirectory(sqlDir);
               // we put the report in the parent directory as we don't want
               // to read it in later on
               sqlTask.setOutputFile("../report.sql.generation");
  @@ -304,20 +386,25 @@
   
               FileSet files = new FileSet();
               
  -            files.setDir(new File(_srcDir));
  -            files.setIncludes(_schemaFiles);
  +            files.setDir(schemaDir);
  +            files.setIncludes(includes);
               sqlTask.addFileset(files);
               sqlTask.execute();
   
  -            readTextsCompressed(outputDir, _initScripts);
  -            deleteDir(outputDir);
  +            readTextsCompressed(sqlDir, _initScripts);
  +            deleteDir(schemaDir);
  +            deleteDir(sqlDir);
           }
           catch (Exception ex)
           {
               // clean-up
  -            if (outputDir != null)
  +            if ((schemaDir != null) && schemaDir.exists())
               {
  -                deleteDir(outputDir);
  +                deleteDir(schemaDir);
  +            }
  +            if ((sqlDir != null) && sqlDir.exists())
  +            {
  +                deleteDir(sqlDir);
               }
               throw new PlatformException(ex);
           }
  @@ -328,7 +415,7 @@
        * 
        * @throws PlatformException If some error occurred
        */
  -    public void createTables() throws PlatformException
  +    public void initDB() throws PlatformException
       {
           if (_initScripts.isEmpty())
           {
  @@ -341,7 +428,7 @@
           
           try
           {
  -            outputDir = new File(getTmpDir(), "sql");
  +            outputDir = new File(getWorkDir(), "sql");
   
               outputDir.mkdir();
               writeCompressedTexts(outputDir, _initScripts);
  @@ -386,14 +473,14 @@
        */
       protected String getDBCreationUrl()
       {
  -        JdbcConnectionDescriptor jcd = getConnectionDescriptor();
  +        JdbcConnectionDescriptor jcd = getConnection();
   
           // currently I only know about specifics for mysql
           if (TORQUE_PLATFORM_MYSQL.equals(getTargetTorquePlatform()))
           {
               // we have to remove the db name as the jdbc driver would try to connect to
               // a non-existing db
  -            // a mysql db-alias has this form: [host&port]/[dbname]?[options]
  +            // the db-alias has this form: [host&port]/[dbname]?[options]
               String dbAliasPrefix = jcd.getDbAlias();
               String dbAliasSuffix = "";
               int    questionPos   = dbAliasPrefix.indexOf('?');
  @@ -413,6 +500,39 @@
               }
               return jcd.getProtocol()+":"+jcd.getSubProtocol()+":"+dbAliasPrefix+dbAliasSuffix;
           }
  +        else if (TORQUE_PLATFORM_POSTGRESQL.equals(getTargetTorquePlatform()))
  +        {
  +            // we have to replace the db name with 'template1'
  +            // the db-alias has this form: [host&port]/[dbname]?[options]
  +            String dbAliasPrefix = jcd.getDbAlias();
  +            String dbAliasSuffix = "";
  +            int    questionPos   = dbAliasPrefix.indexOf('?');
  +
  +            if (questionPos > 0)
  +            {
  +                dbAliasSuffix = dbAliasPrefix.substring(questionPos);
  +                dbAliasPrefix = dbAliasPrefix.substring(0, questionPos);
  +            }
  +
  +            int slashPos = dbAliasPrefix.lastIndexOf('/');
  +
  +            if (slashPos > 0)
  +            {
  +                // it is important that the slash at the end is present
  +                dbAliasPrefix = dbAliasPrefix.substring(0, slashPos + 1);
  +            }
  +            else
  +            {
  +                dbAliasPrefix += "/";
  +            }
  +            dbAliasPrefix += "template1";
  +            if (dbAliasSuffix.length() > 0)
  +            {
  +                dbAliasPrefix += "/";
  +            }
  +            return jcd.getProtocol()+":"+jcd.getSubProtocol()+":"+dbAliasPrefix+dbAliasSuffix;
  +            
  +        }
           else
           {
               return jcd.getProtocol()+":"+jcd.getSubProtocol()+":"+jcd.getDbAlias();
  @@ -427,7 +547,7 @@
        */
       protected String getDBManipulationUrl()
       {
  -        JdbcConnectionDescriptor jcd = getConnectionDescriptor();
  +        JdbcConnectionDescriptor jcd = getConnection();
   
           return jcd.getProtocol()+":"+jcd.getSubProtocol()+":"+jcd.getDbAlias();
       }
  @@ -441,10 +561,22 @@
        */
       private byte[] readTextCompressed(File file) throws IOException
       {
  +        return readStreamCompressed(new FileInputStream(file));
  +    }
  +
  +    /**
  +     * Reads the given text stream and compressed its content.
  +     * 
  +     * @param stream The input stream
  +     * @return A byte array containing the GZIP-compressed content of the stream
  +     * @throws IOException If an error ocurred
  +     */
  +    private byte[] readStreamCompressed(InputStream stream) throws IOException
  +    {
           ByteArrayOutputStream bao    = new ByteArrayOutputStream();
           GZIPOutputStream      gos    = new GZIPOutputStream(bao);
           OutputStreamWriter    output = new OutputStreamWriter(gos);
  -        BufferedReader        input  = new BufferedReader(new FileReader(file));
  +        BufferedReader        input  = new BufferedReader(new InputStreamReader(stream));
           String                line;
   
           while ((line = input.readLine()) != null)
  @@ -453,6 +585,7 @@
               output.write('\n');
           }
           input.close();
  +        stream.close();
           output.close();
           gos.close();
           bao.close();
  @@ -531,22 +664,43 @@
       }
       
       /**
  +     * Sets the working directory.
  +     * 
  +     * @param dir The directory
  +     * @throws IOException If the directory does not exist or cannot be written/read
  +     */
  +    public void setWorkDir(String dir) throws IOException
  +    {
  +        File workDir = new File(dir);
  +
  +        if (!workDir.exists() || !workDir.canWrite() || !workDir.canRead())
  +        {
  +            throw new IOException("Cannot access directory "+dir);
  +        }
  +        _workDir = workDir;
  +    }
  +
  +    /**
        * Returns the temporary directory used by java.
        * 
        * @return The temporary directory
        * @throws IOException If an io error occurred
        */
  -    private File getTmpDir() throws IOException
  +    private File getWorkDir() throws IOException
       {
  -        File   dummy  = File.createTempFile("dummy", ".log");
  -        String tmpDir = dummy.getPath().substring(0, dummy.getPath().lastIndexOf(File.separatorChar));
  -
  -        if ((tmpDir == null) || (tmpDir.length() == 0))
  -        {
  -            tmpDir = ".";
  +        if (_workDir == null)
  +        {    
  +            File   dummy   = File.createTempFile("dummy", ".log");
  +            String workDir = dummy.getPath().substring(0, dummy.getPath().lastIndexOf(File.separatorChar));
  +    
  +            if ((workDir == null) || (workDir.length() == 0))
  +            {
  +                workDir = ".";
  +            }
  +            dummy.delete();
  +            _workDir = new File(workDir);
           }
  -        dummy.delete();
  -        return new File(tmpDir);
  +        return _workDir;
       }
   
       /**
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/platforms/DBHandlingTask.java
  
  Index: DBHandlingTask.java
  ===================================================================
  package org.apache.ojb.broker.platforms;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache ObjectRelationalBridge" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.StringTokenizer;
  
  import org.apache.ojb.broker.PBKey;
  import org.apache.ojb.broker.PersistenceBrokerFactory;
  import org.apache.ojb.broker.metadata.ConnectionRepository;
  import org.apache.ojb.broker.metadata.MetadataManager;
  import org.apache.tools.ant.*;
  import org.apache.tools.ant.types.FileSet;
  
  /**
   * Ant task for performing basic db setup functions.
   * 
   * @author Thomas Dudziak
   */
  public class DBHandlingTask extends Task
  {
      /** The name of the known db handlings */
      private static final String HANDLING_TORQUE = "torque";
      /** The commands */
      private static final String COMMAND_CREATE  = "create";
      private static final String COMMAND_INIT    = "init";
      
      /** The name of the db handling to use */
      private String _handling = HANDLING_TORQUE;
      /** The path to the properties file */
      private String _propertiesFile = null;
      /** The alias of the jdbc connection to use (empty = default connection) */
      private String _jcdAlias = null;
      /** The working directory */
      private String _workDir = null;
      /** The input files */
      private ArrayList _fileSets = new ArrayList();
      /** The commands to perform */
      private String _commands = "";
      
      /**
       * Sets the name of the handling to use.
       * 
       * @param name The short name of the handling
       */
      public void setHandling(String name)
      {
          _handling = (name == null ? HANDLING_TORQUE : name.toLowerCase());
      }
  
      /**
       * Returns the name of the handling that is used.
       * 
       * @return The short name of the handling
       */
      public String getHandling()
      {
          return _handling;
      }
  
      /**
       * Sets the properties file (OJB.properties).
       * 
       * @param path The path to the properties file
       */
      public void setPropertiesFile(String path)
      {
          _propertiesFile = path;
      }
  
      /**
       * Returns the properties file.
       * 
       * @return The path to the properties file
       */
      public String getPropertiesFile()
      {
          return _propertiesFile;
      }
  
      /**
       * Sets the alias of the jdbc connection to use.
       * 
       * @param alias The alias of the connection
       */
      public void setJcdAlias(String alias)
      {
          _jcdAlias = alias;
      }
  
      /**
       * Returns the alias of the jdbc connection.
       * 
       * @return The alias
       */
      public String getJcdAlias()
      {
          return _jcdAlias;
      }
  
      /**
       * Sets the working directory. If none is given, then the system's temporary directory is used.
       * 
       * @param dir The working directory
       */
      public void setWorkDir(String dir)
      {
          _workDir = dir;
      }
  
      /**
       * Returns the working directory.
       * 
       * @return The working directory
       */
      public String getWorkDir()
      {
          return _workDir;
      }
  
      /**
       * Adds a fileset.
       * 
       * @param fileset The additional input files
       */
      public void addFileset(FileSet fileset)
      {
          _fileSets.add(fileset);
      }
  
      /**
       * Sets the list of commands to perform.
       * 
       * @param listOfCommands The comma-separated list of commands
       */
      public void setCommands(String listOfCommands)
      {
          _commands = listOfCommands;
      }
  
      /**
       * Returns the list of commands.
       * 
       * @return The comma-separated list of commands
       */
      public String getCommands()
      {
          return _commands;
      }
  
      /* (non-Javadoc)
       * @see org.apache.tools.ant.Task#execute()
       */
      public void execute() throws BuildException
      {
          if ((_commands == null) || (_commands.length() == 0))
          {
              return;
          }
  
          DBHandling handling = createDBHandling();
  
          try
          {
              if ((_workDir != null) && (_workDir.length() > 0))
              {
                  handling.setWorkDir(_workDir);
                  System.setProperty("user.dir", _workDir);
              }
              for (Iterator it = _fileSets.iterator(); it.hasNext();)
              {
                  addIncludes(handling, (FileSet)it.next());
              }
  
              if ((_propertiesFile != null) && (_propertiesFile.length() > 0))
              {
                  System.setProperty("OJB.properties", _propertiesFile);
              }
  
              ConnectionRepository connRep = MetadataManager.getInstance().connectionRepository();
              PBKey                pbKey   = null;
  
              if ((_jcdAlias == null) || (_jcdAlias.length() == 0))
              {
                  pbKey = PersistenceBrokerFactory.getDefaultKey();
              }
              else
              {
                  pbKey = connRep.getStandardPBKeyForJcdAlias(_jcdAlias);
                  if (pbKey == null)
                  {
                      throw new BuildException("Undefined jcdAlias "+_jcdAlias);
                  }
              }
              handling.setConnection(connRep.getDescriptor(pbKey));
  
              String command;
  
              for (StringTokenizer tokenizer = new StringTokenizer(_commands, ","); tokenizer.hasMoreTokens();)
              {
                  command = tokenizer.nextToken().toLowerCase().trim();
                  if (COMMAND_CREATE.equals(command))
                  {
                      handling.createDB();
                  }
                  else if (COMMAND_INIT.equals(command))
                  {
                      handling.initDB();
                  }
                  else
                  {
                      throw new BuildException("Unknown command "+command);
                  }
              }
          }
          catch (Exception ex)
          {
              throw new BuildException(ex);
          }
      }
  
      /**
       * Creates a db handling object.
       * 
       * @return The db handling object
       * @throws BuildException If the handling is invalid
       */
      private DBHandling createDBHandling() throws BuildException
      {
          if ((_handling == null) || (_handling.length() == 0))
          {
              throw new BuildException("No handling specified");
          }
          try
          {
              String className     = "org.apache.ojb.broker.platforms."+
              					   Character.toTitleCase(_handling.charAt(0))+_handling.substring(1)+
              					   "DBHandling";
              Class  handlingClass = Class.forName(className);
  
              return (DBHandling)handlingClass.newInstance();
          }
          catch (Exception ex)
          {
              throw new BuildException("Invalid handling '"+_handling+"' specified");
          }
      }
  
      /**
       * Adds the includes of the fileset to the handling.
       * 
       * @param handling The handling
       * @param fileSet  The fileset
       */
      private void addIncludes(DBHandling handling, FileSet fileSet) throws BuildException
      {
          DirectoryScanner scanner  = fileSet.getDirectoryScanner(getProject());
          String[]         files    = scanner.getIncludedFiles();
          StringBuffer     includes = new StringBuffer();
  
          for (int idx = 0; idx < files.length; idx++)
          {
              if (idx > 0)
              {
                  includes.append(",");
              }
              includes.append(files[idx]);
          }
          try
          {
              handling.addDBDefinitionFiles(fileSet.getDir(getProject()).getAbsolutePath(), includes.toString());
          }
          catch (IOException ex)
          {
              throw new BuildException(ex);
          }
      }
  }
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/platforms/DBHandling.java
  
  Index: DBHandling.java
  ===================================================================
  package org.apache.ojb.broker.platforms;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache ObjectRelationalBridge" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.IOException;
  import java.io.InputStream;
  import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
  
  /**
   * Interface for classes providing basic database handling (drop, create, init).
   * 
   * @author Thomas Dudziak
   */
  public interface DBHandling
  {
      /**
       * Sets the working directory.
       * 
       * @param dir The directory
       * @throws IOException If the directory does not exist or cannot be written/read
       */
      public void setWorkDir(String dir) throws IOException;
  
      /**
       * Sets the jdbc connection to use.
       * 
       * @param jcd The connection to use
       * @throws PlatformException If the target database cannot be handled
       */
      public void setConnection(JdbcConnectionDescriptor jcd) throws PlatformException;
  
      /**
       * Returns the connection descriptor used by this handling object.
       * 
       * @return The connection descriptor
       */
      public JdbcConnectionDescriptor getConnection();
  
      /**
       * Adds db definition files to use.
       * 
       * @param srcDir          The directory containing the files
       * @param listOfFilenames The filenames in a comma-separated list
       */
      public void addDBDefinitionFiles(String srcDir, String listOfFilenames) throws IOException;
  
      /**
       * Adds an input streams containg part of the db definition to use.
       * 
       * @param schemataStreams Input streams
       */
      public void addDBDefinitionFile(InputStream inputStream) throws IOException;
  
      /**
       * Creates the database.
       * 
       * @throws PlatformException If some error occurred
       */
      public void createDB() throws PlatformException;
  
      /**
       * Creates the tables according to the schema files.
       * 
       * @throws PlatformException If some error occurred
       */
      public void initDB() throws PlatformException;
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org