001/*
002 * (C) Copyright 2018 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 *     pierre
018 */
019package org.nuxeo.ecm.core.bulk;
020
021import java.util.LinkedList;
022import java.util.List;
023import java.util.Queue;
024import java.util.stream.Collectors;
025
026import org.nuxeo.ecm.core.api.NuxeoException;
027import org.nuxeo.runtime.model.ComponentContext;
028import org.nuxeo.runtime.model.ComponentInstance;
029import org.nuxeo.runtime.model.DefaultComponent;
030
031/**
032 * The bulk component.
033 *
034 * @since 10.2
035 */
036public class BulkComponent extends DefaultComponent implements BulkAdminService {
037
038    public static final String BULK_LOG_MANAGER_NAME = "bulk";
039
040    public static final String BULK_KV_STORE_NAME = "bulk";
041
042    public static final String ACTIONS_XP = "actions";
043
044    protected Queue<BulkActionDescriptor> actionsRegistry = new LinkedList<>();
045
046    protected BulkService bulkService;
047
048    @Override
049    @SuppressWarnings("unchecked")
050    public <T> T getAdapter(Class<T> adapter) {
051        if (adapter.isAssignableFrom(BulkService.class)) {
052            return (T) bulkService;
053        } else if (adapter.isAssignableFrom(BulkAdminService.class)) {
054            // BulkAdminService is implemented by the component and not as a service like BulkService because
055            // StreamBulkScroller needs bulk configuration during its initialization. Its initialization happens during
056            // StreamService's start step which is before BulkComponent's start step, so at a moment where services are
057            // not yet created.
058            return (T) this;
059        }
060        return null;
061    }
062
063    @Override
064    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
065        if (ACTIONS_XP.equals(extensionPoint)) {
066            actionsRegistry.add((BulkActionDescriptor) contribution);
067        } else {
068            throw new NuxeoException("Unknown extension point: " + extensionPoint);
069        }
070    }
071
072    @Override
073    public void start(ComponentContext context) {
074        bulkService = new BulkServiceImpl();
075    }
076
077    @Override
078    public void stop(ComponentContext context) {
079        bulkService = null;
080    }
081
082    @SuppressWarnings("SuspiciousMethodCalls")
083    @Override
084    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
085        if (ACTIONS_XP.equals(extensionPoint)) {
086            actionsRegistry.remove(contribution);
087        } else {
088            throw new NuxeoException("Unknown extension point: " + extensionPoint);
089        }
090    }
091
092    // ---------------------
093    // BulkAdminService part
094    // ---------------------
095
096    @Override
097    public List<String> getActions() {
098        return actionsRegistry.stream().map(BulkActionDescriptor::getName).collect(Collectors.toList());
099    }
100}