Coverage report

  %line %branch
org.apache.jetspeed.components.rdbms.ojb.DatabasePlatformConfigurator
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.jetspeed.components.rdbms.ojb;
 18  
 
 19  
 import java.sql.Connection;
 20  
 import java.sql.DatabaseMetaData;
 21  
 import java.sql.SQLException;
 22  
 
 23  
 import javax.sql.DataSource;
 24  
 
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 import org.apache.ojb.broker.PBKey;
 28  
 import org.apache.ojb.broker.accesslayer.LookupException;
 29  
 import org.apache.ojb.broker.metadata.ConnectionRepository;
 30  
 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
 31  
 import org.apache.ojb.broker.metadata.JdbcMetadataUtils;
 32  
 import org.apache.ojb.broker.metadata.MetadataManager;
 33  
 
 34  
 /**
 35  
  * Dynamically configures Database Platform for OJB by looking at the connection string
 36  
  * and figuring out the OJB platform using an OJB metadata utility
 37  
  * Its important to get this right otherwise you will be sending the wrong (most likely HSQL)
 38  
  * flavor of SQL statements to the backend database.
 39  
  * 
 40  
  * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
 41  
  * @version $Id: $
 42  
  *          
 43  
  */
 44  
 public class DatabasePlatformConfigurator
 45  
 {
 46  0
     private static final Log log = LogFactory.getLog(DatabasePlatformConfigurator.class);
 47  
     
 48  
     private DataSource ds;
 49  
     private String jcdAlias;
 50  
     
 51  
     public DatabasePlatformConfigurator(DataSource ds, String jndiName)
 52  0
     {
 53  0
         this.ds = ds;
 54  0
         this.jcdAlias = jndiName;
 55  0
     }
 56  
     
 57  
     public void init()
 58  
     throws Exception
 59  
     {
 60  0
         ConnectionRepository cr = MetadataManager.getInstance().connectionRepository();
 61  0
         JdbcConnectionDescriptor jcd = cr.getDescriptor(new PBKey(jcdAlias));
 62  0
         if (jcd == null)
 63  
         {
 64  0
             jcd = new JdbcConnectionDescriptor();
 65  0
             jcd.setJcdAlias(jcdAlias);
 66  0
             cr.addDescriptor(jcd);
 67  
         }
 68  
         
 69  0
         JdbcMetadataUtils jdbcMetadataUtils = new JdbcMetadataUtils ();
 70  0
         jdbcMetadataUtils.fillJCDFromDataSource(jcd, ds, null, class="keyword">null);
 71  0
         String platform = jcd.getDbms();
 72  0
         if (JdbcMetadataUtils.PLATFORM_ORACLE.equals(platform)) 
 73  
         {
 74  
             // Postprocess to find Oracle version.
 75  0
                 platform = updateOraclePlatform (jcd, ds, platform);
 76  
         }
 77  
         // if platform has explicitly been set, the value takes precedence
 78  0
         if (platform != null) 
 79  
         {
 80  0
             if (!platform.equals(jcd.getDbms())) 
 81  
             {
 82  0
                 log.warn ("Automatically derived RDBMS platform \"" + jcd.getDbms()
 83  
                           + "\" differs from explicitly set platform \"" + platform + "\""); 
 84  
             }
 85  0
             jcd.setDbms(platform);
 86  
         } 
 87  
         else 
 88  
         {
 89  0
             platform = jcd.getDbms();
 90  
         }
 91  0
         System.out.println("##### platform = " + platform);
 92  0
     }
 93  
  
 94  
     /**
 95  
      * @param jcd
 96  
      * @throws LookupException
 97  
      * @throws IllegalAccessException
 98  
      * @throws InstantiationException
 99  
      * throws SQLException
 100  
      */
 101  
     private String updateOraclePlatform(JdbcConnectionDescriptor jcd, DataSource ds, String platform)
 102  
         throws LookupException, IllegalAccessException, InstantiationException, SQLException 
 103  
     {
 104  0
         Connection con = null;
 105  
         try 
 106  
         {
 107  0
             con = ds.getConnection();
 108  0
             DatabaseMetaData metaData = con.getMetaData();
 109  0
             int rdbmsVersion = 0;
 110  
             try 
 111  
             {
 112  
                 // getDatabaseMajorVersion exists since 1.4, so it may
 113  
                 // not be defined for the driver used.
 114  0
                 rdbmsVersion = metaData.getDatabaseMajorVersion();
 115  
             } 
 116  0
             catch (Throwable t) 
 117  
             {
 118  0
                 String dbVersion = metaData.getDatabaseProductVersion();
 119  0
                 String relKey = "Release";
 120  0
                 String major = dbVersion;
 121  0
                 int startPos = dbVersion.indexOf(relKey);
 122  0
                 if (startPos < 0)
 123  
                 {
 124  0
                     log.warn ("Cannot determine Oracle version, no \"Release\" in procuct version: \"" + dbVersion + "\"");
 125  0
                     return platform;
 126  
                 }
 127  0
                 startPos += relKey.length();
 128  0
                 int dotPos = dbVersion.indexOf('.', startPos);
 129  0
                 if (dotPos > 0) {
 130  0
                     major = dbVersion.substring(startPos, dotPos).trim();
 131  
                 }
 132  
                 try
 133  
                 {
 134  0
                     rdbmsVersion = Integer.parseInt(major);
 135  
                 }
 136  0
                 catch (NumberFormatException e)
 137  
                 {
 138  0
                     log.warn ("Cannot determine Oracle version, product version \"" + dbVersion + "\" not layed out as \"... Release N.M.....\"");
 139  0
                     return platform;
 140  0
                 }
 141  0
                 if (log.isDebugEnabled())
 142  
                 {
 143  0
                     log.debug ("Extracted Oracle major version " + rdbmsVersion + " from product version \"" + dbVersion + "\"");
 144  
                 }
 145  0
             }
 146  0
             if (rdbmsVersion >= 9) 
 147  
             {
 148  0
                 jcd.setDbms(JdbcMetadataUtils.PLATFORM_ORACLE9I);
 149  0
                 return JdbcMetadataUtils.PLATFORM_ORACLE9I;
 150  
             }
 151  
         }
 152  
         finally
 153  
         {
 154  0
             if (con != null) 
 155  
             {
 156  0
                 con.close ();
 157  
             }
 158  
         }
 159  0
         return platform;
 160  
     }
 161  
     
 162  
     
 163  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.