/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.karaf.event.service;

import java.util.Deque;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.Consumer;
import java.util.stream.Stream;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

@Component(
           service = {EventHandler.class, EventCollector.class},
           name = "org.apache.karaf.eventadmin.collector",
           immediate = true,
           property = "event.topics=*"
       )
public class EventCollector implements EventHandler {
    private Deque<Event> events;
    private int maxSize;
    private Set<Consumer<Event>> consumers;
    
    public EventCollector() {
        events = new ConcurrentLinkedDeque<>();
        maxSize = 100;
        consumers = new HashSet<>();
    }

    @Override
    public synchronized void handleEvent(Event event) {
        events.addLast(event);
        if (events.size() > maxSize) {
            events.removeFirst();
        }
        consumers.forEach(c -> c.accept(event));
    }

    public Stream<Event> getEvents() {
        return events.stream();
    }
    
    public synchronized void addConsumer(Consumer<Event> eventConsumer) {
        events.forEach(eventConsumer);
        consumers.add(eventConsumer);
    }
    
    public synchronized void removeConsumer(Consumer<Event> eventConsumer) {
        consumers.remove(eventConsumer);
    }

}

@@ -1057,335 +1057,65 @@
 ice.
-component.annotations.Component;%0Aimport org.osgi.service.event.Event;%0Aimport org.osgi.service.event.EventHandler;%0A%0A@Component(%0A           service = %7BEventHandler.class, EventCollector.class%7D,%0A           name = %22org.apache.karaf.eventadmin.collector%22,%0A           immediate = true,%0A           property = %22event.topics=*%22%0A       )
+event.Event;%0Aimport org.osgi.service.event.EventHandler;%0A
 %0Apub

Remove annotation