All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.postgresql.osgi.PGBundleActivator Maven / Gradle / Ivy

There is a newer version: 42.7.4
Show newest version
/*
 * Copyright (c) 2003, PostgreSQL Global Development Group
 * See the LICENSE file in the project root for more information.
 */

package org.postgresql.osgi;

import org.postgresql.Driver;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.jdbc.DataSourceFactory;

import java.util.Dictionary;
import java.util.Hashtable;

/**
 * This class is an OSGi Bundle Activator and should only be used internally by the OSGi Framework.
 */
public class PGBundleActivator implements BundleActivator {
  private ServiceRegistration registration;

  public void start(BundleContext context) throws Exception {
    Dictionary properties = new Hashtable();
    properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, Driver.class.getName());
    properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_NAME, org.postgresql.util.DriverInfo.DRIVER_NAME);
    properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION, org.postgresql.util.DriverInfo.DRIVER_VERSION);
    try {
      registration = context.registerService(DataSourceFactory.class.getName(),
          new PGDataSourceFactory(), properties);
    } catch (NoClassDefFoundError e) {
      String msg = e.getMessage();
      if (msg != null && msg.contains("org/osgi/service/jdbc/DataSourceFactory")) {
        if (!Boolean.getBoolean("pgjdbc.osgi.debug")) {
          return;
        }

        new IllegalArgumentException("Unable to load DataSourceFactory. "
            + "Will ignore DataSourceFactory registration. If you need one, "
            + "ensure org.osgi.enterprise is on the classpath", e).printStackTrace();
        // just ignore. Assume OSGi-enterprise is not loaded
        return;
      }
      throw e;
    }
  }

  public void stop(BundleContext context) throws Exception {
    if (registration != null) {
      registration.unregister();
      registration = null;
    }

    if (Driver.isRegistered()) {
      Driver.deregister();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy