// Path: cicomponents-api/src/main/java/org/cicomponents/ResourceListener.java
// @FunctionalInterface
// public interface ResourceListener<T extends Resource> {
//     void onEmittedResource(ResourceHolder<T> holder,
//                            ResourceEmitter<T> emitter);
// 
//     default boolean isMatchingListener(Class klass) {
//         for (Type type : getClass().getGenericInterfaces()) {
//             if (type instanceof ParameterizedType &&
//                     ((ParameterizedType) type).getRawType() == ResourceListener.class) {
//                 ParameterizedType parameterizedType = (ParameterizedType) type;
//                 Type target = parameterizedType.getActualTypeArguments()[0];
//                 if (target == klass) {
//                     return true;
//                 }
//             }
//         }
//         return false;
//     }
// }
// 
// Path: cicomponents-git-api/src/main/java/org/cicomponents/git/GitRevision.java
// public interface GitRevision extends File, Resource {
//     Git getRepository();
//     ObjectId getRef();
// }
// 
// Path: cicomponents-git-api/src/main/java/org/cicomponents/git/GitRevisionEmitter.java
// public interface GitRevisionEmitter extends ResourceEmitter<GitRevision> {
// }


import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.cicomponents.ResourceListener;
import org.cicomponents.git.GitRevision;
import org.cicomponents.git.GitRevisionEmitter;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.*;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Consumer;
/**
 * Copyright (c) 2016, All Contributors (see CONTRIBUTORS file)
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.cicomponents.git.impl;



@Component(immediate = true, scope = ServiceScope.SINGLETON)
@Slf4j
public class GitEmitterProvider {

    @Reference
    protected Environment environment;

    @Reference(policyOption = ReferencePolicyOption.GREEDY)
    protected Collection<Map.Entry<Map<String, Object>, ResourceListener<GitRevision>>> listeners;

    @SneakyThrows
    private void forEachService(ComponentContext context, Consumer<GitRevisionEmitter> consumer) {
        Collection<ServiceReference<GitRevisionEmitter>> references =
                context.getBundleContext().getServiceReferences(GitRevisionEmitter.class,
                                                                "(objectClass=" + GitRevisionEmitter.class.getName() + ")");
        for (ServiceReference<GitRevisionEmitter> reference : references) {
            GitRevisionEmitter service = context.getBundleContext().getService(reference);
            consumer.accept(service);
            context.getBundleContext().ungetService(reference);
        }
    }

    @SneakyThrows
    @Activate
    protected void activate(ComponentContext context) {
        log.info("Activating with {} potential listener(s)", listeners.size());
        forEachService(context, (emitter) -> {
            if (emitter instanceof AbstractGitMonitor) {
                ((AbstractGitMonitor) emitter).pause();
            }
// Path: cicomponents-api/src/main/java/org/cicomponents/ResourceListener.java
// @FunctionalInterface
// public interface ResourceListener<T extends Resource> {
//     void onEmittedResource(ResourceHolder<T> holder,
//                            ResourceEmitter<T> emitter);
// 
//     default boolean isMatchingListener(Class klass) {
//         for (Type type : getClass().getGenericInterfaces()) {
//             if (type instanceof ParameterizedType &&
//                     ((ParameterizedType) type).getRawType() == ResourceListener.class) {
//                 ParameterizedType parameterizedType = (ParameterizedType) type;
//                 Type target = parameterizedType.getActualTypeArguments()[0];
//                 if (target == klass) {
//                     return true;
//                 }
//             }
//         }
//         return false;
//     }
// }
// 
// Path: cicomponents-git-api/src/main/java/org/cicomponents/git/GitRevision.java
// public interface GitRevision extends File, Resource {
//     Git getRepository();
//     ObjectId getRef();
// }
// 
// Path: cicomponents-git-api/src/main/java/org/cicomponents/git/GitRevisionEmitter.java
// public interface GitRevisionEmitter extends ResourceEmitter<GitRevision> {
// }


// Path: cicomponents-git/src/main/java/org/cicomponents/git/impl/GitEmitterProvider.java
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.cicomponents.ResourceListener;
import org.cicomponents.git.GitRevision;
import org.cicomponents.git.GitRevisionEmitter;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.*;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Consumer;

/**
 * Copyright (c) 2016, All Contributors (see CONTRIBUTORS file)
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.cicomponents.git.impl;



@Component(immediate = true, scope = ServiceScope.SINGLETON)
@Slf4j
public class GitEmitterProvider {

    @Reference
    protected Environment environment;

    @Reference(policyOption = ReferencePolicyOption.GREEDY)
    protected Collection<Map.Entry<Map<String, Object>, ResourceListener<GitRevision>>> listeners;

    @SneakyThrows
    private void forEachService(ComponentContext context, Consumer<GitRevisionEmitter> consumer) {
        Collection<ServiceReference<GitRevisionEmitter>> references =
                context.getBundleContext().getServiceReferences(GitRevisionEmitter.class,
                                                                "(objectClass=" + GitRevisionEmitter.class.getName() + ")");
        for (ServiceReference<GitRevisionEmitter> reference : references) {
            GitRevisionEmitter service = context.getBundleContext().getService(reference);
            consumer.accept(service);
            context.getBundleContext().ungetService(reference);
        }
    }

    @SneakyThrows
    @Activate
    protected void activate(ComponentContext context) {
        log.info("Activating with {} potential listener(s)", listeners.size());
        forEachService(context, (emitter) -> {
            if (emitter instanceof AbstractGitMonitor) {
                ((AbstractGitMonitor) emitter).pause();
            }
        });