You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jm...@apache.org on 2001/12/30 18:38:29 UTC

cvs commit: jakarta-turbine-torque/src/java/org/apache/torque Torque.java

jmcnally    01/12/30 09:38:29

  Modified:    src/java/org/apache/torque Tag: JDBC2POOL_BRANCH Torque.java
  Log:
  added initialization of datasources from properties file
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.41.2.1  +91 -2     jakarta-turbine-torque/src/java/org/apache/torque/Torque.java
  
  Index: Torque.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-torque/src/java/org/apache/torque/Torque.java,v
  retrieving revision 1.41
  retrieving revision 1.41.2.1
  diff -u -r1.41 -r1.41.2.1
  --- Torque.java	10 Dec 2001 02:59:54 -0000	1.41
  +++ Torque.java	30 Dec 2001 17:38:29 -0000	1.41.2.1
  @@ -54,6 +54,10 @@
    * <http://www.apache.org/>.
    */
   
  +import java.beans.PropertyDescriptor;
  +import java.beans.PropertyEditorManager;
  +import java.beans.PropertyEditor;
  +import java.lang.reflect.Method;
   import java.io.File;
   import java.io.FileInputStream;
   import java.util.HashMap;
  @@ -61,8 +65,12 @@
   import java.util.Iterator;
   import java.util.Map;
   import java.util.List;
  +import java.util.ArrayList;
   import java.util.Vector;
   import java.util.Properties;
  +import javax.naming.Context;
  +import javax.naming.InitialContext;
  +import javax.naming.NameAlreadyBoundException;
   import org.apache.commons.collections.ExtendedProperties;
   import org.apache.log4j.Category;
   import org.apache.log4j.PropertyConfigurator;
  @@ -84,7 +92,7 @@
    * @author <a href="mailto:magnus@handtolvur.is">Magn�s ��r Torfason</a>
    * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
    * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
  - * @version $Id: Torque.java,v 1.41 2001/12/10 02:59:54 jmcnally Exp $
  + * @version $Id: Torque.java,v 1.41.2.1 2001/12/30 17:38:29 jmcnally Exp $
    */
   public class Torque
   {
  @@ -201,6 +209,7 @@
           dbMaps = new HashMap();
           pools = new HashMap();
           DBFactory.init(configuration);
  +        initDataSources(configuration);
   
           isInit = true;
           for ( Iterator i=mapBuilders.iterator(); i.hasNext(); ) 
  @@ -222,6 +231,81 @@
           monitor.start();
       }
   
  +    private static final void initDataSources(ExtendedProperties configuration)
  +        throws TorqueException
  +    {
  +        ExtendedProperties c = configuration.subset("datasource");
  +        //Map dsMap = new HashMap();
  +        try
  +        {
  +            Context ctx = new InitialContext();
  +            // add jdbc, if it has not been added already
  +            try
  +            {
  +                ctx.createSubcontext("jdbc");
  +            }
  +            catch(NameAlreadyBoundException nabe)
  +            {
  +                // ignore
  +            }
  +
  +            Iterator i = c.getKeys();
  +            while (i.hasNext())
  +            {
  +                String key = (String)i.next();                
  +                if (key.endsWith("classname"))
  +                {
  +                    String classname = c.getString(key);
  +                    String handle = key.substring(0, key.indexOf('.'));
  +                    //dsMap.put(name, cpdsClassName);
  +                    category.debug("Datasource handle: " + handle + 
  +                                   " classname: " + classname);
  +
  +                    Class dsClass = Class.forName(classname);
  +                    Object ds = dsClass.newInstance();
  +                    ExtendedProperties dsProps = c.subset(handle);
  +                    // use reflection to set properties
  +                    Iterator j = dsProps.getKeys();
  +                    while (j.hasNext())
  +                    {
  +                        String property = (String)j.next();
  +                        if ( !"classname".equals(property) && 
  +                             !"adapter".equals(property) ) 
  +                        {
  +                            category.debug("Setting datasource " + handle +
  +                                           " property: " + property);
  +                            try
  +                            {
  +                                PropertyDescriptor pd = 
  +                                    new PropertyDescriptor(property, dsClass);
  +                                Method setter = pd.getWriteMethod();
  +                                PropertyEditor pe = PropertyEditorManager
  +                                    .findEditor(pd.getPropertyType());
  +                                pe.setAsText(dsProps.getString(property));
  +                                Object[] value = { pe.getValue() };
  +                                setter.invoke(ds, value);
  +                            }
  +                            catch (Exception e)
  +                            {
  +                                category.error(
  +                                    "Property: " + property + " value: "
  +                                    + dsProps.getString(property) +
  +                                    " is not supported by DataSource: " 
  +                                    + classname);
  +                            throw e;
  +                            }
  +                        }
  +                    }
  +                    ctx.bind("jdbc/" + handle, ds);
  +                }
  +            }            
  +        }
  +        catch (Exception e)
  +        {
  +            throw new TorqueException(e);
  +        }    
  +    }
  +
       public static boolean isInit()
       {
           return isInit;
  @@ -457,7 +541,12 @@
           // Setup other ID generators for this map.
           try
           {
  -            DB db = DBFactory.create(getDatabaseProperty(name, "driver"));
  +            String key = getDatabaseProperty(name, "driver");
  +            if ( key == null || key.length() == 0 ) 
  +            {
  +                key = getDatabaseProperty(name, "adapter");
  +            }
  +            DB db = DBFactory.create(key);
               for (int i = 0; i < IDGeneratorFactory.ID_GENERATOR_METHODS.length;
                    i++)
               {
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>