View Javadoc

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      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      {
53          this.ds = ds;
54          this.jcdAlias = jndiName;
55      }
56      
57      public void init()
58      throws Exception
59      {
60          ConnectionRepository cr = MetadataManager.getInstance().connectionRepository();
61          JdbcConnectionDescriptor jcd = cr.getDescriptor(new PBKey(jcdAlias));
62          if (jcd == null)
63          {
64              jcd = new JdbcConnectionDescriptor();
65              jcd.setJcdAlias(jcdAlias);
66              cr.addDescriptor(jcd);
67          }
68          
69          JdbcMetadataUtils jdbcMetadataUtils = new JdbcMetadataUtils ();
70          jdbcMetadataUtils.fillJCDFromDataSource(jcd, ds, null, null);
71          String platform = jcd.getDbms();
72          if (JdbcMetadataUtils.PLATFORM_ORACLE.equals(platform)) 
73          {
74              // Postprocess to find Oracle version.
75                  platform = updateOraclePlatform (jcd, ds, platform);
76          }
77          // if platform has explicitly been set, the value takes precedence
78          if (platform != null) 
79          {
80              if (!platform.equals(jcd.getDbms())) 
81              {
82                  log.warn ("Automatically derived RDBMS platform \"" + jcd.getDbms()
83                            + "\" differs from explicitly set platform \"" + platform + "\""); 
84              }
85              jcd.setDbms(platform);
86          } 
87          else 
88          {
89              platform = jcd.getDbms();
90          }
91          System.out.println("##### platform = " + platform);
92      }
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         Connection con = null;
105         try 
106         {
107             con = ds.getConnection();
108             DatabaseMetaData metaData = con.getMetaData();
109             int rdbmsVersion = 0;
110             try 
111             {
112                 // getDatabaseMajorVersion exists since 1.4, so it may
113                 // not be defined for the driver used.
114                 rdbmsVersion = metaData.getDatabaseMajorVersion();
115             } 
116             catch (Throwable t) 
117             {
118                 String dbVersion = metaData.getDatabaseProductVersion();
119                 String relKey = "Release";
120                 String major = dbVersion;
121                 int startPos = dbVersion.indexOf(relKey);
122                 if (startPos < 0)
123                 {
124                     log.warn ("Cannot determine Oracle version, no \"Release\" in procuct version: \"" + dbVersion + "\"");
125                     return platform;
126                 }
127                 startPos += relKey.length();
128                 int dotPos = dbVersion.indexOf('.', startPos);
129                 if (dotPos > 0) {
130                     major = dbVersion.substring(startPos, dotPos).trim();
131                 }
132                 try
133                 {
134                     rdbmsVersion = Integer.parseInt(major);
135                 }
136                 catch (NumberFormatException e)
137                 {
138                     log.warn ("Cannot determine Oracle version, product version \"" + dbVersion + "\" not layed out as \"... Release N.M.....\"");
139                     return platform;
140                 }
141                 if (log.isDebugEnabled())
142                 {
143                     log.debug ("Extracted Oracle major version " + rdbmsVersion + " from product version \"" + dbVersion + "\"");
144                 }
145             }
146             if (rdbmsVersion >= 9) 
147             {
148                 jcd.setDbms(JdbcMetadataUtils.PLATFORM_ORACLE9I);
149                 return JdbcMetadataUtils.PLATFORM_ORACLE9I;
150             }
151         }
152         finally
153         {
154             if (con != null) 
155             {
156                 con.close ();
157             }
158         }
159         return platform;
160     }
161     
162     
163 }