<commit_before>/*******************************************************************************
 * Copyright (c) 1998, 2009 Oracle. All rights reserved. 
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 
 * which accompanies this distribution. 
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 * http://www.eclipse.org/org/documents/edl-v10.php.
 * 
 * Contributors:
 *     tware,mkeith - Initial OSGi support for JPA 
 *     smith - Cleanup of activator/listener into this class
 *     dclarke - 20090916 - Added clearCachedProviders support
 ******************************************************************************/
package org.eclipse.persistence.javax.persistence.osgi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceProviderResolver;
import javax.persistence.spi.PersistenceProviderResolverHolder;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

/**
 * Uses OSGi services to discover JPA persistence providers
 * 
 * Also acts as a resolver class (called by the Persistence class when resolving
 * providers).
 */
public class Activator implements BundleActivator, PersistenceProviderResolver {

    public static final String PERSISTENCE_PROVIDER = "javax.persistence.spi.PersistenceProvider";

    private BundleContext ctx;
    private ServiceTracker serviceTracker;
    private Map<String, PersistenceProvider> providers;

    public void start(BundleContext context) throws Exception {
        log(Level.FINE, context.getBundle().getSymbolicName() + " - starting...");

        // Init the bundle context
        this.ctx = context;

        // Set up a tracker to add providers as they register themselves
        ServiceTrackerCustomizer customizer = new ProviderTrackerCustomizer(this);
        serviceTracker = new ServiceTracker(ctx, PERSISTENCE_PROVIDER, customizer);
        serviceTracker.open();

        // Set the persistence provider resolver to use OSGi services
        PersistenceProviderResolverHolder.setPersistenceProviderResolver(this);

        this.providers = new HashMap<String, PersistenceProvider>();

        // Provider bundles should require this bundle to be started before
        // they start, so it is very unlikely that provider bundles will have
        // already loaded and registered but we'll do this just in case...
        ServiceReference[] refs = null;
        try {
            refs = ctx.getServiceReferences(PERSISTENCE_PROVIDER, null);
        } catch (InvalidSyntaxException invEx) {
        } // Can't happen since filter is null
        if (refs != null) {
            for (ServiceReference ref : refs) {
                addProvider(ref);
            }
        }

        log(Level.FINE, context.getBundle().getSymbolicName() + " - started");
    }

    /**
     * Obtain the provider from the reference and store it under it's name, if
     * supplied or a generated name otherwise.
     */
    PersistenceProvider addProvider(ServiceReference ref) {
        PersistenceProvider provider = (PersistenceProvider) ctx.getService(ref);
        String providerName = getProviderName(ref);
        this.providers.put(providerName, provider);
        return provider;
    }

    /**
     * Remove the provider keyed by it's name.
     */
    protected void removeProvider(ServiceReference ref) {
        String providerName = getProviderName(ref);
        this.providers.remove(providerName);
    }

    public void stop(BundleContext context) throws Exception {
        log(Level.FINE, context.getBundle().getSymbolicName() + " - stopping...");

        // Close the service tracker
        this.serviceTracker.close();
        this.serviceTracker = null;

        // Nil out the known provider list
        // Note: the holder is in this bundle so it will no longer be available
        // anyways. This is done to ensure all refs to providers in other
        // bundles is removed
        PersistenceProviderResolverHolder.setPersistenceProviderResolver(null);
        this.providers = null;

        log(Level.FINE, context.getBundle().getSymbolicName() + " - stopped...");
    }

    /**
     * Assumes that provider services are registered with property that
     * indicates the name of the provider impl class.
     */
    public String getProviderName(ServiceReference ref) {
        String providerName = (String) ref.getProperty(PERSISTENCE_PROVIDER);

        if (providerName == null) {
            providerName = "PersistenceProvider-" + ref.hashCode();<commit_msg>Revolves Bug 296010--NPE due to uninitialized map.<commit_after>/*******************************************************************************
 * Copyright (c) 1998, 2009 Oracle. All rights reserved. 
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 
 * which accompanies this distribution. 
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 * http://www.eclipse.org/org/documents/edl-v10.php.
 * 
 * Contributors:
 *     tware,mkeith - Initial OSGi support for JPA 
 *     ssmith - Cleanup of activator/listener into this class
 *     dclarke - 20090916 - Added clearCachedProviders support
 *     ssmith - 20091126 - Bug 296010 - providers map not initialized before use
 * 
 ******************************************************************************/
package org.eclipse.persistence.javax.persistence.osgi;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceProviderResolver;
import javax.persistence.spi.PersistenceProviderResolverHolder;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

/**
 * Uses OSGi services to discover JPA persistence providers
 * 
 * Also acts as a resolver class (called by the Persistence class when resolving
 * providers).
 */
public class Activator implements BundleActivator, PersistenceProviderResolver {

    public static final String PERSISTENCE_PROVIDER = "javax.persistence.spi.PersistenceProvider";

    private BundleContext ctx;
    private ServiceTracker serviceTracker;
    private Map<String, PersistenceProvider> providers = new Hashtable<String, PersistenceProvider>();

    public void start(BundleContext context) throws Exception {
        log(Level.FINE, context.getBundle().getSymbolicName() + " - starting...");

        // Init the bundle context
        this.ctx = context;

        // Set up a tracker to add providers as they register themselves
        ServiceTrackerCustomizer customizer = new ProviderTrackerCustomizer(this);
        serviceTracker = new ServiceTracker(ctx, PERSISTENCE_PROVIDER, customizer);
        serviceTracker.open();

        // Set the persistence provider resolver to use OSGi services
        PersistenceProviderResolverHolder.setPersistenceProviderResolver(this);

        log(Level.FINE, context.getBundle().getSymbolicName() + " - started");
    }

    /**
     * Obtain the provider from the reference and store it under it's name, if
     * supplied or a generated name otherwise.
     */
    PersistenceProvider addProvider(ServiceReference ref) {
        PersistenceProvider provider = (PersistenceProvider) ctx.getService(ref);
        String providerName = getProviderName(ref);
        this.providers.put(providerName, provider);
        return provider;
    }

    /**
     * Remove the provider keyed by it's name.
     */
    protected void removeProvider(ServiceReference ref) {
        String providerName = getProviderName(ref);
        this.providers.remove(providerName);
    }

    public void stop(BundleContext context) throws Exception {
        log(Level.FINE, context.getBundle().getSymbolicName() + " - stopping...");

        // Close the service tracker
        this.serviceTracker.close();
        this.serviceTracker = null;

        // Clear the known provider list
        // Note: the holder is in this bundle so it will no longer be available
        // anyways. This is done to ensure all refs to providers in other
        // bundles is removed
        PersistenceProviderResolverHolder.setPersistenceProviderResolver(null);
        this.providers.clear();

        log(Level.FINE, context.getBundle().getSymbolicName() + " - stopped...");
    }

    /**
     * Assumes that provider services are registered with property that
     * indicates the name of the provider impl class.
     */
    public String getProviderName(ServiceReference ref) {
        String providerName = (String) ref.getProperty(PERSISTENCE_PROVIDER);

        if (providerName == null) {
            providerName = "PersistenceProvider-" + ref.hashCode();
{'fraction_non_alphanumeric': 0.11394240729231407, 'fraction_numerical': 0.007043712450797597, 'mean_word_length': 3.390632105502501, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3961882', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2375, 'n_words': 743}