001/*
002 * (C) Copyright 2006-2016 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.core.rendering;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.net.URL;
024import java.util.Collections;
025import java.util.LinkedHashMap;
026import java.util.Map;
027
028import org.apache.commons.io.Charsets;
029import org.apache.commons.io.IOUtils;
030import org.mvel2.templates.CompiledTemplate;
031import org.mvel2.templates.TemplateCompiler;
032import org.mvel2.templates.TemplateRuntime;
033import org.nuxeo.ecm.automation.OperationException;
034import org.nuxeo.runtime.api.Framework;
035import org.nuxeo.runtime.services.resource.ResourceService;
036
037/**
038 * MVEL rendering using a simple cache of compiled template.
039 *
040 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
041 */
042public class MvelRender implements Renderer {
043
044    protected Map<String, CompiledTemplate> cache = Collections.synchronizedMap(new Cache());
045
046    @Override
047    public String render(String uriOrContent, Map<String, Object> root) throws OperationException, IOException {
048        CompiledTemplate compiled;
049        String content;
050        if (uriOrContent.startsWith(Renderer.TEMPLATE_PREFIX)) {
051            String name = uriOrContent.substring(Renderer.TEMPLATE_PREFIX.length());
052            compiled = cache.get(name);
053            if (compiled == null) {
054                URL url = Framework.getService(ResourceService.class).getResource(name);
055                if (url == null) {
056                    throw new OperationException("Rendering resource not found: " + name);
057                }
058                try (InputStream in = url.openStream()) {
059                    content = IOUtils.toString(in, Charsets.UTF_8);
060                }
061                compiled = TemplateCompiler.compileTemplate(content);
062                cache.put(name, compiled);
063            }
064        } else {
065            content = uriOrContent;
066            compiled = TemplateCompiler.compileTemplate(content);
067        }
068
069        Object obj = TemplateRuntime.execute(compiled, root);
070        return obj == null ? "" : obj.toString();
071    }
072
073    @SuppressWarnings("serial")
074    private static class Cache extends LinkedHashMap<String, CompiledTemplate> {
075
076        protected int maxCachedItems;
077
078        private Cache() {
079            this(128);
080        }
081
082        private Cache(int maxCachedItems) {
083            super(maxCachedItems, 1.0f, true);
084            this.maxCachedItems = maxCachedItems;
085        }
086
087        @Override
088        protected boolean removeEldestEntry(Map.Entry<String, CompiledTemplate> eldest) {
089            return size() > maxCachedItems;
090        }
091
092    }
093
094}