<commit_before>/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * 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.openengsb.ui.web.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openengsb.core.common.Domain;
import org.openengsb.core.common.DomainProvider;
import org.openengsb.core.common.ServiceManager;
import org.openengsb.ui.web.service.DomainService;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.springframework.osgi.context.BundleContextAware;

public class OsgiDomainService implements DomainService, BundleContextAware {

    private Log log = LogFactory.getLog(OsgiDomainService.class);

    private List<DomainProvider> domains;
    private BundleContext bundleContext;

    @Override
    public List<DomainProvider> domains() {
        return new ArrayList<DomainProvider>(this.domains);
    }

    @Override
    public List<ServiceManager> serviceManagersForDomain(Class<? extends Domain> domain) {
        List<ServiceManager> serviceManagers = new ArrayList<ServiceManager>();
        try {
            String filter = "(domain=" + domain.getName() + ")";
            ServiceReference[] allServiceReferences = bundleContext.getAllServiceReferences(
                    ServiceManager.class.getName(), filter);

            if (allServiceReferences != null) {
                for (ServiceReference serviceReference : allServiceReferences) {
                    Object service = bundleContext.getService(serviceReference);
                    serviceManagers.add((ServiceManager) service);
                }
            }
        } catch (InvalidSyntaxException e) {
            throw new IllegalStateException(e);
        }
        return serviceManagers;
    }

    @Override
    public List<ServiceReference> serviceReferencesForDomain(Class<? extends Domain> domain) {
        List<ServiceReference> serviceReferences = new ArrayList<ServiceReference>();
        try {
            ServiceReference[] allServiceReferences = bundleContext.getAllServiceReferences(domain.getName(),
                    null);
            if (allServiceReferences != null) {
                serviceReferences = Arrays.asList(allServiceReferences);
            }
        } catch (InvalidSyntaxException e) {
            throw new IllegalStateException(e);
        }
        return serviceReferences;
    }

    @Override
    public List<? extends ServiceReference> getAllServiceInstances() {
        try {
            ServiceReference[] refs = bundleContext.getAllServiceReferences(Domain.class.getName(), null);
            if (refs == null) {
                return Collections.emptyList();
            }
            return Arrays.asList(refs);
        } catch (InvalidSyntaxException e) {
            throw new IllegalStateException("this should never happen, since no filter is used, or is it?");
        }
    }

    @Override
    public Object getService(ServiceReference reference) {
        return bundleContext.getService(reference);
    }

    @Override
    public Object getService(String serviceClass, String serviceId) {
        ServiceReference[] refs;
        log.info(String.format("try to find service for class \"%s\", and id \"%s\"", serviceClass, serviceId));
        try {
            refs = bundleContext.getAllServiceReferences(serviceClass, String.format("(id=%s)", serviceId));
        } catch (InvalidSyntaxException e) {
            throw new IllegalArgumentException("could not find service " + serviceId, e);
        }
        if (refs == null) {
            throw new IllegalArgumentException("no services found for class, " + serviceClass);
        }
        if (refs.length > 1) {
            throw new IllegalStateException(String.format("more than one service with id %s found", serviceId));
        }
        return bundleContext.getService(refs[0]);
    }

    public void setDomains(List<DomainProvider> domains) {
        this.domains = new ArrayList<DomainProvider>(domains);
    }

    @Override
    public void setBundleContext(BundleContext bundleContext) {
        this.bundleContext = bundleContext;

    }
}<commit_msg>fixed serious missunderstanding of spring-dm<commit_after>/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * 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.openengsb.ui.web.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openengsb.core.common.Domain;
import org.openengsb.core.common.DomainProvider;
import org.openengsb.core.common.ServiceManager;
import org.openengsb.ui.web.service.DomainService;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.springframework.osgi.context.BundleContextAware;

public class OsgiDomainService implements DomainService, BundleContextAware {

    private Log log = LogFactory.getLog(OsgiDomainService.class);

    private List<DomainProvider> domains;
    private BundleContext bundleContext;

    @Override
    public List<DomainProvider> domains() {
        return new ArrayList<DomainProvider>(this.domains);
    }

    @Override
    public List<ServiceManager> serviceManagersForDomain(Class<? extends Domain> domain) {
        List<ServiceManager> serviceManagers = new ArrayList<ServiceManager>();
        try {
            String filter = "(domain=" + domain.getName() + ")";
            ServiceReference[] allServiceReferences = bundleContext.getAllServiceReferences(
                    ServiceManager.class.getName(), filter);

            if (allServiceReferences != null) {
                for (ServiceReference serviceReference : allServiceReferences) {
                    Object service = bundleContext.getService(serviceReference);
                    serviceManagers.add((ServiceManager) service);
                }
            }
        } catch (InvalidSyntaxException e) {
            throw new IllegalStateException(e);
        }
        return serviceManagers;
    }

    @Override
    public List<ServiceReference> serviceReferencesForDomain(Class<? extends Domain> domain) {
        List<ServiceReference> serviceReferences = new ArrayList<ServiceReference>();
        try {
            ServiceReference[] allServiceReferences = bundleContext.getAllServiceReferences(domain.getName(),
                    null);
            if (allServiceReferences != null) {
                serviceReferences = Arrays.asList(allServiceReferences);
            }
        } catch (InvalidSyntaxException e) {
            throw new IllegalStateException(e);
        }
        return serviceReferences;
    }

    @Override
    public List<? extends ServiceReference> getAllServiceInstances() {
        try {
            ServiceReference[] refs = bundleContext.getAllServiceReferences(Domain.class.getName(), null);
            if (refs == null) {
                return Collections.emptyList();
            }
            return Arrays.asList(refs);
        } catch (InvalidSyntaxException e) {
            throw new IllegalStateException("this should never happen, since no filter is used, or is it?");
        }
    }

    @Override
    public Object getService(ServiceReference reference) {
        return bundleContext.getService(reference);
    }

    @Override
    public Object getService(String serviceClass, String serviceId) {
        ServiceReference[] refs;
        log.info(String.format("try to find service for class \"%s\", and id \"%s\"", serviceClass, serviceId));
        try {
            refs = bundleContext.getAllServiceReferences(serviceClass, String.format("(id=%s)", serviceId));
        } catch (InvalidSyntaxException e) {
            throw new IllegalArgumentException("could not find service " + serviceId, e);
        }
        if (refs == null) {
            throw new IllegalArgumentException("no services found for class, " + serviceClass);
        }
        if (refs.length > 1) {
            throw new IllegalStateException(String.format("more than one service with id %s found", serviceId));
        }
        return bundleContext.getService(refs[0]);
    }

    public void setDomains(List<DomainProvider> domains) {
        this.domains = domains;
    }

    @Override
    public void setBundleContext(BundleContext bundleContext) {
        this.bundleContext = bundleContext;

    }
}
{'fraction_non_alphanumeric': 0.08926009428161509, 'fraction_numerical': 0.0020496003279360524, 'mean_word_length': 2.984891792568395, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 32, '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': '4335784', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2343, 'n_words': 663}