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