001/*
002 * (C) Copyright 2006-2007 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 *     dragos
018 *
019 * $Id$
020 */
021package org.nuxeo.ecm.platform.rendering.impl;
022
023import java.util.ArrayList;
024import java.util.Collection;
025import java.util.List;
026import java.util.concurrent.ConcurrentHashMap;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.platform.rendering.RenderingContext;
031import org.nuxeo.ecm.platform.rendering.RenderingEngine;
032import org.nuxeo.ecm.platform.rendering.RenderingException;
033import org.nuxeo.ecm.platform.rendering.RenderingResult;
034import org.nuxeo.ecm.platform.rendering.RenderingService;
035import org.nuxeo.runtime.model.ComponentInstance;
036import org.nuxeo.runtime.model.DefaultComponent;
037
038/**
039 * Implementation of RenderingService
040 *
041 * @author <a href="mailto:dm@nuxeo.com">Dragos Mihalache</a>
042 */
043public class RenderingServiceImpl extends DefaultComponent implements RenderingService {
044
045    private static final Log log = LogFactory.getLog(RenderingServiceImpl.class);
046
047    public static final String EP_RENDER_ENGINES = "engines";
048
049    private final ConcurrentHashMap<String, RenderingEngine> engines = new ConcurrentHashMap<>();
050
051    @Override
052    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
053
054        if (log.isDebugEnabled()) {
055            log.debug("register: " + contribution + ", ep: " + extensionPoint);
056        }
057
058        if (extensionPoint.equals(EP_RENDER_ENGINES)) {
059            RenderingEngineDescriptor desc = (RenderingEngineDescriptor) contribution;
060
061            try {
062                RenderingEngine engine = desc.newInstance();
063                engines.put(desc.getFormat(), engine);
064            } catch (ReflectiveOperationException e) {
065                log.error("Cannot register rendering engine for " + desc.getFormat(), e);
066            }
067        }
068    }
069
070    @Override
071    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
072        if (extensionPoint.equals(EP_RENDER_ENGINES)) {
073            RenderingEngineDescriptor desc = (RenderingEngineDescriptor) contribution;
074            engines.remove(desc.getFormat());
075        }
076    }
077
078    @Override
079    public RenderingEngine getEngine(String name) {
080        return engines.get(name);
081    }
082
083    @Override
084    public Collection<RenderingResult> process(RenderingContext renderingCtx) throws RenderingException {
085        List<RenderingResult> ret = new ArrayList<>();
086
087        for (RenderingEngine engine : engines.values()) {
088            if (renderingCtx.accept(engine)) {
089                RenderingResult result = engine.process(renderingCtx);
090                if (result != null) {
091                    ret.add(result);
092                } else if (log.isDebugEnabled()) {
093                    log.debug("rendering ignored by the engine " + engine.getFormatName());
094                }
095            }
096        }
097        return ret;
098    }
099
100    @Override
101    public void registerEngine(RenderingEngine engine) {
102        RenderingEngine existing = engines.put(engine.getFormatName(), engine);
103        if (existing != null) {
104            log.debug("Replaced existing RenderingEngine " + engine.getFormatName());
105        } else if (log.isDebugEnabled()) {
106            log.debug("Registered RenderingEngine " + engine.getFormatName());
107        }
108    }
109
110    @Override
111    public void unregisterEngine(String name) {
112        engines.remove(name);
113    }
114
115}