1   package com.atlassian.spring;
2   
3   import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor;
4   
5   import java.sql.*;
6   
7   /**
8    * A class to lazily instantiate a native JDBC extractor.
9    * <p />
10   * We need to lazily instantiate it because otherwise Spring will construct it for us, and users might get class not found errors (eg if they're
11   * not using Weblogic and Spring tries to load the WeblogicNativeJdbcExtractor, things get ugly).
12   */
13  public class LazyNativeJdbcExtractor implements NativeJdbcExtractor
14  {
15      private NativeJdbcExtractor delegatedExtractor;
16      private Class extractorClass;
17  
18      public LazyNativeJdbcExtractor()
19      {
20  
21      }
22  
23      public void setExtractorClass(Class extractorClass)
24      {
25          this.extractorClass = extractorClass;
26      }
27  
28      private synchronized NativeJdbcExtractor getDelegatedExtractor()
29      {
30          try
31          {
32              if (delegatedExtractor == null)
33              {
34                  delegatedExtractor = (NativeJdbcExtractor) extractorClass.newInstance();
35              }
36          }
37          catch (IllegalAccessException e)
38          {
39              throw new RuntimeException("Error occurred trying to instantiate a native extractor of type: " + extractorClass, e);
40          }
41          catch (InstantiationException e)
42          {
43              throw new RuntimeException("Error occurred trying to instantiate a native extractor of type: " + extractorClass, e);
44          }
45  
46          if (delegatedExtractor != null)
47          {
48              return delegatedExtractor;
49          }
50          else
51          {
52              throw new RuntimeException("Error occurred trying to instantiate a native extractor of type: " + extractorClass);
53          }
54      }
55  
56      public boolean isNativeConnectionNecessaryForNativeStatements()
57      {
58          return getDelegatedExtractor().isNativeConnectionNecessaryForNativeStatements();
59      }
60  
61      public boolean isNativeConnectionNecessaryForNativePreparedStatements()
62      {
63          return getDelegatedExtractor().isNativeConnectionNecessaryForNativePreparedStatements();
64      }
65  
66      public boolean isNativeConnectionNecessaryForNativeCallableStatements()
67      {
68          return getDelegatedExtractor().isNativeConnectionNecessaryForNativeCallableStatements();
69      }
70  
71      public Connection getNativeConnection(Connection con) throws SQLException
72      {
73          return getDelegatedExtractor().getNativeConnection(con);
74      }
75  
76      public Connection getNativeConnectionFromStatement(Statement stmt) throws SQLException
77      {
78          return getDelegatedExtractor().getNativeConnectionFromStatement(stmt);
79      }
80  
81      public Statement getNativeStatement(Statement stmt) throws SQLException
82      {
83          return getDelegatedExtractor().getNativeStatement(stmt);
84      }
85  
86      public PreparedStatement getNativePreparedStatement(PreparedStatement ps) throws SQLException
87      {
88          return getDelegatedExtractor().getNativePreparedStatement(ps);
89      }
90  
91      public CallableStatement getNativeCallableStatement(CallableStatement cs) throws SQLException
92      {
93          return getDelegatedExtractor().getNativeCallableStatement(cs);
94      }
95  
96      public ResultSet getNativeResultSet(ResultSet rs) throws SQLException
97      {
98          return getDelegatedExtractor().getNativeResultSet(rs);
99      }
100 }