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 *     Andrei Nechaev
019 */
020package org.nuxeo.ecm.core.event.pipe;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.common.xmap.annotation.XNode;
025import org.nuxeo.common.xmap.annotation.XNodeMap;
026import org.nuxeo.common.xmap.annotation.XObject;
027
028import java.util.HashMap;
029import java.util.Map;
030
031/**
032 * XMap Descriptor for contributing a new {@link EventBundlePipe}
033 *
034 * @since 8.4
035 */
036@XObject("eventPipe")
037public class EventPipeDescriptor {
038
039    private static final Log log = LogFactory.getLog(EventPipeDescriptor.class);
040
041    @XNode("@name")
042    protected String name;
043
044    @XNode("@priority")
045    protected Integer priority;
046
047    public EventPipeDescriptor() {
048    }
049
050    public EventPipeDescriptor(String name, Class<? extends EventBundlePipe> clazz) {
051        this.name = name;
052        this.clazz = clazz;
053    }
054
055    @XNodeMap(value = "parameters/parameter", key = "@name", type = HashMap.class, componentType = String.class)
056    protected Map<String, String> parameters = new HashMap<>();
057
058    /**
059     * The implementation class.
060     */
061    @XNode("@class")
062    protected Class<? extends EventBundlePipe> clazz;
063
064    public String getName() {
065        return name;
066    }
067
068    public Integer getPriority() {
069        if (priority == null) {
070            return 100;
071        }
072        return priority;
073    }
074
075    public Map<String, String> getParameters() {
076        return parameters;
077    }
078
079    public EventBundlePipe getInstance() {
080        try {
081            return clazz.newInstance();
082        } catch (ReflectiveOperationException e) {
083            throw new RuntimeException(e);
084        }
085    }
086
087    public void merge(EventPipeDescriptor other) {
088        if (other.priority != null) {
089            priority = other.priority;
090        }
091        if (other.clazz != null) {
092            clazz = other.clazz;
093        }
094        parameters.putAll(other.getParameters());
095    }
096
097    @Override
098    public EventPipeDescriptor clone() {
099        EventPipeDescriptor copy = new EventPipeDescriptor(name, clazz);
100        copy.priority=priority;
101        copy.parameters = parameters;
102        return copy;
103    }
104}