Debian Codegraph @

src/main/org/h2/util/OsgiDataSourceFactory.java

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.util;
7
8 import java.sql.SQLException;
9 import java.sql.SQLFeatureNotSupportedException;
10 import java.util.Hashtable;
11 import java.util.Properties;
12 import javax.sql.ConnectionPoolDataSource;
13 import javax.sql.DataSource;
14 import javax.sql.XADataSource;
15 import org.h2.engine.Constants;
16 import org.h2.jdbcx.JdbcDataSource;
17 import org.osgi.framework.BundleContext;
18 import org.osgi.service.jdbc.DataSourceFactory;
19
20 /**
21 * This class implements the OSGi DataSourceFactory interface for the H2 JDBC
22 * driver. The following standard configuration properties are supported:
23 * {@link #JDBC_USER}, {@link #JDBC_PASSWORD}, {@link #JDBC_DESCRIPTION},
24 * {@link #JDBC_DATASOURCE_NAME}, {@link #JDBC_NETWORK_PROTOCOL},
25 * {@link #JDBC_URL}, {@link #JDBC_SERVER_NAME}, {@link #JDBC_PORT_NUMBER}. The
26 * following standard configuration properties are not supported:
27 * {@link #JDBC_ROLE_NAME}, {@link #JDBC_DATABASE_NAME},
28 * {@link #JDBC_INITIAL_POOL_SIZE}, {@link #JDBC_MAX_POOL_SIZE},
29 * {@link #JDBC_MIN_POOL_SIZE}, {@link #JDBC_MAX_IDLE_TIME},
30 * {@link #JDBC_MAX_STATEMENTS}, {@link #JDBC_PROPERTY_CYCLE}. Any other
31 * property will be treated as a H2 specific option. If the {@link #JDBC_URL}
32 * property is passed to any of the DataSource factories, the following
33 * properties will be ignored: {@link #JDBC_DATASOURCE_NAME},
34 * {@link #JDBC_NETWORK_PROTOCOL}, {@link #JDBC_SERVER_NAME},
35 * {@link #JDBC_PORT_NUMBER}.
36 *
37 * @author Per Otterstrom
38 */
39 public class OsgiDataSourceFactory implements DataSourceFactory {
40 private final org.h2.Driver driver;
41
42 public OsgiDataSourceFactory(org.h2.Driver driver) {
43 this.driver = driver;
44 }
45
46 /**
47 * Creates a basic data source.
48 *
49 * @param properties the properties for the data source.
50 * @throws SQLException if unsupported properties are supplied, or if data
51 * source can not be created.
52 * @return a new data source.
53 */
54 @Override
55 public DataSource createDataSource(Properties properties)
56 throws SQLException {
57 // Make copy of properties
58 Properties propertiesCopy = new Properties();
59 if (properties != null) {
60 propertiesCopy.putAll(properties);
61 }
62
63 // Verify that no unsupported standard options are used
64 rejectUnsupportedOptions(propertiesCopy);
65
66 // Standard pool properties in OSGi not applicable here
67 rejectPoolingOptions(propertiesCopy);
68
69 JdbcDataSource dataSource = new JdbcDataSource();
70
71 setupH2DataSource(dataSource, propertiesCopy);
72
73 return dataSource;
74 }
75
76 /**
77 * Creates a pooled data source.
78 *
79 * @param properties the properties for the data source.
80 * @throws SQLException if unsupported properties are supplied, or if data
81 * source can not be created.
82 * @return a new data source.
83 */
84 @Override
85 public ConnectionPoolDataSource createConnectionPoolDataSource(
86 Properties properties) throws SQLException {
87 // Make copy of properties
88 Properties propertiesCopy = new Properties();
89 if (properties != null) {
90 propertiesCopy.putAll(properties);
91 }
92
93 // Verify that no unsupported standard options are used
94 rejectUnsupportedOptions(propertiesCopy);
95
96 // The integrated connection pool is H2 is not configurable
97 rejectPoolingOptions(propertiesCopy);
98
99 JdbcDataSource dataSource = new JdbcDataSource();
100
101 setupH2DataSource(dataSource, propertiesCopy);
102
103 return dataSource;
104 }
105
106 /**
107 * Creates a pooled XA data source.
108 *
109 * @param properties the properties for the data source.
110 * @throws SQLException if unsupported properties are supplied, or if data
111 * source can not be created.
112 * @return a new data source.
113 */
114 @Override
115 public XADataSource createXADataSource(Properties properties)
116 throws SQLException {
117 // Make copy of properties
118 Properties propertiesCopy = new Properties();
119 if (properties != null) {
120 propertiesCopy.putAll(properties);
121 }
122
123 // Verify that no unsupported standard options are used
124 rejectUnsupportedOptions(propertiesCopy);
125
126 // The integrated connection pool is H2 is not configurable
127 rejectPoolingOptions(propertiesCopy);
128
129 JdbcDataSource dataSource = new JdbcDataSource();
130
131 setupH2DataSource(dataSource, propertiesCopy);
132
133 return dataSource;
134 }
135
136 /**
137 * Returns a driver. The H2 driver does not support any properties.
138 *
139 * @param properties must be null or empty list.
140 * @throws SQLException if any property is supplied.
141 * @return a driver.
142 */
143 @Override
144 public java.sql.Driver createDriver(Properties properties)
145 throws SQLException {
146 if (properties != null && !properties.isEmpty()) {
147 // No properties supported
148 throw new SQLException();
149 }
150 return driver;
151 }
152
153 /**
154 * Checker method that will throw if any unsupported standard OSGi options
155 * is present.
156 *
157 * @param p the properties to check
158 * @throws SQLFeatureNotSupportedException if unsupported properties are
159 * present
160 */
161 private static void rejectUnsupportedOptions(Properties p)
162 throws SQLFeatureNotSupportedException {
163 // Unsupported standard properties in OSGi
164 if (p.containsKey(DataSourceFactory.JDBC_ROLE_NAME)) {
165 throw new SQLFeatureNotSupportedException("The " +
166 DataSourceFactory.JDBC_ROLE_NAME +
167 " property is not supported by H2");
168 }
169 if (p.containsKey(DataSourceFactory.JDBC_DATASOURCE_NAME)) {
170 throw new SQLFeatureNotSupportedException("The " +
171 DataSourceFactory.JDBC_DATASOURCE_NAME +
172 " property is not supported by H2");
173 }
174 }
175
176 /**
177 * Applies common OSGi properties to a H2 data source. Non standard
178 * properties will be applied as H2 options.
179 *
180 * @param dataSource the data source to configure
181 * @param p the properties to apply to the data source
182 */
183 private static void setupH2DataSource(JdbcDataSource dataSource,
184 Properties p) {
185 // Setting user and password
186 if (p.containsKey(DataSourceFactory.JDBC_USER)) {
187 dataSource.setUser((String) p.remove(DataSourceFactory.JDBC_USER));
188 }
189 if (p.containsKey(DataSourceFactory.JDBC_PASSWORD)) {
190 dataSource.setPassword((String) p
191 .remove(DataSourceFactory.JDBC_PASSWORD));
192 }
193
194 // Setting description
195 if (p.containsKey(DataSourceFactory.JDBC_DESCRIPTION)) {
196 dataSource.setDescription((String) p
197 .remove(DataSourceFactory.JDBC_DESCRIPTION));
198 }
199
200 // Setting URL
201 StringBuilder connectionUrl = new StringBuilder();
202 if (p.containsKey(DataSourceFactory.JDBC_URL)) {
203 // Use URL if specified
204 connectionUrl.append(p.remove(DataSourceFactory.JDBC_URL));
205 // Remove individual properties
206 p.remove(DataSourceFactory.JDBC_NETWORK_PROTOCOL);
207 p.remove(DataSourceFactory.JDBC_SERVER_NAME);
208 p.remove(DataSourceFactory.JDBC_PORT_NUMBER);
209 p.remove(DataSourceFactory.JDBC_DATABASE_NAME);
210 } else {
211 // Creating URL from individual properties
212 connectionUrl.append(Constants.START_URL);
213
214 // Set network protocol (tcp/ssl) or DB type (mem/file)
215 String protocol = "";
216 if (p.containsKey(DataSourceFactory.JDBC_NETWORK_PROTOCOL)) {
217 protocol = (String) p.remove(DataSourceFactory.JDBC_NETWORK_PROTOCOL);
218 connectionUrl.append(protocol).append(":");
219 }
220
221 // Host name and/or port
222 if (p.containsKey(DataSourceFactory.JDBC_SERVER_NAME)) {
223 connectionUrl.append("//").append(
224 p.remove(DataSourceFactory.JDBC_SERVER_NAME));
225
226 if (p.containsKey(DataSourceFactory.JDBC_PORT_NUMBER)) {
227 connectionUrl.append(":").append(
228 p.remove(DataSourceFactory.JDBC_PORT_NUMBER));
229 }
230
231 connectionUrl.append("/");
232 } else if (p.containsKey(
233 DataSourceFactory.JDBC_PORT_NUMBER)) {
234 // Assume local host if only port was set
235 connectionUrl
236 .append("//localhost:")
237 .append(p.remove(DataSourceFactory.JDBC_PORT_NUMBER))
238 .append("/");
239 } else if (protocol.equals("tcp") || protocol.equals("ssl")) {
240 // Assume local host if network protocol is set, but no host or
241 // port is set
242 connectionUrl.append("//localhost/");
243 }
244
245 // DB path and name
246 if (p.containsKey(DataSourceFactory.JDBC_DATABASE_NAME)) {
247 connectionUrl.append(
248 p.remove(DataSourceFactory.JDBC_DATABASE_NAME));
249 }
250 }
251
252 // Add remaining properties as options
253 for (Object option : p.keySet()) {
254 connectionUrl.append(";").append(option).append("=")
255 .append(p.get(option));
256 }
257
258 if (connectionUrl.length() > Constants.START_URL.length()) {
259 dataSource.setURL(connectionUrl.toString());
260 }
261 }
262
263 /**
264 * Checker method that will throw if any pooling related standard OSGi
265 * options are present.
266 *
267 * @param p the properties to check
268 * @throws SQLFeatureNotSupportedException if unsupported properties are
269 * present
270 */
271 private static void rejectPoolingOptions(Properties p)
272 throws SQLFeatureNotSupportedException {
273 if (p.containsKey(DataSourceFactory.JDBC_INITIAL_POOL_SIZE) ||
274 p.containsKey(DataSourceFactory.JDBC_MAX_IDLE_TIME) ||
275 p.containsKey(DataSourceFactory.JDBC_MAX_POOL_SIZE) ||
276 p.containsKey(DataSourceFactory.JDBC_MAX_STATEMENTS) ||
277 p.containsKey(DataSourceFactory.JDBC_MIN_POOL_SIZE) ||
278 p.containsKey(DataSourceFactory.JDBC_PROPERTY_CYCLE)) {
279 throw new SQLFeatureNotSupportedException(
280 "Pooling properties are not supported by H2");
281 }
282 }
283
284 /**
285 * Register the H2 JDBC driver service.
286 *
287 * @param bundleContext the bundle context
288 * @param driver the driver
289 */
290 static void registerService(BundleContext bundleContext,
291 org.h2.Driver driver) {
292 Hashtable<String, Object> properties = new Hashtable<>();
293 properties.put(
294 DataSourceFactory.OSGI_JDBC_DRIVER_CLASS,
295 org.h2.Driver.class.getName());
296 properties.put(
297 DataSourceFactory.OSGI_JDBC_DRIVER_NAME,
298 "H2 JDBC Driver");
299 properties.put(
300 DataSourceFactory.OSGI_JDBC_DRIVER_VERSION,
301 Constants.FULL_VERSION);
302 properties.put(DataSourceFactory.OSGI_JDBC_CAPABILITY, new String[] {
303 DataSourceFactory.OSGI_JDBC_CAPABILITY_DRIVER,
304 DataSourceFactory.OSGI_JDBC_CAPABILITY_DATASOURCE,
305 DataSourceFactory.OSGI_JDBC_CAPABILITY_CONNECTIONPOOLDATASOURCE,
306 DataSourceFactory.OSGI_JDBC_CAPABILITY_XADATASOURCE
307 });
308 bundleContext.registerService(
309 DataSourceFactory.class.getName(),
310 new OsgiDataSourceFactory(driver), properties);
311 }
312 }