<commit_before>/*
 *   Copyright 2005 The Apache Software Foundation
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
package org.apache.felix.framework;

import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.*;

import org.apache.felix.framework.util.StringMap;
import org.apache.felix.framework.util.Util;
import org.osgi.framework.*;

class ServiceRegistrationImpl implements ServiceRegistration
{
    // Service registry.
    private ServiceRegistry m_registry = null;
    // Bundle implementing the service.
    private Bundle m_bundle = null;
    // Interfaces associated with the service object.
    private String[] m_classes = null;
    // Service Id associated with the service object.
    private Long m_serviceId = null;
    // Service object.
    private Object m_svcObj = null;
    // Service factory interface.
    private ServiceFactory m_factory = null;
    // Associated property dictionary.
    private Map m_propMap = null;
    // Re-usable service reference.
    private ServiceReferenceImpl m_ref = null;

    public ServiceRegistrationImpl(
        ServiceRegistry registry, Bundle bundle,
        String[] classes, Long serviceId,
        Object svcObj, Dictionary dict)
    {
        m_registry = registry;
        m_bundle = bundle;
        m_classes = classes;
        m_serviceId = serviceId;
        m_svcObj = svcObj;
        m_factory = (m_svcObj instanceof ServiceFactory)
            ? (ServiceFactory) m_svcObj : null;

        initializeProperties(dict);

        // This reference is the "standard" reference for this
        // service and will always be returned by getReference().
        // Since all reference to this service are supposed to
        // be equal, we use the hashcode of this reference for
        // a references to this service in ServiceReference.
        m_ref = new ServiceReferenceImpl(this, m_bundle);
    }

    protected boolean isValid()
    {
        return (m_svcObj != null);
    }

    public ServiceReference getReference()
    {
        return m_ref;
    }

    public void setProperties(Dictionary dict)
    {
        // Make sure registration is valid.
        if (!isValid())
        {
            throw new IllegalStateException(
                "The service registration is no longer valid.");
        }
        // Set the properties.
        initializeProperties(dict);
        // Tell registry about it.
        m_registry.servicePropertiesModified(this);
    }

    public void unregister()
    {
        if (m_svcObj != null)
        {
            m_registry.unregisterService(m_bundle, this);
            m_svcObj = null;
            m_factory = null;
        }
        else
        {
            throw new IllegalStateException("Service already unregistered.");
        }
    }

    //
    // Utility methods.
    //

    /**
     * This method determines if the class loader of the service object
     * has access to the specified class.
     * @param clazz the class to test for reachability.
     * @return <tt>true</tt> if the specified class is reachable from the
     *         service object's class loader, <tt>false</tt> otherwise.
    **/
    protected boolean isClassAccessible(Class clazz)
    {
        Class sourceClass = (m_factory != null)
            ? m_factory.getClass() : m_svcObj.getClass();
        try
        {
            Class target = Util.loadClassUsingClass(sourceClass, clazz.getName());
            return (target.getClassLoader() == clazz.getClassLoader());
        }
        catch (Exception ex)
        {
        }
        return false;
    }

    protected Object getProperty(String key)
    {
        return m_propMap.get(key);
    }

    private transient ArrayList m_list = new ArrayList();

    protected String[] getPropertyKeys()
    {
        synchronized (m_list)
        {<commit_msg>Modified class accessibility checking used for service filtering to also try to load from the bundle that registered the service.<commit_after>/*
 *   Copyright 2005 The Apache Software Foundation
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
package org.apache.felix.framework;

import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.*;

import org.apache.felix.framework.util.StringMap;
import org.apache.felix.framework.util.Util;
import org.osgi.framework.*;

class ServiceRegistrationImpl implements ServiceRegistration
{
    // Service registry.
    private ServiceRegistry m_registry = null;
    // Bundle implementing the service.
    private Bundle m_bundle = null;
    // Interfaces associated with the service object.
    private String[] m_classes = null;
    // Service Id associated with the service object.
    private Long m_serviceId = null;
    // Service object.
    private Object m_svcObj = null;
    // Service factory interface.
    private ServiceFactory m_factory = null;
    // Associated property dictionary.
    private Map m_propMap = null;
    // Re-usable service reference.
    private ServiceReferenceImpl m_ref = null;

    public ServiceRegistrationImpl(
        ServiceRegistry registry, Bundle bundle,
        String[] classes, Long serviceId,
        Object svcObj, Dictionary dict)
    {
        m_registry = registry;
        m_bundle = bundle;
        m_classes = classes;
        m_serviceId = serviceId;
        m_svcObj = svcObj;
        m_factory = (m_svcObj instanceof ServiceFactory)
            ? (ServiceFactory) m_svcObj : null;

        initializeProperties(dict);

        // This reference is the "standard" reference for this
        // service and will always be returned by getReference().
        // Since all reference to this service are supposed to
        // be equal, we use the hashcode of this reference for
        // a references to this service in ServiceReference.
        m_ref = new ServiceReferenceImpl(this, m_bundle);
    }

    protected boolean isValid()
    {
        return (m_svcObj != null);
    }

    public ServiceReference getReference()
    {
        return m_ref;
    }

    public void setProperties(Dictionary dict)
    {
        // Make sure registration is valid.
        if (!isValid())
        {
            throw new IllegalStateException(
                "The service registration is no longer valid.");
        }
        // Set the properties.
        initializeProperties(dict);
        // Tell registry about it.
        m_registry.servicePropertiesModified(this);
    }

    public void unregister()
    {
        if (m_svcObj != null)
        {
            m_registry.unregisterService(m_bundle, this);
            m_svcObj = null;
            m_factory = null;
        }
        else
        {
            throw new IllegalStateException("Service already unregistered.");
        }
    }

    //
    // Utility methods.
    //

    /**
     * This method determines if the class loader of the service object
     * has access to the specified class.
     * @param clazz the class to test for reachability.
     * @return <tt>true</tt> if the specified class is reachable from the
     *         service object's class loader, <tt>false</tt> otherwise.
    **/
    protected boolean isClassAccessible(Class clazz)
    {
        try
        {
            // First, try to load the class from the bundle that registered
            // the service.
            Class targetClass = m_bundle.loadClass(clazz.getName());
            if (targetClass != null)
            {
                return (targetClass == clazz);
            }
            // If it cannot be found from the registering bundle, then try to load
            // from the service object or service factory class.
            Class sourceClass = (m_factory != null)
                ? m_factory.getClass() : m_svcObj.getClass();
            targetClass = Util.loadClassUsingClass(sourceClass, clazz.getName());
            return (targetClass == clazz);
        }
        catch (Exception ex)
        {
        }
        return false;
    }

    protected Object getProperty(String key)
    {
        return m_propMap.get(key);
    }

    private transient ArrayList m_list = new ArrayList();

    protected String[] getPropertyKeys()
    {
        synchronized (m_list)
        {
{'fraction_non_alphanumeric': 0.07745950005364231, 'fraction_numerical': 0.001716554017809248, 'mean_word_length': 2.275474349964863, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5836768', 'n_tokens_mistral': 2386, 'n_tokens_neox': 2346, 'n_words': 832}