/*
 * 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.jackrabbit.vault.packaging.events.impl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.apache.jackrabbit.vault.packaging.PackageId;
import org.apache.jackrabbit.vault.packaging.events.PackageEvent;
import org.apache.jackrabbit.vault.packaging.events.PackageEventListener;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Packaging observation helper
 */
@Component(
        service = PackageEventDispatcher.class,
        property = {"service.vendor=The Apache Software Foundation"},
        reference = {
                @Reference(
                        name="packageEventListener",
                        service = PackageEventListener.class,
                        cardinality = ReferenceCardinality.MULTIPLE,
                        policy = ReferencePolicy.DYNAMIC,
                        bind = "bindPackageEventListener",
                        unbind = "unbindPackageEventListener"
                )
        }
)
public class PackageEventDispatcherImpl implements PackageEventDispatcher {

    /**
     * default logger
     */
    private static final Logger log = LoggerFactory.getLogger(PackageEventDispatcherImpl.class);

    /**
     * the registered listener
     */
    private ConcurrentHashMap<Object, PackageEventListener> listeners = new ConcurrentHashMap<Object, PackageEventListener>();

    /**
     * Bind a new listener
     * @param listener the processor
     * @param props service properties
     */
    public void bindPackageEventListener(PackageEventListener listener, Map<String, Object> props) {
        // public for testing
        listeners.put(props.get("component.id"), listener);
        log.debug("Registering package event listener {}", listener.getClass().getName());
    }

    /**
     * Unbinds a listener
     * @param listener the processor
     * @param props service properties
     */
    protected void unbindPackageEventListener(PackageEventListener listener, Map<String, Object> props) {
        if (listeners.remove(props.get("component.id")) != null) {
            log.debug("Unregistering package event listener {}", listener.getClass().getName());
        } else {
            log.warn("unable to unregister package event listener {}", listener.getClass().getName());
        }
    }

    public void dispatch(@Nonnull PackageEvent.Type type, @Nonnull PackageId id, @Nullable PackageId[] related) {
        final EventImpl event = new EventImpl(type, id, related);
        for (PackageEventListener l: listeners.values()) {
            try {
                l.onPackageEvent(event);
            } catch (Throwable e) {
                // ignore
            }
        }
    }

    private static final class EventImpl implements PackageEvent {

        private final Type type;

        private final PackageId id;

        private final PackageId[] related;

        public EventImpl(Type type, PackageId id, PackageId[] related) {
            this.type = type;
            this.id = id;
            this.related = related;
        }

        @Nonnull
        @Override
        public Type getType() {
            return type;
        }

        @Nonnull
        @Override
        public PackageId getId() {
            return id;
        }

        @CheckForNull
        @Override
        public PackageId[] getRelatedIds() {
            return related;
        }
    }

}


@@ -856,16 +856,41 @@
 .impl;%0A%0A
+import java.util.Arrays;%0A
 import j
@@ -4561,27 +4561,1233 @@
 lated;%0A        %7D
+%0A%0A        @Override%0A        public String toString() %7B%0A            return %22EventImpl %5Btype=%22 + type + %22, id=%22 + id + %22, related=%22 + Arrays.toString(related) + %22%5D%22;%0A        %7D%0A%0A        @Override%0A        public int hashCode() %7B%0A            final int prime = 31;%0A            int result = 1;%0A            result = prime * result + ((id == null) ? 0 : id.hashCode());%0A            result = prime * result + Arrays.hashCode(related);%0A            result = prime * result + ((type == null) ? 0 : type.hashCode());%0A            return result;%0A        %7D%0A%0A        @Override%0A        public boolean equals(Object obj) %7B%0A            if (this == obj)%0A                return true;%0A            if (obj == null)%0A                return false;%0A            if (getClass() != obj.getClass())%0A                return false;%0A            EventImpl other = (EventImpl) obj;%0A            if (id == null) %7B%0A                if (other.id != null)%0A                    return false;%0A            %7D else if (!id.equals(other.id))%0A                return false;%0A            if (!Arrays.equals(related, other.related))%0A                return false;%0A            if (type != other.type)%0A                return false;%0A            return true;%0A        %7D
 %0A    %7D%0A%0A%7D%0A%0A

add toString, equals and hashCode to EventImpl