Debian Codegraph @

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

raw
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 org.osgi.framework.BundleActivator;
9 import org.osgi.framework.BundleContext;
10
11 /**
12 * The driver activator loads the H2 driver when starting the bundle. The driver
13 * is unloaded when stopping the bundle.
14 */
15 public class DbDriverActivator implements BundleActivator {
16
17 private static final String DATASOURCE_FACTORY_CLASS =
18 "org.osgi.service.jdbc.DataSourceFactory";
19
20 /**
21 * Start the bundle. If the 'org.osgi.service.jdbc.DataSourceFactory' class
22 * is available in the class path, this will load the database driver and
23 * register the DataSourceFactory service.
24 *
25 * @param bundleContext the bundle context
26 */
27 @Override
28 public void start(BundleContext bundleContext) {
29 org.h2.Driver driver = org.h2.Driver.load();
30 try {
31 JdbcUtils.loadUserClass(DATASOURCE_FACTORY_CLASS);
32 } catch (Exception e) {
33 // class not found - don't register
34 return;
35 }
36 // but don't ignore exceptions in this call
37 OsgiDataSourceFactory.registerService(bundleContext, driver);
38 }
39
40 /**
41 * Stop the bundle. This will unload the database driver. The
42 * DataSourceFactory service is implicitly un-registered by the OSGi
43 * framework.
44 *
45 * @param bundleContext the bundle context
46 */
47 @Override
48 public void stop(BundleContext bundleContext) {
49 org.h2.Driver.unload();
50 }
51
52 }