001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     dragos
016 *
017 * $Id$
018 */
019package org.nuxeo.ecm.platform.rendering.impl;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.platform.rendering.RenderingContext;
030import org.nuxeo.ecm.platform.rendering.RenderingEngine;
031import org.nuxeo.ecm.platform.rendering.RenderingException;
032import org.nuxeo.ecm.platform.rendering.RenderingResult;
033import org.nuxeo.ecm.platform.rendering.RenderingService;
034import org.nuxeo.runtime.model.ComponentInstance;
035import org.nuxeo.runtime.model.DefaultComponent;
036
037/**
038 * Implementation of RenderingService
039 *
040 * @author <a href="mailto:dm@nuxeo.com">Dragos Mihalache</a>
041 */
042public class RenderingServiceImpl extends DefaultComponent implements RenderingService {
043
044    private static final Log log = LogFactory.getLog(RenderingServiceImpl.class);
045
046    public static final String EP_RENDER_ENGINES = "engines";
047
048    private final Map<String, RenderingEngine> engines = new HashMap<String, RenderingEngine>();
049
050    @Override
051    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
052
053        if (log.isDebugEnabled()) {
054            log.debug("register: " + contribution + ", ep: " + extensionPoint);
055        }
056
057        if (extensionPoint.equals(EP_RENDER_ENGINES)) {
058            RenderingEngineDescriptor desc = (RenderingEngineDescriptor) contribution;
059
060            try {
061                RenderingEngine engine = desc.newInstance();
062                engines.put(desc.getFormat(), engine);
063            } catch (ReflectiveOperationException e) {
064                log.error("Cannot register rendering engine for " + desc.getFormat(), e);
065            }
066        }
067    }
068
069    @Override
070    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
071        if (extensionPoint.equals(EP_RENDER_ENGINES)) {
072            RenderingEngineDescriptor desc = (RenderingEngineDescriptor) contribution;
073            engines.remove(desc.getFormat());
074        }
075    }
076
077    public RenderingEngine getEngine(String name) {
078        return engines.get(name);
079    }
080
081    public Collection<RenderingResult> process(RenderingContext renderingCtx) throws RenderingException {
082        List<RenderingResult> ret = new ArrayList<RenderingResult>();
083
084        for (RenderingEngine engine : engines.values()) {
085            if (renderingCtx.accept(engine)) {
086                RenderingResult result = engine.process(renderingCtx);
087                if (result != null) {
088                    ret.add(result);
089                } else if (log.isDebugEnabled()) {
090                    log.debug("rendering ignored by the engine " + engine.getFormatName());
091                }
092            }
093        }
094        return ret;
095    }
096
097    public void registerEngine(RenderingEngine engine) {
098        RenderingEngine existing = engines.put(engine.getFormatName(), engine);
099        if (existing != null) {
100            log.debug("Replaced existing RenderingEngine " + engine.getFormatName());
101        } else if (log.isDebugEnabled()) {
102            log.debug("Registered RenderingEngine " + engine.getFormatName());
103        }
104    }
105
106    public void unregisterEngine(String name) {
107        engines.remove(name);
108    }
109
110}