001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.rendering;
013
014import java.io.IOException;
015import java.io.InputStream;
016import java.net.URL;
017import java.util.Collections;
018import java.util.LinkedHashMap;
019import java.util.Map;
020
021import org.mvel2.templates.CompiledTemplate;
022import org.mvel2.templates.TemplateCompiler;
023import org.mvel2.templates.TemplateRuntime;
024import org.nuxeo.common.utils.FileUtils;
025import org.nuxeo.ecm.automation.OperationException;
026import org.nuxeo.runtime.api.Framework;
027import org.nuxeo.runtime.services.resource.ResourceService;
028
029/**
030 * MVEL rendering using a simple cache of compiled template.
031 *
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public class MvelRender implements Renderer {
035
036    protected Map<String, CompiledTemplate> cache = Collections.synchronizedMap(new Cache());
037
038    @Override
039    public String render(String uriOrContent, Map<String, Object> root) throws OperationException, IOException {
040        CompiledTemplate compiled = null;
041        String content = null;
042        if (uriOrContent.startsWith(Renderer.TEMPLATE_PREFIX)) {
043            String name = uriOrContent.substring(Renderer.TEMPLATE_PREFIX.length());
044            compiled = cache.get(name);
045            if (compiled == null) {
046                URL url = Framework.getService(ResourceService.class).getResource(name);
047                if (url == null) {
048                    throw new OperationException("Rendering resource not found: " + name);
049                }
050                InputStream in = url.openStream();
051                try {
052                    content = FileUtils.read(in);
053                } finally {
054                    in.close();
055                }
056                compiled = TemplateCompiler.compileTemplate(content);
057                cache.put(name, compiled);
058            }
059        } else {
060            content = uriOrContent;
061            compiled = TemplateCompiler.compileTemplate(content);
062        }
063
064        Object obj = TemplateRuntime.execute(compiled, root);
065        return obj == null ? "" : obj.toString();
066    }
067
068    @SuppressWarnings("serial")
069    private static class Cache extends LinkedHashMap<String, CompiledTemplate> {
070
071        protected int maxCachedItems;
072
073        private Cache() {
074            this(128);
075        }
076
077        private Cache(int maxCachedItems) {
078            super(maxCachedItems, 1.0f, true);
079            this.maxCachedItems = maxCachedItems;
080        }
081
082        @Override
083        protected boolean removeEldestEntry(Map.Entry<String, CompiledTemplate> eldest) {
084            return size() > maxCachedItems;
085        }
086
087    }
088
089}