db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer ConnectionFactoryAbstractImpl.java
Date Fri, 07 Jan 2005 23:02:12 GMT
tomdz       2005/01/07 15:02:12

  Modified:    src/java/org/apache/ojb/broker/metadata
                        JdbcConnectionDescriptor.java
                        ConnectionRepository.java
               src/java/org/apache/ojb/broker/accesslayer
                        ConnectionFactoryAbstractImpl.java
  Added:       src/java/org/apache/ojb/broker/metadata
                        JdbcMetadataUtils.java
  Log:
  Added ability to programmatically create a JCD from a local DataSource instance, e.g. for
Struts
  
  Revision  Changes    Path
  1.35      +29 -8     db-ojb/src/java/org/apache/ojb/broker/metadata/JdbcConnectionDescriptor.java
  
  Index: JdbcConnectionDescriptor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/JdbcConnectionDescriptor.java,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- JdbcConnectionDescriptor.java	15 Nov 2004 17:35:35 -0000	1.34
  +++ JdbcConnectionDescriptor.java	7 Jan 2005 23:02:12 -0000	1.35
  @@ -17,6 +17,8 @@
   
   import java.io.Serializable;
   
  +import javax.sql.DataSource;
  +
   import org.apache.commons.lang.SystemUtils;
   import org.apache.commons.lang.builder.ToStringBuilder;
   import org.apache.commons.lang.builder.ToStringStyle;
  @@ -57,6 +59,7 @@
       private SequenceDescriptor sequenceDescriptor;
       private ObjectCacheDescriptor objectCacheDescriptor;
       private BatchDescriptor batchDescriptor;
  +    private transient DataSource dataSource;
   
       private transient PlatformFactory platformFactory;
   
  @@ -103,6 +106,26 @@
       }
   
       /**
  +     * Returns the data source that this connection descriptor represents if any.
  +     * 
  +     * @return The data source or <code>null</code>
  +     */
  +    public DataSource getDataSource()
  +    {
  +        return dataSource;        
  +    }
  +
  +    /**
  +     * Sets the data source that this connection descriptor represents.
  +     * 
  +     * @param dataSource The data source
  +     */
  +    public void setDataSource(DataSource dataSource)
  +    {
  +        this.dataSource = dataSource;
  +    }
  +
  +    /**
        * Get the alias name for this descriptor.
        */
       public String getJcdAlias()
  @@ -129,7 +152,7 @@
   
       public boolean isDataSource()
       {
  -        return getDatasourceName() != null;
  +        return (getDataSource() != null) || (getDatasourceName() != null);
       }
   
       /**
  @@ -369,16 +392,14 @@
               }
               catch(NumberFormatException nfe)
               {
  -                setJdbcLevel(2);
  -                logger.info("JDBC level not null, and was not numeric (Value=" + jdbcLevel
+ ")");
  -                logger.info("set to default of jdbc level of 2.0 ");
  +                setJdbcLevel(2.0);
  +                logger.info("Specified JDBC level was not numeric (Value=" + jdbcLevel
+ "), used default jdbc level of 2.0 ");
               }
           }
           else
           {
  -            setJdbcLevel(2);
  -            logger.info("JDBC level was null");
  -            logger.info("set to default of jdbc level of 2.0 ");
  +            setJdbcLevel(2.0);
  +            logger.info("Specified JDBC level was null, used default jdbc level of 2.0
");
           }
       }
   
  
  
  
  1.16      +50 -42    db-ojb/src/java/org/apache/ojb/broker/metadata/ConnectionRepository.java
  
  Index: ConnectionRepository.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/ConnectionRepository.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- ConnectionRepository.java	27 May 2004 21:02:46 -0000	1.15
  +++ ConnectionRepository.java	7 Jan 2005 23:02:12 -0000	1.16
  @@ -28,44 +28,22 @@
   import java.util.Iterator;
   import java.util.List;
   
  +import javax.sql.DataSource;
  +
   public class ConnectionRepository implements Serializable, XmlCapable
   {
   	private static final long serialVersionUID = -5581126412817848887L;
       private static Logger log = LoggerFactory.getLogger(ConnectionRepository.class);
   
  -    /** Maps the sub-protocl part of a jdbc connection url to a OJB platform name */
  -    private static HashMap jdbcSubProtocolToPlatform = new HashMap();
  -    
  -    static
  -    {
  -        jdbcSubProtocolToPlatform.put("db2", "Db2");
  -        jdbcSubProtocolToPlatform.put("firebirdsql", "Firebird");
  -        jdbcSubProtocolToPlatform.put("hsqldb", "Hsqldb");
  -        jdbcSubProtocolToPlatform.put("informix-sqli", "Informix");
  -        // this might also mean Sapdb but we have no means to distinguish them
  -        jdbcSubProtocolToPlatform.put("sapdb", "MaxDB");
  -        // could be a different database but they usually have their own drivers and don't
  -        // need the jdbc-odbc bridge so we guess it is Access
  -        jdbcSubProtocolToPlatform.put("odbc", "MsAccess");
  -        jdbcSubProtocolToPlatform.put("inetdae7a", "MsSQLServer");
  -        jdbcSubProtocolToPlatform.put("inetdae7", "MsSQLServer");
  -        jdbcSubProtocolToPlatform.put("inetdae6", "MsSQLServer");
  -        jdbcSubProtocolToPlatform.put("inetdae", "MsSQLServer");
  -        jdbcSubProtocolToPlatform.put("inetpool", "MsSQLServer");
  -        jdbcSubProtocolToPlatform.put("mysql", "MySQL");
  -        jdbcSubProtocolToPlatform.put("oracle", "Oracle");
  -        jdbcSubProtocolToPlatform.put("inetora", "Oracle");
  -        jdbcSubProtocolToPlatform.put("postgresql", "PostgreSQL");
  -        jdbcSubProtocolToPlatform.put("sybase", "Sybase");
  -    }
  -
       private HashMap jcdMap;
       private Hashtable jcdAliasToPBKeyMap;
  +    private JdbcMetadataUtils utils;
   
       public ConnectionRepository()
       {
  -        jcdMap = new HashMap();
  +        jcdMap             = new HashMap();
           jcdAliasToPBKeyMap = new Hashtable();
  +        utils              = new JdbcMetadataUtils();
       }
   
       /**
  @@ -174,40 +152,70 @@
        */
       public JdbcConnectionDescriptor addDescriptor(String jcdAlias, String jdbcDriver, String
jdbcConnectionUrl, String username, String password)
       {
  -        JdbcConnectionDescriptor jcd = new JdbcConnectionDescriptor();
  -
  -        int pos = jdbcConnectionUrl.indexOf(':');
  -        int lastPos;
  +        JdbcConnectionDescriptor jcd   = new JdbcConnectionDescriptor();
  +        HashMap                  props = utils.parseConnectionUrl(jdbcConnectionUrl);
   
  -        jcd.setProtocol(jdbcConnectionUrl.substring(0, pos));
  -        lastPos = pos;
  -        pos     = jdbcConnectionUrl.indexOf(':', lastPos + 1);
  -
  -        String subProtocol = jdbcConnectionUrl.substring(lastPos + 1, pos);
  -        
  -        jcd.setSubProtocol(subProtocol);
  -        jcd.setDbAlias(jdbcConnectionUrl.substring(pos + 1));
  +        jcd.setJcdAlias(jcdAlias);
  +        jcd.setProtocol((String)props.get(JdbcMetadataUtils.PROPERTY_PROTOCOL));
  +        jcd.setSubProtocol((String)props.get(JdbcMetadataUtils.PROPERTY_SUBPROTOCOL));
  +        jcd.setDbAlias((String)props.get(JdbcMetadataUtils.PROPERTY_DBALIAS));
   
  -        String platform = (String)jdbcSubProtocolToPlatform.get(subProtocol);
  +        String platform = utils.findPlatformFor(jcd.getSubProtocol(), jdbcDriver);
   
           jcd.setDbms(platform);
  -        jcd.setJcdAlias(jcdAlias);
           jcd.setJdbcLevel(2.0);
           jcd.setDriver(jdbcDriver);
  +        if (username != null)
  +        {
  +           jcd.setUserName(username);
  +           jcd.setPassWord(password);
  +        }
           if ("default".equals(jcdAlias))
           {
               jcd.setDefaultConnection(true);
  +            MetadataManager.getInstance().setDefaultPBKey(new PBKey(jcd.getJcdAlias(),
jcd.getUserName(), jcd.getPassWord()));
           }
   
  +        addDescriptor(jcd);
  +        return jcd;
  +    }
  +    
  +    /**
  +     * Creates and adds a new connection descriptor for the given JDBC data source.
  +     * This method tries to guess the platform to be used, but it should be checked
  +     * afterwards nonetheless using the {@link JdbcConnectionDescriptor#getDbms()} method.
  +     * Note that the descriptor won't have a value for the driver because it is not possible
  +     * to retrieve the driver classname from the data source. 
  +     * 
  +     * @param jcdAlias   The connection alias for the created connection; if 'default'
is used,
  +     *                   then the new descriptor will become the default connection descriptor
  +     * @param dataSource The data source
  +     * @param username   The user name (can be <code>null</code>) 
  +     * @param password   The password (can be <code>null</code>) 
  +     * @return The created connection descriptor
  +     * @see JdbcConnectionDescriptor#getDbms()
  +     */
  +    public JdbcConnectionDescriptor addDescriptor(String jcdAlias, DataSource dataSource,
String username, String password)
  +    {
  +        JdbcConnectionDescriptor jcd = new JdbcConnectionDescriptor();
  +
  +        jcd.setJcdAlias(jcdAlias);
  +        jcd.setDataSource(dataSource);
           if (username != null)
           {
              jcd.setUserName(username);
              jcd.setPassWord(password);
           }
  +        utils.fillJCDFromDataSource(jcd, dataSource, username, password);
  +        if ("default".equals(jcdAlias))
  +        {
  +            jcd.setDefaultConnection(true);
  +            MetadataManager.getInstance().setDefaultPBKey(new PBKey(jcd.getJcdAlias(),
jcd.getUserName(), jcd.getPassWord()));
  +        }
           addDescriptor(jcd);
           return jcd;
       }
  -    
  +
       /**
        * Remove a descriptor.
        * @param validKey  This could be the {@link JdbcConnectionDescriptor}
  
  
  
  1.2       +395 -0    db-ojb/src/java/org/apache/ojb/broker/metadata/JdbcMetadataUtils.java
  
  
  
  
  1.14      +17 -4     db-ojb/src/java/org/apache/ojb/broker/accesslayer/ConnectionFactoryAbstractImpl.java
  
  Index: ConnectionFactoryAbstractImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/ConnectionFactoryAbstractImpl.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- ConnectionFactoryAbstractImpl.java	18 Dec 2004 13:32:10 -0000	1.13
  +++ ConnectionFactoryAbstractImpl.java	7 Jan 2005 23:02:12 -0000	1.14
  @@ -124,8 +124,10 @@
           if (jcd.isDataSource())
           {
               if (log.isDebugEnabled())
  +            {
                   log.debug("do datasource lookup, name: " + jcd.getDatasourceName() +
                           ", user: " + jcd.getUserName());
  +            }
               conn = newConnectionFromDataSource();
           }
           else
  @@ -188,8 +190,11 @@
       }
   
       /**
  -     *
  -     * @return an instance of Connection from the named Datasource
  +     * Creates a new connection from the data source that the connection descriptor
  +     * represents. If the connection descriptor does not directly contain the data source
  +     * then a JNDI lookup is performed to retrieve the data source.
  +     * 
  +     * @return A connection instance
        * @throws LookupException if we can't get a connection from the datasource either
due to a
        *          naming exception, a failed sanity check, or a SQLException.
        */
  @@ -198,7 +203,15 @@
       {
           Connection retval = null;
           // use JNDI lookup
  -        DataSource ds = (DataSource) dataSourceCache.get(jcd.getDatasourceName());
  +        DataSource ds = jcd.getDataSource();
  +
  +        if (ds == null)
  +        {
  +            // [tomdz] Would it suffice to store the datasources only at the JCDs ?
  +            //         Only possible problem would be serialization of the JCD because
  +            //         the data source object in the JCD does not 'survive' this
  +            ds = (DataSource) dataSourceCache.get(jcd.getDatasourceName());
  +        }
           try
           {
               if (ds == null)
  
  
  

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


Mime
View raw message