You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by he...@apache.org on 2003/06/21 13:50:13 UTC

cvs commit: db-torque/src/java/org/apache/torque/avalon TorqueComponent.java

henning     2003/06/21 04:50:13

  Modified:    src/java/org/apache/torque/avalon TorqueComponent.java
  Log:
  - Clean up the Avalon code.
  
  - copy static methods from the Torque wrapper to be accessible from
    the component. So you can use the methods on the component when
    using Torque under Avalon.
  
  Revision  Changes    Path
  1.2       +214 -26   db-torque/src/java/org/apache/torque/avalon/TorqueComponent.java
  
  Index: TorqueComponent.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/avalon/TorqueComponent.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TorqueComponent.java	20 Jun 2003 00:01:10 -0000	1.1
  +++ TorqueComponent.java	21 Jun 2003 11:50:13 -0000	1.2
  @@ -54,6 +54,8 @@
    * <http://www.apache.org/>.
    */
   
  +import java.sql.Connection;
  +
   import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.activity.Initializable;
   import org.apache.avalon.framework.configuration.Configurable;
  @@ -66,8 +68,11 @@
   
   import org.apache.commons.lang.StringUtils;
   
  -import org.apache.torque.Torque;
   import org.apache.torque.TorqueException;
  +import org.apache.torque.TorqueSingleton;
  +import org.apache.torque.adapter.DB;
  +import org.apache.torque.manager.AbstractBaseManager;
  +import org.apache.torque.map.DatabaseMap;
   
   /**
    * Avalon component for Torque.
  @@ -80,35 +85,58 @@
           extends AbstractLogEnabled
           implements Configurable, Initializable, Disposable, Contextualizable
   {
  -
       /** The Avalon Context */
       private Context context = null;
   
  +    /** The Configuration File for Torque */
  +    private String configFile = null;
  +
  +
  +    /**
  +     * Get a reference to the actual Torque Core
  +     *
  +     * @return A Torque Singleton reference
  +     */
  +    private TorqueSingleton getTorque()
  +    {
  +        return TorqueSingleton.getInstance();
  +    }
  +
  +    /*
  +     * ========================================================================
  +     *
  +     * Avalon Component Interfaces
  +     *
  +     * ========================================================================
  +     */
  +
       /**
        * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
        */
  -    public void configure(Configuration configuration) throws ConfigurationException
  +    public void configure(Configuration configuration)
  +            throws ConfigurationException
       {
           getLogger().debug("configure(" + configuration + ")");
   
           String configFile = configuration.getChild("configfile").getValue();
  -        String applicationRoot = null;
  +        String appRoot = null;
   
           try
           {
  -            applicationRoot = (context == null) ? null : (String) context.get("componentAppRoot");
  +            appRoot = (context == null) 
  +                    ? null : (String) context.get("componentAppRoot");
           }
           catch (ContextException ce)
           {
               getLogger().error("Could not load Application Root from Context");
           }
   
  -        if (StringUtils.isNotEmpty(applicationRoot))
  +        if (StringUtils.isNotEmpty(appRoot))
           {
  -            if (applicationRoot.endsWith("/"))
  +            if (appRoot.endsWith("/"))
               {
  -                applicationRoot = applicationRoot.substring(0, applicationRoot.length() - 1);
  -                getLogger().debug("Application Root changed to " + applicationRoot);
  +                appRoot = appRoot.substring(0, appRoot.length() - 1);
  +                getLogger().debug("Application Root changed to " + appRoot);
               }
   
               if (configFile.startsWith("/"))
  @@ -118,23 +146,16 @@
               }
               
               StringBuffer sb = new StringBuffer();
  -            sb.append(applicationRoot);
  +            sb.append(appRoot);
               sb.append('/');
               sb.append(configFile);
               
               configFile = sb.toString();
           }
   
  -        getLogger().debug("Config File is " + configFile );
  +        getLogger().debug("Config File is " + configFile);
   
  -        try 
  -        {
  -            Torque.init(configFile);
  -        }
  -        catch (TorqueException tex)
  -        {
  -            throw new ConfigurationException("Torque could not be configured", tex);
  -        }
  +        this.configFile = configFile;
       }
   
       /**
  @@ -149,12 +170,11 @@
       /**
        * @see org.apache.avalon.framework.activity.Initializable#initialize()
        */
  -    public void initialize() throws Exception
  +    public void initialize()
  +            throws Exception
       {
           getLogger().debug("initialize()");
  -
  -        Torque torque = new Torque();
  -        torque.initialize();        
  +        getTorque().init(configFile);
       }
   
       /**
  @@ -163,8 +183,176 @@
       public void dispose()
       {
           getLogger().debug("dispose()");
  +        getTorque().shutdown();
  +    }
  +
  +    /*
  +     * ========================================================================
  +     *
  +     * Torque Methods, accessible from the Component
  +     *
  +     * ========================================================================
  +     */
   
  -        Torque torque = new Torque();
  -        torque.dispose();        
  +    /**
  +     * Determine whether Torque has already been initialized.
  +     *
  +     * @return true if Torque is already initialized
  +     */
  +    public boolean isInit()
  +    {
  +        return getTorque().isInit();
  +    }
  +
  +    /**
  +     * Get the configuration for this component.
  +     *
  +     * @return the Configuration
  +     */
  +    public org.apache.commons.configuration.Configuration getConfiguration()
  +    {
  +        return getTorque().getConfiguration();
  +    }
  +
  +    /**
  +     * This method returns a Manager for the given name.
  +     *
  +     * @param name name of the manager
  +     * @return a Manager
  +     */
  +    public AbstractBaseManager getManager(String name)
  +    {
  +        return getTorque().getManager(name);
  +    }
  +
  +    /**
  +     * This methods returns either the Manager from the configuration file,
  +     * or the default one provided by the generated code.
  +     *
  +     * @param name name of the manager
  +     * @param defaultClassName the class to use if name has not been configured
  +     * @return a Manager
  +     */
  +    public AbstractBaseManager getManager(String name,
  +            String defaultClassName)
  +    {
  +        return getTorque().getManager(name, defaultClassName);
  +    }
  +
  +    /**
  +     * Returns the default database map information.
  +     *
  +     * @return A DatabaseMap.
  +     * @throws TorqueException Any exceptions caught during processing will be
  +     *         rethrown wrapped into a TorqueException.
  +     */
  +    public DatabaseMap getDatabaseMap()
  +            throws TorqueException
  +    {
  +        return getTorque().getDatabaseMap();
  +    }
  +
  +    /**
  +     * Returns the database map information. Name relates to the name
  +     * of the connection pool to associate with the map.
  +     *
  +     * @param name The name of the database corresponding to the
  +     *        <code>DatabaseMap</code> to retrieve.
  +     * @return The named <code>DatabaseMap</code>.
  +     * @throws TorqueException Any exceptions caught during processing will be
  +     *         rethrown wrapped into a TorqueException.
  +     */
  +    public DatabaseMap getDatabaseMap(String name)
  +            throws TorqueException
  +    {
  +        return getTorque().getDatabaseMap(name);
  +    }
  +
  +    /**
  +     * Register a MapBuilder
  +     *
  +     * @param className the MapBuilder
  +     */
  +    public void registerMapBuilder(String className)
  +    {
  +        getTorque().registerMapBuilder(className);
  +    }
  +
  +    /**
  +     * This method returns a Connection from the default pool.
  +     *
  +     * @return The requested connection.
  +     * @throws TorqueException Any exceptions caught during processing will be
  +     *         rethrown wrapped into a TorqueException.
  +     */
  +    public Connection getConnection()
  +            throws TorqueException
  +    {
  +        return getTorque().getConnection();
  +    }
  +
  +    /**
  +     *
  +     * @param name The database name.
  +     * @return a database connection
  +     * @throws TorqueException Any exceptions caught during processing will be
  +     *         rethrown wrapped into a TorqueException.
  +     */
  +    public Connection getConnection(String name)
  +            throws TorqueException
  +    {
  +        return getTorque().getConnection(name);
  +    }
  +
  +    /**
  +     * This method returns a Connecton using the given parameters.
  +     * You should only use this method if you need user based access to the
  +     * database!
  +     *
  +     * @param name The database name.
  +     * @param username The name of the database user.
  +     * @param password The password of the database user.
  +     * @return A Connection.
  +     * @throws TorqueException Any exceptions caught during processing will be
  +     *         rethrown wrapped into a TorqueException.
  +     */
  +    public Connection getConnection(String name, String username,
  +            String password)
  +            throws TorqueException
  +    {
  +        return getTorque().getConnection(name, username, password);
  +    }
  +    /**
  +     * Returns database adapter for a specific connection pool.
  +     *
  +     * @param name A pool name.
  +     * @return The corresponding database adapter.
  +     * @throws TorqueException Any exceptions caught during processing will be
  +     *         rethrown wrapped into a TorqueException.
  +     */
  +    public DB getDB(String name)
  +            throws TorqueException
  +    {
  +        return getTorque().getDB(name);
  +    }
  +
  +    /**
  +     * Returns the name of the default database.
  +     *
  +     * @return name of the default DB
  +     */
  +    public String getDefaultDB()
  +    {
  +        return getTorque().getDefaultDB();
  +    }
  +
  +    /**
  +     * Closes a connection.
  +     *
  +     * @param con A Connection to close.
  +     */
  +    public void closeConnection(Connection con)
  +    {
  +        getTorque().closeConnection(con);
       }
   }