001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     tiry
018 */
019package org.nuxeo.ecm.core.event.pipe.dispatch;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XNodeMap;
025import org.nuxeo.common.xmap.annotation.XObject;
026
027import java.util.HashMap;
028import java.util.Map;
029
030/**
031 * XMap Descriptor for contributing a new {@link EventBundleDispatcher}
032 *
033 * @since 8.4
034 */
035@XObject("eventDispatcher")
036public class EventDispatcherDescriptor {
037
038    public static final Log log = LogFactory.getLog(EventDispatcherDescriptor.class);
039
040    public EventDispatcherDescriptor() {
041    }
042
043    public EventDispatcherDescriptor(String name, Class<? extends EventBundleDispatcher> clazz) {
044        this.name = name;
045        this.clazz = clazz;
046    }
047
048    @XNode("@name")
049    protected String name;
050
051    @XNodeMap(value = "parameters/parameter", key = "@name", type = HashMap.class, componentType = String.class)
052    Map<String, String> parameters = new HashMap<>();
053
054    public String getName() {
055        return name == null ? clazz.getName() : name;
056    }
057
058    /**
059     * The implementation class.
060     */
061    @XNode("@class")
062    protected Class<? extends EventBundleDispatcher> clazz;
063
064    public Map<String, String> getParameters() {
065        return parameters;
066    }
067
068    public EventBundleDispatcher getInstance() {
069        try {
070            return clazz.getDeclaredConstructor().newInstance();
071        } catch (ReflectiveOperationException e) {
072            throw new RuntimeException(e);
073        }
074    }
075
076    @Override
077    public EventDispatcherDescriptor clone() {
078        EventDispatcherDescriptor copy = new EventDispatcherDescriptor(name, clazz);
079        copy.parameters = new HashMap<>(parameters);
080        return copy;
081    }
082}