001/*
002 * (C) Copyright 2015 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 *     Nuxeo
018 */
019
020package org.nuxeo.ecm.showcase.content.service;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.impl.blob.URLBlob;
026import org.nuxeo.ecm.showcase.content.ShowcaseContentImporter;
027import org.nuxeo.runtime.model.ComponentInstance;
028import org.nuxeo.runtime.model.ContributionFragmentRegistry;
029import org.nuxeo.runtime.model.DefaultComponent;
030import org.nuxeo.runtime.model.SimpleContributionRegistry;
031
032import java.io.IOException;
033import java.util.Arrays;
034import java.util.List;
035import java.util.stream.Collectors;
036
037/**
038 * @since 8.4
039 */
040public class ShowcaseContentServiceImpl extends DefaultComponent implements ShowcaseContentService {
041
042    private static final Log log = LogFactory.getLog(ShowcaseContentServiceImpl.class);
043
044    public static final String EP_CONTENTS = "contents";
045
046    private ContributionFragmentRegistry<ShowcaseContentDescriptor> registry = new ShowcaseContentDescriptorSimpleContributionRegistry();
047
048    private static class ShowcaseContentDescriptorSimpleContributionRegistry
049            extends SimpleContributionRegistry<ShowcaseContentDescriptor> {
050        @Override
051        public String getContributionId(ShowcaseContentDescriptor contrib) {
052            return contrib.getName();
053        }
054    }
055
056    @Override
057    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
058        if (EP_CONTENTS.equals(extensionPoint)) {
059            ShowcaseContentDescriptor content = (ShowcaseContentDescriptor) contribution;
060            content.computeBlobUrl(contributor);
061            registry.addContribution(content);
062        }
063    }
064
065    @Override
066    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
067        if (EP_CONTENTS.equals(extensionPoint)) {
068            registry.removeContribution((ShowcaseContentDescriptor) contribution);
069        }
070    }
071
072    @Override
073    public void triggerImporters(CoreSession session) {
074        getContributions().forEach(c -> {
075            // XXX Should use a dedicated Worker...
076            URLBlob blob = new URLBlob(c.blobUrl);
077            try {
078                ShowcaseContentImporter.run(session, c.getName(), blob);
079            } catch (IOException e) {
080                log.warn(String.format("Unable to import %s: %s", c.getName(), e), e);
081            }
082        });
083    }
084
085    protected List<ShowcaseContentDescriptor> getContributions() {
086        return Arrays.stream(registry.getFragments())
087                     .map(f -> f.object)
088                     .filter(c -> c.enabled)
089                     .collect(Collectors.toList());
090    }
091}