1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
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 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
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 |
|
|
75 |
0 |
platform = updateOraclePlatform (jcd, ds, platform); |
76 |
|
} |
77 |
|
|
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 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
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 |
|
|
113 |
|
|
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 |
|
} |