001/*
002 * (C) Copyright 2020 Nuxeo (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 *     bdelbosc
018 */
019package org.nuxeo.ecm.core.event.stream;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.nuxeo.common.xmap.annotation.XNode;
025import org.nuxeo.common.xmap.annotation.XNodeMap;
026import org.nuxeo.common.xmap.annotation.XObject;
027import org.nuxeo.ecm.core.api.NuxeoException;
028import org.nuxeo.runtime.model.Descriptor;
029import org.nuxeo.runtime.stream.StreamProcessorDescriptor;
030
031/**
032 * Defines a Domain Event Producer
033 *
034 * @since 11.4
035 */
036@XObject("domainEventProducer")
037public class DomainEventProducerDescriptor implements Descriptor {
038
039    @XNode("@name")
040    protected String name;
041
042    @XNode("@enabled")
043    protected boolean isEnabled = true;
044
045    @XNode("@class")
046    protected Class<? extends DomainEventProducer> domainEventProducerClass;
047
048    @XNodeMap(value = "option", key = "@name", type = HashMap.class, componentType = String.class)
049    protected Map<String, String> options = new HashMap<>();
050
051    @XNode("stream")
052    protected StreamProcessorDescriptor.StreamDescriptor stream;
053
054    @Override
055    public String getId() {
056        return name;
057    }
058
059    public boolean isEnabled() {
060        return isEnabled;
061    }
062
063    public void setEnabled(boolean isEnabled) {
064        this.isEnabled = isEnabled;
065    }
066
067    public DomainEventProducer newInstance() {
068        try {
069            return domainEventProducerClass.getDeclaredConstructor(String.class, String.class)
070                                           .newInstance(name, stream.name);
071        } catch (ReflectiveOperationException e) {
072            throw new NuxeoException("Cannot instantiate DomainEventProducer: " + name, e);
073        }
074    }
075
076    public StreamProcessorDescriptor.StreamDescriptor getStream() {
077        return stream;
078    }
079
080    public String getName() {
081        return name;
082    }
083}